Skip to content

Commit

Permalink
fix: validate deploy job name
Browse files Browse the repository at this point in the history
If the deploy job name is not set, an internal error will occur.
This commit adds validation logic so that we can ensure that the deploy job name
is provided if the deploy job command is specified.
  • Loading branch information
pawelkuc committed Feb 28, 2024
1 parent 7867a7c commit 81ab63a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/validation/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package validation

import (
"errors"

apps "github.com/ninech/apis/apps/v1alpha1"
)

// ConfigValidator validates a Config
type ConfigValidator struct {
Config apps.Config
}

// Validate validates the config
func (c ConfigValidator) Validate() error {
if c.Config.DeployJob != nil {
if len(c.Config.DeployJob.Name) == 0 {
return errors.New("deploy job name cannot be empty")
}
}
return nil
}
9 changes: 9 additions & 0 deletions create/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ func (app *applicationCmd) Run(ctx context.Context, client *api.Client) error {
}
}

if newApp.Spec.ForProvider.Config.DeployJob != nil {
configValidator := &validation.ConfigValidator{
Config: newApp.Spec.ForProvider.Config,
}
if err := configValidator.Validate(); err != nil {
return fmt.Errorf("error when validating application config: %w", err)
}
}

c := newCreator(client, newApp, strings.ToLower(apps.ApplicationKind))
appWaitCtx, cancel := context.WithTimeout(ctx, app.WaitTimeout)
defer cancel()
Expand Down
9 changes: 9 additions & 0 deletions update/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ func (cmd *applicationCmd) Run(ctx context.Context, client *api.Client) error {
}
}

if app.Spec.ForProvider.Config.DeployJob != nil {
configValidator := &validation.ConfigValidator{
Config: app.Spec.ForProvider.Config,
}
if err := configValidator.Validate(); err != nil {
return fmt.Errorf("error when validating application config: %w", err)
}
}

return nil
})

Expand Down

0 comments on commit 81ab63a

Please sign in to comment.