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: enable setting dry_run for plugin #24

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .vela/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# - .cluster: (default: "")
# - .context: (default: "")
# - .namespace: (default: "")
# - .dry_run: (default: false)
# - .files: (default: "[]")
# - .containers: (default: "{}")
# - .output: (default: "json")
Expand All @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
5 changes: 5 additions & 0 deletions cmd/vela-kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cmd/vela-kubernetes/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestKubernetes_Apply_Command(t *testing.T) {
}

a := &Apply{
DryRun: false,
Files: []string{"apply.yml"},
Output: "json",
}
Expand All @@ -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),
)
Expand All @@ -54,6 +56,7 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) {
}

a := &Apply{
DryRun: false,
Files: []string{"apply.yml"},
Output: "json",
}
Expand All @@ -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",
}
Expand All @@ -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",
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"),
},
Expand All @@ -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"),
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion cmd/vela-kubernetes/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestKubernetes_Patch_Command(t *testing.T) {
Image: "alpine",
},
},
DryRun: false,
Output: "json",
RawContainers: `[{"name": "container", "image": "alpine"}]`,
}
Expand All @@ -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),
)

Expand Down Expand Up @@ -65,6 +67,7 @@ func TestKubernetes_Patch_Exec_Error(t *testing.T) {
Image: "alpine",
},
},
DryRun: false,
Output: "json",
RawContainers: `[{"name": "container", "image": "alpine"}]`,
}
Expand All @@ -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"}]`,
}
Expand All @@ -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: "!@#$%^&*()",
}
Expand All @@ -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 {
Expand All @@ -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"}]`,
}
Expand Down