Skip to content

Commit

Permalink
module command: Add Error Code for Kyma Warning State (#1692)
Browse files Browse the repository at this point in the history
* module command: Add Error Code for Kyma Warning State

* Fix Linting Issues
  • Loading branch information
LeelaChacha authored Jul 6, 2023
1 parent 03423bf commit f92606a
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 11 deletions.
6 changes: 2 additions & 4 deletions cmd/kyma/alpha/disable/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,8 @@ func (cmd *command) run(ctx context.Context, l *zap.SugaredLogger, moduleName st

if cmd.opts.Wait {
waitStep := cmd.NewStep("Waiting for Kyma to become Ready")
if err = moduleInteractor.WaitUntilReady(
ctx,
); err != nil {
waitStep.Failuref("Kyma did not get Ready")
if err = moduleInteractor.WaitUntilReady(ctx); err != nil {
waitStep.Failuref("kyma did not get ready: %s", err)
return err
}
waitStep.Successf("Kyma is Ready")
Expand Down
2 changes: 1 addition & 1 deletion cmd/kyma/alpha/enable/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (cmd *command) run(ctx context.Context, l *zap.SugaredLogger, moduleName st
if cmd.opts.Wait {
waitStep := cmd.NewStep("Waiting for Kyma to become Ready")
if err = moduleInteractor.WaitUntilReady(ctx); err != nil {
waitStep.Failuref("Kyma did not get Ready")
waitStep.Failuref("kyma did not get ready: %s", err)
return err
}
waitStep.Successf("Kyma is Ready")
Expand Down
4 changes: 2 additions & 2 deletions cmd/kyma/alpha/enable/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func TestChannelValidation(t *testing.T) {
ControllerName: "-",
Channel: "fast",
CustomResourcePolicy: "-",
}, unstructured.Unstructured{}, false)
}, unstructured.Unstructured{}, false, false, false)
template2, _ := testutils.ModuleTemplateFactory(v1beta2.Module{
Name: "not-test",
ControllerName: "-",
Channel: "alpha",
CustomResourcePolicy: "-",
}, unstructured.Unstructured{}, false)
}, unstructured.Unstructured{}, false, false, false)
allTemplates := v1beta2.ModuleTemplateList{
TypeMeta: metav1.TypeMeta{},
ListMeta: metav1.ListMeta{},
Expand Down
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func main() {
command := kyma.NewCmd(cli.NewOptions())

err := command.Execute()

if err != nil {
os.Exit(1)
os.Exit(cli.GetExitCode(err))
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/kyma-incubator/reconciler v0.0.0-20230630093527-31217ab6a174
github.com/kyma-project/hydroform/function v0.0.0-20230628151226-25b31247e585
github.com/kyma-project/hydroform/provision v0.0.0-20230418133637-1ea26b368bb6
github.com/kyma-project/lifecycle-manager v0.0.0-20230515084114-0cd8e8e80748
github.com/kyma-project/lifecycle-manager v0.0.0-20230626125722-e28fc6438d83
github.com/mandelsoft/vfs v0.0.0-20220805210647-bf14a11bfe31
github.com/open-component-model/ocm v0.3.0-rc.1
github.com/opencontainers/go-digest v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,8 @@ github.com/kyma-project/hydroform/provision v0.0.0-20230418133637-1ea26b368bb6 h
github.com/kyma-project/hydroform/provision v0.0.0-20230418133637-1ea26b368bb6/go.mod h1:eXIZaENQU/Y4sq20z3AgiFg6hSZUP4DmQ2JTMtFt+A8=
github.com/kyma-project/istio/operator v0.0.0-20221129102055-d37c5c8e6add h1:ohTtnIlNXSQPHB/nMZvBXs+uBnTqQ6HRSgAvptyWzlc=
github.com/kyma-project/istio/operator v0.0.0-20221129102055-d37c5c8e6add/go.mod h1:K5jDoHdqW8EI2vXX+7J+RnEmk4YQRnd6fTmuuSyQ3Qg=
github.com/kyma-project/lifecycle-manager v0.0.0-20230515084114-0cd8e8e80748 h1:9XdRAm5obA1/cHdrstolxz0TwxZdmgTyTw31oPrjTHQ=
github.com/kyma-project/lifecycle-manager v0.0.0-20230515084114-0cd8e8e80748/go.mod h1:G+HHF0oOUQYQ8g2VNvpYe1wreR42ri/upK3lj5D4J2c=
github.com/kyma-project/lifecycle-manager v0.0.0-20230626125722-e28fc6438d83 h1:kA6QUuA6oSOQe9F0gW2e8x4dGRdol9UAsw8zPIUKswo=
github.com/kyma-project/lifecycle-manager v0.0.0-20230626125722-e28fc6438d83/go.mod h1:mik/72CzndvOf8OH11a+CGufl2Ua6Lqk8HIUet3u7To=
github.com/kyma-project/module-manager/operator v0.0.0-20221020113457-620af4f4b365 h1:50FLPAynQ7++CJb2ZEdfnCRO68cDcJoa+eo3qUwElc4=
github.com/kyma-project/module-manager/operator v0.0.0-20221020113457-620af4f4b365/go.mod h1:N0kmS8pTNXq+7/Bdr6vfogmw03H+uYMCauTQ6U6+kA0=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
Expand Down
5 changes: 5 additions & 0 deletions internal/cli/alpha/module/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package module

import "errors"

var ErrKymaInWarningState = errors.New("kyma CR in warning state error")
2 changes: 2 additions & 0 deletions internal/cli/alpha/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ func IsKymaReady(l *zap.SugaredLogger, obj runtime.Object) error {
)
}
return nil
case v1beta2.StateWarning:
return ErrKymaInWarningState
default:
lastOperation := kyma.Status.LastOperation
if lastOperation.Operation == "" {
Expand Down
48 changes: 48 additions & 0 deletions internal/cli/exit_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"github.com/avast/retry-go"
"github.com/kyma-project/cli/internal/cli/alpha/module"
"github.com/pkg/errors"
)

var ErrorCodeMap = map[error]int{
module.ErrKymaInWarningState: 2,
}

func GetExitCode(err error) int {
switch err := err.(type) {
default:
return handleSingleError(err)
case retry.Error:
return handleListOfErrors(err)
}
}

func handleSingleError(err error) int {
if errorCode, found := mapNestedErrorToCode(err); found {
return errorCode
}
return 1
}

func handleListOfErrors(errorList retry.Error) int {
for _, err := range errorList {
if errorCode, found := mapNestedErrorToCode(err); found {
return errorCode
}
}
return 1
}

func mapNestedErrorToCode(err error) (int, bool) {
for {
if errorCode, ok := ErrorCodeMap[err]; ok {
return errorCode, true
}
err = errors.Unwrap(err)
if err == nil {
return -1, false
}
}
}
50 changes: 50 additions & 0 deletions internal/cli/exit_code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"errors"
"fmt"
"github.com/avast/retry-go"
"github.com/kyma-project/cli/internal/cli/alpha/module"
"testing"
)

func TestGetExitCode(t *testing.T) {
tests := []struct {
name string
err error
want int
}{
{
name: "correct exit code for single error",
err: module.ErrKymaInWarningState,
want: ErrorCodeMap[module.ErrKymaInWarningState],
},
{
name: "correct exit code for wrapped error",
err: fmt.Errorf("wrapped error 2: %w",
fmt.Errorf("wrapped error 1: %w",
module.ErrKymaInWarningState)),
want: ErrorCodeMap[module.ErrKymaInWarningState],
},
{
name: "correct exit code for error in a list",
err: retry.Error([]error{
errors.New("random non-mapped error"),
module.ErrKymaInWarningState,
}),
want: ErrorCodeMap[module.ErrKymaInWarningState],
},
{
name: "default exit code for non-mapped error",
err: errors.New("random non-mapped error"),
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetExitCode(tt.err); got != tt.want {
t.Errorf("GetExitCode() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit f92606a

Please sign in to comment.