Skip to content

Commit

Permalink
feat: dry-run's client/server/none options with backwards compat (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
colindean authored Mar 31, 2023
1 parent e22275c commit 28bbcc3
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ The following parameters are used to configure the `apply` action:

| Name | Description | Required | Default | Environment Variables |
| --------- | ------------------------------------------------ | -------- | ------- | ------------------------------------------- |
| `dry_run` | enables pretending to perform the apply | `false` | `false` | `PARAMETER_DRY_RUN`<br>`KUBERNETES_DRY_RUN` |
| `dry_run` | enables pretending to perform the apply (`true`/`client`, `false`/`none`, `server`) | `false` | `false` | `PARAMETER_DRY_RUN`<br>`KUBERNETES_DRY_RUN` |
| `files` | list of Kubernetes files or directories to apply | `true` | `N/A` | `PARAMETER_FILES`<br>`KUBERNETES_FILES` |
| `output` | set the output for the apply | `false` | `N/A` | `PARAMETER_OUTPUT`<br>`KUBERNETES_OUTPUT` |

Expand Down
15 changes: 13 additions & 2 deletions cmd/vela-kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const applyAction = "apply"
// Apply represents the plugin configuration for Apply config information.
type Apply struct {
// enables pretending to apply the files
DryRun bool
DryRun string
// Kubernetes files or directories to apply
Files []string
// sets the output for the apply command
Expand Down Expand Up @@ -59,7 +59,18 @@ func (a *Apply) Command(c *Config, file string) *exec.Cmd {
flags = append(flags, "apply")

// add flag for dry run from provided apply dry run
flags = append(flags, fmt.Sprintf("--dry-run=%t", a.DryRun))
if len(a.DryRun) > 0 {
dryRunOpt := a.DryRun

switch a.DryRun {
case "true":
dryRunOpt = "client"
case "false":
dryRunOpt = "none"
}

flags = append(flags, fmt.Sprintf("--dry-run=%s", dryRunOpt))
}

// add flag for file from provided apply file
flags = append(flags, fmt.Sprintf("--filename=%s", file))
Expand Down
86 changes: 81 additions & 5 deletions cmd/vela-kubernetes/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestKubernetes_Apply_Command(t *testing.T) {
}

a := &Apply{
DryRun: false,
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
}
Expand All @@ -37,7 +37,83 @@ func TestKubernetes_Apply_Command(t *testing.T) {
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
fmt.Sprintf("--dry-run=%t", a.DryRun),
"--dry-run=none",
fmt.Sprintf("--filename=%s", file),
fmt.Sprintf("--output=%s", a.Output),
)

got := a.Command(c, file)

if !reflect.DeepEqual(got, want) {
t.Errorf("Command is %v, want %v", got, want)
}
}
}
func TestKubernetes_Apply_Command_WithDryRunTrue(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}

a := &Apply{
DryRun: "true",
Files: []string{"apply.yml"},
Output: "json",
}

// nolint: gosec // testing purposes
for _, file := range a.Files {
want := exec.Command(
_kubectl,
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
"--dry-run=client",
fmt.Sprintf("--filename=%s", file),
fmt.Sprintf("--output=%s", a.Output),
)

got := a.Command(c, file)

if !reflect.DeepEqual(got, want) {
t.Errorf("Command is %v, want %v", got, want)
}
}
}
func TestKubernetes_Apply_Command_WithDryRunAnythingNonBoolean(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
File: "file",
Namespace: "namespace",
Path: "~/.kube/config",
}

a := &Apply{
DryRun: "server",
Files: []string{"apply.yml"},
Output: "json",
}

// nolint: gosec // testing purposes
for _, file := range a.Files {
want := exec.Command(
_kubectl,
fmt.Sprintf("--kubeconfig=%s", c.Path),
fmt.Sprintf("--cluster=%s", c.Cluster),
fmt.Sprintf("--context=%s", c.Context),
fmt.Sprintf("--namespace=%s", c.Namespace),
"apply",
fmt.Sprintf("--dry-run=%s", a.DryRun),
fmt.Sprintf("--filename=%s", file),
fmt.Sprintf("--output=%s", a.Output),
)
Expand All @@ -61,7 +137,7 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) {
}

a := &Apply{
DryRun: false,
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
}
Expand All @@ -75,7 +151,7 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) {
func TestKubernetes_Apply_Validate(t *testing.T) {
// setup types
a := &Apply{
DryRun: false,
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
}
Expand All @@ -89,7 +165,7 @@ func TestKubernetes_Apply_Validate(t *testing.T) {
func TestKubernetes_Apply_Validate_NoFiles(t *testing.T) {
// setup types
a := &Apply{
DryRun: false,
DryRun: "false",
Output: "json",
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func run(c *cli.Context) error {
p := &Plugin{
// apply configuration
Apply: &Apply{
DryRun: c.Bool("dry_run"),
DryRun: c.String("dry_run"),
Files: c.StringSlice("files"),
Output: c.String("output"),
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/vela-kubernetes/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestKubernetes_Plugin_Exec_Apply_Error(t *testing.T) {
// setup types
p := &Plugin{
Apply: &Apply{
DryRun: false,
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
},
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestKubernetes_Plugin_Validate_Apply(t *testing.T) {
// setup types
p := &Plugin{
Apply: &Apply{
DryRun: false,
DryRun: "false",
Files: []string{"apply.yml"},
Output: "json",
},
Expand Down

0 comments on commit 28bbcc3

Please sign in to comment.