Skip to content

Commit 2bb6ab0

Browse files
committed
Get credentials as part of create client function
1 parent dbd0714 commit 2bb6ab0

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

pkg/webhook/preflight/nutanix/checker.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1111
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1212

13-
prismv4 "github.com/nutanix-cloud-native/prism-go-client/v4"
14-
1513
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
1614
)
1715

@@ -22,40 +20,31 @@ func (n *Checker) Init(
2220
kclient ctrlclient.Client,
2321
cluster *clusterv1.Cluster,
2422
) []preflight.Check {
23+
// Read Nutanix specs from the cluster.
2524
prismCentralEndpointSpec,
2625
controlPlaneNutanixNodeSpec,
2726
nutanixNodeSpecByMachineDeploymentName,
28-
errCauses := readNutanixSpecs(cluster)
27+
errCauses := specsFromCluster(cluster)
2928
if len(errCauses) > 0 {
3029
return initErrorCheck(errCauses...)
3130
}
3231

32+
// If no Nutanix specs are found, no checks need to run.
3333
if controlPlaneNutanixNodeSpec == nil && len(nutanixNodeSpecByMachineDeploymentName) == 0 {
34-
// No Nutanix specs found, no checks to run.
3534
return nil
3635
}
3736

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)
37+
// Create a Nutanix client, because all checks require it.
38+
nv4client, errCauses := newV4Client(ctx, kclient, cluster.Namespace, prismCentralEndpointSpec)
4139
if len(errCauses) > 0 {
4240
return initErrorCheck(errCauses...)
4341
}
4442

45-
nv4client, err := prismv4.NewV4Client(*credentials)
46-
if err != nil {
47-
return initErrorCheck(
48-
preflight.Cause{
49-
Message: fmt.Sprintf("failed to initialize Nutanix v4 client: %s", err),
50-
Field: "",
51-
})
52-
}
53-
5443
// Initialize checks.
5544
checks := []preflight.Check{}
5645
if controlPlaneNutanixNodeSpec != nil {
5746
checks = append(checks,
58-
vmImageCheck(
47+
newVMImageCheck(
5948
nv4client,
6049
controlPlaneNutanixNodeSpec,
6150
"cluster.spec.topology[.name=clusterConfig].value.controlPlane.nutanix",
@@ -67,7 +56,7 @@ func (n *Checker) Init(
6756
continue
6857
}
6958
checks = append(checks,
70-
vmImageCheck(
59+
newVMImageCheck(
7160
nv4client,
7261
nutanixNodeSpecByMachineDeploymentName[md.Name],
7362
fmt.Sprintf(

pkg/webhook/preflight/nutanix/credentials.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,45 @@ import (
66

77
corev1 "k8s.io/api/core/v1"
88
"k8s.io/apimachinery/pkg/types"
9-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
109
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1110

1211
prismgoclient "github.com/nutanix-cloud-native/prism-go-client"
1312
prismcredentials "github.com/nutanix-cloud-native/prism-go-client/environment/credentials"
13+
prismv4 "github.com/nutanix-cloud-native/prism-go-client/v4"
1414

1515
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1616
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
1717
)
1818

1919
const credentialsSecretDataKey = "credentials"
2020

21+
func newV4Client(ctx context.Context,
22+
client ctrlclient.Client,
23+
clusterNamespace string,
24+
prismCentralEndpointSpec *carenv1.NutanixPrismCentralEndpointSpec,
25+
) (*prismv4.Client, []preflight.Cause) {
26+
credentials, causes := getCredentials(ctx, client, clusterNamespace, prismCentralEndpointSpec)
27+
if len(causes) > 0 {
28+
return nil, causes
29+
}
30+
31+
nv4client, err := prismv4.NewV4Client(*credentials)
32+
if err != nil {
33+
return nil, []preflight.Cause{
34+
{
35+
Message: fmt.Sprintf("failed to create Prism Central client: %s", err),
36+
Field: "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint",
37+
},
38+
}
39+
}
40+
41+
return nv4client, nil
42+
}
43+
2144
func getCredentials(
2245
ctx context.Context,
2346
client ctrlclient.Client,
24-
cluster *clusterv1.Cluster,
47+
clusterNamespace string,
2548
prismCentralEndpointSpec *carenv1.NutanixPrismCentralEndpointSpec,
2649
) (*prismgoclient.Credentials, []preflight.Cause) {
2750
if prismCentralEndpointSpec == nil {
@@ -36,7 +59,7 @@ func getCredentials(
3659
if prismCentralEndpointSpec.Credentials.SecretRef.Name == "" {
3760
return nil, []preflight.Cause{
3861
{
39-
Message: "Prism Central credentials secret reference is missing the name",
62+
Message: "Prism Central credentials reference is missing the name",
4063
Field: "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials.secretRef.name",
4164
},
4265
}
@@ -46,14 +69,14 @@ func getCredentials(
4669
if err := client.Get(
4770
ctx,
4871
types.NamespacedName{
49-
Namespace: cluster.Namespace,
72+
Namespace: clusterNamespace,
5073
Name: prismCentralEndpointSpec.Credentials.SecretRef.Name,
5174
},
5275
credentialsSecret,
5376
); err != nil {
5477
return nil, []preflight.Cause{
5578
{
56-
Message: fmt.Sprintf("failed to get the credentials Secret: %s", err),
79+
Message: fmt.Sprintf("failed to get credentials Secret: %s", err),
5780
Field: "cluster.spec.topology.variables[.name=clusterConfig].nutanix.prismCentralEndpoint.credentials.secretRef",
5881
},
5982
}

pkg/webhook/preflight/nutanix/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
1414
)
1515

16-
func vmImageCheck(
16+
func newVMImageCheck(
1717
client *prismv4.Client,
1818
nutanixNodeSpec *carenv1.NutanixNodeSpec,
1919
nutanixNodeSpecField string,

pkg/webhook/preflight/nutanix/specs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight"
1111
)
1212

13-
func readNutanixSpecs(
13+
func specsFromCluster(
1414
cluster *clusterv1.Cluster,
1515
) (*carenv1.NutanixPrismCentralEndpointSpec, *carenv1.NutanixNodeSpec, map[string]*carenv1.NutanixNodeSpec, []preflight.Cause) {
1616
var prismCentralEndpointSpec *carenv1.NutanixPrismCentralEndpointSpec

0 commit comments

Comments
 (0)