Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e test to cover region in providerID clusterv1 machine object #2210

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ e2e-templates: $(addprefix $(E2E_NO_ARTIFACT_TEMPLATES_DIR)/, \
cluster-template-without-lb.yaml \
cluster-template.yaml \
cluster-template-flatcar.yaml \
cluster-template-k8s-upgrade.yaml \
cluster-template-k8s-upgrade.yaml \
cluster-template-flatcar-sysext.yaml \
cluster-template-no-bastion.yaml)
cluster-template-no-bastion.yaml \
cluster-template-providerID-with-region-override.yaml \
cluster-template-providerID-with-region-default.yaml)
# Currently no templates that require CI artifacts
# $(addprefix $(E2E_TEMPLATES_DIR)/, add-templates-here.yaml) \

Expand Down
15 changes: 8 additions & 7 deletions controllers/openstackmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
})
openStackMachine.Status.Addresses = addresses

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

result := r.reconcileMachineState(scope, openStackMachine, machine, machineServer)
if result != nil {
return *result, nil
Expand Down Expand Up @@ -573,13 +576,11 @@ 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)
if identityRef == nil {
return nil, fmt.Errorf("unable to get identityRef from provided objects")
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not trying to be picky but I want to ensure we create code that will be maintainable and debuggable. I don't see error management here. What if the identifyRef wasn't found? Shouldn't we return an error?
Or from the callers, if identityRef is nil, we should return an error?

I think either way we need to stop and send a clean error if we can't find an identityRef. Especially because we use a pointer in one of the cases and I like to see pointer checks before using them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, I ask myself about that too.
I took the choose of that the responsibility of caller since objects ...infrav1.IdentityRefProvider could have nil pointer to IdentityRef field, I don't wanna return an error because the value of objects could be nil as a normal behavior.

In my 2 callers functions last parameters are openStackCluster type of infrav1.OpenStackCluster which is not a pointer https://github.com/kubernetes-sigs/cluster-api-provider-openstack/blob/main/api/v1beta1/openstackcluster_types.go#L194 so I'm sure that identityRef variable here and here and correctly defined.

But I'm agree maybe tomorrow it could be different, so I could validate function return from caller for more maintainability.

}

// Scope contains arguments common to most operations.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../default

patches:
- path: patch-machine-template-identity-ref.yaml
target:
kind: OpenStackCluster
- path: patch-kubeadm-config-template-provider-id.yaml
target:
kind: KubeadmConfigTemplate
- path: patch-kubeadm-control-plane-provider-id.yaml
target:
kind: KubeadmControlPlane
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- op: replace
path: /spec/template/spec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- op: replace
path: /spec/kubeadmConfigSpec/initConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
- op: replace
path: /spec/kubeadmConfigSpec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- op: add
path: /spec/identityRef/region
value: ${CAPO_REGION}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../default

patches:
- path: patch-kubeadm-config-template-provider-id.yaml
target:
kind: KubeadmConfigTemplate
- path: patch-kubeadm-control-plane-provider-id.yaml
target:
kind: KubeadmControlPlane
- path: patch-openstack-machine-template-identityRef.yaml
target:
kind: OpenStackMachineTemplate
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- op: replace
path: /spec/template/spec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- op: replace
path: /spec/kubeadmConfigSpec/initConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
- op: replace
path: /spec/kubeadmConfigSpec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- op: add
path: /spec/template/spec/identityRef
value:
cloudName: ${OPENSTACK_CLOUD}
name: ${CLUSTER_NAME}-cloud-config
region: ${CAPO_REGION}
12 changes: 8 additions & 4 deletions test/e2e/shared/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ func getOpenStackClusterFromMachine(ctx context.Context, client client.Client, m
return openStackCluster, err
}

