@@ -4,45 +4,151 @@ import (
4
4
"context"
5
5
"fmt"
6
6
7
- vmmv4 "github.com/nutanix/ntnx-api-golang-clients/vmm-go-client/v4/models/vmm/v4/content"
8
-
9
7
prismv4 "github.com/nutanix-cloud-native/prism-go-client/v4"
8
+ vmmv4 "github.com/nutanix/ntnx-api-golang-clients/vmm-go-client/v4/models/vmm/v4/content"
9
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
10
11
11
capxv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"
12
12
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
13
+ "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
13
14
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
14
15
)
15
16
16
- func (n * Checker ) VMImageCheck (details * carenv1.NutanixMachineDetails , field string ) preflight.Check {
17
- return func (ctx context.Context ) preflight.CheckResult {
18
- result := preflight.CheckResult {
19
- Allowed : true ,
20
- Field : field ,
17
+ func (n * Checker ) VMImageCheck (ctx context.Context ) preflight.CheckResult {
18
+ result := preflight.CheckResult {
19
+ Allowed : true ,
20
+ }
21
+
22
+ // Check control plane VM image.
23
+ clusterConfig , err := variables .UnmarshalClusterConfigVariable (n .cluster .Spec .Topology .Variables )
24
+ if err != nil {
25
+ result .Error = true
26
+ result .Causes = append (result .Causes , metav1.StatusCause {
27
+ Type : "VMImageCheck" ,
28
+ Message : fmt .Sprintf (
29
+ "failed to unmarshal topology variable %q: %s" ,
30
+ carenv1 .ClusterConfigVariableName ,
31
+ err ,
32
+ ),
33
+ Field : "cluster.spec.topology.variables" ,
34
+ })
35
+ } else if clusterConfig != nil {
36
+ if clusterConfig .ControlPlane == nil || clusterConfig .ControlPlane .Nutanix == nil {
37
+ result .Causes = append (result .Causes , metav1.StatusCause {
38
+ Type : "VMImageCheck" ,
39
+ Message : "missing Nutanix configuration in cluster topology" ,
40
+ Field : "cluster.spec.topology.controlPlane.nutanix" ,
41
+ })
21
42
}
22
43
23
- if details .ImageLookup != nil {
24
- result .Allowed = false
25
- result .Message = "ImageLookup is not yet supported"
26
- return result
44
+ n .vmImageCheckForMachineDetails (
45
+ ctx ,
46
+ & clusterConfig .ControlPlane .Nutanix .MachineDetails ,
47
+ "controlPlane.nutanix.machineDetails" ,
48
+ & result ,
49
+ )
50
+ }
51
+
52
+ // Check worker VM images.
53
+ if n .cluster .Spec .Topology .Workers == nil {
54
+ return result
55
+ }
56
+
57
+ for _ , md := range n .cluster .Spec .Topology .Workers .MachineDeployments {
58
+ if md .Variables == nil {
59
+ continue
27
60
}
28
61
29
- if details .Image != nil {
30
- images , err := getVMImages (n .nutanixClient , details .Image )
31
- if err != nil {
32
- result .Allowed = false
33
- result .Error = true
34
- result .Message = fmt .Sprintf ("failed to count matching VM Images: %s" , err )
35
- return result
62
+ workerConfig , err := variables .UnmarshalWorkerConfigVariable (md .Variables .Overrides )
63
+ if err != nil {
64
+ result .Error = true
65
+ result .Causes = append (result .Causes , metav1.StatusCause {
66
+ Type : "VMImageCheck" ,
67
+ Message : fmt .Sprintf (
68
+ "failed to unmarshal topology variable %q: %s" ,
69
+ carenv1 .WorkerConfigVariableName ,
70
+ err ,
71
+ ),
72
+ Field : fmt .Sprintf (
73
+ "cluster.spec.topology.workers.machineDeployments[.name=%s].variables.overrides" ,
74
+ md .Name ,
75
+ ),
76
+ })
77
+ } else if workerConfig != nil {
78
+ if workerConfig .Nutanix == nil {
79
+ result .Causes = append (result .Causes , metav1.StatusCause {
80
+ Type : "VMImageCheck" ,
81
+ Message : "missing Nutanix configuration in worker machine deployment" ,
82
+ Field : fmt .Sprintf ("cluster.spec.topology.workers.machineDeployments[.name=%s]" +
83
+ ".variables.overrides[.name=workerConfig].value.nutanix" , md .Name ),
84
+ })
85
+ } else {
86
+ n .vmImageCheckForMachineDetails (
87
+ ctx ,
88
+ & workerConfig .Nutanix .MachineDetails ,
89
+ fmt .Sprintf (
90
+ "workers.machineDeployments[.name=%s].variables.overrides[.name=workerConfig].value.nutanix.machineDetails" ,
91
+ md .Name ,
92
+ ),
93
+ & result ,
94
+ )
36
95
}
96
+ }
97
+ }
37
98
38
- if len (images ) != 1 {
39
- result .Allowed = false
40
- result .Message = fmt .Sprintf ("expected to find 1 VM Image, found %d" , len (images ))
41
- return result
42
- }
99
+ return result
100
+ }
101
+
102
+ func (n * Checker ) vmImageCheckForMachineDetails (
103
+ ctx context.Context ,
104
+ details * carenv1.NutanixMachineDetails ,
105
+ field string ,
106
+ result * preflight.CheckResult ,
107
+ ) {
108
+ if details .ImageLookup != nil {
109
+ result .Allowed = false
110
+ result .Error = true
111
+ result .Causes = append (result .Causes , metav1.StatusCause {
112
+ Type : "VMImageCheck" ,
113
+ Message : "ImageLookup is not yet supported" ,
114
+ Field : field ,
115
+ })
116
+ return
117
+ }
118
+
119
+ if details .Image != nil {
120
+ client , err := n .v4client (ctx , n .client , n .cluster .Namespace )
121
+ if err != nil {
122
+ result .Allowed = false
123
+ result .Error = true
124
+ result .Causes = append (result .Causes , metav1.StatusCause {
125
+ Type : "VMImageCheck" ,
126
+ Message : fmt .Sprintf ("failed to get Nutanix client: %s" , err ),
127
+ Field : field ,
128
+ })
129
+ return
43
130
}
44
131
45
- return result
132
+ images , err := getVMImages (client , details .Image )
133
+ if err != nil {
134
+ result .Allowed = false
135
+ result .Error = true
136
+ result .Causes = append (result .Causes , metav1.StatusCause {
137
+ Type : "VMImageCheck" ,
138
+ Message : fmt .Sprintf ("failed to count matching VM Images: %s" , err ),
139
+ Field : field ,
140
+ })
141
+ return
142
+ }
143
+
144
+ if len (images ) != 1 {
145
+ result .Allowed = false
146
+ result .Causes = append (result .Causes , metav1.StatusCause {
147
+ Type : "VMImageCheck" ,
148
+ Message : fmt .Sprintf ("expected to find 1 VM Image, found %d" , len (images )),
149
+ Field : field ,
150
+ })
151
+ }
46
152
}
47
153
}
48
154
0 commit comments