github.com/koderover/helm@v2.17.0+incompatible/pkg/helm/environment/environment.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /*Package environment describes the operating environment for Tiller. 18 19 Tiller's environment encapsulates all of the service dependencies Tiller has. 20 These dependencies are expressed as interfaces so that alternate implementations 21 (mocks, etc.) can be easily generated. 22 */ 23 package environment 24 25 import ( 26 "os" 27 "path/filepath" 28 29 "github.com/spf13/pflag" 30 31 "k8s.io/client-go/util/homedir" 32 "k8s.io/helm/pkg/helm/helmpath" 33 ) 34 35 const ( 36 // DefaultTLSCaCert is the default value for HELM_TLS_CA_CERT 37 DefaultTLSCaCert = "$HELM_HOME/ca.pem" 38 // DefaultTLSCert is the default value for HELM_TLS_CERT 39 DefaultTLSCert = "$HELM_HOME/cert.pem" 40 // DefaultTLSKeyFile is the default value for HELM_TLS_KEY_FILE 41 DefaultTLSKeyFile = "$HELM_HOME/key.pem" 42 // DefaultTLSEnable is the default value for HELM_TLS_ENABLE 43 DefaultTLSEnable = false 44 // DefaultTLSVerify is the default value for HELM_TLS_VERIFY 45 DefaultTLSVerify = false 46 ) 47 48 // DefaultHelmHome is the default HELM_HOME. 49 var DefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm") 50 51 // EnvSettings describes all of the environment settings. 52 type EnvSettings struct { 53 // TillerHost is the host and port of Tiller. 54 TillerHost string 55 // TillerConnectionTimeout is the duration (in seconds) helm will wait to establish a connection to Tiller. 56 TillerConnectionTimeout int64 57 // TillerNamespace is the namespace in which Tiller runs. 58 TillerNamespace string 59 // Home is the local path to the Helm home directory. 60 Home helmpath.Home 61 // Debug indicates whether or not Helm is running in Debug mode. 62 Debug bool 63 // KubeContext is the name of the kubeconfig context. 64 KubeContext string 65 // KubeConfig is the path to an explicit kubeconfig file. This overwrites the value in $KUBECONFIG 66 KubeConfig string 67 // TLSEnable tells helm to communicate with Tiller via TLS 68 TLSEnable bool 69 // TLSVerify tells helm to communicate with Tiller via TLS and to verify remote certificates served by Tiller 70 TLSVerify bool 71 // TLSServerName tells helm to verify the hostname on the returned certificates from Tiller 72 TLSServerName string 73 // TLSCaCertFile is the path to a TLS CA certificate file 74 TLSCaCertFile string 75 // TLSCertFile is the path to a TLS certificate file 76 TLSCertFile string 77 // TLSKeyFile is the path to a TLS key file 78 TLSKeyFile string 79 } 80 81 // AddFlags binds flags to the given flagset. 82 func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) { 83 fs.StringVar((*string)(&s.Home), "home", DefaultHelmHome, "Location of your Helm config. Overrides $HELM_HOME") 84 fs.StringVar(&s.TillerHost, "host", "", "Address of Tiller. Overrides $HELM_HOST") 85 fs.StringVar(&s.KubeContext, "kube-context", "", "Name of the kubeconfig context to use") 86 fs.StringVar(&s.KubeConfig, "kubeconfig", "", "Absolute path of the kubeconfig file to be used") 87 fs.BoolVar(&s.Debug, "debug", false, "Enable verbose output") 88 fs.StringVar(&s.TillerNamespace, "tiller-namespace", "kube-system", "Namespace of Tiller") 89 fs.Int64Var(&s.TillerConnectionTimeout, "tiller-connection-timeout", int64(300), "The duration (in seconds) Helm will wait to establish a connection to Tiller") 90 } 91 92 // AddFlagsTLS adds the flags for supporting client side TLS to the given flagset. 93 func (s *EnvSettings) AddFlagsTLS(fs *pflag.FlagSet) { 94 fs.StringVar(&s.TLSServerName, "tls-hostname", s.TillerHost, "The server name used to verify the hostname on the returned certificates from the server") 95 fs.StringVar(&s.TLSCaCertFile, "tls-ca-cert", DefaultTLSCaCert, "Path to TLS CA certificate file") 96 fs.StringVar(&s.TLSCertFile, "tls-cert", DefaultTLSCert, "Path to TLS certificate file") 97 fs.StringVar(&s.TLSKeyFile, "tls-key", DefaultTLSKeyFile, "Path to TLS key file") 98 fs.BoolVar(&s.TLSVerify, "tls-verify", DefaultTLSVerify, "Enable TLS for request and verify remote") 99 fs.BoolVar(&s.TLSEnable, "tls", DefaultTLSEnable, "Enable TLS for request") 100 } 101 102 // Init sets values from the environment. 103 func (s *EnvSettings) Init(fs *pflag.FlagSet) { 104 for name, envar := range envMap { 105 setFlagFromEnv(name, envar, fs) 106 } 107 } 108 109 // InitTLS sets TLS values from the environment. 110 func (s *EnvSettings) InitTLS(fs *pflag.FlagSet) { 111 for name, envar := range tlsEnvMap { 112 setFlagFromEnv(name, envar, fs) 113 } 114 } 115 116 // envMap maps flag names to envvars 117 var envMap = map[string]string{ 118 "debug": "HELM_DEBUG", 119 "home": "HELM_HOME", 120 "host": "HELM_HOST", 121 "tiller-namespace": "TILLER_NAMESPACE", 122 } 123 124 var tlsEnvMap = map[string]string{ 125 "tls-hostname": "HELM_TLS_HOSTNAME", 126 "tls-ca-cert": "HELM_TLS_CA_CERT", 127 "tls-cert": "HELM_TLS_CERT", 128 "tls-key": "HELM_TLS_KEY", 129 "tls-verify": "HELM_TLS_VERIFY", 130 "tls": "HELM_TLS_ENABLE", 131 } 132 133 // PluginDirs is the path to the plugin directories. 134 func (s EnvSettings) PluginDirs() string { 135 if d, ok := os.LookupEnv("HELM_PLUGIN"); ok { 136 return d 137 } 138 return s.Home.Plugins() 139 } 140 141 // HelmKeyPassphrase is the passphrase used to sign a helm chart. 142 func (s EnvSettings) HelmKeyPassphrase() string { 143 if d, ok := os.LookupEnv("HELM_KEY_PASSPHRASE"); ok { 144 return d 145 } 146 return "" 147 } 148 149 // setFlagFromEnv looks up and sets a flag if the corresponding environment variable changed. 150 // if the flag with the corresponding name was set during fs.Parse(), then the environment 151 // variable is ignored. 152 func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) { 153 if fs.Changed(name) { 154 return 155 } 156 if v, ok := os.LookupEnv(envar); ok { 157 fs.Set(name, v) 158 } 159 } 160 161 // Deprecated 162 const ( 163 HomeEnvVar = "HELM_HOME" 164 PluginEnvVar = "HELM_PLUGIN" 165 PluginDisableEnvVar = "HELM_NO_PLUGINS" 166 HostEnvVar = "HELM_HOST" 167 DebugEnvVar = "HELM_DEBUG" 168 )