From b579cd595c0d43fdc1f02ce6ff5732261b73b700 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Thu, 13 Feb 2020 08:27:00 -0600 Subject: [PATCH] chore: remove dead code (#14) * refactor: config logic for plugin * improvement: check for file before writing * chore: remove dead code * test(config): clean up typos --- cmd/vela-kubernetes/command.go | 103 ++++++---------------------- cmd/vela-kubernetes/command_test.go | 99 +++----------------------- cmd/vela-kubernetes/config_test.go | 16 ++--- 3 files changed, 37 insertions(+), 181 deletions(-) diff --git a/cmd/vela-kubernetes/command.go b/cmd/vela-kubernetes/command.go index 4652030..737673d 100644 --- a/cmd/vela-kubernetes/command.go +++ b/cmd/vela-kubernetes/command.go @@ -31,92 +31,29 @@ func execCmd(e *exec.Cmd) error { return e.Run() } -// applyCmd is a helper function to apply -// the provided configuration for a resource. -func applyCmd(file string) *exec.Cmd { - logrus.Trace("returning applyCmd") - - return exec.Command( - kubectlBin, - "apply", - "--output", - "json", - "--filename", - file, - ) -} - -// patchCmd is a helper function to update -// the fields of a resource using the -// Kubernetes merging strategy. -func patchCmd(file string) *exec.Cmd { - logrus.Trace("returning patchCmd") - - return exec.Command( - kubectlBin, - "patch", - "--output", - "json", - "--filename", - file, - ) -} - -// replaceCmd is a helper function to replace -// the provided configuration for a resource. -func replaceCmd(file string) *exec.Cmd { - logrus.Trace("returning replaceCmd") - - return exec.Command( - kubectlBin, - "replace", - "--output", - "json", - "--filename", - file, - ) -} - -// statusWatchCmd is a helper function to inspect -// the current status of the latest rollout and -// wait for that rollout to finish. -func statusWatchCmd(status string) *exec.Cmd { - logrus.Trace("returning statusWatchCmd") +// versionCmd is a helper function to output +// the client and server version information +// for the configured context. +func versionCmd(c *Config) *exec.Cmd { + logrus.Trace("creating kubectl version command from plugin configuration") - return exec.Command( - kubectlBin, - "rollout", - "status", - "--watch", - "true", - status, - ) -} + // variable to store flags for command + var flags []string -// statusNoWatchCmd is a helper function to inspect -// the current status of the latest rollout without -// waiting for that rollout to finish. -func statusNoWatchCmd(status string) *exec.Cmd { - logrus.Trace("returning statusNoWatchCmd") + // check if config namespace is provided + if len(c.Namespace) > 0 { + // add flag for namespace from provided config namespace + flags = append(flags, fmt.Sprintf("--namespace=%s", c.Namespace)) + } - return exec.Command( - kubectlBin, - "rollout", - "status", - "--watch", - "false", - status, - ) -} + // check if config context is provided + if len(c.Context) > 0 { + // add flag for context from provided config context + flags = append(flags, fmt.Sprintf("--context=%s", c.Context)) + } -// versionCmd is a helper function to output -// the client and server version information -// for the configured context. -func versionCmd() *exec.Cmd { - logrus.Trace("returning versionCmd") + // add flag for version kubectl command + flags = append(flags, "version") - return exec.Command( - kubectlBin, - "version", - ) + return exec.Command(kubectlBin, flags...) } diff --git a/cmd/vela-kubernetes/command_test.go b/cmd/vela-kubernetes/command_test.go index abbf456..89001f4 100644 --- a/cmd/vela-kubernetes/command_test.go +++ b/cmd/vela-kubernetes/command_test.go @@ -5,6 +5,7 @@ package main import ( + "fmt" "os/exec" "reflect" "testing" @@ -20,104 +21,22 @@ func TestKubernetes_execCmd(t *testing.T) { } } -func TestKubernetes_applyCmd(t *testing.T) { - // setup types - want := exec.Command( - kubectlBin, - "apply", - "--output", - "json", - "--filename", - "apply.yml", - ) - - got := applyCmd("apply.yml") - - if !reflect.DeepEqual(got, want) { - t.Errorf("applyCmd is %v, want %v", got, want) - } -} - -func TestKubernetes_patchCmd(t *testing.T) { - // setup types - want := exec.Command( - kubectlBin, - "patch", - "--output", - "json", - "--filename", - "patch.yml", - ) - - got := patchCmd("patch.yml") - - if !reflect.DeepEqual(got, want) { - t.Errorf("patchCmd is %v, want %v", got, want) - } -} - -func TestKubernetes_replaceCmd(t *testing.T) { - // setup types - want := exec.Command( - kubectlBin, - "replace", - "--output", - "json", - "--filename", - "replace.yml", - ) - - got := replaceCmd("replace.yml") - - if !reflect.DeepEqual(got, want) { - t.Errorf("replaceCmd is %v, want %v", got, want) - } -} - -func TestKubernetes_statusWatchCmd(t *testing.T) { - // setup types - want := exec.Command( - kubectlBin, - "rollout", - "status", - "--watch", - "true", - "myStatus", - ) - - got := statusWatchCmd("myStatus") - - if !reflect.DeepEqual(got, want) { - t.Errorf("statusWatchCmd is %v, want %v", got, want) - } -} - -func TestKubernetes_statusNoWatchCmd(t *testing.T) { +func TestKubernetes_versionCmd(t *testing.T) { // setup types - want := exec.Command( - kubectlBin, - "rollout", - "status", - "--watch", - "false", - "myStatus", - ) - - got := statusNoWatchCmd("myStatus") - - if !reflect.DeepEqual(got, want) { - t.Errorf("statusNoWatchCmd is %v, want %v", got, want) + c := &Config{ + File: "file", + Context: "context", + Namespace: "namespace", } -} -func TestKubernetes_versionCmd(t *testing.T) { - // setup types want := exec.Command( kubectlBin, + fmt.Sprintf("--namespace=%s", c.Namespace), + fmt.Sprintf("--context=%s", c.Context), "version", ) - got := versionCmd() + got := versionCmd(c) if !reflect.DeepEqual(got, want) { t.Errorf("versionCmd is %v, want %v", got, want) diff --git a/cmd/vela-kubernetes/config_test.go b/cmd/vela-kubernetes/config_test.go index 8de595c..405898e 100644 --- a/cmd/vela-kubernetes/config_test.go +++ b/cmd/vela-kubernetes/config_test.go @@ -12,7 +12,7 @@ import ( func TestKubernetes_Config_Validate(t *testing.T) { // setup types - c := Config{ + c := &Config{ File: "file", Context: "context", Namespace: "namespace", @@ -26,7 +26,7 @@ func TestKubernetes_Config_Validate(t *testing.T) { func TestKubernetes_Config_Validate_NoFile(t *testing.T) { // setup types - c := Config{ + c := &Config{ Context: "context", Namespace: "namespace", } @@ -39,7 +39,7 @@ func TestKubernetes_Config_Validate_NoFile(t *testing.T) { func TestKubernetes_Config_Validate_NoContext(t *testing.T) { // setup types - c := Config{ + c := &Config{ File: "file", Namespace: "namespace", } @@ -52,7 +52,7 @@ func TestKubernetes_Config_Validate_NoContext(t *testing.T) { func TestKubernetes_Config_Validate_NoNamespace(t *testing.T) { // setup types - c := Config{ + c := &Config{ File: "file", Context: "context", } @@ -63,12 +63,12 @@ func TestKubernetes_Config_Validate_NoNamespace(t *testing.T) { } } -func TestKubernetes_Kubernetes_Write(t *testing.T) { +func TestKubernetes_Config_Write(t *testing.T) { // setup filesystem appFS = afero.NewMemMapFs() // setup types - c := Config{ + c := &Config{ File: "file", Context: "context", Namespace: "namespace", @@ -80,12 +80,12 @@ func TestKubernetes_Kubernetes_Write(t *testing.T) { } } -func TestKubernetes_Kubernetes_Write_NoFile(t *testing.T) { +func TestKubernetes_Config_Write_NoFile(t *testing.T) { // setup filesystem appFS = afero.NewMemMapFs() // setup types - c := Config{ + c := &Config{ Context: "context", Namespace: "namespace", }