@@ -11,13 +11,15 @@ import (
11
11
"net/netip"
12
12
13
13
v1 "k8s.io/api/admission/v1"
14
+ apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
14
15
"k8s.io/apimachinery/pkg/util/validation/field"
15
16
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
16
17
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
17
18
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
18
19
19
20
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
20
21
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
22
+ commonvariables "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
21
23
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils"
22
24
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/helpers"
23
25
)
@@ -220,41 +222,69 @@ func validateWorkerFailureDomainConfig(
220
222
"workerConfig" ,
221
223
)
222
224
223
- // Get the machineDetails from cluster variable "workerConfig" if it is configured
224
- defaultWorkerConfig , err := variables .UnmarshalWorkerConfigVariable (cluster .Spec .Topology .Variables )
225
- if err != nil {
226
- fldErrs = append (fldErrs , field .InternalError (workerConfigVarPath ,
227
- fmt .Errorf ("failed to unmarshal cluster topology variable %q: %w" , v1alpha1 .WorkerConfigVariableName , err )))
228
- }
225
+ // Merge the global cluster variables with the worker config overrides.
226
+ mdVariables := commonvariables .ClusterVariablesToVariablesMap (cluster .Spec .Topology .Variables )
229
227
230
228
if cluster .Spec .Topology .Workers != nil {
231
229
for i := range cluster .Spec .Topology .Workers .MachineDeployments {
232
230
md := cluster .Spec .Topology .Workers .MachineDeployments [i ]
233
231
hasFailureDomain := md .FailureDomain != nil && * md .FailureDomain != ""
232
+ wcfgPath := workerConfigVarPath
234
233
235
- // Get the machineDetails from the overrides variable "workerConfig" if it is configured,
236
- // otherwise use the defaultWorkerConfig if it is configured.
237
- var workerConfig * variables.WorkerNodeConfigSpec
234
+ // Get the md variable overrides.
235
+ var mdVariableOverrides map [string ]apiextensionsv1.JSON
238
236
if md .Variables != nil && len (md .Variables .Overrides ) > 0 {
239
- workerConfig , err = variables .UnmarshalWorkerConfigVariable (md .Variables .Overrides )
240
- if err != nil {
241
- fldErrs = append (fldErrs , field .InternalError (
242
- workerConfigMDVarOverridePath ,
243
- fmt .Errorf (
244
- "failed to unmarshal worker overrides variable %q: %w" ,
245
- v1alpha1 .WorkerConfigVariableName ,
246
- err ,
247
- ),
248
- ))
237
+ wcfgPath = workerConfigMDVarOverridePath
238
+ mdVariableOverrides = commonvariables .ClusterVariablesToVariablesMap (md .Variables .Overrides )
239
+
240
+ // If mdVariables is nil, initialize it with mdVariableOverrides, otherwise merge global and
241
+ // variable overrides.
242
+ if mdVariables == nil {
243
+ mdVariables = mdVariableOverrides
244
+ } else {
245
+ // Merge global and variable overrides if global variables are present.
246
+ mergedVariables , err := commonvariables .MergeVariableOverridesWithGlobal (
247
+ mdVariableOverrides ,
248
+ mdVariables ,
249
+ )
250
+ if err != nil {
251
+ fldErrs = append (fldErrs , field .InternalError (
252
+ workerConfigMDVarOverridePath ,
253
+ fmt .Errorf (
254
+ "failed to merge global and worker variable overrides for MachineDeployment %q: %w" ,
255
+ md .Name ,
256
+ err ,
257
+ ),
258
+ ))
259
+ }
260
+
261
+ mdVariables = mergedVariables
249
262
}
250
263
}
251
264
252
- wcfgPath := workerConfigMDVarOverridePath
253
- if workerConfig == nil {
254
- workerConfig = defaultWorkerConfig
255
- wcfgPath = workerConfigVarPath
265
+ workerConfigVarJSON , workerConfigPresent := mdVariables [v1alpha1 .WorkerConfigVariableName ]
266
+ if ! workerConfigPresent {
267
+ continue
268
+ }
269
+
270
+ workerConfigClusterVar := clusterv1.ClusterVariable {
271
+ Name : v1alpha1 .WorkerConfigVariableName ,
272
+ Value : * workerConfigVarJSON .DeepCopy (),
273
+ }
274
+
275
+ workerConfig := variables.WorkerNodeConfigSpec {}
276
+ if err := variables .UnmarshalClusterVariable (& workerConfigClusterVar , & workerConfig ); err != nil {
277
+ fldErrs = append (fldErrs , field .InternalError (
278
+ workerConfigMDVarOverridePath ,
279
+ fmt .Errorf (
280
+ "failed to unmarshal worker overrides variable %q: %w" ,
281
+ v1alpha1 .WorkerConfigVariableName ,
282
+ err ,
283
+ ),
284
+ ))
256
285
}
257
- if workerConfig == nil || workerConfig .Nutanix == nil {
286
+
287
+ if workerConfig .Nutanix == nil {
258
288
continue
259
289
}
260
290
0 commit comments