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: add exec methods for plugin #15

Merged
merged 6 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 .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
26 changes: 23 additions & 3 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(*Patch).Command - image is unused (from unparam)

logrus.Trace("creating kubectl patch command from plugin configuration")

// variable to store flags for command
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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

G204: Subprocess launched with function call as argument or cmd arguments (from gosec)

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