Skip to content

Commit b5fa8d3

Browse files
committed
test(e2e): cover region in providerID clusterv1 machine object
Signed-off-by: MatthieuFin <[email protected]>
1 parent 5b66f35 commit b5fa8d3

17 files changed

+248
-37
lines changed

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ e2e-templates: $(addprefix $(E2E_NO_ARTIFACT_TEMPLATES_DIR)/, \
176176
cluster-template-flatcar.yaml \
177177
cluster-template-k8s-upgrade.yaml \
178178
cluster-template-flatcar-sysext.yaml \
179-
cluster-template-no-bastion.yaml)
179+
cluster-template-no-bastion.yaml \
180+
cluster-template-providerID-with-region-override.yaml \
181+
cluster-template-providerID-with-region-default.yaml)
180182
# Currently no templates that require CI artifacts
181183
# $(addprefix $(E2E_TEMPLATES_DIR)/, add-templates-here.yaml) \
182184

controllers/openstackmachine_controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,9 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
391391
Address: instanceStatus.Name(),
392392
})
393393
openStackMachine.Status.Addresses = addresses
394+
if openStackMachine.Spec.IdentityRef == nil {
395+
openStackMachine.Spec.IdentityRef = &openStackCluster.Spec.IdentityRef
396+
}
394397

395398
result := r.reconcileMachineState(scope, openStackMachine, machine, machineServer)
396399
if result != nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ../default
5+
6+
patches:
7+
- path: patch-machine-template-identity-ref.yaml
8+
target:
9+
kind: OpenStackCluster
10+
- path: patch-kubeadm-config-template-provider-id.yaml
11+
target:
12+
kind: KubeadmConfigTemplate
13+
- path: patch-kubeadm-control-plane-provider-id.yaml
14+
target:
15+
kind: KubeadmControlPlane
16+
#- path: patch-openstack-machine-template-identityRef.yaml
17+
# target:
18+
# kind: OpenStackMachineTemplate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- op: replace
3+
path: /spec/template/spec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
4+
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- op: replace
3+
path: /spec/kubeadmConfigSpec/initConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
4+
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
5+
- op: replace
6+
path: /spec/kubeadmConfigSpec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
7+
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
8+
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- op: add
3+
path: /spec/identityRef/region
4+
value: ${CAPO_REGION}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- op: add
3+
path: /spec/template/spec/identityRef
4+
value:
5+
cloudName: ${OPENSTACK_CLOUD}
6+
name: ${CLUSTER_NAME}-cloud-config
7+
region: ${CAPO_REGION}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ../default
5+
6+
patches:
7+
#- path: patch-machine-template-identity-ref.yaml
8+
# target:
9+
# kind: OpenStackCluster
10+
- path: patch-kubeadm-config-template-provider-id.yaml
11+
target:
12+
kind: KubeadmConfigTemplate
13+
- path: patch-kubeadm-control-plane-provider-id.yaml
14+
target:
15+
kind: KubeadmControlPlane
16+
- path: patch-openstack-machine-template-identityRef.yaml
17+
target:
18+
kind: OpenStackMachineTemplate
19+
#- path: patch-no-bastion.yaml
20+
# target:
21+
# kind: OpenStackCluster
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- op: replace
3+
path: /spec/template/spec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
4+
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- op: replace
3+
path: /spec/kubeadmConfigSpec/initConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
4+
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
5+
- op: replace
6+
path: /spec/kubeadmConfigSpec/joinConfiguration/nodeRegistration/kubeletExtraArgs/provider-id
7+
value: "openstack://${CAPO_REGION}/{{ instance_id }}"
8+
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- op: add
3+
path: /spec/identityRef/region
4+
value: ${CAPO_REGION}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- op: replace
3+
path: /spec/bastion/enabled
4+
value: false
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- op: add
3+
path: /spec/template/spec/identityRef
4+
value:
5+
cloudName: ${OPENSTACK_CLOUD}
6+
name: ${CLUSTER_NAME}-cloud-config
7+
region: ${CAPO_REGION}
8+

test/e2e/shared/common.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ func getOpenStackClusterFromMachine(ctx context.Context, client client.Client, m
174174
return openStackCluster, err
175175
}
176176

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

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

204-
srv, err := GetOpenStackServerWithIP(o.E2EContext, getIDFromProviderID(*m.Spec.ProviderID), openStackCluster)
208+
srv, err := GetOpenStackServerWithIP(o.E2EContext, GetIDFromProviderID(*m.Spec.ProviderID), openStackCluster)
205209
if err != nil {
206210
return fmt.Errorf("error getting OpenStack server: %w", err)
207211
}

