From 62c6fe1b49a9e874e671043fa0a083922231e0b5 Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Mon, 16 Oct 2023 15:53:02 +0200 Subject: [PATCH 1/4] Add some error handling --- cmd/kyma/alpha/create/module/opts.go | 6 ++++-- pkg/module/blob/blob.go | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmd/kyma/alpha/create/module/opts.go b/cmd/kyma/alpha/create/module/opts.go index 60900f7da..52c16254d 100644 --- a/cmd/kyma/alpha/create/module/opts.go +++ b/cmd/kyma/alpha/create/module/opts.go @@ -92,14 +92,16 @@ func (o *Options) validatePath() error { } func (o *Options) validateChannel() error { - if len(o.Channel) < ChannelMinLength || len(o.Channel) > ChannelMaxLength { return fmt.Errorf( "invalid channel length, length should between %d and %d, %w", ChannelMinLength, ChannelMaxLength, ErrChannelValidation, ) } - matched, _ := regexp.MatchString(`^[a-z]+$`, o.Channel) + matched, err := regexp.MatchString(`^[a-z]+$`, o.Channel) + if err != nil { + return fmt.Errorf("failed to evaluate regex for channel: %w", err) + } if !matched { return fmt.Errorf("invalid channel format, only allow characters from a-z") } diff --git a/pkg/module/blob/blob.go b/pkg/module/blob/blob.go index 878a59024..253a2e5fe 100644 --- a/pkg/module/blob/blob.go +++ b/pkg/module/blob/blob.go @@ -346,8 +346,12 @@ func addFileToTar( return fmt.Errorf("unable to open file %q: %w", path, err) } if _, err := io.Copy(tw, file); err != nil { - _ = file.Close() - return fmt.Errorf("unable to add file to tar %q: %w", path, err) + copyErr := err + err = file.Close() + if err != nil { + return fmt.Errorf("unable to close file %q: %w", path, err) + } + return fmt.Errorf("unable to add file to tar %q: %w", path, copyErr) } if err := file.Close(); err != nil { return fmt.Errorf("unable to close file %q: %w", path, err) From 74e05166c4403bc9e7910a34b1c1d18d4f90d0cb Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Mon, 16 Oct 2023 15:55:18 +0200 Subject: [PATCH 2/4] More error handling for regex --- cmd/kyma/alpha/create/module/moduleconfig.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/kyma/alpha/create/module/moduleconfig.go b/cmd/kyma/alpha/create/module/moduleconfig.go index 593a974b8..5f70915ec 100644 --- a/cmd/kyma/alpha/create/module/moduleconfig.go +++ b/cmd/kyma/alpha/create/module/moduleconfig.go @@ -89,7 +89,10 @@ func (cv *configValidator) validateName() *configValidator { if len(cv.config.Name) > moduleNameMaxLen { return fmt.Errorf("%w, module name length cannot exceed 255 characters", ErrNameValidation) } - matched, _ := regexp.MatchString(moduleNamePattern, cv.config.Name) + matched, err := regexp.MatchString(moduleNamePattern, cv.config.Name) + if err != nil { + return fmt.Errorf("failed to evaluate regex for module name pattern: %w", err) + } if !matched { return fmt.Errorf("%w for input %q, name must match the required pattern, e.g: 'github.com/path-to/your-repo'", ErrNameValidation, cv.config.Name) } @@ -108,7 +111,10 @@ func (cv *configValidator) validateNamespace() *configValidator { if len(cv.config.Namespace) > namespaceMaxLen { return fmt.Errorf("%w, module name length cannot exceed 253 characters", ErrNamespaceValidation) } - matched, _ := regexp.MatchString(namespacePattern, cv.config.Namespace) + matched, err := regexp.MatchString(namespacePattern, cv.config.Namespace) + if err != nil { + return fmt.Errorf("failed to evaluate regex for module namespace pattern: %w", err) + } if !matched { return fmt.Errorf("%w for input %q, namespace must contain only small alphanumeric characters and hyphens", ErrNamespaceValidation, cv.config.Namespace) } @@ -155,7 +161,10 @@ func (cv *configValidator) validateChannel() *configValidator { "%w for input %q, invalid channel length, length should between %d and %d", ErrChannelValidation, cv.config.Channel, ChannelMinLength, ChannelMaxLength) } - matched, _ := regexp.MatchString(`^[a-z]+$`, cv.config.Channel) + matched, err := regexp.MatchString(`^[a-z]+$`, cv.config.Channel) + if err != nil { + return fmt.Errorf("failed to evaluate regex for channel: %w", err) + } if !matched { return fmt.Errorf("%w for input %q, invalid channel format, only allow characters from a-z", ErrChannelValidation, cv.config.Channel) } From a8f582d5aedb23a7cfe5e05feaf905816aa9e2d9 Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Mon, 16 Oct 2023 16:13:49 +0200 Subject: [PATCH 3/4] Condest reconciler call --- internal/deploy/deploy.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 9eaab04b6..5e08d7f58 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -73,33 +73,33 @@ func doReconciliation(opts Options, delete bool) (*service.ReconciliationResult, } manifests := make(chan ComponentStatus) - runtimeBuilder := service.NewRuntimeBuilder(reconciliation.NewInMemoryReconciliationRepository(), opts.Logger) - reconcilationResult, err := runtimeBuilder.RunLocal(func(component string, msg *reconciler.CallbackMessage) { - var state ComponentState - var errorRecieved error + statusFunc := func(component string, msg *reconciler.CallbackMessage) { + var status ComponentStatus switch msg.Status { case reconciler.StatusSuccess: - state = Success - errorRecieved = nil + status = ComponentStatus{component, Success, nil, msg.Manifest} case reconciler.StatusFailed: - errorRecieved = errors.Errorf("%s", msg.Error) - state = RecoverableError + status = ComponentStatus{component, + RecoverableError, + errors.Errorf("%s", msg.Error), + msg.Manifest} case reconciler.StatusError: - errorRecieved = errors.Errorf("%s", msg.Error) - state = UnrecoverableError + status = ComponentStatus{component, + UnrecoverableError, + errors.Errorf("%s", msg.Error), + msg.Manifest} } - status := ComponentStatus{component, state, errorRecieved, msg.Manifest} - if opts.DryRun { go manifestCollector(manifests) manifests <- status } opts.StatusFunc(status) + } - }). + reconcilationResult, err := runtimeBuilder.RunLocal(statusFunc). WithSchedulerConfig(&service.SchedulerConfig{ PreComponents: opts.Components.PrerequisiteNames(), DeleteStrategy: ds, From 107ac595e5c3d5a318c08876cb7ce62631eaae1b Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Mon, 16 Oct 2023 16:19:44 +0200 Subject: [PATCH 4/4] Add default status --- internal/deploy/deploy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 5e08d7f58..c840b26a2 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -75,7 +75,7 @@ func doReconciliation(opts Options, delete bool) (*service.ReconciliationResult, manifests := make(chan ComponentStatus) runtimeBuilder := service.NewRuntimeBuilder(reconciliation.NewInMemoryReconciliationRepository(), opts.Logger) statusFunc := func(component string, msg *reconciler.CallbackMessage) { - var status ComponentStatus + status := ComponentStatus{component, "", nil, msg.Manifest} switch msg.Status { case reconciler.StatusSuccess: status = ComponentStatus{component, Success, nil, msg.Manifest}