From f80ff0510a7b591c2e476808e31fdd4971778a80 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Wed, 12 Feb 2020 19:45:05 -0600 Subject: [PATCH 1/5] fix: typo in actions pipeline --- .github/workflows/prerelease.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/prerelease.yml b/.github/workflows/prerelease.yml index 36f567f..15c0252 100644 --- a/.github/workflows/prerelease.yml +++ b/.github/workflows/prerelease.yml @@ -25,7 +25,7 @@ jobs: go build -a \ -ldflags '-s -w -extldflags "-static"' \ -o release/vela-kubernetes \ - github.com/go-vela/vela-kubernetes/cmd/vela-artifactory + github.com/go-vela/vela-kubernetes/cmd/vela-kubernetes - name: publish uses: elgohr/Publish-Docker-Github-Action@master From d7a5a12c028fa1f3f086ddb7dca02f737531430c Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Wed, 12 Feb 2020 19:46:01 -0600 Subject: [PATCH 2/5] feat(apply): add exec method --- cmd/vela-kubernetes/apply.go | 24 ++++++++++++++++++++++-- cmd/vela-kubernetes/apply_test.go | 18 ++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cmd/vela-kubernetes/apply.go b/cmd/vela-kubernetes/apply.go index 265387e..8b3a002 100644 --- a/cmd/vela-kubernetes/apply.go +++ b/cmd/vela-kubernetes/apply.go @@ -17,8 +17,8 @@ type Apply struct { Files []string } -// Command formats and outputs the Apply command from the -// provided configuration to apply to resources. +// Command formats and outputs the Apply command from +// the provided configuration to apply to resources. func (a *Apply) Command(c *Config, file string) *exec.Cmd { logrus.Trace("creating kubectl apply command from plugin configuration") @@ -52,6 +52,26 @@ func (a *Apply) Command(c *Config, file string) *exec.Cmd { return exec.Command(kubectlBin, flags...) } +// Exec formats and runs the commands for applying +// the provided configuration to the resources. +func (a *Apply) Exec(c *Config) error { + logrus.Debug("running apply with provided configuration") + + // iterate through all files to apply + for _, file := range a.Files { + // create the apply command for the file + cmd := a.Command(c, file) + + // run the apply command for the file + err := execCmd(cmd) + if err != nil { + return err + } + } + + return nil +} + // Validate verifies the Apply is properly configured. func (a *Apply) Validate() error { logrus.Trace("validating apply configuration") diff --git a/cmd/vela-kubernetes/apply_test.go b/cmd/vela-kubernetes/apply_test.go index 2a3061f..bb41c5a 100644 --- a/cmd/vela-kubernetes/apply_test.go +++ b/cmd/vela-kubernetes/apply_test.go @@ -41,6 +41,24 @@ func TestKubernetes_Apply_Command(t *testing.T) { } } +func TestKubernetes_Apply_Exec_Error(t *testing.T) { + // setup types + c := &Config{ + File: "file", + Context: "context", + Namespace: "namespace", + } + + a := &Apply{ + Files: []string{"apply.yml"}, + } + + err := a.Exec(c) + if err == nil { + t.Errorf("Exec should have returned err") + } +} + func TestKubernetes_Apply_Validate(t *testing.T) { // setup types a := &Apply{ From 6591d623035d30f9566f61fa98e2f4b4194735a8 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Wed, 12 Feb 2020 19:46:23 -0600 Subject: [PATCH 3/5] feat(patch): add exec method --- cmd/vela-kubernetes/patch.go | 26 +++++++++++++++--- cmd/vela-kubernetes/patch_test.go | 44 ++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/cmd/vela-kubernetes/patch.go b/cmd/vela-kubernetes/patch.go index cc31c9b..36ae4ec 100644 --- a/cmd/vela-kubernetes/patch.go +++ b/cmd/vela-kubernetes/patch.go @@ -17,9 +17,9 @@ type Patch struct { Images []string } -// Command formats and outputs the Patch command from the -// provided configuration to patch resources. -func (p *Patch) Command(c *Config) *exec.Cmd { +// Command formats and outputs the Patch command from +// the provided configuration to patch resources. +func (p *Patch) Command(c *Config, image string) *exec.Cmd { logrus.Trace("creating kubectl patch command from plugin configuration") // variable to store flags for command @@ -46,6 +46,26 @@ func (p *Patch) Command(c *Config) *exec.Cmd { return exec.Command(kubectlBin, flags...) } +// Exec formats and runs the commands for patching +// the provided configuration to the resources. +func (p *Patch) Exec(c *Config) error { + logrus.Debug("running patch with provided configuration") + + // iterate through all images to patch + for _, image := range p.Images { + // create the patch command for the image + cmd := p.Command(c, image) + + // run the patch command for the image + err := execCmd(cmd) + if err != nil { + return err + } + } + + return nil +} + // Validate verifies the Patch is properly configured. func (p *Patch) Validate() error { logrus.Trace("validating patch configuration") diff --git a/cmd/vela-kubernetes/patch_test.go b/cmd/vela-kubernetes/patch_test.go index 236e9aa..65147b0 100644 --- a/cmd/vela-kubernetes/patch_test.go +++ b/cmd/vela-kubernetes/patch_test.go @@ -23,18 +23,38 @@ func TestKubernetes_Patch_Command(t *testing.T) { Images: []string{"images"}, } - want := exec.Command( - kubectlBin, - fmt.Sprintf("--namespace=%s", c.Namespace), - fmt.Sprintf("--context=%s", c.Context), - "patch", - "--output=json", - ) - - got := p.Command(c) - - if !reflect.DeepEqual(got, want) { - t.Errorf("Command is %v, want %v", got, want) + for _, image := range p.Images { + want := exec.Command( + kubectlBin, + fmt.Sprintf("--namespace=%s", c.Namespace), + fmt.Sprintf("--context=%s", c.Context), + "patch", + "--output=json", + ) + + got := p.Command(c, image) + + if !reflect.DeepEqual(got, want) { + t.Errorf("Command is %v, want %v", got, want) + } + } +} + +func TestKubernetes_Patch_Exec_Error(t *testing.T) { + // setup types + c := &Config{ + File: "file", + Context: "context", + Namespace: "namespace", + } + + p := &Patch{ + Images: []string{"images"}, + } + + err := p.Exec(c) + if err == nil { + t.Errorf("Exec should have returned err") } } From fb8971c88e96b8c098a48780a8f56d1bea85a219 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Wed, 12 Feb 2020 19:46:42 -0600 Subject: [PATCH 4/5] feat(status): add exec method --- cmd/vela-kubernetes/status.go | 20 ++++++++++++++++++++ cmd/vela-kubernetes/status_test.go | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/cmd/vela-kubernetes/status.go b/cmd/vela-kubernetes/status.go index 6052abc..16e6085 100644 --- a/cmd/vela-kubernetes/status.go +++ b/cmd/vela-kubernetes/status.go @@ -61,6 +61,26 @@ func (s *Status) Command(c *Config, resource string) *exec.Cmd { return exec.Command(kubectlBin, flags...) } +// Exec formats and runs the commands for watching the +// status on resources from the provided configuration. +func (s *Status) Exec(c *Config) error { + logrus.Debug("running status with provided configuration") + + // iterate through all resources to watch status + for _, resource := range s.Resources { + // create the status command for the resource + cmd := s.Command(c, resource) + + // run the status command for the resource + err := execCmd(cmd) + if err != nil { + return err + } + } + + return nil +} + // Validate verifies the Status is properly configured. func (s *Status) Validate() error { logrus.Trace("validating status configuration") diff --git a/cmd/vela-kubernetes/status_test.go b/cmd/vela-kubernetes/status_test.go index 8015503..fc5231b 100644 --- a/cmd/vela-kubernetes/status_test.go +++ b/cmd/vela-kubernetes/status_test.go @@ -45,6 +45,25 @@ func TestKubernetes_Status_Command(t *testing.T) { } } +func TestKubernetes_Status_Exec_Error(t *testing.T) { + // setup types + c := &Config{ + File: "file", + Context: "context", + Namespace: "namespace", + } + + s := &Status{ + Resources: []string{"resources"}, + Timeout: 5 * time.Minute, + } + + err := s.Exec(c) + if err == nil { + t.Errorf("Exec should have returned err") + } +} + func TestKubernetes_Status_Validate(t *testing.T) { // setup types s := &Status{ From 023c7c4284668e8b3774d8c09ab363f60db7ef60 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Thu, 13 Feb 2020 09:09:56 -0600 Subject: [PATCH 5/5] chore: fix linter error --- cmd/vela-kubernetes/patch.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/vela-kubernetes/patch.go b/cmd/vela-kubernetes/patch.go index 36ae4ec..43fe377 100644 --- a/cmd/vela-kubernetes/patch.go +++ b/cmd/vela-kubernetes/patch.go @@ -20,7 +20,7 @@ type Patch struct { // Command formats and outputs the Patch command from // the provided configuration to patch resources. func (p *Patch) Command(c *Config, image string) *exec.Cmd { - logrus.Trace("creating kubectl patch command from plugin configuration") + logrus.Tracef("creating kubectl patch command for %s from plugin configuration", image) // variable to store flags for command var flags []string @@ -37,7 +37,7 @@ func (p *Patch) Command(c *Config, image string) *exec.Cmd { flags = append(flags, fmt.Sprintf("--context=%s", c.Context)) } - // add flag for apply kubectl command + // add flag for patch kubectl command flags = append(flags, "patch") // add flag for output