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: enable providing custom output #17

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions cmd/vela-kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
type Apply struct {
// Kubernetes files or directories to apply
Files []string
// sets the output for the apply command
Output string
}

// Command formats and outputs the Apply command from
Expand Down Expand Up @@ -46,8 +48,11 @@ func (a *Apply) Command(c *Config, file string) *exec.Cmd {
flags = append(flags, fmt.Sprintf("--filename=%s", file))
}

// add flag for output
flags = append(flags, "--output=json")
// check if apply output is provided
if len(a.Output) > 0 {
// add flag for output from provided apply output
flags = append(flags, fmt.Sprintf("--output=%s", a.Output))
}

return exec.Command(kubectlBin, flags...)
}
Expand Down
15 changes: 10 additions & 5 deletions cmd/vela-kubernetes/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func TestKubernetes_Apply_Command(t *testing.T) {
}

a := &Apply{
Files: []string{"apply.yml"},
Files: []string{"apply.yml"},
Output: "json",
}

for _, file := range a.Files {
Expand All @@ -30,7 +31,7 @@ func TestKubernetes_Apply_Command(t *testing.T) {
fmt.Sprintf("--context=%s", c.Context),
"apply",
fmt.Sprintf("--filename=%s", file),
"--output=json",
fmt.Sprintf("--output=%s", a.Output),
)

got := a.Command(c, file)
Expand All @@ -50,7 +51,8 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) {
}

a := &Apply{
Files: []string{"apply.yml"},
Files: []string{"apply.yml"},
Output: "json",
}

err := a.Exec(c)
Expand All @@ -62,7 +64,8 @@ func TestKubernetes_Apply_Exec_Error(t *testing.T) {
func TestKubernetes_Apply_Validate(t *testing.T) {
// setup types
a := &Apply{
Files: []string{"apply.yml"},
Files: []string{"apply.yml"},
Output: "json",
}

err := a.Validate()
Expand All @@ -73,7 +76,9 @@ func TestKubernetes_Apply_Validate(t *testing.T) {

func TestKubernetes_Apply_Validate_NoFiles(t *testing.T) {
// setup types
a := &Apply{}
a := &Apply{
Output: "json",
}

err := a.Validate()
if err == nil {
Expand Down
16 changes: 15 additions & 1 deletion cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func main() {
Name: "apply.files",
Usage: "kubernetes files or directories to apply",
},
cli.StringFlag{
EnvVar: "PARAMETER_OUTPUT,APPLY_OUTPUT",
Name: "apply.output",
Usage: "set output for apply - options: (json|yaml|wide)",
Value: "json",
},

// Config Flags

Expand All @@ -79,6 +85,12 @@ func main() {
Name: "patch.containers",
Usage: "containers from files to patch",
},
cli.StringFlag{
EnvVar: "PARAMETER_OUTPUT,PATCH_OUTPUT",
Name: "patch.output",
Usage: "set output for patch - options: (json|yaml|wide)",
Value: "json",
},

// Status Flags

Expand Down Expand Up @@ -133,7 +145,8 @@ func run(c *cli.Context) error {
p := &Plugin{
// apply configuration
Apply: &Apply{
Files: c.StringSlice("apply.files"),
Files: c.StringSlice("apply.files"),
Output: c.String("apply.output"),
},
// config configuration
Config: &Config{
Expand All @@ -143,6 +156,7 @@ func run(c *cli.Context) error {
},
// patch configuration
Patch: &Patch{
Output: c.String("patch.output"),
RawContainers: c.String("patch.containers"),
},
// status configuration
Expand Down
13 changes: 9 additions & 4 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ func (c *Container) Validate() error {

// Patch represents the plugin configuration for Patch config information.
type Patch struct {
// raw input of containers provided for plugin
RawContainers string
// container images from files to patch
Containers []*Container
// sets the output for the patch command
Output string
// raw input of containers provided for plugin
RawContainers string
}

// Command formats and outputs the Patch command from
Expand All @@ -126,8 +128,11 @@ func (p *Patch) Command(c *Config, container *Container) *exec.Cmd {
// add flag for patch kubectl command
flags = append(flags, "patch")

// add flag for output
flags = append(flags, "--output=json")
// check if patch output is provided
if len(p.Output) > 0 {
// add flag for output from provided patch output
flags = append(flags, fmt.Sprintf("--output=%s", p.Output))
}

return exec.Command(kubectlBin, flags...)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/vela-kubernetes/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestKubernetes_Patch_Command(t *testing.T) {
Image: "alpine",
},
},
Output: "json",
RawContainers: `[{"name": "container", "image": "alpine"}]`,
}

Expand All @@ -35,7 +36,7 @@ func TestKubernetes_Patch_Command(t *testing.T) {
fmt.Sprintf("--namespace=%s", c.Namespace),
fmt.Sprintf("--context=%s", c.Context),
"patch",
"--output=json",
fmt.Sprintf("--output=%s", p.Output),
)

got := p.Command(c, container)
Expand All @@ -61,6 +62,7 @@ func TestKubernetes_Patch_Exec_Error(t *testing.T) {
Image: "alpine",
},
},
Output: "json",
RawContainers: `[{"name": "container", "image": "alpine"}]`,
}

Expand All @@ -73,6 +75,7 @@ func TestKubernetes_Patch_Exec_Error(t *testing.T) {
func TestKubernetes_Patch_Validate(t *testing.T) {
// setup types
p := &Patch{
Output: "json",
RawContainers: `[{"name": "container", "image": "alpine"}]`,
}

Expand All @@ -85,6 +88,7 @@ func TestKubernetes_Patch_Validate(t *testing.T) {
func TestKubernetes_Patch_Validate_Invalid(t *testing.T) {
// setup types
p := &Patch{
Output: "json",
RawContainers: "!@#$%^&*()",
}

Expand All @@ -107,6 +111,7 @@ func TestKubernetes_Patch_Validate_NoContainers(t *testing.T) {
func TestKubernetes_Patch_Validate_NoContainerName(t *testing.T) {
// setup types
p := &Patch{
Output: "json",
RawContainers: `[{"image": "alpine"}]`,
}

Expand All @@ -119,6 +124,7 @@ func TestKubernetes_Patch_Validate_NoContainerName(t *testing.T) {
func TestKubernetes_Patch_Validate_NoContainerImage(t *testing.T) {
// setup types
p := &Patch{
Output: "json",
RawContainers: `[{"name": "container"}]`,
}

Expand Down