From aed372164de47a81a1796f5e8e3eaf799eca4583 Mon Sep 17 00:00:00 2001 From: Prankul Mahajan <84079249+prankulmahajan@users.noreply.github.com> Date: Thu, 30 Sep 2021 22:37:07 +0530 Subject: [PATCH] Add Unclassified error type (#20) * Add Unclassified error type * Update volume interface package * Fix error messages --- common/iam/token_exchange_iks.go | 11 +++++++++-- common/messages/messages.go | 11 +++++++++++ common/messages/messages_en.go | 14 ++++++++++++++ common/messages/reason_code.go | 4 ++++ go.mod | 3 +-- go.sum | 6 ++---- iks/provider/provider.go | 13 +++++++++++-- 7 files changed, 52 insertions(+), 10 deletions(-) diff --git a/common/iam/token_exchange_iks.go b/common/iam/token_exchange_iks.go index fd0538d..a73381e 100644 --- a/common/iam/token_exchange_iks.go +++ b/common/iam/token_exchange_iks.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "net/http" + "strings" "time" "github.com/IBM-Cloud/ibm-cloud-cli-sdk/common/rest" @@ -151,10 +152,16 @@ func (r *tokenExchangeIKSRequest) sendTokenExchangeRequest() (*tokenExchangeIKSR r.logger.Info("Sending IAM token exchange request to container api server") resp, err := r.client.Do(r.request, &successV, &errorV) if err != nil { + errString := err.Error() r.logger.Error("IAM token exchange request failed", zap.Reflect("Response", resp), zap.Error(err)) - return nil, - util.NewError("ErrorUnclassified", + if strings.Contains(errString, "no such host") { + return nil, util.NewError("EndpointNotReachable", errString) + } else if strings.Contains(errString, "Timeout") { + return nil, util.NewError("Timeout", errString) + } else { + return nil, util.NewError("ErrorUnclassified", "IAM token exchange request failed", err) + } } if resp != nil && resp.StatusCode == 200 { diff --git a/common/messages/messages.go b/common/messages/messages.go index 8be32b5..86bab8b 100644 --- a/common/messages/messages.go +++ b/common/messages/messages.go @@ -21,6 +21,7 @@ import ( "fmt" util "github.com/IBM/ibmcloud-volume-interface/lib/utils" + "github.com/IBM/ibmcloud-volume-interface/lib/utils/reasoncode" ) // MessagesEn ... @@ -55,3 +56,13 @@ func GetUserError(code string, err error, args ...interface{}) error { } return userMsg } + +// GetUserErrorCode returns reason code string if a util.Message, else ErrorUnclassified string +func GetUserErrorCode(err error) string { + if uErr, isPerr := err.(util.Message); isPerr { + if code := uErr.Code; code != "" { + return code + } + } + return string(reasoncode.ErrorUnclassified) +} diff --git a/common/messages/messages_en.go b/common/messages/messages_en.go index addcd9a..f0ce75e 100644 --- a/common/messages/messages_en.go +++ b/common/messages/messages_en.go @@ -30,6 +30,20 @@ var messagesEn = map[string]util.Message{ RC: 400, Action: "Verify that you entered the correct IBM Cloud user name and password. If the error persists, the authentication service might be unavailable. Wait a few minutes and try again. ", }, + "EndpointNotReachable": { + Code: "EndpointNotReachable", + Description: "IAM TOKEN exchange request failed.", + Type: util.FailedAccessToken, + RC: 500, + Action: "Verify that iks_token_exchange_endpoint_private_url is reachable from the cluster. You can find this url by running 'kubectl get secret storage-secret-storage -n kube-system'.", + }, + "Timeout": { + Code: "Timeout", + Description: "IAM Token exchange endpoint is not reachable.", + Type: util.FailedAccessToken, + RC: 503, + Action: "Wait for a few mninutes and try again. If the error persists user can open a container network issue.", + }, "ErrorRequiredFieldMissing": { Code: "ErrorRequiredFieldMissing", Description: "[%s] is required to complete the operation.", diff --git a/common/messages/reason_code.go b/common/messages/reason_code.go index a26a8a7..f5779a9 100644 --- a/common/messages/reason_code.go +++ b/common/messages/reason_code.go @@ -18,6 +18,10 @@ package messages const ( + //Timeout indicates IAM_TOKEN exchange request failed due to timeout + Timeout = "Timeout" + //EndpointNotReachable indicates IAM_TOKEN exchange request failed due to incorrect endpoint + EndpointNotReachable = "EndpointNotReachable" //AuthenticationFailed indicate authentication to IAM endpoint failed. e,g IAM_TOKEN refresh AuthenticationFailed = "AuthenticationFailed" //VolumeAttachFailed indicates if volume attach to instance is failed diff --git a/go.mod b/go.mod index fac5db4..884694c 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,9 @@ go 1.15 require ( github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7 - github.com/IBM/ibmcloud-volume-interface v1.0.0-beta7 + github.com/IBM/ibmcloud-volume-interface v1.0.0-beta8 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/fatih/structs v1.1.0 - github.com/pierrre/gotestcover v0.0.0-20160517101806-924dca7d15f0 // indirect github.com/satori/go.uuid v1.2.0 github.com/stretchr/testify v1.6.1 go.uber.org/zap v1.15.0 diff --git a/go.sum b/go.sum index 0317416..ca42158 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7 h1:eHgfQl6IeSmzWUyiSi13CvoFYsovoyqWlpHX0pa9J54= github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.6.7/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs= -github.com/IBM/ibmcloud-volume-interface v1.0.0-beta7 h1:cl1situtbkJwEsRDw1CW2KpJkmznmjzhWWnSPH3TzvM= -github.com/IBM/ibmcloud-volume-interface v1.0.0-beta7/go.mod h1:2LxqkERml3eBxfRkQniuiRmKkAnl5tfC0ttlP2gupJM= +github.com/IBM/ibmcloud-volume-interface v1.0.0-beta8 h1:0oCBQidejAlLI5f5PlBHIytArhJI1aEOBCCOMoxNOPs= +github.com/IBM/ibmcloud-volume-interface v1.0.0-beta8/go.mod h1:2LxqkERml3eBxfRkQniuiRmKkAnl5tfC0ttlP2gupJM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -63,8 +63,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/pierrre/gotestcover v0.0.0-20160517101806-924dca7d15f0 h1:i5VIxp6QB8oWZ8IkK8zrDgeT6ORGIUeiN+61iETwJbI= -github.com/pierrre/gotestcover v0.0.0-20160517101806-924dca7d15f0/go.mod h1:4xpMLz7RBWyB+ElzHu8Llua96TRCB3YwX+l5EP1wmHk= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/iks/provider/provider.go b/iks/provider/provider.go index 1a3bb4f..ba5f9c9 100644 --- a/iks/provider/provider.go +++ b/iks/provider/provider.go @@ -21,6 +21,8 @@ import ( "context" "github.com/IBM/ibmcloud-volume-interface/lib/provider" + util "github.com/IBM/ibmcloud-volume-interface/lib/utils" + utilReasonCode "github.com/IBM/ibmcloud-volume-interface/lib/utils/reasoncode" "github.com/IBM/ibmcloud-volume-interface/provider/local" vpcprovider "github.com/IBM/ibmcloud-volume-vpc/block/provider" vpcconfig "github.com/IBM/ibmcloud-volume-vpc/block/vpcconfig" @@ -75,8 +77,15 @@ func (iksp *IksVpcBlockProvider) OpenSession(ctx context.Context, contextCredent vpcContextCredentials, err := ccf.ForIAMAccessToken(iksp.iksBlockProvider.Config.VPCConfig.APIKey, ctxLogger) if err != nil { ctxLogger.Error("Error occurred while generating IAM token for VPC", zap.Error(err)) - userErr := userError.GetUserError(string(userError.AuthenticationFailed), err) - return nil, userErr + if util.ErrorReasonCode(err) == utilReasonCode.EndpointNotReachable { + userErr := userError.GetUserError(string(userError.EndpointNotReachable), err) + return nil, userErr + } + if util.ErrorReasonCode(err) == utilReasonCode.Timeout { + userErr := userError.GetUserError(string(userError.Timeout), err) + return nil, userErr + } + return nil, err } session, err := iksp.vpcBlockProvider.OpenSession(ctx, vpcContextCredentials, ctxLogger) if err != nil {