test/e2e/shared/defaults.go

+29-27
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,35 @@ import (
3434
)
3535

3636
const (
37-
DefaultSSHKeyPairName = "cluster-api-provider-openstack-sigs-k8s-io"
38-
KubeContext = "KUBE_CONTEXT"
39-
KubernetesVersion = "KUBERNETES_VERSION"
40-
CCMPath = "CCM"
41-
CCMResources = "CCM_RESOURCES"
42-
OpenStackBastionFlavorAlt = "OPENSTACK_BASTION_MACHINE_FLAVOR_ALT"
43-
OpenStackCloudYAMLFile = "OPENSTACK_CLOUD_YAML_FILE"
44-
OpenStackCloud = "OPENSTACK_CLOUD"
45-
OpenStackCloudCACertB64 = "OPENSTACK_CLOUD_CACERT_B64"
46-
OpenStackCloudAdmin = "OPENSTACK_CLOUD_ADMIN"
47-
OpenStackFailureDomain = "OPENSTACK_FAILURE_DOMAIN" //nolint:gosec // Linter thinks this could be credentials...
48-
OpenStackFailureDomainAlt = "OPENSTACK_FAILURE_DOMAIN_ALT"
49-
OpenStackVolumeTypeAlt = "OPENSTACK_VOLUME_TYPE_ALT"
50-
OpenStackImageName = "OPENSTACK_IMAGE_NAME"
51-
OpenStackNodeMachineFlavor = "OPENSTACK_NODE_MACHINE_FLAVOR"
52-
SSHUserMachine = "SSH_USER_MACHINE"
53-
FlavorDefault = ""
54-
FlavorNoBastion = "no-bastion"
55-
FlavorWithoutLB = "without-lb"
56-
FlavorMultiNetwork = "multi-network"
57-
FlavorMultiAZ = "multi-az"
58-
FlavorV1alpha7 = "v1alpha7"
59-
FlavorMDRemediation = "md-remediation"
60-
FlavorKCPRemediation = "kcp-remediation"
61-
FlavorFlatcar = "flatcar"
62-
FlavorKubernetesUpgrade = "k8s-upgrade"
63-
FlavorFlatcarSysext = "flatcar-sysext"
37+
DefaultSSHKeyPairName = "cluster-api-provider-openstack-sigs-k8s-io"
38+
KubeContext = "KUBE_CONTEXT"
39+
KubernetesVersion = "KUBERNETES_VERSION"
40+
CCMPath = "CCM"
41+
CCMResources = "CCM_RESOURCES"
42+
OpenStackBastionFlavorAlt = "OPENSTACK_BASTION_MACHINE_FLAVOR_ALT"
43+
OpenStackCloudYAMLFile = "OPENSTACK_CLOUD_YAML_FILE"
44+
OpenStackCloud = "OPENSTACK_CLOUD"
45+
OpenStackCloudCACertB64 = "OPENSTACK_CLOUD_CACERT_B64"
46+
OpenStackCloudAdmin = "OPENSTACK_CLOUD_ADMIN"
47+
OpenStackFailureDomain = "OPENSTACK_FAILURE_DOMAIN" //nolint:gosec // Linter thinks this could be credentials...
48+
OpenStackFailureDomainAlt = "OPENSTACK_FAILURE_DOMAIN_ALT"
49+
OpenStackVolumeTypeAlt = "OPENSTACK_VOLUME_TYPE_ALT"
50+
OpenStackImageName = "OPENSTACK_IMAGE_NAME"
51+
OpenStackNodeMachineFlavor = "OPENSTACK_NODE_MACHINE_FLAVOR"
52+
SSHUserMachine = "SSH_USER_MACHINE"
53+
FlavorDefault = ""
54+
FlavorNoBastion = "no-bastion"
55+
FlavorWithoutLB = "without-lb"
56+
FlavorProviderIDWithRegionOverride = "providerID-with-region-override"
57+
FlavorProviderIDWithRegionDefault = "providerID-with-region-default"
58+
FlavorMultiNetwork = "multi-network"
59+
FlavorMultiAZ = "multi-az"
60+
FlavorV1alpha7 = "v1alpha7"
61+
FlavorMDRemediation = "md-remediation"
62+
FlavorKCPRemediation = "kcp-remediation"
63+
FlavorFlatcar = "flatcar"
64+
FlavorKubernetesUpgrade = "k8s-upgrade"
65+
FlavorFlatcarSysext = "flatcar-sysext"
6466
)
6567

