Skip to content

Commit

Permalink
feat: add inheritParentEnvs and inheritEnvs
Browse files Browse the repository at this point in the history
  • Loading branch information
York Chen committed Oct 2, 2023
1 parent 4f5c7ca commit 0720813
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 19 deletions.
8 changes: 8 additions & 0 deletions config/crds/troubleshoot.sh_hostcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,12 @@ spec:
type: array
exclude:
type: BoolString
inheritEnvs:
items:
type: string
type: array
inheritParentEnvs:
type: boolean
input:
additionalProperties:
type: string
Expand All @@ -1383,6 +1389,8 @@ spec:
required:
- args
- command
- inheritEnvs
- inheritParentEnvs
type: object
subnetAvailable:
properties:
Expand Down
8 changes: 8 additions & 0 deletions config/crds/troubleshoot.sh_hostpreflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,12 @@ spec:
type: array
exclude:
type: BoolString
inheritEnvs:
items:
type: string
type: array
inheritParentEnvs:
type: boolean
input:
additionalProperties:
type: string
Expand All @@ -1383,6 +1389,8 @@ spec:
required:
- args
- command
- inheritEnvs
- inheritParentEnvs
type: object
subnetAvailable:
properties:
Expand Down
8 changes: 8 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11692,6 +11692,12 @@ spec:
type: array
exclude:
type: BoolString
inheritEnvs:
items:
type: string
type: array
inheritParentEnvs:
type: boolean
input:
additionalProperties:
type: string
Expand All @@ -11701,6 +11707,8 @@ spec:
required:
- args
- command
- inheritEnvs
- inheritParentEnvs
type: object
subnetAvailable:
properties:
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
75 changes: 57 additions & 18 deletions pkg/collect/host_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
13 changes: 12 additions & 1 deletion schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -11452,7 +11452,9 @@
"type": "object",
"required": [
"args",
"command"
"command",
"inheritEnvs",
"inheritParentEnvs"
],
"properties": {
"args": {
Expand All @@ -11476,6 +11478,15 @@
"exclude": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
},
"inheritEnvs": {
"type": "array",
"items": {
"type": "string"
}
},
"inheritParentEnvs": {
"type": "boolean"
},
"input": {
"type": "object",
"additionalProperties": {
Expand Down

0 comments on commit 0720813

Please sign in to comment.