diff --git a/common-lib/k8sResource/errors/constants.go b/common-lib/k8sResource/errors/constants.go new file mode 100644 index 000000000..3f961aa4a --- /dev/null +++ b/common-lib/k8sResource/errors/constants.go @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package errors + +import ( + "google.golang.org/grpc/codes" +) + +// list of error strings from Helm. These are part of the errors we check for presence in Helm's error messages. +const ( + ClusterUnreachableErrorMsg = "cluster unreachable" + CrdPreconditionErrorMsg = "ensure crds are installed first" + ArrayStringMismatchErrorMsg = "got array expected string" + NotFoundErrorMsg = "not found" //this is a generic type constant, an error could be namespace "ns1" not found or service "ser1" not found. + InvalidValueErrorMsg = "invalid value" + OperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress" + ForbiddenErrorMsg = "forbidden" +) + +// list of internal errors, these errors are easy for the users to understand +const ( + InternalClusterUnreachableErrorMsg = "cluster unreachable" + InternalOperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress" +) + +type errorGrpcCodeTuple struct { + errorMsg string + grpcCode codes.Code +} + +var helmErrorInternalErrorMap = map[string]errorGrpcCodeTuple{ + ClusterUnreachableErrorMsg: {errorMsg: InternalClusterUnreachableErrorMsg, grpcCode: codes.DeadlineExceeded}, + OperationInProgressErrorMsg: {errorMsg: InternalOperationInProgressErrorMsg, grpcCode: codes.FailedPrecondition}, +} + +var DynamicErrorMapping = map[string]codes.Code{ + NotFoundErrorMsg: codes.NotFound, + ForbiddenErrorMsg: codes.PermissionDenied, + InvalidValueErrorMsg: codes.InvalidArgument, + ArrayStringMismatchErrorMsg: codes.InvalidArgument, + CrdPreconditionErrorMsg: codes.FailedPrecondition, +} diff --git a/common-lib/k8sResource/errors/errors.go b/common-lib/k8sResource/errors/errors.go new file mode 100644 index 000000000..6d503e68c --- /dev/null +++ b/common-lib/k8sResource/errors/errors.go @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package errors + +import ( + "google.golang.org/grpc/status" + "strings" +) + +// ConvertHelmErrorToInternalError converts known error message from helm to internal error and also maps it with proper grpc code +func ConvertHelmErrorToInternalError(err error) error { + genericError := getInternalErrorForGenericErrorTypes(err) + if genericError != nil { + return genericError + } + var internalError error + for helmErrMsg, internalErr := range helmErrorInternalErrorMap { + if strings.Contains(err.Error(), helmErrMsg) { + internalError = status.New(internalErr.grpcCode, internalErr.errorMsg).Err() + } + } + return internalError +} + +// getInternalErrorForGenericErrorTypes returns all those kinds of errors which are generic in nature and also dynamic, make sure to return all generic and dynamic errors from this func. instead of putting them in helmErrorInternalErrorMap +func getInternalErrorForGenericErrorTypes(err error) error { + /* + for example:- + 1. if namespace is not found err is:- namespace "ns1" not found, + 2. in case ingress class not found error is of type ingress class: IngressClass.networking.k8s.io "ingress1" not found, + 3. when some resource is forbidden then err can be of many formats one of which is:- Unable to continue with install: could not get information about the resource Ingress "prakash-1-prakash-env3-ingress" in namespace "prakash-ns3": ingresses.networking.k8s.io "prakash-1-prakash-env3-ingress" is forbidden... + etc.. + */ + for errorMsg, code := range DynamicErrorMapping { + if strings.Contains(strings.ToLower(err.Error()), errorMsg) { + return status.New(code, err.Error()).Err() + } + } + + return nil +} diff --git a/common-lib/k8sResource/listChildObjects.go b/common-lib/k8sResource/listChildObjects.go index f79bda52e..f1a90ecd3 100644 --- a/common-lib/k8sResource/listChildObjects.go +++ b/common-lib/k8sResource/listChildObjects.go @@ -3,9 +3,11 @@ package k8sResource import ( "context" "errors" + customErr "github.com/devtron-labs/common-lib/k8sResource/errors" k8sUtils "github.com/devtron-labs/common-lib/utils/k8s" k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean" coreV1 "k8s.io/api/core/v1" + errors2 "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -196,8 +198,15 @@ func (impl *K8sServiceImpl) getChildObject(client *dynamicClient.DynamicClient, filterListStartTime := time.Now() childrenObjectsList, k8sErr := childResourceClient.List(context.Background(), options) if k8sErr != nil { - impl.logger.Errorw("error in getting child listObjects", filterObjRequest.GetLoggerMetadata("counter", counter, "timeTaken", time.Since(filterListStartTime).Seconds(), "err", k8sErr)...) - return nil, k8sErr + statusError, matched := k8sErr.(*errors2.StatusError) + if !matched || statusError.ErrStatus.Reason != metaV1.StatusReasonNotFound { + internalErr := customErr.ConvertHelmErrorToInternalError(k8sErr) + if internalErr != nil { + k8sErr = internalErr + } + impl.logger.Errorw("error in getting child listObjects", filterObjRequest.GetLoggerMetadata("counter", counter, "timeTaken", time.Since(filterListStartTime).Seconds(), "err", k8sErr)...) + return nil, k8sErr + } } impl.logger.Debugw("listing child objects", filterObjRequest.GetLoggerMetadata("counter", counter, "timeTaken", time.Since(filterListStartTime).Seconds())...) filterObjRequest = filterObjRequest.WithListObjects(childrenObjectsList) diff --git a/git-sensor/pkg/git/RepositoryManager.go b/git-sensor/pkg/git/RepositoryManager.go index b2a7eb146..9c661cdd5 100644 --- a/git-sensor/pkg/git/RepositoryManager.go +++ b/git-sensor/pkg/git/RepositoryManager.go @@ -247,10 +247,11 @@ func (impl *RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location res, errMsg, err := impl.gitManager.Fetch(gitCtx, location) if err == nil && len(res) > 0 { + onlySSHWarning := IsOutputOnlySSHWarning(res) impl.logger.Infow("repository updated", "location", url) //updated middleware.GitPullDuration.WithLabelValues("true", "true").Observe(time.Since(start).Seconds()) - return true, r, "", nil + return !onlySSHWarning, r, "", nil } else if err == nil && len(res) == 0 { impl.logger.Debugw("no update for ", "path", url) middleware.GitPullDuration.WithLabelValues("true", "false").Observe(time.Since(start).Seconds()) @@ -263,6 +264,19 @@ func (impl *RepositoryManagerImpl) Fetch(gitCtx GitContext, url string, location } +func IsOutputOnlySSHWarning(output string) bool { + outputSplit := strings.Split(output, "\n") + if len(outputSplit) > 1 { + return false + } + for _, line := range outputSplit { + if strings.Contains(line, "Warning: Permanently added") { + return true + } + } + return false +} + func (impl *RepositoryManagerImpl) GetCommitForTag(gitCtx GitContext, checkoutPath, tag string) (*GitCommitBase, error) { var err error start := time.Now() diff --git a/kubelink/error/constants.go b/kubelink/error/constants.go index fd28472f2..612087ebf 100644 --- a/kubelink/error/constants.go +++ b/kubelink/error/constants.go @@ -16,42 +16,7 @@ package error -import ( - "google.golang.org/grpc/codes" -) - // list of error strings from Helm. These are part of the errors we check for presence in Helm's error messages. const ( - YAMLToJSONConversionError = "error converting YAML to JSON" - ClusterUnreachableErrorMsg = "cluster unreachable" - CrdPreconditionErrorMsg = "ensure crds are installed first" - ArrayStringMismatchErrorMsg = "got array expected string" - NotFoundErrorMsg = "not found" //this is a generic type constant, an error could be namespace "ns1" not found or service "ser1" not found. - InvalidValueErrorMsg = "invalid value" - OperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress" - ForbiddenErrorMsg = "forbidden" -) - -// list of internal errors, these errors are easy for the users to understand -const ( - InternalClusterUnreachableErrorMsg = "cluster unreachable" - InternalOperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress" + YAMLToJSONConversionError = "error converting YAML to JSON" ) - -type errorGrpcCodeTuple struct { - errorMsg string - grpcCode codes.Code -} - -var helmErrorInternalErrorMap = map[string]errorGrpcCodeTuple{ - ClusterUnreachableErrorMsg: {errorMsg: InternalClusterUnreachableErrorMsg, grpcCode: codes.DeadlineExceeded}, - OperationInProgressErrorMsg: {errorMsg: InternalOperationInProgressErrorMsg, grpcCode: codes.FailedPrecondition}, -} - -var DynamicErrorMapping = map[string]codes.Code{ - NotFoundErrorMsg: codes.NotFound, - ForbiddenErrorMsg: codes.PermissionDenied, - InvalidValueErrorMsg: codes.InvalidArgument, - ArrayStringMismatchErrorMsg: codes.InvalidArgument, - CrdPreconditionErrorMsg: codes.FailedPrecondition, -} diff --git a/kubelink/error/utils.go b/kubelink/error/utils.go index 8abe0ff10..2567319dc 100644 --- a/kubelink/error/utils.go +++ b/kubelink/error/utils.go @@ -17,39 +17,10 @@ package error import ( - "google.golang.org/grpc/status" - "strings" + customErr "github.com/devtron-labs/common-lib/k8sResource/errors" ) // ConvertHelmErrorToInternalError converts known error message from helm to internal error and also maps it with proper grpc code func ConvertHelmErrorToInternalError(err error) error { - genericError := getInternalErrorForGenericErrorTypes(err) - if genericError != nil { - return genericError - } - var internalError error - for helmErrMsg, internalErr := range helmErrorInternalErrorMap { - if strings.Contains(err.Error(), helmErrMsg) { - internalError = status.New(internalErr.grpcCode, internalErr.errorMsg).Err() - } - } - return internalError -} - -// getInternalErrorForGenericErrorTypes returns all those kinds of errors which are generic in nature and also dynamic, make sure to return all generic and dynamic errors from this func. instead of putting them in helmErrorInternalErrorMap -func getInternalErrorForGenericErrorTypes(err error) error { - /* - for example:- - 1. if namespace is not found err is:- namespace "ns1" not found, - 2. in case ingress class not found error is of type ingress class: IngressClass.networking.k8s.io "ingress1" not found, - 3. when some resource is forbidden then err can be of many formats one of which is:- Unable to continue with install: could not get information about the resource Ingress "prakash-1-prakash-env3-ingress" in namespace "prakash-ns3": ingresses.networking.k8s.io "prakash-1-prakash-env3-ingress" is forbidden... - etc.. - */ - for errorMsg, code := range DynamicErrorMapping { - if strings.Contains(strings.ToLower(err.Error()), errorMsg) { - return status.New(code, err.Error()).Err() - } - } - - return nil + return customErr.ConvertHelmErrorToInternalError(err) } diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/errors/constants.go b/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/errors/constants.go new file mode 100644 index 000000000..3f961aa4a --- /dev/null +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/errors/constants.go @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package errors + +import ( + "google.golang.org/grpc/codes" +) + +// list of error strings from Helm. These are part of the errors we check for presence in Helm's error messages. +const ( + ClusterUnreachableErrorMsg = "cluster unreachable" + CrdPreconditionErrorMsg = "ensure crds are installed first" + ArrayStringMismatchErrorMsg = "got array expected string" + NotFoundErrorMsg = "not found" //this is a generic type constant, an error could be namespace "ns1" not found or service "ser1" not found. + InvalidValueErrorMsg = "invalid value" + OperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress" + ForbiddenErrorMsg = "forbidden" +) + +// list of internal errors, these errors are easy for the users to understand +const ( + InternalClusterUnreachableErrorMsg = "cluster unreachable" + InternalOperationInProgressErrorMsg = "another operation (install/upgrade/rollback) is in progress" +) + +type errorGrpcCodeTuple struct { + errorMsg string + grpcCode codes.Code +} + +var helmErrorInternalErrorMap = map[string]errorGrpcCodeTuple{ + ClusterUnreachableErrorMsg: {errorMsg: InternalClusterUnreachableErrorMsg, grpcCode: codes.DeadlineExceeded}, + OperationInProgressErrorMsg: {errorMsg: InternalOperationInProgressErrorMsg, grpcCode: codes.FailedPrecondition}, +} + +var DynamicErrorMapping = map[string]codes.Code{ + NotFoundErrorMsg: codes.NotFound, + ForbiddenErrorMsg: codes.PermissionDenied, + InvalidValueErrorMsg: codes.InvalidArgument, + ArrayStringMismatchErrorMsg: codes.InvalidArgument, + CrdPreconditionErrorMsg: codes.FailedPrecondition, +} diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/errors/errors.go b/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/errors/errors.go new file mode 100644 index 000000000..6d503e68c --- /dev/null +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/errors/errors.go @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package errors + +import ( + "google.golang.org/grpc/status" + "strings" +) + +// ConvertHelmErrorToInternalError converts known error message from helm to internal error and also maps it with proper grpc code +func ConvertHelmErrorToInternalError(err error) error { + genericError := getInternalErrorForGenericErrorTypes(err) + if genericError != nil { + return genericError + } + var internalError error + for helmErrMsg, internalErr := range helmErrorInternalErrorMap { + if strings.Contains(err.Error(), helmErrMsg) { + internalError = status.New(internalErr.grpcCode, internalErr.errorMsg).Err() + } + } + return internalError +} + +// getInternalErrorForGenericErrorTypes returns all those kinds of errors which are generic in nature and also dynamic, make sure to return all generic and dynamic errors from this func. instead of putting them in helmErrorInternalErrorMap +func getInternalErrorForGenericErrorTypes(err error) error { + /* + for example:- + 1. if namespace is not found err is:- namespace "ns1" not found, + 2. in case ingress class not found error is of type ingress class: IngressClass.networking.k8s.io "ingress1" not found, + 3. when some resource is forbidden then err can be of many formats one of which is:- Unable to continue with install: could not get information about the resource Ingress "prakash-1-prakash-env3-ingress" in namespace "prakash-ns3": ingresses.networking.k8s.io "prakash-1-prakash-env3-ingress" is forbidden... + etc.. + */ + for errorMsg, code := range DynamicErrorMapping { + if strings.Contains(strings.ToLower(err.Error()), errorMsg) { + return status.New(code, err.Error()).Err() + } + } + + return nil +} diff --git a/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/listChildObjects.go b/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/listChildObjects.go index f79bda52e..f1a90ecd3 100644 --- a/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/listChildObjects.go +++ b/kubelink/vendor/github.com/devtron-labs/common-lib/k8sResource/listChildObjects.go @@ -3,9 +3,11 @@ package k8sResource import ( "context" "errors" + customErr "github.com/devtron-labs/common-lib/k8sResource/errors" k8sUtils "github.com/devtron-labs/common-lib/utils/k8s" k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean" coreV1 "k8s.io/api/core/v1" + errors2 "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -196,8 +198,15 @@ func (impl *K8sServiceImpl) getChildObject(client *dynamicClient.DynamicClient, filterListStartTime := time.Now() childrenObjectsList, k8sErr := childResourceClient.List(context.Background(), options) if k8sErr != nil { - impl.logger.Errorw("error in getting child listObjects", filterObjRequest.GetLoggerMetadata("counter", counter, "timeTaken", time.Since(filterListStartTime).Seconds(), "err", k8sErr)...) - return nil, k8sErr + statusError, matched := k8sErr.(*errors2.StatusError) + if !matched || statusError.ErrStatus.Reason != metaV1.StatusReasonNotFound { + internalErr := customErr.ConvertHelmErrorToInternalError(k8sErr) + if internalErr != nil { + k8sErr = internalErr + } + impl.logger.Errorw("error in getting child listObjects", filterObjRequest.GetLoggerMetadata("counter", counter, "timeTaken", time.Since(filterListStartTime).Seconds(), "err", k8sErr)...) + return nil, k8sErr + } } impl.logger.Debugw("listing child objects", filterObjRequest.GetLoggerMetadata("counter", counter, "timeTaken", time.Since(filterListStartTime).Seconds())...) filterObjRequest = filterObjRequest.WithListObjects(childrenObjectsList)