From 3a7165f999dbaee8c31cfb1fcce8613ac9c47be5 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Tue, 11 Feb 2020 10:37:44 -0600 Subject: [PATCH] feat: add cmds for kubectl --- cmd/vela-kubernetes/command.go | 90 ++++++++++++++++++++++++ cmd/vela-kubernetes/command_test.go | 105 ++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) diff --git a/cmd/vela-kubernetes/command.go b/cmd/vela-kubernetes/command.go index 5f9bca9..4652030 100644 --- a/cmd/vela-kubernetes/command.go +++ b/cmd/vela-kubernetes/command.go @@ -30,3 +30,93 @@ 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") + + return exec.Command( + kubectlBin, + "rollout", + "status", + "--watch", + "true", + status, + ) +} + +// 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") + + return exec.Command( + kubectlBin, + "rollout", + "status", + "--watch", + "false", + status, + ) +} + +// 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") + + return exec.Command( + kubectlBin, + "version", + ) +} diff --git a/cmd/vela-kubernetes/command_test.go b/cmd/vela-kubernetes/command_test.go index 6530029..abbf456 100644 --- a/cmd/vela-kubernetes/command_test.go +++ b/cmd/vela-kubernetes/command_test.go @@ -6,6 +6,7 @@ package main import ( "os/exec" + "reflect" "testing" ) @@ -18,3 +19,107 @@ func TestKubernetes_execCmd(t *testing.T) { t.Errorf("execCmd returned err: %v", err) } } + +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) { + // 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) + } +} + +func TestKubernetes_versionCmd(t *testing.T) { + // setup types + want := exec.Command( + kubectlBin, + "version", + ) + + got := versionCmd() + + if !reflect.DeepEqual(got, want) { + t.Errorf("versionCmd is %v, want %v", got, want) + } +}