From 9a36d5567ae51f82f3b4b9562c02c80099e1c077 Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Mon, 15 May 2017 15:31:13 +0900 Subject: [PATCH] kubectl plugin: Expand environment variables in the command of plugin.yaml This commit improves kubectl plugins to expand environment variables in the command of plugin.yaml. --- pkg/kubectl/plugins/runner.go | 3 ++- pkg/kubectl/plugins/runner_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/plugins/runner.go b/pkg/kubectl/plugins/runner.go index c1c4233a0b8..dd60f84db41 100644 --- a/pkg/kubectl/plugins/runner.go +++ b/pkg/kubectl/plugins/runner.go @@ -18,6 +18,7 @@ package plugins import ( "io" + "os" "os/exec" "strings" @@ -47,7 +48,7 @@ type ExecPluginRunner struct{} // Run takes a given plugin and runs it in a given context using os/exec, returning // any error found while running. func (r *ExecPluginRunner) Run(plugin *Plugin, ctx RunningContext) error { - command := strings.Split(plugin.Command, " ") + command := strings.Split(os.ExpandEnv(plugin.Command), " ") base := command[0] args := []string{} if len(command) > 1 { diff --git a/pkg/kubectl/plugins/runner_test.go b/pkg/kubectl/plugins/runner_test.go index 9bf0bf4a097..1304b71cec2 100644 --- a/pkg/kubectl/plugins/runner_test.go +++ b/pkg/kubectl/plugins/runner_test.go @@ -18,6 +18,7 @@ package plugins import ( "bytes" + "os" "testing" ) @@ -38,8 +39,16 @@ func TestExecRunner(t *testing.T) { command: "false", expectedErr: "exit status 1", }, + { + name: "env", + command: "echo $KUBECTL_PLUGINS_TEST", + expectedMsg: "ok\n", + }, } + os.Setenv("KUBECTL_PLUGINS_TEST", "ok") + defer os.Unsetenv("KUBECTL_PLUGINS_TEST") + for _, test := range tests { outBuf := bytes.NewBuffer([]byte{})