diff --git a/api/util/apps.go b/api/util/apps.go index 5b81efb..57fbfaa 100644 --- a/api/util/apps.go +++ b/api/util/apps.go @@ -107,6 +107,16 @@ func EnvVarToString(envs apps.EnvVars) string { return strings.Join(keyValuePairs, ";") } +func EnvVarByName(envVars apps.EnvVars, name string) *apps.EnvVar { + for _, e := range envVars { + if e.Name == name { + return &e + } + } + + return nil +} + type GitAuth struct { Username *string Password *string diff --git a/update/application.go b/update/application.go index b171fc1..e76cd13 100644 --- a/update/application.go +++ b/update/application.go @@ -12,6 +12,9 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// BuildTrigger is used to request a retry-build for the application. +const BuildTrigger = "BUILD_TRIGGER" + // all fields need to be pointers so we can detect if they have been set by // the user. type applicationCmd struct { @@ -27,6 +30,7 @@ type applicationCmd struct { BuildEnv *map[string]string `help:"Environment variables names which are passed to the app build process."` DeleteBuildEnv *[]string `help:"Build environment variables which are to be deleted."` DeployJob *deployJob `embed:"" prefix:"deploy-job-"` + RetryBuild *bool `help:"Retries build for the application if set to true." placeholder:"false"` } type gitConfig struct { @@ -137,10 +141,15 @@ func (cmd *applicationCmd) applyUpdates(app *apps.Application) { } app.Spec.ForProvider.Config.Env = util.UpdateEnvVars(app.Spec.ForProvider.Config.Env, env, delEnv) - var buildEnv map[string]string - if cmd.Env != nil { + buildEnv := make(map[string]string) + if cmd.BuildEnv != nil { buildEnv = *cmd.BuildEnv } + + if cmd.RetryBuild != nil && *cmd.RetryBuild { + buildEnv[BuildTrigger] = time.Now().UTC().Format(time.RFC3339) + } + var buildDelEnv []string if cmd.DeleteEnv != nil { buildDelEnv = *cmd.DeleteBuildEnv diff --git a/update/application_test.go b/update/application_test.go index 70ccadd..90459b1 100644 --- a/update/application_test.go +++ b/update/application_test.go @@ -125,6 +125,8 @@ func TestApplication(t *testing.T) { 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) + // RetryBuild should be not set by default: + assert.Nil(t, util.EnvVarByName(updated.Spec.ForProvider.BuildEnv, BuildTrigger)) }, }, "reset env variables": { @@ -209,6 +211,26 @@ func TestApplication(t *testing.T) { assert.Nil(t, updated.Spec.ForProvider.Config.DeployJob) }, }, + "retry build": { + orig: existingApp, + cmd: applicationCmd{ + Name: pointer.String(existingApp.Name), + RetryBuild: pointer.Bool(true), + }, + checkApp: func(t *testing.T, cmd applicationCmd, orig, updated *apps.Application) { + assert.NotNil(t, util.EnvVarByName(updated.Spec.ForProvider.BuildEnv, BuildTrigger)) + }, + }, + "do not retry build": { + orig: existingApp, + cmd: applicationCmd{ + Name: pointer.String(existingApp.Name), + RetryBuild: pointer.Bool(false), + }, + checkApp: func(t *testing.T, cmd applicationCmd, orig, updated *apps.Application) { + assert.Nil(t, util.EnvVarByName(updated.Spec.ForProvider.BuildEnv, BuildTrigger)) + }, + }, } for name, tc := range cases {