6668
// DefaultScheme returns the default scheme to use for testing.

test/e2e/shared/suite.go

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ func AllNodesBeforeSuite(e2eCtx *E2EContext, data []byte) {
201201
SetEnvVar("OPENSTACK_CLOUD_YAML_B64", getEncodedOpenStackCloudYAML(openStackCloudYAMLFile), true)
202202
SetEnvVar("OPENSTACK_CLOUD_PROVIDER_CONF_B64", getEncodedOpenStackCloudProviderConf(openStackCloudYAMLFile, openStackCloud), true)
203203
SetEnvVar("OPENSTACK_SSH_KEY_NAME", DefaultSSHKeyPairName, false)
204+
clouds := getParsedOpenStackCloudYAML(openStackCloudYAMLFile)
205+
cloud, ok := clouds.Clouds[openStackCloud]
206+
Expect(ok).To(BeTrue())
207+
SetEnvVar("CAPO_REGION", cloud.RegionName, false)
204208
}
205209

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

test/e2e/suites/e2e/e2e_test.go

+105-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ import (
5959
"sigs.k8s.io/cluster-api-provider-openstack/test/e2e/shared"
6060
)
6161

62-
const specName = "e2e"
62+
const (
63+
specName = "e2e"
64+
testSecurityGroupName = "testSecGroup"
65+
)
6366

6467
// Additional images required for flatcar tests.
6568
func flatcarImages(e2eCtx *shared.E2EContext) []shared.DownloadImage {
@@ -510,7 +513,6 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
510513

511514
shared.Logf("Creating MachineDeployment with custom port options")
512515
md3Name := clusterName + "-md-3"
513-
testSecurityGroupName := "testSecGroup"
514516
// create required test security group
515517
var securityGroupCleanup func(ctx context.Context)
516518
securityGroupCleanup, err = shared.CreateOpenStackSecurityGroup(ctx, e2eCtx, testSecurityGroupName, "Test security group")
@@ -679,6 +681,106 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
679681
})
680682
})
681683

