Use logger for helm output (#1585)
* use logger for helm output Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> * update integration test output Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> * make logging output configurable Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> * also compare stderr in integration tests Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com> --------- Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									f98d0673b8
								
							
						
					
					
						commit
						824e5a8b92
					
				|  | @ -60,7 +60,13 @@ func NewRootCmd(globalConfig *config.GlobalOptions) (*cobra.Command, error) { | ||||||
| 			case globalConfig.Quiet: | 			case globalConfig.Quiet: | ||||||
| 				logLevel = "warn" | 				logLevel = "warn" | ||||||
| 			} | 			} | ||||||
| 			logger = helmexec.NewLogger(os.Stderr, logLevel) | 
 | ||||||
|  | 			// If the log output is not set, default to stderr.
 | ||||||
|  | 			logOut := globalConfig.LogOutput | ||||||
|  | 			if logOut == nil { | ||||||
|  | 				logOut = os.Stderr | ||||||
|  | 			} | ||||||
|  | 			logger = helmexec.NewLogger(logOut, logLevel) | ||||||
| 			globalConfig.SetLogger(logger) | 			globalConfig.SetLogger(logger) | ||||||
| 			return nil | 			return nil | ||||||
| 		}, | 		}, | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ package config | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"io" | ||||||
| 	"os" | 	"os" | ||||||
| 
 | 
 | ||||||
