@@ -12,135 +12,83 @@ 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"
17
15
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
18
16
)
19
17
20
- type Checker struct {
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
27
- }
18
+ type Checker struct {}
28
19
29
20
func (n * Checker ) Init (
30
21
ctx context.Context ,
31
- client ctrlclient.Client ,
22
+ kclient ctrlclient.Client ,
32
23
cluster * clusterv1.Cluster ,
33
24
) []preflight.Check {
34
- n .client = client
35
- n .cluster = cluster
36
-
37
- var err error
25
+ prismCentralEndpointSpec ,
26
+ controlPlaneNutanixNodeSpec ,
27
+ nutanixNodeSpecByMachineDeploymentName ,
28
+ errCauses := readNutanixSpecs (cluster )
29
+ if len (errCauses ) > 0 {
30
+ return initErrorCheck (errCauses ... )
31
+ }
38
32
39
- n .nutanixSpec , n .nutanixControlPlaneNodeSpec , n .nutanixNodeSpecByMachineDeploymentName , err = getData (cluster )
40
- if err != nil {
41
- return []preflight.Check {
42
- errorCheck ("TBD" , "TBD" ),
43
- }
33
+ if controlPlaneNutanixNodeSpec == nil && len (nutanixNodeSpecByMachineDeploymentName ) == 0 {
34
+ // No Nutanix specs found, no checks to run.
35
+ return nil
44
36
}
45
37
46
- credentials , err := getCredentials (ctx , client , cluster , n .nutanixSpec )
47
- if err != nil {
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
- }
38
+ // At least one Nutanix spec is defined. Get credentials and create a client,
39
+ // because all checks require them.
40
+ credentials , errCauses := getCredentials (ctx , kclient , cluster , prismCentralEndpointSpec )
41
+ if len (errCauses ) > 0 {
42
+ return initErrorCheck (errCauses ... )
54
43
}
55
44
56
- n . v4client , err = v4client ( credentials , n . nutanixSpec )
45
+ nv4client , err := prismv4 . NewV4Client ( * credentials )
57
46
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
- }
47
+ return initErrorCheck (
48
+ preflight.Cause {
49
+ Message : fmt .Sprintf ("failed to initialize Nutanix v4 client: %s" , err ),
50
+ Field : "" ,
51
+ })
64
52
}
65
53
66
54
// Initialize checks.
67
55
checks := []preflight.Check {}
68
- if n . nutanixControlPlaneNodeSpec != nil {
56
+ if controlPlaneNutanixNodeSpec != nil {
69
57
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
- },
58
+ vmImageCheck (
59
+ nv4client ,
60
+ controlPlaneNutanixNodeSpec ,
61
+ "cluster.spec.topology[.name=clusterConfig].value.controlPlane.nutanix" ,
62
+ ),
76
63
)
77
64
}
78
- for mdName , nodeSpec := range n .nutanixNodeSpecByMachineDeploymentName {
65
+ for _ , md := range cluster .Spec .Topology .Workers .MachineDeployments {
66
+ if nutanixNodeSpecByMachineDeploymentName [md .Name ] == nil {
67
+ continue
68
+ }
79
69
checks = append (checks ,
80
- func (ctx context.Context ) preflight.CheckResult {
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
- },
70
+ vmImageCheck (
71
+ nv4client ,
72
+ nutanixNodeSpecByMachineDeploymentName [md .Name ],
73
+ fmt .Sprintf (
74
+ "cluster.spec.topology.workers.machineDeployments[.name=%s].variables[.name=workerConfig].value.nutanix" ,
75
+ md .Name ,
76
+ ),
77
+ ),
90
78
)
91
79
}
92
80
return checks
93
81
}
94
82
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
- },
106
- },
107
- }
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 )
117
-
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 )
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
- )
83
+ func initErrorCheck (causes ... preflight.Cause ) []preflight.Check {
84
+ return []preflight.Check {
85
+ func (ctx context.Context ) preflight.CheckResult {
86
+ return preflight.CheckResult {
87
+ Name : "Nutanix" ,
88
+ Allowed : false ,
89
+ Error : true ,
90
+ Causes : causes ,
138
91
}
139
- if workerConfig != nil && workerConfig .Nutanix != nil {
140
- nutanixNodeSpecByMachineDeploymentName [md .Name ] = workerConfig .Nutanix
141
- }
142
- }
92
+ },
143
93
}
144
-
145
- return nutanixSpec , controlPlaneNutanixNodeSpec , nutanixNodeSpecByMachineDeploymentName , nil
146
94
}
0 commit comments