diff --git a/.vela/template.yml b/.vela/template.yml index e8524bb..e837e08 100644 --- a/.vela/template.yml +++ b/.vela/template.yml @@ -9,6 +9,7 @@ # - .cluster: (default: "") # - .context: (default: "") # - .namespace: (default: "") +# - .dry_run: (default: false) # - .files: (default: "[]") # - .containers: (default: "{}") # - .output: (default: "json") @@ -28,6 +29,7 @@ steps: cluster: {{ default "" .cluster }} context: {{ default "" .context }} namespace: {{ default "" .namespace }} + dry_run: {{ default "false" .dry_run }} files: {{ default "[]" .files }} containers: {{ default "{}" .containers }} output: {{ default "json" .output }} diff --git a/Makefile b/Makefile index eb33388..b3d5dd8 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,7 @@ docker-run: -e PARAMETER_CLUSTER \ -e PARAMETER_CONTEXT \ -e PARAMETER_NAMESPACE \ + -e PARAMETER_DRY_RUN \ -e PARAMETER_LOG_LEVEL \ -e PARAMETER_FILES \ -e PARAMETER_CONTAINERS \ diff --git a/cmd/vela-kubernetes/apply.go b/cmd/vela-kubernetes/apply.go index bf115ff..64f5942 100644 --- a/cmd/vela-kubernetes/apply.go +++ b/cmd/vela-kubernetes/apply.go @@ -13,6 +13,8 @@ import ( // Apply represents the plugin configuration for Apply config information. type Apply struct { + // enables pretending to apply the files + DryRun bool // Kubernetes files or directories to apply Files []string // sets the output for the apply command @@ -48,6 +50,9 @@ func (a *Apply) Command(c *Config, file string) *exec.Cmd { // add flag for apply kubectl command flags = append(flags, "apply") + // add flag for dry run mode + flags = append(flags, fmt.Sprintf("--dry-run=%t", a.DryRun)) + // check if file is provided if len(file) > 0 { // add flag for file from provided apply file diff --git a/cmd/vela-kubernetes/apply_test.go b/cmd/vela-kubernetes/apply_test.go index 72d67b9..dec6d46 100644 --- a/cmd/vela-kubernetes/apply_test.go +++ b/cmd/vela-kubernetes/apply_test.go @@ -21,6 +21,7 @@ func TestKubernetes_Apply_Command(t *testing.T) { } a := &Apply{ + DryRun: false, Files: []string{"apply.yml"}, Output: "json", } @@ -32,6 +33,7 @@ func TestKubernetes_Apply_Command(t *testing.T) { fmt.Sprintf("--cluster=%s", c.Cluster), fmt.Sprintf("--context=%s", c.Context), "apply", + fmt.Sprintf("--dry-run=%t", a.DryRun), fmt.Sprintf("--filename=%s", file), fmt.Sprintf("--output=%s", a.Output), ) @@ -54,6 +56,7 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) { } a := &Apply{ + DryRun: false, Files: []string{"apply.yml"}, Output: "json", } @@ -67,6 +70,7 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) { func TestKubernetes_Apply_Validate(t *testing.T) { // setup types a := &Apply{ + DryRun: false, Files: []string{"apply.yml"}, Output: "json", } @@ -80,6 +84,7 @@ func TestKubernetes_Apply_Validate(t *testing.T) { func TestKubernetes_Apply_Validate_NoFiles(t *testing.T) { // setup types a := &Apply{ + DryRun: false, Output: "json", } diff --git a/cmd/vela-kubernetes/main.go b/cmd/vela-kubernetes/main.go index 23a5cf9..ad49c3d 100644 --- a/cmd/vela-kubernetes/main.go +++ b/cmd/vela-kubernetes/main.go @@ -48,6 +48,11 @@ func main() { // Apply Flags + cli.BoolFlag{ + EnvVar: "PARAMETER_DRY_RUN,APPLY_DRY_RUN", + Name: "apply.dry_run", + Usage: "enables pretending to apply the files", + }, cli.StringSliceFlag{ EnvVar: "PARAMETER_FILES,APPLY_FILES", Name: "apply.files", @@ -90,6 +95,11 @@ func main() { Name: "patch.containers", Usage: "containers from files to patch", }, + cli.BoolFlag{ + EnvVar: "PARAMETER_DRY_RUN,PATCH_DRY_RUN", + Name: "patch.dry_run", + Usage: "enables pretending to patch the containers", + }, cli.StringFlag{ EnvVar: "PARAMETER_OUTPUT,PATCH_OUTPUT", Name: "patch.output", @@ -155,6 +165,7 @@ func run(c *cli.Context) error { p := &Plugin{ // apply configuration Apply: &Apply{ + DryRun: c.Bool("apply.dry_run"), Files: c.StringSlice("apply.files"), Output: c.String("apply.output"), }, @@ -167,6 +178,7 @@ func run(c *cli.Context) error { }, // patch configuration Patch: &Patch{ + DryRun: c.Bool("patch.dry_run"), Output: c.String("patch.output"), RawContainers: c.String("patch.containers"), }, diff --git a/cmd/vela-kubernetes/patch.go b/cmd/vela-kubernetes/patch.go index 89bac67..9475a69 100644 --- a/cmd/vela-kubernetes/patch.go +++ b/cmd/vela-kubernetes/patch.go @@ -66,6 +66,8 @@ spec: type Patch struct { // container images from files to patch Containers []*Container + // enables pretending to patch the containers from the files + DryRun bool // sets the output for the patch command Output string // raw input of containers provided for plugin @@ -101,6 +103,9 @@ func (p *Patch) Command(c *Config, container *Container) *exec.Cmd { // add flag for patch kubectl command flags = append(flags, "patch") + // add flag for dry run mode + flags = append(flags, fmt.Sprintf("--local=%t", p.DryRun)) + // check if patch output is provided if len(p.Output) > 0 { // add flag for output from provided patch output diff --git a/cmd/vela-kubernetes/patch_test.go b/cmd/vela-kubernetes/patch_test.go index cf86b3a..57b70bb 100644 --- a/cmd/vela-kubernetes/patch_test.go +++ b/cmd/vela-kubernetes/patch_test.go @@ -27,6 +27,7 @@ func TestKubernetes_Patch_Command(t *testing.T) { Image: "alpine", }, }, + DryRun: false, Output: "json", RawContainers: `[{"name": "container", "image": "alpine"}]`, } @@ -38,6 +39,7 @@ func TestKubernetes_Patch_Command(t *testing.T) { fmt.Sprintf("--cluster=%s", c.Cluster), fmt.Sprintf("--context=%s", c.Context), "patch", + fmt.Sprintf("--local=%t", p.DryRun), fmt.Sprintf("--output=%s", p.Output), ) @@ -65,6 +67,7 @@ func TestKubernetes_Patch_Exec_Error(t *testing.T) { Image: "alpine", }, }, + DryRun: false, Output: "json", RawContainers: `[{"name": "container", "image": "alpine"}]`, } @@ -78,6 +81,7 @@ func TestKubernetes_Patch_Exec_Error(t *testing.T) { func TestKubernetes_Patch_Validate(t *testing.T) { // setup types p := &Patch{ + DryRun: false, Output: "json", RawContainers: `[{"name": "container", "image": "alpine"}]`, } @@ -91,6 +95,7 @@ func TestKubernetes_Patch_Validate(t *testing.T) { func TestKubernetes_Patch_Validate_Invalid(t *testing.T) { // setup types p := &Patch{ + DryRun: false, Output: "json", RawContainers: "!@#$%^&*()", } @@ -103,7 +108,10 @@ func TestKubernetes_Patch_Validate_Invalid(t *testing.T) { func TestKubernetes_Patch_Validate_NoRawContainers(t *testing.T) { // setup types - p := &Patch{} + p := &Patch{ + DryRun: false, + Output: "json", + } err := p.Validate() if err == nil { @@ -114,6 +122,7 @@ func TestKubernetes_Patch_Validate_NoRawContainers(t *testing.T) { func TestKubernetes_Patch_Validate_NoRawContainerName(t *testing.T) { // setup types p := &Patch{ + DryRun: false, Output: "json", RawContainers: `[{"image": "alpine"}]`, }