| 	"go.uber.org/zap" | 	"go.uber.org/zap" | ||||||
|  | @ -65,6 +66,8 @@ type GlobalOptions struct { | ||||||
| 	Interactive bool | 	Interactive bool | ||||||
| 	// Args is the list of arguments to pass to the Helm binary.
 | 	// Args is the list of arguments to pass to the Helm binary.
 | ||||||
| 	Args string | 	Args string | ||||||
|  | 	// LogOutput is the writer to use for writing logs.
 | ||||||
|  | 	LogOutput io.Writer | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Logger returns the logger to use.
 | // Logger returns the logger to use.
 | ||||||
|  |  | ||||||
|  | @ -279,7 +279,7 @@ func (helm *execer) SyncRelease(context HelmContext, name, chart string, flags . | ||||||
| 	flags = append(flags, "--history-max", strconv.Itoa(context.HistoryMax)) | 	flags = append(flags, "--history-max", strconv.Itoa(context.HistoryMax)) | ||||||
| 
 | 
 | ||||||
| 	out, err := helm.exec(append(append(preArgs, "upgrade", "--install", name, chart), flags...), env, nil) | 	out, err := helm.exec(append(append(preArgs, "upgrade", "--install", name, chart), flags...), env, nil) | ||||||
| 	helm.write(nil, out) | 	helm.info(out) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -288,7 +288,7 @@ func (helm *execer) ReleaseStatus(context HelmContext, name string, flags ...str | ||||||
| 	preArgs := make([]string, 0) | 	preArgs := make([]string, 0) | ||||||
| 	env := make(map[string]string) | 	env := make(map[string]string) | ||||||
| 	out, err := helm.exec(append(append(preArgs, "status", name), flags...), env, nil) | 	out, err := helm.exec(append(append(preArgs, "status", name), flags...), env, nil) | ||||||
| 	helm.write(nil, out) | 	helm.info(out) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -309,7 +309,7 @@ func (helm *execer) List(context HelmContext, filter string, flags ...string) (s | ||||||
| 	lines := strings.Split(string(out), "\n") | 	lines := strings.Split(string(out), "\n") | ||||||
| 	lines = lines[1:] | 	lines = lines[1:] | ||||||
| 	out = []byte(strings.Join(lines, "\n")) | 	out = []byte(strings.Join(lines, "\n")) | ||||||
| 	helm.write(nil, out) | 	helm.info(out) | ||||||
| 	return string(out), err | 	return string(out), err | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -451,7 +451,7 @@ func (helm *execer) TemplateRelease(name string, chart string, flags ...string) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (helm *execer) DiffRelease(context HelmContext, name, chart string, suppressDiff bool, flags ...string) error { | func (helm *execer) DiffRelease(context HelmContext, name, chart string, suppressDiff bool, flags ...string) error { | ||||||
| 	if context.Writer != nil { | 	if context.Writer != nil && !suppressDiff { | ||||||
| 		fmt.Fprintf(context.Writer, "Comparing release=%v, chart=%v\n", name, redactedURL(chart)) | 		fmt.Fprintf(context.Writer, "Comparing release=%v, chart=%v\n", name, redactedURL(chart)) | ||||||
| 	} else { | 	} else { | ||||||
| 		helm.logger.Infof("Comparing release=%v, chart=%v", name, redactedURL(chart)) | 		helm.logger.Infof("Comparing release=%v, chart=%v", name, redactedURL(chart)) | ||||||
|  | @ -491,6 +491,7 @@ func (helm *execer) DiffRelease(context HelmContext, name, chart string, suppres | ||||||
| func (helm *execer) Lint(name, chart string, flags ...string) error { | func (helm *execer) Lint(name, chart string, flags ...string) error { | ||||||
| 	helm.logger.Infof("Linting release=%v, chart=%v", name, chart) | 	helm.logger.Infof("Linting release=%v, chart=%v", name, chart) | ||||||
| 	out, err := helm.exec(append([]string{"lint", chart}, flags...), map[string]string{}, nil) | 	out, err := helm.exec(append([]string{"lint", chart}, flags...), map[string]string{}, nil) | ||||||
|  | 	// Always write to stdout to write the linting result to eg. a file
 | ||||||
| 	helm.write(nil, out) | 	helm.write(nil, out) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  | @ -541,7 +542,7 @@ func (helm *execer) DeleteRelease(context HelmContext, name string, flags ...str | ||||||
| 	preArgs := make([]string, 0) | 	preArgs := make([]string, 0) | ||||||
| 	env := make(map[string]string) | 	env := make(map[string]string) | ||||||
| 	out, err := helm.exec(append(append(preArgs, "delete", name), flags...), env, nil) | 	out, err := helm.exec(append(append(preArgs, "delete", name), flags...), env, nil) | ||||||
| 	helm.write(nil, out) | 	helm.info(out) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -551,7 +552,7 @@ func (helm *execer) TestRelease(context HelmContext, name string, flags ...strin | ||||||
| 	env := make(map[string]string) | 	env := make(map[string]string) | ||||||
| 	args := []string{"test", name} | 	args := []string{"test", name} | ||||||
| 	out, err := helm.exec(append(append(preArgs, args...), flags...), env, nil) | 	out, err := helm.exec(append(append(preArgs, args...), flags...), env, nil) | ||||||
| 	helm.write(nil, out) | 	helm.info(out) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -3,26 +3,35 @@ diff_args_output_dir="${cases_dir}/diff-args/output" | ||||||
| 
 | 
 | ||||||
| diff_args_tmp=$(mktemp -d) | diff_args_tmp=$(mktemp -d) | ||||||
| diff_args_reverse=${diff_args_tmp}/diff.args.build.yaml | diff_args_reverse=${diff_args_tmp}/diff.args.build.yaml | ||||||
|  | diff_args_reverse_stderr=${diff_args_tmp}/diff.args.build.stderr.yaml | ||||||
| 
 | 
 | ||||||
| case_title="diff args" | case_title="diff args" | ||||||
| diff_out_file=${diff_args_output_dir}/diff | diff_out_file=${diff_args_output_dir}/diff | ||||||
| apply_out_file=${diff_args_output_dir}/apply | apply_out_file=${diff_args_output_dir}/apply | ||||||
|  | diff_out_stderr_file=${diff_args_output_dir}/diff-stderr | ||||||
|  | apply_out_stderr_file=${diff_args_output_dir}/apply-stderr | ||||||
| if [[ $EXTRA_HELMFILE_FLAGS == *--enable-live-output* ]]; then | if [[ $EXTRA_HELMFILE_FLAGS == *--enable-live-output* ]]; then | ||||||
|     apply_out_file=${diff_args_output_dir}/apply-live |  | ||||||
|     diff_out_file=${diff_args_output_dir}/diff-live |     diff_out_file=${diff_args_output_dir}/diff-live | ||||||
|  |     apply_out_file=${diff_args_output_dir}/apply-live | ||||||
|  |     diff_out_stderr_file=${diff_args_output_dir}/diff-live-stderr | ||||||
|  |     apply_out_stderr_file=${diff_args_output_dir}/apply-live-stderr | ||||||
| fi | fi | ||||||
| 
 | 
 | ||||||
| test_start "$case_title" | test_start "$case_title" | ||||||
| info "Comparing ${case_title} diff for output ${diff_args_reverse} with ${diff_out_file}" | info "Comparing ${case_title} diff for output ${diff_args_reverse} with ${diff_out_file}" | ||||||
|  | info "Comparing ${case_title} diff for output ${diff_args_reverse_stderr} with ${diff_out_stderr_file}" | ||||||
| for i in $(seq 10); do | for i in $(seq 10); do | ||||||
|     info "Comparing diff-args diff log #$i" |     info "Comparing diff-args diff log #$i" | ||||||
|     ${helmfile} -f ${diff_args_input_dir}/helmfile.yaml diff > ${diff_args_reverse} || fail "\"helmfile diff\" shouldn't fail" |     ${helmfile} -f ${diff_args_input_dir}/helmfile.yaml diff 1> ${diff_args_reverse} 2> ${diff_args_reverse_stderr} || fail "\"helmfile diff\" shouldn't fail" | ||||||
|     diff -u ${diff_out_file} ${diff_args_reverse} || fail "\"helmfile diff\" should be consistent" |     diff -u ${diff_out_file} ${diff_args_reverse} || fail "\"helmfile diff\" should be consistent" | ||||||
|  |     diff -u ${diff_out_stderr_file} ${diff_args_reverse_stderr} || fail "\"helmfile diff\" should be consistent (stderr)" | ||||||
|     echo code=$? |     echo code=$? | ||||||
| done | done | ||||||
| info "Comparing ${case_title} apply for output ${diff_args_reverse} with ${apply_out_file}" | info "Comparing ${case_title} apply for output ${diff_args_reverse} with ${apply_out_file}" | ||||||
| ${helmfile} -f ${diff_args_input_dir}/helmfile.yaml apply | grep -vE "^(LAST DEPLOYED|installed)"  > ${diff_args_reverse} || fail "\"helmfile apply\" shouldn't fail" | info "Comparing ${case_title} apply for stdout ${diff_args_reverse_stderr} with ${apply_out_stderr_file}" | ||||||
| diff -u ${apply_out_file} ${diff_args_reverse} || fail "\"helmfile apply\" should be consistent" | ${helmfile} -f ${diff_args_input_dir}/helmfile.yaml apply 1> ${diff_args_reverse} 2> ${diff_args_reverse_stderr} || fail "\"helmfile apply\" shouldn't fail" | ||||||
|  | diff -u ${apply_out_file} <(grep -vE "^(LAST DEPLOYED|installed)" ${diff_args_reverse}) || fail "\"helmfile apply\" should be consistent" | ||||||
|  | diff -u ${apply_out_stderr_file} <(grep -vE "^(LAST DEPLOYED|installed)" ${diff_args_reverse_stderr}) || fail "\"helmfile apply\" should be consistent (stderr)" | ||||||
| echo "clean up diff args resources" | echo "clean up diff args resources" | ||||||
| ${helmfile} -f ${diff_args_input_dir}/helmfile.yaml destroy || fail "\"helmfile destroy\" shouldn't fail" | ${helmfile} -f ${diff_args_input_dir}/helmfile.yaml destroy || fail "\"helmfile destroy\" shouldn't fail" | ||||||
| test_pass "$case_title" | test_pass "$case_title" | ||||||
|  | @ -67,11 +67,3 @@ helmfile-tests, installed-httpbin, Service (v1) has been added: | ||||||
| +     release: installed | +     release: installed | ||||||
| +   type: LoadBalancer | +   type: LoadBalancer | ||||||
| 
 | 
 | ||||||
| Release "installed" does not exist. Installing it now. |  | ||||||
| NAME: installed |  | ||||||
| NAMESPACE: helmfile-tests |  | ||||||
| STATUS: deployed |  | ||||||
| REVISION: 1 |  | ||||||
| TEST SUITE: None |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -74,4 +74,3 @@ NAMESPACE: helmfile-tests | ||||||
| STATUS: deployed | STATUS: deployed | ||||||
| REVISION: 1 | REVISION: 1 | ||||||
| TEST SUITE: None | TEST SUITE: None | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -0,0 +1,10 @@ | ||||||
|  | Live output is enabled | ||||||
|  | Building dependency release=installed, chart=../../../charts/httpbin | ||||||
|  | Listing releases matching ^uninstalled$ | ||||||
|  | Upgrading release=installed, chart=../../../charts/httpbin | ||||||
|  | Listing releases matching ^installed$ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | UPDATED RELEASES: | ||||||
|  | NAME        NAMESPACE        CHART                     VERSION   DURATION | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,16 @@ | ||||||
|  | Building dependency release=installed, chart=../../../charts/httpbin | ||||||
|  | Listing releases matching ^uninstalled$ | ||||||
|  | Upgrading release=installed, chart=../../../charts/httpbin | ||||||
|  | Release "installed" does not exist. Installing it now. | ||||||
|  | NAME: installed | ||||||
|  | NAMESPACE: helmfile-tests | ||||||
|  | STATUS: deployed | ||||||
|  | REVISION: 1 | ||||||
|  | TEST SUITE: None | ||||||
|  | 
 | ||||||
|  | Listing releases matching ^installed$ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | UPDATED RELEASES: | ||||||
|  | NAME        NAMESPACE        CHART                     VERSION   DURATION | ||||||
|  | 
 | ||||||
|  | @ -0,0 +1,3 @@ | ||||||
|  | Live output is enabled | ||||||
|  | Building dependency release=installed, chart=../../../charts/httpbin | ||||||
|  | Listing releases matching ^uninstalled$ | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | Building dependency release=installed, chart=../../../charts/httpbin | ||||||
|  | Listing releases matching ^uninstalled$ | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| Comparing release=foo, chart=../../../charts/raw |  | ||||||
| Comparing release=baz, chart=../../../charts/raw | Comparing release=baz, chart=../../../charts/raw | ||||||
| ******************** | ******************** | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -13,5 +13,4 @@ helmfile-tests, baz-2, ConfigMap (v1) has been added: | ||||||
| +   namespace: helmfile-tests | +   namespace: helmfile-tests | ||||||
| + data: | + data: | ||||||
| +   baz: BAZ | +   baz: BAZ | ||||||
| Comparing release=foo, chart=../../../charts/raw |  | ||||||
| Comparing release=baz, chart=../../../charts/raw | Comparing release=baz, chart=../../../charts/raw | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue