* fix(helm): fix regression with TLS flags/envvars
This change fixes some of the assumptions made in an earlier commit. Helm's TLS flags and environment variables were not respected because they were parsed well before execution (during settings.AddFlagsTLS()), causing erroneous behaviour at runtime. By re-introducing environment.Init(), Helm can properly parse environment variables at the correct time.
One change that had to occur in this PR is the fact that we need to call settings.Init() each time we call settings.AddFlagsTLS(). This is because each command owns its own FlagSet, so we need to parse each flagset to read and propagate the environment variables correctly.
I also noticed that we were maintaining two separate variables for each TLS value. Refactoring out some of the older code to all use the settings object makes the code much cleaner to read and fixes an issue where setting a flag or environment variable would propagate to the settings object, but we'd be reading from tlsEnable.
I've also added some unit tests to ensure this regression doesn't occur again.
Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
* fix bug where os.ExpandEnv() on the default value causes differing behaviour
Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
* add more context to the TODO/FIXME messages
Signed-off-by: Matthew Fisher <matt.fisher@microsoft.com>
f.BoolVarP(&d.force,"force","f",false,"forces Tiller uninstall even if there are releases installed, or if Tiller is not in ready state. Releases are not deleted.)")
f.BoolVar(&d.removeHelmHome,"remove-helm-home",false,"if set deletes $HELM_HOME")
f.BoolVar(&rollback.wait,"wait",false,"if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment are in a ready state before marking the release as successful. It will wait for as long as --timeout")
f.StringVar(&rollback.description,"description","","specify a description for the release")
fs.StringVar(&s.TillerNamespace,"tiller-namespace","kube-system","namespace of Tiller")
fs.Int64Var(&s.TillerConnectionTimeout,"tiller-connection-timeout",int64(300),"the duration (in seconds) Helm will wait to establish a connection to tiller")
envMap:=map[string]string{
"debug":"HELM_DEBUG",
"home":"HELM_HOME",
"host":"HELM_HOST",
"tiller-namespace":"TILLER_NAMESPACE",
}
forname,envar:=rangeenvMap{
setFlagFromEnv(name,envar,fs)
}
}
// AddFlagsTLS adds the flags for supporting client side TLS to the given flagset.
@ -108,23 +97,38 @@ func (s *EnvSettings) AddFlagsTLS(fs *pflag.FlagSet) {
fs.StringVar(&s.TLSKeyFile,"tls-key",DefaultTLSKeyFile,"path to TLS key file")
fs.BoolVar(&s.TLSVerify,"tls-verify",DefaultTLSVerify,"enable TLS for request and verify remote")
fs.BoolVar(&s.TLSEnable,"tls",DefaultTLSEnable,"enable TLS for request")
}
envMap:=map[string]string{
"tls-hostname":"HELM_TLS_HOSTNAME",
"tls-ca-cert":"HELM_TLS_CA_CERT",
"tls-cert":"HELM_TLS_CERT",
"tls-key":"HELM_TLS_KEY",
"tls-verify":"HELM_TLS_VERIFY",
"tls":"HELM_TLS_ENABLE",
// Init sets values from the environment.
func(s*EnvSettings)Init(fs*pflag.FlagSet){
forname,envar:=rangeenvMap{
setFlagFromEnv(name,envar,fs)
}
}
forname,envar:=rangeenvMap{
// InitTLS sets TLS values from the environment.
func(s*EnvSettings)InitTLS(fs*pflag.FlagSet){
forname,envar:=rangetlsEnvMap{
setFlagFromEnv(name,envar,fs)
}
}
// Init is deprecated; calling `.AddFlags` or `.AddFlagsTLS` directly will set the flags to their default values from the environment, so this is a no-op.
func(s*EnvSettings)Init(fs*pflag.FlagSet){}
// envMap maps flag names to envvars
varenvMap=map[string]string{
"debug":"HELM_DEBUG",
"home":"HELM_HOME",
"host":"HELM_HOST",
"tiller-namespace":"TILLER_NAMESPACE",
}
vartlsEnvMap=map[string]string{
"tls-hostname":"HELM_TLS_HOSTNAME",
"tls-ca-cert":"HELM_TLS_CA_CERT",
"tls-cert":"HELM_TLS_CERT",
"tls-key":"HELM_TLS_KEY",
"tls-verify":"HELM_TLS_VERIFY",
"tls":"HELM_TLS_ENABLE",
}
// PluginDirs is the path to the plugin directories.
func(sEnvSettings)PluginDirs()string{
@ -134,6 +138,9 @@ func (s EnvSettings) PluginDirs() string {
returns.Home.Plugins()
}
// setFlagFromEnv looks up and sets a flag if the corresponding environment variable changed.
// if the flag with the corresponding name was set during fs.Parse(), then the environment