Skip to content

Commit

Permalink
chore: Fix some CLI checkmarx issues (#1807)
Browse files Browse the repository at this point in the history
* Add some error handling

* More error handling for regex

* Condest reconciler call

* Add default status
  • Loading branch information
lindnerby authored Oct 17, 2023
1 parent dea2690 commit e4e4451
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
15 changes: 12 additions & 3 deletions cmd/kyma/alpha/create/module/moduleconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/kyma/alpha/create/module/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
26 changes: 13 additions & 13 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
status := ComponentStatus{component, "", nil, msg.Manifest}
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,
Expand Down
8 changes: 6 additions & 2 deletions pkg/module/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e4e4451

Please sign in to comment.