diff --git a/pkg/client/client.go b/pkg/client/client.go index c89eacc796..29c7f83b73 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -69,9 +69,10 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta if prismCentralInfo.Port == 0 { return nil, fmt.Errorf("cannot get credentials if Prism Port is not set") } - credentialRef := prismCentralInfo.CredentialRef - if credentialRef == nil { - return nil, fmt.Errorf("credentialRef must be set on prismCentral attribute for cluster %s in namespace %s", nutanixCluster.Name, nutanixCluster.Namespace) + credentialRef, err := GetCredentialRefForCluster(nutanixCluster) + if err != nil { + //nolint:wrapcheck // error is alredy wrapped + return nil, err } // If namespace is empty, use the cluster namespace if credentialRef.Namespace == "" { @@ -94,7 +95,7 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta // Add env provider for CAPX manager npe, err := n.getManagerNutanixPrismEndpoint() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create prism endpoint %w", err) } // If namespaces is not set, set it to the namespace of the CAPX manager if npe.CredentialRef.Namespace == "" { @@ -123,7 +124,7 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta // fetch endpoint details me, err := env.GetManagementEndpoint(envTypes.Topology{}) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get management endpoint object %w", err) } creds := prismgoclient.Credentials{ URL: me.Address.Host, @@ -155,15 +156,14 @@ func (n *NutanixClientHelper) GetClient(cred prismgoclient.Credentials, addition } cli, err := nutanixClientV3.NewV3Client(cred, clientOpts...) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to create new nutanix client %w", err) } // Check if the client is working _, err = cli.V3.GetCurrentLoggedInUser(context.Background()) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get current logged in user with client %w", err) } - return cli, nil } @@ -171,10 +171,10 @@ func (n *NutanixClientHelper) getManagerNutanixPrismEndpoint() (*credentialTypes npe := &credentialTypes.NutanixPrismEndpoint{} config, err := n.readEndpointConfig() if err != nil { - return npe, err + return nil, fmt.Errorf("failed to read config %w", err) } if err = json.Unmarshal(config, npe); err != nil { - return npe, err + return nil, fmt.Errorf("failed to unmarshal config %w", err) } if npe.CredentialRef == nil { return nil, fmt.Errorf("credentialRef must be set on CAPX manager") @@ -196,16 +196,16 @@ func GetCredentialRefForCluster(nutanixCluster *infrav1.NutanixCluster) (*creden if nutanixCluster == nil { return nil, fmt.Errorf("cannot get credential reference if nutanix cluster object is nil") } - prismCentralinfo := nutanixCluster.Spec.PrismCentral - if prismCentralinfo == nil { + prismCentralInfo := nutanixCluster.Spec.PrismCentral + if prismCentralInfo == nil { return nil, nil } - if prismCentralinfo.CredentialRef == nil { + if prismCentralInfo.CredentialRef == nil { return nil, fmt.Errorf("credentialRef must be set on prismCentral attribute for cluster %s in namespace %s", nutanixCluster.Name, nutanixCluster.Namespace) } - if prismCentralinfo.CredentialRef.Kind != credentialTypes.SecretKind { + if prismCentralInfo.CredentialRef.Kind != credentialTypes.SecretKind { return nil, nil } - return prismCentralinfo.CredentialRef, nil + return prismCentralInfo.CredentialRef, nil }