From 0720813c037970cc785802abe37c89803b83c52c Mon Sep 17 00:00:00 2001 From: York Chen Date: Mon, 2 Oct 2023 15:56:24 -0400 Subject: [PATCH] feat: add inheritParentEnvs and inheritEnvs --- .../crds/troubleshoot.sh_hostcollectors.yaml | 8 ++ .../crds/troubleshoot.sh_hostpreflights.yaml | 8 ++ .../crds/troubleshoot.sh_supportbundles.yaml | 8 ++ .../v1beta2/hostcollector_shared.go | 2 + pkg/collect/host_run.go | 75 ++++++++++++++----- .../supportbundle-troubleshoot-v1beta2.json | 13 +++- 6 files changed, 95 insertions(+), 19 deletions(-) diff --git a/config/crds/troubleshoot.sh_hostcollectors.yaml b/config/crds/troubleshoot.sh_hostcollectors.yaml index 59762738b..bc9d804eb 100644 --- a/config/crds/troubleshoot.sh_hostcollectors.yaml +++ b/config/crds/troubleshoot.sh_hostcollectors.yaml @@ -1374,6 +1374,12 @@ spec: type: array exclude: type: BoolString + inheritEnvs: + items: + type: string + type: array + inheritParentEnvs: + type: boolean input: additionalProperties: type: string @@ -1383,6 +1389,8 @@ spec: required: - args - command + - inheritEnvs + - inheritParentEnvs type: object subnetAvailable: properties: diff --git a/config/crds/troubleshoot.sh_hostpreflights.yaml b/config/crds/troubleshoot.sh_hostpreflights.yaml index 673f34d71..3231be6c0 100644 --- a/config/crds/troubleshoot.sh_hostpreflights.yaml +++ b/config/crds/troubleshoot.sh_hostpreflights.yaml @@ -1374,6 +1374,12 @@ spec: type: array exclude: type: BoolString + inheritEnvs: + items: + type: string + type: array + inheritParentEnvs: + type: boolean input: additionalProperties: type: string @@ -1383,6 +1389,8 @@ spec: required: - args - command + - inheritEnvs + - inheritParentEnvs type: object subnetAvailable: properties: diff --git a/config/crds/troubleshoot.sh_supportbundles.yaml b/config/crds/troubleshoot.sh_supportbundles.yaml index f01530adf..8cf90a692 100644 --- a/config/crds/troubleshoot.sh_supportbundles.yaml +++ b/config/crds/troubleshoot.sh_supportbundles.yaml @@ -11692,6 +11692,12 @@ spec: type: array exclude: type: BoolString + inheritEnvs: + items: + type: string + type: array + inheritParentEnvs: + type: boolean input: additionalProperties: type: string @@ -11701,6 +11707,8 @@ spec: required: - args - command + - inheritEnvs + - inheritParentEnvs type: object subnetAvailable: properties: diff --git a/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go b/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go index 25c261b3c..918b52d78 100644 --- a/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go @@ -185,6 +185,8 @@ type HostRun struct { OutputDir string `json:"outputDir,omitempty" yaml:"outputDir,omitempty"` Input map[string]string `json:"input,omitempty" yaml:"input,omitempty"` Env []string `json:"env,omitempty" yaml:"env,omitempty"` + InheritEnvs []string `json:"inheritEnvs" yaml:"inheritEnvs,omitempty"` + InheritParentEnvs bool `json:"inheritParentEnvs" yaml:"inheritParentEnvs,omitempty"` } type HostCollect struct { diff --git a/pkg/collect/host_run.go b/pkg/collect/host_run.go index 771334913..2c9106235 100644 --- a/pkg/collect/host_run.go +++ b/pkg/collect/host_run.go @@ -14,12 +14,12 @@ import ( ) type HostRunInfo struct { - Command string `json:"command"` - ExitCode string `json:"exitCode"` - Error string `json:"error"` - OutputDir string `json:"outputDir"` - Input string `json:"input"` - Env string `json:"env"` + Command string `json:"command"` + ExitCode string `json:"exitCode"` + Error string `json:"error"` + OutputDir string `json:"outputDir"` + Input string `json:"input"` + Env []string `json:"env"` } type CollectHostRun struct { @@ -50,15 +50,18 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] cmd := exec.Command(runHostCollector.Command, runHostCollector.Args...) - collectorRelativePath := filepath.Join("host-collectors/run-host", collectorName) - - if runHostCollector.Env != nil { - for i := range runHostCollector.Env { - cmd.Env = append(cmd.Env, - fmt.Sprintf("%s=%s", runHostCollector.Env[i], os.Getenv(runHostCollector.Env[i]))) - } + runInfo := &HostRunInfo{ + Command: cmd.String(), + ExitCode: "0", + } + err = c.processEnvVars(cmd, runInfo) + if err != nil { + return nil, errors.Wrap(err, "failed to parse env variable") } + + collectorRelativePath := filepath.Join("host-collectors/run-host", collectorName) + // if we choose to save result for the command run if runHostCollector.OutputDir != "" { cmdOutputTempDir, err = os.MkdirTemp("", runHostCollector.OutputDir) @@ -98,11 +101,6 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] cmd.Stdout = &stdout cmd.Stderr = &stderr - runInfo := HostRunInfo{ - Command: cmd.String(), - ExitCode: "0", - } - err = cmd.Run() if err != nil { if werr, ok := err.(*exec.ExitError); ok { @@ -132,3 +130,44 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][] return output, nil } + +func (c *CollectHostRun) processEnvVars(cmd *exec.Cmd, runInfo *HostRunInfo) error { + guaranteedEnvs := []string{"PATH", "KUBECONFIG"} + for _, key := range guaranteedEnvs { + guaranteedEnvVal, found := os.LookupEnv(key) + if found { + cmd.Env = append(cmd.Env, + fmt.Sprintf("%s=%s", key, guaranteedEnvVal)) + } + } + + runHostCollector := c.hostCollector + if runHostCollector.InheritParentEnvs == true { + cmd.Env = append(cmd.Env, os.Environ()...) + } else if runHostCollector.InheritEnvs != nil { + for _, key := range runHostCollector.InheritEnvs { + envVal, found := os.LookupEnv(key) + if !found { + return errors.New(fmt.Sprintf("inherit env variable is not found: %s", key)) + } + cmd.Env = append(cmd.Env, + fmt.Sprintf("%s=%s", key, envVal)) + } + } + + if runHostCollector.Env != nil { + for i := range runHostCollector.Env { + parts := strings.Split(runHostCollector.Env[i], "=") + if len(parts) == 2 && parts[0] != "" && parts[1] != "" { + cmd.Env = append(cmd.Env, + fmt.Sprintf("%s=%s", runHostCollector.Env[i], os.Getenv(runHostCollector.Env[i]))) + } else { + return errors.New(fmt.Sprintf("env variable entry is missing '=' : %s", runHostCollector.Env[i])) + } + } + } + + runInfo.Env = os.Environ() + + return nil +} diff --git a/schemas/supportbundle-troubleshoot-v1beta2.json b/schemas/supportbundle-troubleshoot-v1beta2.json index 8062beff9..7f46008ab 100644 --- a/schemas/supportbundle-troubleshoot-v1beta2.json +++ b/schemas/supportbundle-troubleshoot-v1beta2.json @@ -11452,7 +11452,9 @@ "type": "object", "required": [ "args", - "command" + "command", + "inheritEnvs", + "inheritParentEnvs" ], "properties": { "args": { @@ -11476,6 +11478,15 @@ "exclude": { "oneOf": [{"type": "string"},{"type": "boolean"}] }, + "inheritEnvs": { + "type": "array", + "items": { + "type": "string" + } + }, + "inheritParentEnvs": { + "type": "boolean" + }, "input": { "type": "object", "additionalProperties": {