// getIDFromProviderID returns the server ID part of a provider ID string.
func getIDFromProviderID(providerID string) string {
return strings.TrimPrefix(providerID, "openstack:///")
// GetIDFromProviderID returns the server ID part of a provider ID string.
func GetIDFromProviderID(providerID string) string {
providerIDSplit := strings.SplitN(providerID, "://", 2)
Expect(providerIDSplit[0]).To(Equal("openstack"))
providerIDPathSplit := strings.SplitN(providerIDSplit[1], "/", 2)
// providerIDPathSplit[0] contain region name, could be empty
return providerIDPathSplit[1]
}

type OpenStackLogCollector struct {
Expand All @@ -201,7 +205,7 @@ func (o OpenStackLogCollector) CollectMachineLog(ctx context.Context, management
}
ip := m.Status.Addresses[0].Address

srv, err := GetOpenStackServerWithIP(o.E2EContext, getIDFromProviderID(*m.Spec.ProviderID), openStackCluster)
srv, err := GetOpenStackServerWithIP(o.E2EContext, GetIDFromProviderID(*m.Spec.ProviderID), openStackCluster)
if err != nil {
return fmt.Errorf("error getting OpenStack server: %w", err)
}
Expand Down
56 changes: 29 additions & 27 deletions test/e2e/shared/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,35 @@ import (
)

const (
DefaultSSHKeyPairName = "cluster-api-provider-openstack-sigs-k8s-io"
KubeContext = "KUBE_CONTEXT"
KubernetesVersion = "KUBERNETES_VERSION"
CCMPath = "CCM"
CCMResources = "CCM_RESOURCES"
OpenStackBastionFlavorAlt = "OPENSTACK_BASTION_MACHINE_FLAVOR_ALT"
OpenStackCloudYAMLFile = "OPENSTACK_CLOUD_YAML_FILE"
OpenStackCloud = "OPENSTACK_CLOUD"
OpenStackCloudCACertB64 = "OPENSTACK_CLOUD_CACERT_B64"
OpenStackCloudAdmin = "OPENSTACK_CLOUD_ADMIN"
OpenStackFailureDomain = "OPENSTACK_FAILURE_DOMAIN" //nolint:gosec // Linter thinks this could be credentials...
OpenStackFailureDomainAlt = "OPENSTACK_FAILURE_DOMAIN_ALT"
OpenStackVolumeTypeAlt = "OPENSTACK_VOLUME_TYPE_ALT"
OpenStackImageName = "OPENSTACK_IMAGE_NAME"
OpenStackNodeMachineFlavor = "OPENSTACK_NODE_MACHINE_FLAVOR"
SSHUserMachine = "SSH_USER_MACHINE"
FlavorDefault = ""
FlavorNoBastion = "no-bastion"
FlavorWithoutLB = "without-lb"
FlavorMultiNetwork = "multi-network"
FlavorMultiAZ = "multi-az"
FlavorV1alpha7 = "v1alpha7"
FlavorMDRemediation = "md-remediation"
FlavorKCPRemediation = "kcp-remediation"
FlavorFlatcar = "flatcar"
FlavorKubernetesUpgrade = "k8s-upgrade"
FlavorFlatcarSysext = "flatcar-sysext"
DefaultSSHKeyPairName = "cluster-api-provider-openstack-sigs-k8s-io"
KubeContext = "KUBE_CONTEXT"
KubernetesVersion = "KUBERNETES_VERSION"
CCMPath = "CCM"
CCMResources = "CCM_RESOURCES"
OpenStackBastionFlavorAlt = "OPENSTACK_BASTION_MACHINE_FLAVOR_ALT"
OpenStackCloudYAMLFile = "OPENSTACK_CLOUD_YAML_FILE"
OpenStackCloud = "OPENSTACK_CLOUD"
OpenStackCloudCACertB64 = "OPENSTACK_CLOUD_CACERT_B64"
OpenStackCloudAdmin = "OPENSTACK_CLOUD_ADMIN"
OpenStackFailureDomain = "OPENSTACK_FAILURE_DOMAIN" //nolint:gosec // Linter thinks this could be credentials...
OpenStackFailureDomainAlt = "OPENSTACK_FAILURE_DOMAIN_ALT"
OpenStackVolumeTypeAlt = "OPENSTACK_VOLUME_TYPE_ALT"
OpenStackImageName = "OPENSTACK_IMAGE_NAME"
OpenStackNodeMachineFlavor = "OPENSTACK_NODE_MACHINE_FLAVOR"
SSHUserMachine = "SSH_USER_MACHINE"
FlavorDefault = ""
FlavorNoBastion = "no-bastion"
FlavorWithoutLB = "without-lb"
FlavorProviderIDWithRegionOverride = "providerID-with-region-override"
FlavorProviderIDWithRegionDefault = "providerID-with-region-default"
FlavorMultiNetwork = "multi-network"
FlavorMultiAZ = "multi-az"
FlavorV1alpha7 = "v1alpha7"
FlavorMDRemediation = "md-remediation"
FlavorKCPRemediation = "kcp-remediation"
FlavorFlatcar = "flatcar"
FlavorKubernetesUpgrade = "k8s-upgrade"
FlavorFlatcarSysext = "flatcar-sysext"
)

// DefaultScheme returns the default scheme to use for testing.
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/shared/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func AllNodesBeforeSuite(e2eCtx *E2EContext, data []byte) {
SetEnvVar("OPENSTACK_CLOUD_YAML_B64", getEncodedOpenStackCloudYAML(openStackCloudYAMLFile), true)
SetEnvVar("OPENSTACK_CLOUD_PROVIDER_CONF_B64", getEncodedOpenStackCloudProviderConf(openStackCloudYAMLFile, openStackCloud), true)
SetEnvVar("OPENSTACK_SSH_KEY_NAME", DefaultSSHKeyPairName, false)
clouds := getParsedOpenStackCloudYAML(openStackCloudYAMLFile)
cloud, ok := clouds.Clouds[openStackCloud]
Expect(ok).To(BeTrue())
SetEnvVar("CAPO_REGION", cloud.RegionName, false)
}

// AllNodesAfterSuite is cleanup that runs on all ginkgo parallel nodes after the test suite finishes.
Expand Down
Loading