feat: add exec-plugin argument and environment support (#5316)

* feat: add exec-plugin argument and environment support

Previously, the documentation lead to think that this is working, but
it's not been implemented.

This PR is fixing this

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

* chore: disable linting for env var split

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>

---------

Signed-off-by: Matthias Riegler <matthias.riegler@ankorstore.com>
This commit is contained in:
Matthias Riegler
2025-02-16 22:28:21 +01:00
committed by GitHub
parent 3be1af6798
commit cc7a71c288
5 changed files with 92 additions and 5 deletions

View File

@@ -281,8 +281,8 @@ func (r RunFns) getFunctionsFromFunctions() ([]kio.Filter, error) {
return r.getFunctionFilters(true, r.Functions...)
}
// mergeContainerEnv will merge the envs specified by command line (imperative) and config
// file (declarative). If they have same key, the imperative value will be respected.
// mergeContainerEnv is container-specific and will merge the envs specified by command line (imperative)
// and config file (declarative). If they have same key, the imperative value will be respected.
func (r RunFns) mergeContainerEnv(envs []string) []string {
imperative := runtimeutil.NewContainerEnvFromStringSlice(r.Env)
declarative := runtimeutil.NewContainerEnvFromStringSlice(envs)
@@ -297,6 +297,28 @@ func (r RunFns) mergeContainerEnv(envs []string) []string {
return declarative.Raw()
}
// mergeExecEnv will merge the envs specified by command line (imperative) and config
// file (declarative). If they have same key, the imperative value will be respected.
func (r RunFns) mergeExecEnv(envs []string) []string {
envMap := map[string]string{}
for _, env := range append(envs, r.Env...) {
res := strings.Split(env, "=")
//nolint:gomnd
if len(res) == 2 {
envMap[res[0]] = res[1]
}
}
mergedEnv := []string{}
for key, value := range envMap {
mergedEnv = append(mergedEnv, fmt.Sprintf("%s=%s", key, value))
}
// Sort the envs to make the output deterministic
sort.Strings(mergedEnv)
return mergedEnv
}
func (r RunFns) getFunctionFilters(global bool, fns ...*yaml.RNode) (
[]kio.Filter, error) {
var fltrs []kio.Filter
@@ -494,6 +516,8 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode, currentUser
if r.EnableExec && spec.Exec.Path != "" {
ef := &exec.Filter{
Path: spec.Exec.Path,
Args: spec.Exec.Args,
Env: r.mergeExecEnv(spec.Exec.Env),
WorkingDir: r.WorkingDir,
}

View File

@@ -1282,3 +1282,55 @@ func TestRunFns_mergeContainerEnv(t *testing.T) {
})
}
}
func TestRunFns_mergeExecEnv(t *testing.T) {
testcases := []struct {
name string
instance RunFns
inputEnvs []string
expect []string
}{
{
name: "all empty",
instance: RunFns{},
expect: []string{},
},
{
name: "empty command line envs",
instance: RunFns{},
inputEnvs: []string{"foo=bar"},
expect: []string{"foo=bar"},
},
{
name: "empty declarative envs",
instance: RunFns{
Env: []string{"foo=bar"},
},
expect: []string{"foo=bar"},
},
{
name: "same key",
instance: RunFns{
Env: []string{"foo=bar"},
},
inputEnvs: []string{"foo=bar1"},
expect: []string{"foo=bar"},
},
{
name: "same exported key",
instance: RunFns{
Env: []string{"foo=bar"},
},
inputEnvs: []string{"foo1=bar1"},
expect: []string{"foo1=bar1", "foo=bar"},
},
}
for i := range testcases {
tc := testcases[i]
t.Run(tc.name, func(t *testing.T) {
envs := tc.instance.mergeExecEnv(tc.inputEnvs)
assert.Equal(t, tc.expect, envs)
})
}
}