github.com/x-helm/helm@v3.0.0-beta.3+incompatible/cmd/helm/helm.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 main // import "helm.sh/helm/cmd/helm" 18 19 import ( 20 "flag" 21 "fmt" 22 "log" 23 "os" 24 "strings" 25 "sync" 26 27 "github.com/spf13/cobra" 28 "github.com/spf13/pflag" 29 "k8s.io/cli-runtime/pkg/genericclioptions" 30 "k8s.io/klog" 31 32 // Import to initialize client auth plugins. 33 _ "k8s.io/client-go/plugin/pkg/client/auth" 34 35 "helm.sh/helm/pkg/action" 36 "helm.sh/helm/pkg/cli" 37 "helm.sh/helm/pkg/gates" 38 "helm.sh/helm/pkg/kube" 39 "helm.sh/helm/pkg/storage" 40 "helm.sh/helm/pkg/storage/driver" 41 ) 42 43 // FeatureGateOCI is the feature gate for checking if `helm chart` and `helm registry` commands should work 44 const FeatureGateOCI = gates.Gate("HELM_EXPERIMENTAL_OCI") 45 46 var ( 47 settings = cli.New() 48 config genericclioptions.RESTClientGetter 49 configOnce sync.Once 50 ) 51 52 func init() { 53 log.SetFlags(log.Lshortfile) 54 } 55 56 func debug(format string, v ...interface{}) { 57 if settings.Debug { 58 format = fmt.Sprintf("[debug] %s\n", format) 59 log.Output(2, fmt.Sprintf(format, v...)) 60 } 61 } 62 63 func initKubeLogs() { 64 pflag.CommandLine.SetNormalizeFunc(wordSepNormalizeFunc) 65 gofs := flag.NewFlagSet("klog", flag.ExitOnError) 66 klog.InitFlags(gofs) 67 pflag.CommandLine.AddGoFlagSet(gofs) 68 pflag.CommandLine.Set("logtostderr", "true") 69 } 70 71 func main() { 72 initKubeLogs() 73 74 actionConfig := new(action.Configuration) 75 cmd := newRootCmd(actionConfig, os.Stdout, os.Args[1:]) 76 77 // Initialize the rest of the actionConfig 78 initActionConfig(actionConfig, false) 79 80 if err := cmd.Execute(); err != nil { 81 debug("%+v", err) 82 os.Exit(1) 83 } 84 } 85 86 func initActionConfig(actionConfig *action.Configuration, allNamespaces bool) { 87 kc := kube.New(kubeConfig()) 88 kc.Log = debug 89 90 clientset, err := kc.Factory.KubernetesClientSet() 91 if err != nil { 92 // TODO return error 93 log.Fatal(err) 94 } 95 var namespace string 96 if !allNamespaces { 97 namespace = getNamespace() 98 } 99 100 var store *storage.Storage 101 switch os.Getenv("HELM_DRIVER") { 102 case "secret", "secrets", "": 103 d := driver.NewSecrets(clientset.CoreV1().Secrets(namespace)) 104 d.Log = debug 105 store = storage.Init(d) 106 case "configmap", "configmaps": 107 d := driver.NewConfigMaps(clientset.CoreV1().ConfigMaps(namespace)) 108 d.Log = debug 109 store = storage.Init(d) 110 case "memory": 111 d := driver.NewMemory() 112 store = storage.Init(d) 113 default: 114 // Not sure what to do here. 115 panic("Unknown driver in HELM_DRIVER: " + os.Getenv("HELM_DRIVER")) 116 } 117 118 actionConfig.RESTClientGetter = kubeConfig() 119 actionConfig.KubeClient = kc 120 actionConfig.Releases = store 121 actionConfig.Log = debug 122 } 123 124 func kubeConfig() genericclioptions.RESTClientGetter { 125 configOnce.Do(func() { 126 config = kube.GetConfig(settings.KubeConfig, settings.KubeContext, settings.Namespace) 127 }) 128 return config 129 } 130 131 func getNamespace() string { 132 if settings.Namespace != "" { 133 return settings.Namespace 134 } 135 136 if ns, _, err := kubeConfig().ToRawKubeConfigLoader().Namespace(); err == nil { 137 return ns 138 } 139 return "default" 140 } 141 142 // wordSepNormalizeFunc changes all flags that contain "_" separators 143 func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName { 144 return pflag.NormalizedName(strings.ReplaceAll(name, "_", "-")) 145 } 146 147 func checkOCIFeatureGate() func(_ *cobra.Command, _ []string) error { 148 return func(_ *cobra.Command, _ []string) error { 149 if !FeatureGateOCI.IsEnabled() { 150 return FeatureGateOCI.Error() 151 } 152 return nil 153 } 154 }