Skip to content

Commit 93299d2

Browse files
committed
fixup! feat: Nutanix VM image preflight check
Refactor to simplify, and remove need to checker factory.
1 parent 826bc00 commit 93299d2

File tree

9 files changed

+156
-154
lines changed

9 files changed

+156
-154
lines changed

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func main() {
225225
Handler: preflight.New(mgr.GetClient(), admission.NewDecoder(mgr.GetScheme()),
226226
[]preflight.Checker{
227227
// Add your preflight checkers here.
228-
preflightnutanix.New,
228+
preflightnutanix.Checker,
229229
}...,
230230
),
231231
})

pkg/webhook/preflight/nutanix/checker.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,58 @@ import (
1717
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
1818
)
1919

20-
func New(kclient ctrlclient.Client, cluster *clusterv1.Cluster) preflight.Checker {
21-
return &nutanixChecker{
22-
kclient: kclient,
23-
cluster: cluster,
24-
25-
nclientFactory: newClient,
26-
27-
initNutanixConfigurationFunc: initNutanixConfiguration,
28-
initCredentialsCheckFunc: initCredentialsCheck,
29-
initVMImageChecksFunc: initVMImageChecks,
30-
}
20+
var Checker = &nutanixChecker{
21+
configurationCheckFactory: newConfigurationCheck,
22+
credentialsCheckFactory: newCredentialsCheck,
23+
vmImageChecksFactory: newVMImageChecks,
3124
}
3225

3326
type nutanixChecker struct {
34-
kclient ctrlclient.Client
35-
cluster *clusterv1.Cluster
36-
37-
nutanixClusterConfigSpec *carenv1.NutanixClusterConfigSpec
38-
nutanixWorkerNodeConfigSpecByMachineDeploymentName map[string]*carenv1.NutanixWorkerNodeConfigSpec
39-
40-
nclient client
41-
nclientFactory func(prismgoclient.Credentials) (client, error)
42-
43-
initNutanixConfigurationFunc func(
44-
n *nutanixChecker,
27+
configurationCheckFactory func(
28+
cd *checkDependencies,
4529
) preflight.Check
4630

47-
initCredentialsCheckFunc func(
31+
credentialsCheckFactory func(
4832
ctx context.Context,
49-
n *nutanixChecker,
33+
nclientFactory func(prismgoclient.Credentials) (client, error),
34+
cd *checkDependencies,
5035
) preflight.Check
5136

52-
initVMImageChecksFunc func(
53-
n *nutanixChecker,
37+
vmImageChecksFactory func(
38+
cd *checkDependencies,
5439
) []preflight.Check
40+
}
41+
42+
type checkDependencies struct {
43+
kclient ctrlclient.Client
44+
cluster *clusterv1.Cluster
5545

56-
log logr.Logger
46+
nutanixClusterConfigSpec *carenv1.NutanixClusterConfigSpec
47+
nutanixWorkerNodeConfigSpecByMachineDeploymentName map[string]*carenv1.NutanixWorkerNodeConfigSpec
48+
49+
nclient client
50+
log logr.Logger
5751
}
5852

5953
func (n *nutanixChecker) Init(
6054
ctx context.Context,
55+
kclient ctrlclient.Client,
56+
cluster *clusterv1.Cluster,
6157
) []preflight.Check {
62-
n.log = ctrl.LoggerFrom(ctx).WithName("preflight/nutanix")
58+
cd := &checkDependencies{
59+
kclient: kclient,
60+
cluster: cluster,
61+
log: ctrl.LoggerFrom(ctx).WithName("preflight/nutanix"),
62+
}
6363

6464
checks := []preflight.Check{
6565
// The configuration check must run first, because it initializes data used by all other checks,
6666
// and the credentials check second, because it initializes the Nutanix clients used by other checks.
67-
n.initNutanixConfigurationFunc(n),
68-
n.initCredentialsCheckFunc(ctx, n),
67+
n.configurationCheckFactory(cd),
68+
n.credentialsCheckFactory(ctx, newClient, cd),
6969
}
7070

71-
checks = append(checks, n.initVMImageChecksFunc(n)...)
71+
checks = append(checks, n.vmImageChecksFactory(cd)...)
7272

7373
// Add more checks here as needed.
7474

pkg/webhook/preflight/nutanix/checker_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/assert"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1314

15+
prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
16+
1417
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1518
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
1619
)
@@ -98,34 +101,34 @@ func TestNutanixChecker_Init(t *testing.T) {
98101
for _, tt := range tests {
99102
t.Run(tt.name, func(t *testing.T) {
100103
// Create the checker
101-
checker := &nutanixChecker{
102-
cluster: &clusterv1.Cluster{},
103-
nutanixClusterConfigSpec: tt.nutanixConfig,
104-
nutanixWorkerNodeConfigSpecByMachineDeploymentName: tt.workerNodeConfigs,
105-
}
104+
checker := &nutanixChecker{}
106105

107106
// Mock the sub-check functions to track their calls
108107
configCheckCalled := false
109108
credsCheckCalled := false
110109
vmImageCheckCount := 0
111110

112-
checker.initNutanixConfigurationFunc = func(n *nutanixChecker) preflight.Check {
111+
checker.configurationCheckFactory = func(cd *checkDependencies) preflight.Check {
113112
configCheckCalled = true
114113
return &mockCheck{
115114
name: tt.expectedFirstCheckName,
116115
result: preflight.CheckResult{Allowed: true},
117116
}
118117
}
119118

120-
checker.initCredentialsCheckFunc = func(ctx context.Context, n *nutanixChecker) preflight.Check {
119+
checker.credentialsCheckFactory = func(
120+
ctx context.Context,
121+
nclientFactory func(prismgoclient.Credentials) (client, error),
122+
cd *checkDependencies,
123+
) preflight.Check {
121124
credsCheckCalled = true
122125
return &mockCheck{
123126
name: tt.expectedSecondCheckName,
124127
result: preflight.CheckResult{Allowed: true},
125128
}
126129
}
127130

128-
checker.initVMImageChecksFunc = func(n *nutanixChecker) []preflight.Check {
131+
checker.vmImageChecksFactory = func(cd *checkDependencies) []preflight.Check {
129132
checks := []preflight.Check{}
130133
for i := 0; i < tt.vmImageCheckCount; i++ {
131134
vmImageCheckCount++
@@ -143,10 +146,12 @@ func TestNutanixChecker_Init(t *testing.T) {
143146

144147
// Call Init
145148
ctx := context.Background()
146-
checks := checker.Init(ctx)
147-
148-
// Verify the logger was set
149-
assert.NotNil(t, checker.log)
149+
checks := checker.Init(ctx, nil, &clusterv1.Cluster{
150+
ObjectMeta: metav1.ObjectMeta{
151+
Name: "test-cluster",
152+
Namespace: "default",
153+
},
154+
})
150155

151156
// Verify correct number of checks
152157
assert.Len(t, checks, tt.expectedCheckCount)

pkg/webhook/preflight/nutanix/credentials.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,27 @@ func (c *credentialsCheck) Run(_ context.Context) preflight.CheckResult {
3030
return c.result
3131
}
3232

33-
func initCredentialsCheck(
33+
func newCredentialsCheck(
3434
ctx context.Context,
35-
n *nutanixChecker,
35+
nclientFactory func(prismgoclient.Credentials) (client, error),
36+
cd *checkDependencies,
3637
) preflight.Check {
37-
n.log.V(5).Info("Initializing Nutanix credentials check")
38+
cd.log.V(5).Info("Initializing Nutanix credentials check")
3839

3940
credentialsCheck := &credentialsCheck{
4041
result: preflight.CheckResult{
4142
Allowed: true,
4243
},
4344
}
4445

45-
if n.nutanixClusterConfigSpec == nil && len(n.nutanixWorkerNodeConfigSpecByMachineDeploymentName) == 0 {
46+
if cd.nutanixClusterConfigSpec == nil && len(cd.nutanixWorkerNodeConfigSpecByMachineDeploymentName) == 0 {
4647
// If there is no Nutanix configuration at all, the credentials check is not needed.
4748
return credentialsCheck
4849
}
4950

5051
// There is some Nutanix configuration, so the credentials check is needed.
5152
// However, the credentials configuration is missing, so we cannot perform the check.
52-
if n.nutanixClusterConfigSpec == nil || n.nutanixClusterConfigSpec.Nutanix == nil {
53+
if cd.nutanixClusterConfigSpec == nil || cd.nutanixClusterConfigSpec.Nutanix == nil {
5354
credentialsCheck.result.Allowed = false
5455
credentialsCheck.result.Error = true
5556
credentialsCheck.result.Causes = append(credentialsCheck.result.Causes,
@@ -62,7 +63,7 @@ func initCredentialsCheck(
6263
}
6364

6465
// Get the credentials data in order to initialize the credentials and clients.
65-
prismCentralEndpointSpec := n.nutanixClusterConfigSpec.Nutanix.PrismCentralEndpoint
66+
prismCentralEndpointSpec := cd.nutanixClusterConfigSpec.Nutanix.PrismCentralEndpoint
6667

6768
host, port, err := prismCentralEndpointSpec.ParseURL()
6869
if err != nil {
@@ -79,10 +80,10 @@ func initCredentialsCheck(
7980
}
8081

8182
credentialsSecret := &corev1.Secret{}
82-
err = n.kclient.Get(
83+
err = cd.kclient.Get(
8384
ctx,
8485
types.NamespacedName{
85-
Namespace: n.cluster.Namespace,
86+
Namespace: cd.cluster.Namespace,
8687
Name: prismCentralEndpointSpec.Credentials.SecretRef.Name,
8788
},
8889
credentialsSecret,
@@ -154,7 +155,7 @@ func initCredentialsCheck(
154155
}
155156

156157
// Initialize the Nutanix client.
157-
nclient, err := n.nclientFactory(credentials)
158+
nclient, err := nclientFactory(credentials)
158159
if err != nil {
159160
credentialsCheck.result.Allowed = false
160161
credentialsCheck.result.Error = true
@@ -183,7 +184,7 @@ func initCredentialsCheck(
183184
}
184185

185186
// We initialized both clients, and verified the credentials using the v3 client.
186-
n.nclient = nclient
187+
cd.nclient = nclient
187188

188189
return credentialsCheck
189190
}

0 commit comments

Comments
 (0)