684+
Describe("Workload cluster providerID identityRef.Region field per OpenStackMachineTemplate override OpenStackCluster identityRef.Region field", func() {
685+
It("should create machines with identityRef.Region field which override CLuster one", func(ctx context.Context) {
686+
shared.Logf("Creating a cluster")
687+
clusterName := fmt.Sprintf("cluster-%s", namespace.Name)
688+
configCluster := defaultConfigCluster(clusterName, namespace.Name)
689+
configCluster.ControlPlaneMachineCount = ptr.To(int64(1))
690+
configCluster.WorkerMachineCount = ptr.To(int64(0))
691+
configCluster.Flavor = shared.FlavorProviderIDWithRegionOverride
692+
createCluster(ctx, configCluster, clusterResources)
693+
md := clusterResources.MachineDeployments
694+
695+
workerMachines := framework.GetMachinesByMachineDeployments(ctx, framework.GetMachinesByMachineDeploymentsInput{
696+
Lister: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
697+
ClusterName: clusterName,
698+
Namespace: namespace.Name,
699+
MachineDeployment: *md[0],
700+
})
701+
controlPlaneMachines := framework.GetControlPlaneMachinesByCluster(ctx, framework.GetControlPlaneMachinesByClusterInput{
702+
Lister: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
703+
ClusterName: clusterName,
704+
Namespace: namespace.Name,
705+
})
706+
Expect(workerMachines).To(HaveLen(0))
707+
Expect(controlPlaneMachines).To(HaveLen(1))
708+
709+
shared.Logf("Creating MachineDeployment with identityRef Region field defined")
710+
// create required test security group
711+
var securityGroupCleanup func(ctx context.Context)
712+
securityGroupCleanup, err = shared.CreateOpenStackSecurityGroup(ctx, e2eCtx, testSecurityGroupName, "Test security group")
713+
Expect(err).To(BeNil())
714+
postClusterCleanup = append(postClusterCleanup, securityGroupCleanup)
715+
716+
_, clientOpts, _, err := shared.GetTenantProviderClient(e2eCtx)
717+
Expect(err).To(BeNil(), "Cannot create providerClient")
718+
719+
controlPlaneMachine := controlPlaneMachines[0]
720+
721+
shared.Logf("Fetching serverID control plane")
722+
allControlPlaneServers, err := shared.DumpOpenStackServers(e2eCtx, servers.ListOpts{Name: controlPlaneMachine.Name})
723+
Expect(err).To(BeNil())
724+
Expect(allControlPlaneServers).To(HaveLen(1))
725+
controlPlaneServerID := allControlPlaneServers[0].ID
726+
Expect(err).To(BeNil())
727+
728+
Expect(*(controlPlaneMachine.Spec.ProviderID)).To(
729+
Equal(fmt.Sprintf("openstack://%s/%s", clientOpts.RegionName, controlPlaneServerID)),
730+
fmt.Sprintf("ControlPlane machine should have providerID string `openstack://%s/uuid`", clientOpts.RegionName))
731+
})
732+
})
733+
734+
Describe("Workload cluster providerID identityRef.Region field inherited from OpenStackCluster identityRef.Region field", func() {
735+
It("should create machines without identityRef.Region field and inherit from Cluster object", func(ctx context.Context) {
736+
shared.Logf("Creating a cluster")
737+
clusterName := fmt.Sprintf("cluster-%s", namespace.Name)
738+
configCluster := defaultConfigCluster(clusterName, namespace.Name)
739+
configCluster.ControlPlaneMachineCount = ptr.To(int64(1))
740+
configCluster.WorkerMachineCount = ptr.To(int64(0))
741+
configCluster.Flavor = shared.FlavorProviderIDWithRegionDefault
742+
createCluster(ctx, configCluster, clusterResources)
743+
md := clusterResources.MachineDeployments
744+
745+
workerMachines := framework.GetMachinesByMachineDeployments(ctx, framework.GetMachinesByMachineDeploymentsInput{
746+
Lister: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
747+
ClusterName: clusterName,
748+
Namespace: namespace.Name,
749+
MachineDeployment: *md[0],
750+
})
751+
controlPlaneMachines := framework.GetControlPlaneMachinesByCluster(ctx, framework.GetControlPlaneMachinesByClusterInput{
752+
Lister: e2eCtx.Environment.BootstrapClusterProxy.GetClient(),
753+
ClusterName: clusterName,
754+
Namespace: namespace.Name,
755+
})
756+
Expect(workerMachines).To(HaveLen(0))
757+
Expect(controlPlaneMachines).To(HaveLen(1))
758+
759+
shared.Logf("Creating MachineDeployment with identityRef Region field defined")
760+
// create required test security group
761+
var securityGroupCleanup func(ctx context.Context)
762+
securityGroupCleanup, err = shared.CreateOpenStackSecurityGroup(ctx, e2eCtx, testSecurityGroupName, "Test security group")
763+
Expect(err).To(BeNil())
764+
postClusterCleanup = append(postClusterCleanup, securityGroupCleanup)
765+
766+
_, clientOpts, _, err := shared.GetTenantProviderClient(e2eCtx)
767+
Expect(err).To(BeNil(), "Cannot create providerClient")
768+
769+
controlPlaneMachine := controlPlaneMachines[0]
770+
771+
shared.Logf("Fetching serverID control plane")
772+
allControlPlaneServers, err := shared.DumpOpenStackServers(e2eCtx, servers.ListOpts{Name: controlPlaneMachine.Name})
773+
Expect(err).To(BeNil())
774+
Expect(allControlPlaneServers).To(HaveLen(1))
775+
controlPlaneServerID := allControlPlaneServers[0].ID
776+
Expect(err).To(BeNil())
777+
778+
Expect(*(controlPlaneMachine.Spec.ProviderID)).To(
779+
Equal(fmt.Sprintf("openstack://%s/%s", clientOpts.RegionName, controlPlaneServerID)),
780+
fmt.Sprintf("ControlPlane machine should have providerID string `openstack://%s/uuid`", clientOpts.RegionName))
781+
})
782+
})
783+
682784
Describe("Workload cluster (multiple attached networks)", func() {
683785
var (
684786
clusterName string
@@ -1035,9 +1137,7 @@ func getInstanceIDForMachine(machine *clusterv1.Machine) string {
10351137
providerID := machine.Spec.ProviderID
10361138
Expect(providerID).NotTo(BeNil())
10371139

1038-
providerIDSplit := strings.SplitN(*providerID, ":///", 2)
1039-
Expect(providerIDSplit[0]).To(Equal("openstack"))
1040-
return providerIDSplit[1]
1140+
return shared.GetIDFromProviderID(*machine.Spec.ProviderID)
10411141
}
10421142

10431143
func isErrorEventExists(namespace, machineDeploymentName, eventReason, errorMsg string, eList *corev1.EventList) bool {

0 commit comments

Comments
 (0)