@@ -3,72 +3,66 @@ package nutanix
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "sync"
7
+
8
+ corev1 "k8s.io/api/core/v1"
9
+ "k8s.io/apimachinery/pkg/types"
6
10
7
11
prism "github.com/nutanix-cloud-native/prism-go-client"
8
12
prismcredentials "github.com/nutanix-cloud-native/prism-go-client/environment/credentials"
9
13
prismv4 "github.com/nutanix-cloud-native/prism-go-client/v4"
10
- corev1 "k8s.io/api/core/v1"
11
- "k8s.io/apimachinery/pkg/types"
12
- ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
13
14
14
- carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
15
15
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
16
16
)
17
17
18
- func (n * Checker ) v4client (
19
- ctx context.Context ,
20
- client ctrlclient.Client ,
21
- clusterNamespace string ,
22
- ) (
23
- * prismv4.Client ,
24
- error ,
25
- ) {
26
- n .clientMutex .Lock ()
27
- defer n .clientMutex .Unlock ()
28
- if n .nutanixClient != nil {
29
- return n .nutanixClient , nil
30
- }
31
-
32
- clusterConfig , err := variables .UnmarshalClusterConfigVariable (n .cluster .Spec .Topology .Variables )
33
- if err != nil {
34
- return nil , fmt .Errorf ("failed to unmarshal cluster variable %q: %w" , carenv1 .ClusterConfigVariableName , err )
35
- }
18
+ // clientV4 creates a new Prism V4 client for the Nutanix cluster.
19
+ // It retrieves the Prism Central credentials from the provided client and
20
+ // uses them to create the client. The client is cached for future use.
21
+ // The function returns an error if the credentials cannot be retrieved or
22
+ // if the Prism Central endpoint cannot be parsed.
23
+ func (n * Checker ) clientV4 (ctx context.Context , clusterConfig * variables.ClusterConfigSpec ) (* prismv4.Client , error ) {
24
+ return sync .OnceValues (func () (* prismv4.Client , error ) {
25
+ if clusterConfig == nil || clusterConfig .Nutanix == nil {
26
+ return nil , fmt .Errorf ("clusterConfig variable is nil or does not contain Nutanix config" )
27
+ }
36
28
37
- if clusterConfig .Nutanix == nil {
38
- return nil , fmt .Errorf ("missing Nutanix configuration in cluster topology " )
39
- }
29
+ if clusterConfig .Nutanix . PrismCentralEndpoint . Credentials . SecretRef . Name == "" {
30
+ return nil , fmt .Errorf ("prism Central credentials reference SecretRef.Name has no value " )
31
+ }
40
32
41
- if clusterConfig .Nutanix .PrismCentralEndpoint .Credentials .SecretRef .Name == "" {
42
- return nil , fmt .Errorf ("prism Central credentials reference SecretRef.Name has no value" )
43
- }
33
+ credentialsSecret := & corev1.Secret {}
34
+ if err := n .client .Get (
35
+ ctx ,
36
+ types.NamespacedName {
37
+ Namespace : n .cluster .Namespace ,
38
+ Name : clusterConfig .Nutanix .PrismCentralEndpoint .Credentials .SecretRef .Name ,
39
+ },
40
+ credentialsSecret ,
41
+ ); err != nil {
42
+ return nil , fmt .Errorf ("failed to get Prism Central credentials Secret: %w" , err )
43
+ }
44
44
45
- credentialsSecret := & corev1.Secret {}
46
- if err := client .Get (
47
- ctx ,
48
- types.NamespacedName {
49
- Namespace : clusterNamespace ,
50
- Name : clusterConfig .Nutanix .PrismCentralEndpoint .Credentials .SecretRef .Name ,
51
- },
52
- credentialsSecret ,
53
- ); err != nil {
54
- return nil , fmt .Errorf ("failed to get Prism Central credentials Secret: %w" , err )
55
- }
45
+ // Get username and password
46
+ credentials , err := prismcredentials .ParseCredentials (credentialsSecret .Data ["credentials" ])
47
+ if err != nil {
48
+ return nil , fmt .Errorf ("failed to parse Prism Central credentials from Secret: %w" , err )
49
+ }
56
50
57
- // Get username and password
58
- credentials , err := prismcredentials .ParseCredentials (credentialsSecret .Data ["credentials" ])
59
- if err != nil {
60
- return nil , fmt .Errorf ("failed to parse Prism Central credentials from Secret: %w" , err )
61
- }
51
+ host , port , err := clusterConfig .Nutanix .PrismCentralEndpoint .ParseURL ()
52
+ if err != nil {
53
+ return nil , fmt .Errorf ("failed to parse Prism Central endpoint: %w" , err )
54
+ }
62
55
63
- host , port , err := clusterConfig .Nutanix .PrismCentralEndpoint .ParseURL ()
64
- if err != nil {
65
- return nil , fmt .Errorf ("failed to parse Prism Central endpoint: %w" , err )
66
- }
67
-
68
- return prismv4 .NewV4Client (prism.Credentials {
69
- Endpoint : fmt .Sprintf ("%s:%d" , host , port ),
70
- Username : credentials .Username ,
71
- Password : credentials .Password ,
72
- Insecure : clusterConfig .Nutanix .PrismCentralEndpoint .Insecure ,
73
- })
56
+ nutanixClient , err := prismv4 .NewV4Client (prism.Credentials {
57
+ Endpoint : fmt .Sprintf ("%s:%d" , host , port ),
58
+ Username : credentials .Username ,
59
+ Password : credentials .Password ,
60
+ Insecure : clusterConfig .Nutanix .PrismCentralEndpoint .Insecure ,
61
+ })
62
+ if err != nil {
63
+ return nil , fmt .Errorf ("failed to create Prism V4 client: %w" , err )
64
+ }
65
+ n .nutanixClient = nutanixClient
66
+ return n .nutanixClient , nil
67
+ })()
74
68
}
0 commit comments