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: dry-run's client/server/none options with backwards compat #139

Merged
merged 5 commits into from
Mar 31, 2023
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: 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