@@ -12,16 +12,18 @@ import (
12
12
13
13
prismv4 "github.com/nutanix-cloud-native/prism-go-client/v4"
14
14
15
+ carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
16
+ "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
15
17
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
16
- preflightutil "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/util"
17
18
)
18
19
19
20
type Checker struct {
20
- client ctrlclient.Client
21
- cluster * clusterv1.Cluster
22
-
23
- nutanixClient * prismv4.Client
24
- variablesGetter * preflightutil.VariablesGetter
21
+ client ctrlclient.Client
22
+ cluster * clusterv1.Cluster
23
+ nutanixSpec * carenv1.NutanixSpec
24
+ nutanixControlPlaneNodeSpec * carenv1.NutanixNodeSpec
25
+ nutanixNodeSpecByMachineDeploymentName map [string ]* carenv1.NutanixNodeSpec
26
+ v4client * prismv4.Client
25
27
}
26
28
27
29
func (n * Checker ) Init (
@@ -31,49 +33,114 @@ func (n *Checker) Init(
31
33
) []preflight.Check {
32
34
n .client = client
33
35
n .cluster = cluster
34
- n .variablesGetter = preflightutil .NewVariablesGetter (cluster )
35
36
36
- // Initialize the Nutanix client. If it fails, return a check that indicates the error.
37
- clusterConfig , err := n .variablesGetter .ClusterConfig ()
37
+ var err error
38
+
39
+ n .nutanixSpec , n .nutanixControlPlaneNodeSpec , n .nutanixNodeSpecByMachineDeploymentName , err = getData (cluster )
38
40
if err != nil {
39
41
return []preflight.Check {
40
- func (ctx context.Context ) preflight.CheckResult {
41
- return preflight.CheckResult {
42
- Name : "NutanixClientInitialization" ,
43
- Allowed : false ,
44
- Error : true ,
45
- Causes : []preflight.Cause {
46
- {
47
- Message : fmt .Sprintf ("failed to read clusterConfig variable: %s" , err ),
48
- Field : "cluster.spec.topology.variables" ,
49
- },
50
- },
51
- }
52
- },
42
+ errorCheck ("TBD" , "TBD" ),
53
43
}
54
44
}
55
45
56
- n .nutanixClient , err = v4client (ctx , client , cluster , clusterConfig .Nutanix )
57
- // TODO Verify the credentials by making a users API call.
46
+ credentials , err := getCredentials (ctx , client , cluster , n .nutanixSpec )
58
47
if err != nil {
59
48
return []preflight.Check {
49
+ errorCheck (
50
+ fmt .Sprintf ("failed to get Nutanix credentials: %s" , err ),
51
+ "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials" ,
52
+ ),
53
+ }
54
+ }
55
+
56
+ n .v4client , err = v4client (credentials , n .nutanixSpec )
57
+ if err != nil {
58
+ return []preflight.Check {
59
+ errorCheck (
60
+ fmt .Sprintf ("failed to initialize Nutanix v4 client: %s" , err ),
61
+ "cluster.spec.topology.variables[.name=clusterConfig].nutanix" ,
62
+ ),
63
+ }
64
+ }
65
+
66
+ // Initialize checks.
67
+ checks := []preflight.Check {}
68
+ if n .nutanixControlPlaneNodeSpec != nil {
69
+ checks = append (checks ,
70
+ func (ctx context.Context ) preflight.CheckResult {
71
+ return n .vmImageCheckForMachineDetails (
72
+ & n .nutanixControlPlaneNodeSpec .MachineDetails ,
73
+ "cluster.spec.topology[.name=clusterConfig].value.controlPlane.nutanix.machineDetails" ,
74
+ )
75
+ },
76
+ )
77
+ }
78
+ for mdName , nodeSpec := range n .nutanixNodeSpecByMachineDeploymentName {
79
+ checks = append (checks ,
60
80
func (ctx context.Context ) preflight.CheckResult {
61
- return preflight.CheckResult {
62
- Name : "NutanixClientInitialization" ,
63
- Allowed : false ,
64
- Error : true ,
65
- Causes : []preflight.Cause {
66
- {
67
- Message : fmt .Sprintf ("failed to initialize Nutanix client: %s" , err ),
68
- Field : "cluster.spec.topology.variables[.name=clusterConfig].nutanix" ,
69
- },
70
- },
71
- }
81
+ return n .vmImageCheckForMachineDetails (
82
+ & nodeSpec .MachineDetails ,
83
+ fmt .Sprintf (
84
+ "cluster.spec.topology.workers.machineDeployments[.name=%s]" +
85
+ ".overrides[.name=workerConfig].value.nutanix.machineDetails" ,
86
+ mdName ,
87
+ ),
88
+ )
89
+ },
90
+ )
91
+ }
92
+ return checks
93
+ }
94
+
95
+ func errorCheck (msg , field string ) preflight.Check {
96
+ return func (ctx context.Context ) preflight.CheckResult {
97
+ return preflight.CheckResult {
98
+ Name : "Nutanix" ,
99
+ Allowed : false ,
100
+ Error : true ,
101
+ Causes : []preflight.Cause {
102
+ {
103
+ Message : msg ,
104
+ Field : field ,
105
+ },
72
106
},
73
107
}
74
108
}
109
+ }
110
+
111
+ func getData (
112
+ cluster * clusterv1.Cluster ,
113
+ ) (* carenv1.NutanixSpec , * carenv1.NutanixNodeSpec , map [string ]* carenv1.NutanixNodeSpec , error ) {
114
+ nutanixSpec := & carenv1.NutanixSpec {}
115
+ controlPlaneNutanixNodeSpec := & carenv1.NutanixNodeSpec {}
116
+ nutanixNodeSpecByMachineDeploymentName := make (map [string ]* carenv1.NutanixNodeSpec )
75
117
76
- return []preflight.Check {
77
- n .VMImages ,
118
+ clusterConfig , err := variables .UnmarshalClusterConfigVariable (cluster .Spec .Topology .Variables )
119
+ if err != nil {
120
+ return nil , nil , nil , fmt .Errorf ("failed to unmarshal .variables[.name=clusterConfig]: %w" , err )
78
121
}
122
+ if clusterConfig != nil && clusterConfig .Nutanix != nil {
123
+ nutanixSpec = clusterConfig .Nutanix
124
+ }
125
+
126
+ if clusterConfig .ControlPlane != nil && clusterConfig .ControlPlane .Nutanix != nil {
127
+ controlPlaneNutanixNodeSpec = clusterConfig .ControlPlane .Nutanix
128
+ }
129
+
130
+ if cluster .Spec .Topology .Workers != nil {
131
+ for _ , md := range cluster .Spec .Topology .Workers .MachineDeployments {
132
+ workerConfig , err := variables .UnmarshalWorkerConfigVariable (md .Variables .Overrides )
133
+ if err != nil {
134
+ return nil , nil , nil , fmt .Errorf (
135
+ "failed to unmarshal .variables[.name=workerConfig] for machine deployment %s: %w" ,
136
+ md .Name , err ,
137
+ )
138
+ }
139
+ if workerConfig != nil && workerConfig .Nutanix != nil {
140
+ nutanixNodeSpecByMachineDeploymentName [md .Name ] = workerConfig .Nutanix
141
+ }
142
+ }
143
+ }
144
+
145
+ return nutanixSpec , controlPlaneNutanixNodeSpec , nutanixNodeSpecByMachineDeploymentName , nil
79
146
}
0 commit comments