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