Skip to content

Commit

Permalink
fix: improve error handling for Prism Client (#354)
Browse files Browse the repository at this point in the history
* fix: use wrapper errors to clearly denote issues in client building

* fix: adds a function to properly sanitize the address

* fix: adds tests for ip address case given

* fix: uses a defined type for port error

* fix: clean up variable naming

* fix: remove validation here to be moved into prism-client
  • Loading branch information
faiq authored and thunderboltsid committed Apr 29, 2024
1 parent a2e74d2 commit f31c5f6
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const (
capxNamespaceKey = "POD_NAMESPACE"
)

var ErrPrismAddressNotSet = fmt.Errorf("cannot get credentials if Prism Address is not set")
var ErrPrismPortNotSet = fmt.Errorf("cannot get credentials if Prism Port is not set")

type NutanixClientHelper struct {
secretInformer coreinformers.SecretInformer
configMapInformer coreinformers.ConfigMapInformer
Expand All @@ -64,14 +67,15 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
prismCentralInfo := nutanixCluster.Spec.PrismCentral
if prismCentralInfo != nil {
if prismCentralInfo.Address == "" {
return nil, fmt.Errorf("cannot get credentials if Prism Address is not set")
return nil, ErrPrismAddressNotSet
}
if prismCentralInfo.Port == 0 {
return nil, fmt.Errorf("cannot get credentials if Prism Port is not set")
return nil, ErrPrismPortNotSet
}
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 == "" {
Expand All @@ -94,7 +98,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 == "" {
Expand All @@ -115,15 +119,14 @@ func (n *NutanixClientHelper) GetClientFromEnvironment(ctx context.Context, nuta
*npe,
n.secretInformer,
n.configMapInformer))

// init env with providers
env := environment.NewEnvironment(
providers...,
)
// 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,
Expand Down Expand Up @@ -155,26 +158,25 @@ 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
}

func (n *NutanixClientHelper) getManagerNutanixPrismEndpoint() (*credentialTypes.NutanixPrismEndpoint, error) {
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")
Expand All @@ -196,16 +198,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
}

0 comments on commit f31c5f6

Please sign in to comment.