Skip to content

Commit

Permalink
feat: add env, refacotr input and output
Browse files Browse the repository at this point in the history
added .env to allow user specify which environment vars to import

also use tempdir for input and output so that the program won't know the bundle paths
  • Loading branch information
York Chen committed Sep 19, 2023
1 parent ec01287 commit b900971
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 55 deletions.
12 changes: 7 additions & 5 deletions config/crds/troubleshoot.sh_hostcollectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1353,19 +1353,21 @@ spec:
type: string
command:
type: string
config:
additionalProperties:
env:
items:
type: string
type: object
type: array
exclude:
type: BoolString
input:
additionalProperties:
type: string
type: object
outputDir:
type: string
required:
- args
- command
- config
- outputDir
type: object
subnetAvailable:
properties:
Expand Down
12 changes: 7 additions & 5 deletions config/crds/troubleshoot.sh_hostpreflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1353,19 +1353,21 @@ spec:
type: string
command:
type: string
config:
additionalProperties:
env:
items:
type: string
type: object
type: array
exclude:
type: BoolString
input:
additionalProperties:
type: string
type: object
outputDir:
type: string
required:
- args
- command
- config
- outputDir
type: object
subnetAvailable:
properties:
Expand Down
12 changes: 7 additions & 5 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11613,19 +11613,21 @@ spec:
type: string
command:
type: string
config:
additionalProperties:
env:
items:
type: string
type: object
type: array
exclude:
type: BoolString
input:
additionalProperties:
type: string
type: object
outputDir:
type: string
required:
- args
- command
- config
- outputDir
type: object
subnetAvailable:
properties:
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/troubleshoot/v1beta2/hostcollector_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ type HostRun struct {
HostCollectorMeta `json:",inline" yaml:",inline"`
Command string `json:"command"`
Args []string `json:"args"`
OutputDir string `json:"outputDir" yaml:"outputDir"`
Config map[string]string `json:"config" yaml:"config"`
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"`
}

type HostCollect struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 36 additions & 28 deletions pkg/collect/host_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"gopkg.in/yaml.v2"
"os"
"os/exec"
"path"
"path/filepath"
"strings"

Expand All @@ -20,7 +18,8 @@ type HostRunInfo struct {
ExitCode string `json:"exitCode"`
Error string `json:"error"`
OutputDir string `json:"outputDir"`
Config string `json:"config"`
Input string `json:"input"`
Env string `json:"env"`
}

type CollectHostRun struct {
Expand All @@ -43,40 +42,44 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
collectorName = "run-host"
}
var (
bundleOutputPath string
bundleOutputRelativePath string
cmdConfigFilePath string
cmdConfigFileRelativePath string
bundleOutputRelativePath string
cmdOutputTempDir string
cmdInputTempDir string
)

cmd := exec.Command(runHostCollector.Command, runHostCollector.Args...)

cmd.Env = append(os.Environ(),
fmt.Sprintf("BUNDLE_PATH=%s", c.BundlePath),
)
collectorRelativePath := filepath.Join("host-collectors/run-host", collectorName)

if runHostCollector.Env != nil {
cmd.Env = append(cmd.Env, runHostCollector.Env...)
}
// if we choose to save result for the command run
if runHostCollector.OutputDir != "" {
bundleOutputRelativePath = filepath.Join(collectorRelativePath, runHostCollector.OutputDir)
bundleOutputPath = path.Join(c.BundlePath, bundleOutputRelativePath)
cmdOutputTempDir = os.TempDir()
defer os.RemoveAll(cmdOutputTempDir)
cmd.Env = append(cmd.Env,
fmt.Sprintf("WORKSPACE_DIR=%s", bundleOutputPath),
fmt.Sprintf("TS_WORKSPACE_DIR=%s", cmdOutputTempDir),
)
}

output := NewResult()

