@@ -10,6 +10,7 @@ import (
1010 "github.com/go-logr/logr/testr"
1111 "github.com/stretchr/testify/assert"
1212 v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
13+ "k8s.io/utils/ptr"
1314 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1415
1516 carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
@@ -58,7 +59,7 @@ func TestNewConfigurationCheck(t *testing.T) {
5859 Name : carenv1 .ClusterConfigVariableName ,
5960 Value : v1.JSON {
6061 Raw : []byte (
61- `{"controlPlane": {"nutanix": {"prismElement ": {"address ": "pe.example.com "}}}}` ,
62+ `{"controlPlane": {"nutanix": {"cluster ": {"name ": "cluster-from-variable "}}}}` ,
6263 ),
6364 },
6465 },
@@ -96,7 +97,7 @@ func TestNewConfigurationCheck(t *testing.T) {
9697 Name : carenv1 .WorkerConfigVariableName ,
9798 Value : v1.JSON {
9899 Raw : []byte (
99- `{"nutanix": {"prismElement ": {"address ": "pe.example.com "}}}` ,
100+ `{"nutanix": {"cluster ": {"name ": "cluster-from-override "}}}` ,
100101 ),
101102 },
102103 },
@@ -183,8 +184,8 @@ func TestNewConfigurationCheck(t *testing.T) {
183184 InternalError : true ,
184185 Causes : []preflight.Cause {
185186 {
186- Message : "Failed to unmarshal topology machineDeployment variable \" workerConfig\" : failed to unmarshal json: invalid character 'i' looking for beginning of object key string. Review the Cluster." , ///nolint:lll // The message is long.
187- Field :
"$.spec.topology.workers.machineDeployments[[email protected] ==\" md-0\" ].variables[[email protected] =workerConfig].value.nutanix.machineDetails" , ///nolint:lll // The field is long. 187+ Message : "Failed to unmarshal variable \" workerConfig\" : failed to unmarshal json: invalid character 'i' looking for beginning of object key string. Review the Cluster." , ///nolint:lll // The message is long.
188+ Field :
"$.spec.topology.workers.machineDeployments[[email protected] ==\" md-0\" ].variables[[email protected] ==' workerConfig' ].value.nutanix" , ///nolint:lll // The field is long. 188189 },
189190 },
190191 },
@@ -238,7 +239,7 @@ func TestNewConfigurationCheck(t *testing.T) {
238239 Name : carenv1 .WorkerConfigVariableName ,
239240 Value : v1.JSON {
240241 Raw : []byte (
241- `{"nutanix": {"prismElement ": {"address ": "pe1.example.com "}}}` ,
242+ `{"nutanix": {"cluster ": {"name ": "cluster-from-override "}}}` ,
242243 ),
243244 },
244245 },
@@ -253,7 +254,7 @@ func TestNewConfigurationCheck(t *testing.T) {
253254 Name : carenv1 .WorkerConfigVariableName ,
254255 Value : v1.JSON {
255256 Raw : []byte (
256- `{"nutanix": {"prismElement ": {"address ": "pe2.example.com "}}}` ,
257+ `{"nutanix": {"cluster ": {"name ": "cluster-from-override "}}}` ,
257258 ),
258259 },
259260 },
@@ -272,6 +273,89 @@ func TestNewConfigurationCheck(t *testing.T) {
272273 expectedWorkerNodeConfigSpecMapNotEmpty : true ,
273274 expectedWorkerNodeConfigSpecMapEntryCount : 2 ,
274275 },
276+ {
277+ name : "worker config from cluster variables" ,
278+ cluster : & clusterv1.Cluster {
279+ Spec : clusterv1.ClusterSpec {
280+ Topology : & clusterv1.Topology {
281+ Variables : []clusterv1.ClusterVariable {
282+ {
283+ Name : carenv1 .ClusterConfigVariableName ,
284+ Value : v1.JSON {
285+ Raw : []byte (`{}` ),
286+ },
287+ },
288+ {
289+ Name : carenv1 .WorkerConfigVariableName ,
290+ Value : v1.JSON {
291+ Raw : []byte (
292+ `{"nutanix": {"cluster": {"name": "cluster-from-variable"}}}` ,
293+ ),
294+ },
295+ },
296+ },
297+ Workers : & clusterv1.WorkersTopology {
298+ MachineDeployments : []clusterv1.MachineDeploymentTopology {
299+ {
300+ Name : "md-0" ,
301+ },
302+ },
303+ },
304+ },
305+ },
306+ },
307+ expectedResult : preflight.CheckResult {
308+ Allowed : true ,
309+ },
310+ expectedNutanixClusterConfigSpec : false ,
311+ expectedWorkerNodeConfigSpecMapNotEmpty : true ,
312+ expectedWorkerNodeConfigSpecMapEntryCount : 1 ,
313+ },
314+ {
315+ name : "worker config with failure domain" ,
316+ cluster : & clusterv1.Cluster {
317+ Spec : clusterv1.ClusterSpec {
318+ Topology : & clusterv1.Topology {
319+ Variables : []clusterv1.ClusterVariable {
320+ {
321+ Name : carenv1 .ClusterConfigVariableName ,
322+ Value : v1.JSON {
323+ Raw : []byte (
324+ `{"nutanix": {"failureDomains": ["fd-1", "fd-2", "fd-3"]}}` ,
325+ ),
326+ },
327+ },
328+ },
329+ Workers : & clusterv1.WorkersTopology {
330+ MachineDeployments : []clusterv1.MachineDeploymentTopology {
331+ {
332+ Name : "md-0" ,
333+ Variables : & clusterv1.MachineDeploymentVariables {
334+ Overrides : []clusterv1.ClusterVariable {
335+ {
336+ Name : carenv1 .WorkerConfigVariableName ,
337+ Value : v1.JSON {
338+ Raw : []byte (
339+ `{"nutanix": {"cluster": {"name": "worker-cluster"}, "subnets": [{"name": "worker-subnet"}]}}` ,
340+ ),
341+ },
342+ },
343+ },
344+ },
345+ FailureDomain : ptr .To ("fd-1" ),
346+ },
347+ },
348+ },
349+ },
350+ },
351+ },
352+ expectedResult : preflight.CheckResult {
353+ Allowed : true ,
354+ },
355+ expectedNutanixClusterConfigSpec : true ,
356+ expectedWorkerNodeConfigSpecMapNotEmpty : true ,
357+ expectedWorkerNodeConfigSpecMapEntryCount : 1 ,
358+ },
275359 {
276360 name : "worker config without nutanix field" ,
277361 cluster : & clusterv1.Cluster {
@@ -342,6 +426,77 @@ func TestNewConfigurationCheck(t *testing.T) {
342426 expectedWorkerNodeConfigSpecMapNotEmpty : false ,
343427 expectedWorkerNodeConfigSpecMapEntryCount : 0 ,
344428 },
429+ {
430+ name : "mixed worker scenarios - with/without overrides and with/without failure domains" ,
431+ cluster : & clusterv1.Cluster {
432+ Spec : clusterv1.ClusterSpec {
433+ Topology : & clusterv1.Topology {
434+ Variables : []clusterv1.ClusterVariable {
435+ {
436+ Name : carenv1 .ClusterConfigVariableName ,
437+ Value : v1.JSON {
438+ Raw : []byte (`{"nutanix": {"failureDomains": ["fd-1", "fd-2", "fd-3"]}}` ),
439+ },
440+ },
441+ {
442+ Name : carenv1 .WorkerConfigVariableName ,
443+ Value : v1.JSON {
444+ Raw : []byte (`{"nutanix": {"cluster": {"name": "cluster-from-variable"}}}` ),
445+ },
446+ },
447+ },
448+ Workers : & clusterv1.WorkersTopology {
449+ MachineDeployments : []clusterv1.MachineDeploymentTopology {
450+ {
451+ Name : "md-with-overrides" ,
452+ Variables : & clusterv1.MachineDeploymentVariables {
453+ Overrides : []clusterv1.ClusterVariable {
454+ {
455+ Name : carenv1 .WorkerConfigVariableName ,
456+ Value : v1.JSON {
457+ Raw : []byte (
458+ `{"nutanix": {"cluster": {"name": "cluster-from-override"}}}` ,
459+ ),
460+ },
461+ },
462+ },
463+ },
464+ },
465+ {
466+ Name : "md-without-overrides" ,
467+ },
468+ {
469+ Name : "md-with-overrides-and-fd" ,
470+ FailureDomain : ptr .To ("fd-1" ),
471+ Variables : & clusterv1.MachineDeploymentVariables {
472+ Overrides : []clusterv1.ClusterVariable {
473+ {
474+ Name : carenv1 .WorkerConfigVariableName ,
475+ Value : v1.JSON {
476+ Raw : []byte (
477+ `{"nutanix": {"cluster": {"name": "cluster-from-override"}}}` ,
478+ ),
479+ },
480+ },
481+ },
482+ },
483+ },
484+ {
485+ Name : "md-without-overrides-and-fd" ,
486+ FailureDomain : ptr .To ("fd-1" ),
487+ },
488+ },
489+ },
490+ },
491+ },
492+ },
493+ expectedResult : preflight.CheckResult {
494+ Allowed : true ,
495+ },
496+ expectedNutanixClusterConfigSpec : true ,
497+ expectedWorkerNodeConfigSpecMapNotEmpty : true ,
498+ expectedWorkerNodeConfigSpecMapEntryCount : 4 ,
499+ },
345500 }
346501
347502 for _ , tt := range tests {
0 commit comments