Skip to content
Merged
56 changes: 56 additions & 0 deletions common-lib/k8sResource/errors/constants.go
Original file line number Diff line number Diff line change
@@ -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,
}
55 changes: 55 additions & 0 deletions common-lib/k8sResource/errors/errors.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 11 additions & 2 deletions common-lib/k8sResource/listChildObjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion git-sensor/pkg/git/RepositoryManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()
Expand Down
37 changes: 1 addition & 36 deletions kubelink/error/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
33 changes: 2 additions & 31 deletions kubelink/error/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading