77 "encoding/json"
88 "errors"
99 "fmt"
10+ "io"
1011 "os"
1112 "os/exec"
1213 "strings"
@@ -31,34 +32,29 @@ func SerializeSpec(spec *troubleshootv1beta2.HostPreflightSpec) ([]byte, error)
3132
3233// Run runs the provided host preflight spec locally. This function is meant to be
3334// used when upgrading a local node.
34- func Run (ctx context.Context , spec * troubleshootv1beta2.HostPreflightSpec , proxy * ecv1beta1.ProxySpec ) (* Output , string , error ) {
35+ func Run (ctx context.Context , spec * troubleshootv1beta2.HostPreflightSpec , proxy * ecv1beta1.ProxySpec ) (* Output , error ) {
3536 // Deduplicate collectors and analyzers before running preflights
3637 spec .Collectors = dedup (spec .Collectors )
3738 spec .Analyzers = dedup (spec .Analyzers )
3839
3940 fpath , err := saveHostPreflightFile (spec )
4041 if err != nil {
41- return nil , "" , fmt .Errorf ("unable to save preflight locally: %w" , err )
42+ return nil , fmt .Errorf ("unable to save preflight locally: %w" , err )
4243 }
4344 defer os .Remove (fpath )
4445 binpath := defaults .PathToEmbeddedClusterBinary ("kubectl-preflight" )
4546 stdout := bytes .NewBuffer (nil )
46- stderr := bytes .NewBuffer (nil )
4747 cmd := exec .Command (binpath , "--interactive=false" , "--format=json" , fpath )
48- cmd .Env = os .Environ ()
49- cmd .Env = proxyEnv (cmd .Env , proxy )
50- cmd .Env = pathEnv (cmd .Env )
51- cmd .Stdout , cmd .Stderr = stdout , stderr
48+ cmd .Env = proxyEnv (proxy )
49+ cmd .Stdout , cmd .Stderr = stdout , io .Discard
5250 if err = cmd .Run (); err == nil {
53- out , err := OutputFromReader (stdout )
54- return out , stderr .String (), err
51+ return OutputFromReader (stdout )
5552 }
5653 var exit * exec.ExitError
5754 if ! errors .As (err , & exit ) || exit .ExitCode () < 2 {
58- return nil , stderr . String (), fmt .Errorf ("error running host preflight: %w, stderr=%q " , err , stderr . String () )
55+ return nil , fmt .Errorf ("unknown error running host preflight: %w" , err )
5956 }
60- out , err := OutputFromReader (stdout )
61- return out , stderr .String (), err
57+ return OutputFromReader (stdout )
6258}
6359
6460// saveHostPreflightFile saves the provided spec to a temporary file and returns
@@ -101,40 +97,20 @@ func dedup[T any](objs []T) []T {
10197 return out
10298}
10399
104- func proxyEnv (env [] string , proxy * ecv1beta1.ProxySpec ) []string {
105- next := []string {}
106- for _ , e := range env {
100+ func proxyEnv (proxy * ecv1beta1.ProxySpec ) []string {
101+ env := []string {}
102+ for _ , e := range os . Environ () {
107103 switch strings .SplitN (e , "=" , 2 )[0 ] {
108104 // Unset proxy environment variables
109105 case "HTTP_PROXY" , "HTTPS_PROXY" , "NO_PROXY" , "http_proxy" , "https_proxy" , "no_proxy" :
110- default :
111- next = append (next , e )
106+ continue
112107 }
108+ env = append (env , e )
113109 }
114110 if proxy != nil {
115- next = append (next , fmt .Sprintf ("HTTP_PROXY=%s" , proxy .HTTPProxy ))
116- next = append (next , fmt .Sprintf ("HTTPS_PROXY=%s" , proxy .HTTPSProxy ))
117- next = append (next , fmt .Sprintf ("NO_PROXY=%s" , proxy .NoProxy ))
118- }
119- return next
120- }
121-
122- func pathEnv (env []string ) []string {
123- path := ""
124- next := []string {}
125- for _ , e := range env {
126- switch strings .SplitN (e , "=" , 2 )[0 ] {
127- // Unset PATH environment variable
128- case "PATH" :
129- path = strings .SplitN (e , "=" , 2 )[1 ]
130- default :
131- next = append (next , e )
132- }
133- }
134- if path != "" {
135- next = append (next , fmt .Sprintf ("PATH=%s:%s" , path , defaults .EmbeddedClusterBinsSubDir ()))
136- } else {
137- next = append (next , fmt .Sprintf ("PATH=%s" , defaults .EmbeddedClusterBinsSubDir ()))
111+ env = append (env , fmt .Sprintf ("HTTP_PROXY=%s" , proxy .HTTPProxy ))
112+ env = append (env , fmt .Sprintf ("HTTPS_PROXY=%s" , proxy .HTTPSProxy ))
113+ env = append (env , fmt .Sprintf ("NO_PROXY=%s" , proxy .NoProxy ))
138114 }
139- return next
115+ return env
140116}
0 commit comments