Skip to content

Commit

Permalink
feat: add exec methods for plugin (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockopp authored Feb 13, 2020
1 parent b579cd5 commit bcca6f5
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions cmd/vela-kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions cmd/vela-kubernetes/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
30 changes: 25 additions & 5 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ 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 {
logrus.Trace("creating kubectl patch command from plugin configuration")
// 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.Tracef("creating kubectl patch command for %s from plugin configuration", image)

// variable to store flags for command
var flags []string
Expand All @@ -37,7 +37,7 @@ func (p *Patch) Command(c *Config) *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
Expand All @@ -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")
Expand Down
44 changes: 32 additions & 12 deletions cmd/vela-kubernetes/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/vela-kubernetes/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 19 additions & 0 deletions cmd/vela-kubernetes/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit bcca6f5

Please sign in to comment.