Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deploy-job to config create/update #68

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions create/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ninech/nctl/api"
"github.com/ninech/nctl/api/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)

// all fields need to be pointers so we can detect if they have been set by
Expand All @@ -17,6 +18,7 @@ type configCmd struct {
Replicas *int32 `help:"Amount of replicas of the running app."`
Env *map[string]string `help:"Environment variables which are passed to the app at runtime."`
BasicAuth *bool `help:"Enable/Disable basic authentication for applications."`
DeployJob deployJob `embed:"" prefix:"deploy-job-"`
}

func (cmd *configCmd) Run(ctx context.Context, client *api.Client) error {
Expand All @@ -31,6 +33,20 @@ func (cmd *configCmd) newProjectConfig(namespace string) *apps.ProjectConfig {
env = util.EnvVarsFromMap(*cmd.Env)
}

var deployJob *apps.DeployJob
if len(cmd.DeployJob.Command) != 0 && len(cmd.DeployJob.Name) != 0 {
deployJob = &apps.DeployJob{
Job: apps.Job{
Name: cmd.DeployJob.Name,
Command: cmd.DeployJob.Command,
},
FiniteJob: apps.FiniteJob{
Retries: pointer.Int32(cmd.DeployJob.Retries),
Timeout: &metav1.Duration{Duration: cmd.DeployJob.Timeout},
},
}
}

return &apps.ProjectConfig{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Expand All @@ -44,6 +60,7 @@ func (cmd *configCmd) newProjectConfig(namespace string) *apps.ProjectConfig {
Port: cmd.Port,
Env: env,
EnableBasicAuth: cmd.BasicAuth,
DeployJob: deployJob,
},
},
},
Expand Down
11 changes: 11 additions & 0 deletions create/project_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package create
import (
"context"
"testing"
"time"

apps "github.com/ninech/apis/apps/v1alpha1"
"github.com/ninech/nctl/api"
Expand Down Expand Up @@ -32,6 +33,10 @@ func TestProjectConfig(t *testing.T) {
Replicas: pointer.Int32(42),
Env: &map[string]string{"key1": "val1"},
BasicAuth: pointer.Bool(true),
DeployJob: deployJob{
Command: "exit 0", Name: "exit",
Retries: 1, Timeout: time.Minute * 5,
},
},
project: "namespace-1",
checkConfig: func(t *testing.T, cmd configCmd, cfg *apps.ProjectConfig) {
Expand All @@ -41,6 +46,10 @@ func TestProjectConfig(t *testing.T) {
assert.Equal(t, *cmd.Replicas, *cfg.Spec.ForProvider.Config.Replicas)
assert.Equal(t, *cmd.BasicAuth, *cfg.Spec.ForProvider.Config.EnableBasicAuth)
assert.Equal(t, util.EnvVarsFromMap(*cmd.Env), cfg.Spec.ForProvider.Config.Env)
assert.Equal(t, cmd.DeployJob.Command, cfg.Spec.ForProvider.Config.DeployJob.Command)
assert.Equal(t, cmd.DeployJob.Name, cfg.Spec.ForProvider.Config.DeployJob.Name)
assert.Equal(t, cmd.DeployJob.Timeout, cfg.Spec.ForProvider.Config.DeployJob.Timeout.Duration)
assert.Equal(t, cmd.DeployJob.Retries, *cfg.Spec.ForProvider.Config.DeployJob.Retries)
},
},
"some fields not set": {
Expand All @@ -56,6 +65,7 @@ func TestProjectConfig(t *testing.T) {
assert.Nil(t, cfg.Spec.ForProvider.Config.EnableBasicAuth)
assert.Equal(t, *cmd.Replicas, *cfg.Spec.ForProvider.Config.Replicas)
assert.Empty(t, cfg.Spec.ForProvider.Config.Env)
assert.Nil(t, cfg.Spec.ForProvider.Config.DeployJob)
},
},
"all fields not set": {
Expand All @@ -68,6 +78,7 @@ func TestProjectConfig(t *testing.T) {
assert.Nil(t, cfg.Spec.ForProvider.Config.Replicas)
assert.Empty(t, cfg.Spec.ForProvider.Config.Env)
assert.Nil(t, cfg.Spec.ForProvider.Config.EnableBasicAuth)
assert.Nil(t, cfg.Spec.ForProvider.Config.DeployJob)
},
},
}
Expand Down
32 changes: 16 additions & 16 deletions update/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (cmd *applicationCmd) applyUpdates(app *apps.Application) {
app.Spec.ForProvider.Config.EnableBasicAuth = cmd.BasicAuth
}
if cmd.DeployJob != nil {
cmd.applyDeployJobUpdates(app)
cmd.DeployJob.applyUpdates(&app.Spec.ForProvider.Config)
}

var env map[string]string
Expand Down Expand Up @@ -167,31 +167,31 @@ func (cmd *applicationCmd) applyUpdates(app *apps.Application) {
app.Spec.ForProvider.BuildEnv = util.UpdateEnvVars(app.Spec.ForProvider.BuildEnv, buildEnv, buildDelEnv)
}

