Skip to content

Commit

Permalink
Merge pull request #68 from ninech/config-deploy-job
Browse files Browse the repository at this point in the history
feat: add deploy-job to config create/update
  • Loading branch information
ctrox authored Nov 6, 2023
2 parents 6a20c17 + a8c6b2d commit 06e9542
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 17 deletions.
7 changes: 6 additions & 1 deletion api/util/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
// DNSSetupURL redirects to the proper deplo.io docs entry about
// how to setup custom hosts
DNSSetupURL = "https://docs.nine.ch/a/myshbw3EY1"
NoneText = "<none>"
)

func UnverifiedAppHosts(app *apps.Application) []string {
Expand Down Expand Up @@ -96,7 +97,11 @@ func UpdateEnvVars(oldEnvs []apps.EnvVar, newEnvs map[string]string, toDelete []

func EnvVarToString(envs apps.EnvVars) string {
if envs == nil {
return "nil"
return NoneText
}

if len(envs) == 0 {
return NoneText
}

var keyValuePairs []string
Expand Down
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
14 changes: 14 additions & 0 deletions get/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func printProjectConfigs(configs []apps.ProjectConfig, get *Cmd, out io.Writer,
"REPLICAS",
"PORT",
"ENVIRONMENT_VARIABLES",
"BASIC_AUTH",
"DEPLOY_JOB",
"AGE",
)
}
Expand All @@ -77,6 +79,16 @@ func printProjectConfigs(configs []apps.ProjectConfig, get *Cmd, out io.Writer,
port = strconv.Itoa(int(*c.Spec.ForProvider.Config.Port))
}

basicAuth := false
if c.Spec.ForProvider.Config.EnableBasicAuth != nil {
basicAuth = *c.Spec.ForProvider.Config.EnableBasicAuth
}

deployJobName := util.NoneText
if c.Spec.ForProvider.Config.DeployJob != nil {
deployJobName = c.Spec.ForProvider.Config.DeployJob.Name
}

get.writeTabRow(
w,
c.ObjectMeta.Namespace,
Expand All @@ -85,6 +97,8 @@ func printProjectConfigs(configs []apps.ProjectConfig, get *Cmd, out io.Writer,
replicas,
port,
util.EnvVarToString(c.Spec.ForProvider.Config.Env),
strconv.FormatBool(basicAuth),
deployJobName,
duration.HumanDuration(time.Since(c.ObjectMeta.CreationTimestamp.Time)),
)
}
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

0 comments on commit 06e9542

Please sign in to comment.