Skip to content

Commit

Permalink
refactor(scope): GetIdentityRefFromObjects
Browse files Browse the repository at this point in the history
factorize identityRef lookup from multiple object to implement logic of identityRef's inheritage in one place (scope factory)
  • Loading branch information
MatthieuFin committed Oct 30, 2024
1 parent 4bdd8e8 commit dc72b3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
15 changes: 5 additions & 10 deletions controllers/openstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
Address: instanceStatus.Name(),
})
openStackMachine.Status.Addresses = addresses
if openStackMachine.Spec.IdentityRef == nil {
openStackMachine.Spec.IdentityRef = &openStackCluster.Spec.IdentityRef
}

_, identityRef := r.ScopeFactory.GetIdentityRefFromObjects(openStackMachine, openStackCluster)
openStackMachine.Spec.IdentityRef = identityRef

result := r.reconcileMachineState(scope, openStackMachine, machine, machineServer)
if result != nil {
Expand Down Expand Up @@ -576,13 +576,8 @@ func (r *OpenStackMachineReconciler) getOrCreateMachineServer(ctx context.Contex
}
if apierrors.IsNotFound(err) {
// Use credentials from the machine object by default, falling back to cluster credentials.
identityRef := func() infrav1.OpenStackIdentityReference {
if openStackMachine.Spec.IdentityRef != nil {
return *openStackMachine.Spec.IdentityRef
}
return openStackCluster.Spec.IdentityRef
}()
machineServerSpec := openStackMachineSpecToOpenStackServerSpec(&openStackMachine.Spec, identityRef, compute.InstanceTags(&openStackMachine.Spec, openStackCluster), failureDomain, userDataRef, getManagedSecurityGroup(openStackCluster, machine), openStackCluster.Status.Network.ID)
_, identityRef := r.ScopeFactory.GetIdentityRefFromObjects(openStackMachine, openStackCluster)
machineServerSpec := openStackMachineSpecToOpenStackServerSpec(&openStackMachine.Spec, *identityRef, compute.InstanceTags(&openStackMachine.Spec, openStackCluster), failureDomain, userDataRef, getManagedSecurityGroup(openStackCluster, machine), openStackCluster.Status.Network.ID)
machineServer = &infrav1alpha1.OpenStackServer{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
Expand Down
1 change: 1 addition & 0 deletions pkg/scope/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// MockScopeFactory implements both the ScopeFactory and ClientScope interfaces. It can be used in place of the default ProviderScopeFactory
// when we want to use mocked service clients which do not attempt to connect to a running OpenStack cloud.
type MockScopeFactory struct {
*defaultScopeFactory
ComputeClient *mock.MockComputeClient
NetworkClient *mock.MockNetworkClient
VolumeClient *mock.MockVolumeClient
Expand Down
11 changes: 2 additions & 9 deletions pkg/scope/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,12 @@ const (
)

type providerScopeFactory struct {
*defaultScopeFactory
clientCache *cache.LRUExpireCache
}

func (f *providerScopeFactory) NewClientScopeFromObject(ctx context.Context, ctrlClient client.Client, defaultCACert []byte, logger logr.Logger, objects ...infrav1.IdentityRefProvider) (Scope, error) {
var namespace *string
var identityRef *infrav1.OpenStackIdentityReference

for _, o := range objects {
namespace, identityRef = o.GetIdentityRef()
if namespace != nil || identityRef != nil {
break
}
}
namespace, identityRef := f.GetIdentityRefFromObjects(objects...)

if namespace == nil || identityRef == nil {
return nil, fmt.Errorf("unable to get identityRef from provided objects")
Expand Down
15 changes: 15 additions & 0 deletions pkg/scope/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ func NewFactory(maxCacheSize int) Factory {
type Factory interface {
// NewClientScopeFromObject creates a new scope from the first object which returns an OpenStackIdentityRef
NewClientScopeFromObject(ctx context.Context, ctrlClient client.Client, defaultCACert []byte, logger logr.Logger, objects ...infrav1.IdentityRefProvider) (Scope, error)
GetIdentityRefFromObjects(objects ...infrav1.IdentityRefProvider) (*string, *infrav1.OpenStackIdentityReference)
}

type defaultScopeFactory struct{}

func (f *defaultScopeFactory) GetIdentityRefFromObjects(objects ...infrav1.IdentityRefProvider) (*string, *infrav1.OpenStackIdentityReference) {
var namespace *string
var identityRef *infrav1.OpenStackIdentityReference
for _, o := range objects {
namespace, identityRef = o.GetIdentityRef()
if identityRef != nil {
break
}
}
return namespace, identityRef
}

// Scope contains arguments common to most operations.
Expand Down

0 comments on commit dc72b3c

Please sign in to comment.