if runHostCollector.Config != nil {
cmdConfigFileRelativePath = filepath.Join(collectorRelativePath, "command-config.yaml")
cmdConfigFilePath = path.Join(c.BundlePath, cmdConfigFileRelativePath)
configBytes, err := yaml.Marshal(runHostCollector.Config)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to marshal the command config in collector: %s", collectorName))
if runHostCollector.Input != nil {
cmdInputTempDir = os.TempDir()
defer os.RemoveAll(cmdOutputTempDir)
for inFilename, inFileContent := range runHostCollector.Input {
if strings.Contains(inFileContent, "/") {
return nil, errors.New("Input filename contains '/'")
}
cmdInputFilePath := filepath.Join(cmdInputTempDir, inFilename)
err := os.WriteFile(cmdInputFilePath, []byte(inFileContent), 0644)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to write input file: %s to temp directory", inFilename))
}
}
output.SaveResult(c.BundlePath, cmdConfigFileRelativePath, bytes.NewBuffer(configBytes))
cmd.Env = append(cmd.Env,
fmt.Sprintf("CONFIG=%s", cmdConfigFilePath),
fmt.Sprintf("TS_INPUT_DIR=%s", cmdInputTempDir),
)
}

Expand All @@ -85,10 +88,8 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
cmd.Stderr = &stderr

runInfo := HostRunInfo{
Command: cmd.String(),
ExitCode: "0",
OutputDir: bundleOutputRelativePath,
Config: cmdConfigFilePath,
Command: cmd.String(),
ExitCode: "0",
}

err := cmd.Run()
Expand All @@ -101,8 +102,8 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
}
}

resultInfo := filepath.Join(collectorRelativePath, "collector-info.json")
result := filepath.Join(collectorRelativePath, "collector.txt")
resultInfo := filepath.Join("host-collectors/run-host", collectorName+"-info.json")
result := filepath.Join("host-collectors/run-host", collectorName+".txt")

b, err := json.Marshal(runInfo)
if err != nil {
Expand All @@ -113,7 +114,14 @@ func (c *CollectHostRun) Collect(progressChan chan<- interface{}) (map[string][]
output.SaveResult(c.BundlePath, result, bytes.NewBuffer(stdout.Bytes()))
// walkthrough the output directory and save result for each file
if runHostCollector.OutputDir != "" {
output.SaveResults(c.BundlePath, bundleOutputRelativePath)
runInfo.OutputDir = runHostCollector.OutputDir
bundleOutputRelativePath = filepath.Join(collectorRelativePath, runHostCollector.OutputDir)
output.SaveResults(c.BundlePath, bundleOutputRelativePath, cmdOutputTempDir)
}

if runHostCollector.Input != nil {
bundleInputRelativePath := filepath.Join(collectorRelativePath, "input")
output.SaveResults(c.BundlePath, bundleInputRelativePath, cmdInputTempDir)
}

return output, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/collect/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,14 @@ func (r CollectorResult) SaveResult(bundlePath string, relativePath string, read
return nil
}

// SaveResults walk a directory and call SaveResult on all files retrieved from the walk.
func (r CollectorResult) SaveResults(bundlePath string, relativePath string) error {
// SaveResults walk a target directory and call SaveResult on all files retrieved from the walk.
func (r CollectorResult) SaveResults(bundlePath, relativePath, targetDir string) error {
dirPath := path.Join(bundlePath, relativePath)
if err := os.MkdirAll(dirPath, 0777); err != nil {
return errors.Wrap(err, "failed to create output file directory")
}

err := filepath.WalkDir(dirPath, func(path string, d os.DirEntry, err error) error {
err := filepath.WalkDir(targetDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return errors.Wrap(err, "error from WalkDirFunc")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/preflight/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ PASS
if err != nil {
t.Fatal(err)
}
kubeconfig, err := k3s.Config(c)
kubeconfig, err := k3s.Input(c)
if err != nil {
t.Fatal(err)
}
Expand Down
16 changes: 10 additions & 6 deletions schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -11399,9 +11399,7 @@
"type": "object",
"required": [
"args",
"command",
"config",
"outputDir"
"command"
],
"properties": {
"args": {
Expand All @@ -11416,15 +11414,21 @@
"command": {
"type": "string"
},
"config": {
"type": "object",
"additionalProperties": {
"env": {
"type": "array",
"items": {
"type": "string"
}
},
"exclude": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
},
"input": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"outputDir": {
"type": "string"
}
Expand Down

0 comments on commit b900971

Please sign in to comment.