github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/helm/environment/environment.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  // DefaultHelmHome is the default HELM_HOME.
    36  var DefaultHelmHome = filepath.Join(homedir.HomeDir(), ".helm")
    37  
    38  // EnvSettings describes all of the environment settings.
    39  type EnvSettings struct {
    40  	// TillerHost is the host and port of Tiller.
    41  	TillerHost string
    42  	// TillerNamespace is the namespace in which Tiller runs.
    43  	TillerNamespace string
    44  	// Home is the local path to the Helm home directory.
    45  	Home helmpath.Home
    46  	// Debug indicates whether or not Helm is running in Debug mode.
    47  	Debug bool
    48  	// KubeContext is the name of the kubeconfig context.
    49  	KubeContext string
    50  	// KubeConfig is the name of the kubeconfig file.
    51  	KubeConfig string
    52  }
    53  
    54  // AddFlags binds flags to the given flagset.
    55  func (s *EnvSettings) AddFlags(fs *pflag.FlagSet) {
    56  	fs.StringVar((*string)(&s.Home), "home", DefaultHelmHome, "location of your Helm config. Overrides $HELM_HOME")
    57  	fs.StringVar(&s.TillerHost, "host", "", "address of Tiller. Overrides $HELM_HOST")
    58  	fs.StringVar(&s.KubeContext, "kube-context", "", "name of the kubeconfig context to use")
    59  	fs.StringVar(&s.KubeConfig, "kubeconfig", "", "path to kubeconfig file. Overrides $KUBECONFIG")
    60  	fs.BoolVar(&s.Debug, "debug", false, "enable verbose output")
    61  	fs.StringVar(&s.TillerNamespace, "tiller-namespace", "kube-system", "namespace of Tiller")
    62  }
    63  
    64  // Init sets values from the environment.
    65  func (s *EnvSettings) Init(fs *pflag.FlagSet) {
    66  	for name, envar := range envMap {
    67  		setFlagFromEnv(name, envar, fs)
    68  	}
    69  }
    70  
    71  // PluginDirs is the path to the plugin directories.
    72  func (s EnvSettings) PluginDirs() string {
    73  	if d, ok := os.LookupEnv("HELM_PLUGIN"); ok {
    74  		return d
    75  	}
    76  	return s.Home.Plugins()
    77  }
    78  
    79  // envMap maps flag names to envvars
    80  var envMap = map[string]string{
    81  	"debug":            "HELM_DEBUG",
    82  	"home":             "HELM_HOME",
    83  	"host":             "HELM_HOST",
    84  	"kubeconfig":       "KUBECONFIG",
    85  	"tiller-namespace": "TILLER_NAMESPACE",
    86  }
    87  
    88  func setFlagFromEnv(name, envar string, fs *pflag.FlagSet) {
    89  	if fs.Changed(name) {
    90  		return
    91  	}
    92  	if v, ok := os.LookupEnv(envar); ok {
    93  		fs.Set(name, v)
    94  	}
    95  }
    96  
    97  // Deprecated
    98  const (
    99  	HomeEnvVar          = "HELM_HOME"
   100  	PluginEnvVar        = "HELM_PLUGIN"
   101  	PluginDisableEnvVar = "HELM_NO_PLUGINS"
   102  	HostEnvVar          = "HELM_HOST"
   103  	DebugEnvVar         = "HELM_DEBUG"
   104  )