diff --git a/cmd/vela-kubernetes/apply.go b/cmd/vela-kubernetes/apply.go index 8b3a002..34cbe8e 100644 --- a/cmd/vela-kubernetes/apply.go +++ b/cmd/vela-kubernetes/apply.go @@ -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 @@ -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...) } diff --git a/cmd/vela-kubernetes/apply_test.go b/cmd/vela-kubernetes/apply_test.go index bb41c5a..09fe767 100644 --- a/cmd/vela-kubernetes/apply_test.go +++ b/cmd/vela-kubernetes/apply_test.go @@ -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 { @@ -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) @@ -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) @@ -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() @@ -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 { diff --git a/cmd/vela-kubernetes/main.go b/cmd/vela-kubernetes/main.go index 1150214..2c67d0e 100644 --- a/cmd/vela-kubernetes/main.go +++ b/cmd/vela-kubernetes/main.go @@ -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 @@ -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 @@ -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{ @@ -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 diff --git a/cmd/vela-kubernetes/patch.go b/cmd/vela-kubernetes/patch.go index 9f7a13d..4d3abe0 100644 --- a/cmd/vela-kubernetes/patch.go +++ b/cmd/vela-kubernetes/patch.go @@ -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 @@ -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...) } diff --git a/cmd/vela-kubernetes/patch_test.go b/cmd/vela-kubernetes/patch_test.go index a6439c5..b574707 100644 --- a/cmd/vela-kubernetes/patch_test.go +++ b/cmd/vela-kubernetes/patch_test.go @@ -26,6 +26,7 @@ func TestKubernetes_Patch_Command(t *testing.T) { Image: "alpine", }, }, + Output: "json", RawContainers: `[{"name": "container", "image": "alpine"}]`, } @@ -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) @@ -61,6 +62,7 @@ func TestKubernetes_Patch_Exec_Error(t *testing.T) { Image: "alpine", }, }, + Output: "json", RawContainers: `[{"name": "container", "image": "alpine"}]`, } @@ -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"}]`, } @@ -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: "!@#$%^&*()", } @@ -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"}]`, } @@ -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"}]`, }