mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 21:53:52 +00:00
Merge pull request #45810 from superbrothers/expand-env-vals
Automatic merge from submit-queue (batch tested with PRs 45247, 45810, 45034, 45898, 45899) kubectl plugin: Expand environment variables in the command of plugin.yaml **What this PR does / why we need it**: This commit improves kubectl plugins to expand environment variables in the command of plugin.yaml. I'd like to use environment variables in the command of plugin.yaml as follows: ```yaml name: hello shortDesc: "The hello plugin" longDesc: > The hello plugin is a new plugin used by test-cmd to test multiple plugin locations. command: $HOME/path/to/plugins/hello.sh ``` **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
@@ -18,6 +18,7 @@ package plugins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -47,7 +48,7 @@ type ExecPluginRunner struct{}
|
|||||||
// Run takes a given plugin and runs it in a given context using os/exec, returning
|
// Run takes a given plugin and runs it in a given context using os/exec, returning
|
||||||
// any error found while running.
|
// any error found while running.
|
||||||
func (r *ExecPluginRunner) Run(plugin *Plugin, ctx RunningContext) error {
|
func (r *ExecPluginRunner) Run(plugin *Plugin, ctx RunningContext) error {
|
||||||
command := strings.Split(plugin.Command, " ")
|
command := strings.Split(os.ExpandEnv(plugin.Command), " ")
|
||||||
base := command[0]
|
base := command[0]
|
||||||
args := []string{}
|
args := []string{}
|
||||||
if len(command) > 1 {
|
if len(command) > 1 {
|
||||||
|
@@ -18,6 +18,7 @@ package plugins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,8 +39,16 @@ func TestExecRunner(t *testing.T) {
|
|||||||
command: "false",
|
command: "false",
|
||||||
expectedErr: "exit status 1",
|
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 {
|
for _, test := range tests {
|
||||||
outBuf := bytes.NewBuffer([]byte{})
|
outBuf := bytes.NewBuffer([]byte{})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user