func (cmd *applicationCmd) applyDeployJobUpdates(app *apps.Application) {
if cmd.DeployJob.Enabled != nil && !*cmd.DeployJob.Enabled {
func (job deployJob) applyUpdates(cfg *apps.Config) {
if job.Enabled != nil && !*job.Enabled {
// if enabled is explicitly set to false we set the DeployJob field to
// nil on the API, to completely remove the object.
app.Spec.ForProvider.Config.DeployJob = nil
cfg.DeployJob = nil
return
}

if cmd.DeployJob.Name != nil && len(*cmd.DeployJob.Name) != 0 {
ensureDeployJob(app).Spec.ForProvider.Config.DeployJob.Name = *cmd.DeployJob.Name
if job.Name != nil && len(*job.Name) != 0 {
ensureDeployJob(cfg).DeployJob.Name = *job.Name
}
if cmd.DeployJob.Command != nil && len(*cmd.DeployJob.Command) != 0 {
ensureDeployJob(app).Spec.ForProvider.Config.DeployJob.Command = *cmd.DeployJob.Command
if job.Command != nil && len(*job.Command) != 0 {
ensureDeployJob(cfg).DeployJob.Command = *job.Command
}
if cmd.DeployJob.Retries != nil {
ensureDeployJob(app).Spec.ForProvider.Config.DeployJob.Retries = cmd.DeployJob.Retries
if job.Retries != nil {
ensureDeployJob(cfg).DeployJob.Retries = job.Retries
}
if cmd.DeployJob.Timeout != nil {
ensureDeployJob(app).Spec.ForProvider.Config.DeployJob.Timeout = &metav1.Duration{Duration: *cmd.DeployJob.Timeout}
if job.Timeout != nil {
ensureDeployJob(cfg).DeployJob.Timeout = &metav1.Duration{Duration: *job.Timeout}
}
}

func ensureDeployJob(app *apps.Application) *apps.Application {
if app.Spec.ForProvider.Config.DeployJob == nil {
app.Spec.ForProvider.Config.DeployJob = &apps.DeployJob{}
func ensureDeployJob(cfg *apps.Config) *apps.Config {
if cfg.DeployJob == nil {
cfg.DeployJob = &apps.DeployJob{}
}
return app
return cfg
}
4 changes: 4 additions & 0 deletions update/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type configCmd struct {
Replicas *int32 `help:"Amount of replicas of the running app."`
Env *map[string]string `help:"Environment variables which are passed to the app at runtime."`
BasicAuth *bool `help:"Enable/Disable basic authentication for applications."`
DeployJob *deployJob `embed:"" prefix:"deploy-job-"`
}

func (cmd *configCmd) Run(ctx context.Context, client *api.Client) error {
Expand Down Expand Up @@ -59,4 +60,7 @@ func (cmd *configCmd) applyUpdates(cfg *apps.ProjectConfig) {
if cmd.BasicAuth != nil {
cfg.Spec.ForProvider.Config.EnableBasicAuth = cmd.BasicAuth
}
if cmd.DeployJob != nil {
cmd.DeployJob.applyUpdates(&cfg.Spec.ForProvider.Config)
}
}
9 changes: 9 additions & 0 deletions update/project_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package update
import (
"context"
"testing"
"time"

"github.com/alecthomas/kong"
apps "github.com/ninech/apis/apps/v1alpha1"
Expand Down Expand Up @@ -83,13 +84,21 @@ func TestConfig(t *testing.T) {
Replicas: pointer.Int32(2),
Env: &map[string]string{"zoo": "bar"},
BasicAuth: pointer.Bool(true),
DeployJob: &deployJob{
Command: pointer.String("exit 0"), Name: pointer.String("exit"),
Retries: pointer.Int32(1), Timeout: pointer.Duration(time.Minute * 5),
},
},
checkConfig: func(t *testing.T, cmd configCmd, orig, updated *apps.ProjectConfig) {
assert.Equal(t, apps.ApplicationSize(*cmd.Size), updated.Spec.ForProvider.Config.Size)
assert.Equal(t, *cmd.Port, *updated.Spec.ForProvider.Config.Port)
assert.Equal(t, *cmd.Replicas, *updated.Spec.ForProvider.Config.Replicas)
assert.Equal(t, *cmd.BasicAuth, *updated.Spec.ForProvider.Config.EnableBasicAuth)
assert.Equal(t, util.EnvVarsFromMap(*cmd.Env), updated.Spec.ForProvider.Config.Env)
assert.Equal(t, *cmd.DeployJob.Command, updated.Spec.ForProvider.Config.DeployJob.Command)
assert.Equal(t, *cmd.DeployJob.Name, updated.Spec.ForProvider.Config.DeployJob.Name)
assert.Equal(t, *cmd.DeployJob.Timeout, updated.Spec.ForProvider.Config.DeployJob.Timeout.Duration)
assert.Equal(t, *cmd.DeployJob.Retries, *updated.Spec.ForProvider.Config.DeployJob.Retries)
},
},
}
Expand Down