github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/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 cmd // import "helm.sh/helm/v3/cmd/helm" 18 19 import ( 20 "fmt" 21 "io" 22 "log" 23 "os" 24 "strings" 25 26 "github.com/spf13/cobra" 27 "sigs.k8s.io/yaml" 28 29 // Import to initialize client auth plugins. 30 _ "k8s.io/client-go/plugin/pkg/client/auth" 31 32 "helm.sh/helm/v3/pkg/action" 33 "helm.sh/helm/v3/pkg/cli" 34 "helm.sh/helm/v3/pkg/kube" 35 kubefake "helm.sh/helm/v3/pkg/kube/fake" 36 "helm.sh/helm/v3/pkg/release" 37 "helm.sh/helm/v3/pkg/storage/driver" 38 ) 39 40 var settings = cli.New() 41 42 func init() { 43 log.SetFlags(log.Lshortfile) 44 } 45 46 func debug(format string, v ...interface{}) { 47 if settings.Debug { 48 format = fmt.Sprintf("[debug] %s\n", format) 49 log.Output(2, fmt.Sprintf(format, v...)) 50 } 51 } 52 53 func warning(format string, v ...interface{}) { 54 format = fmt.Sprintf("WARNING: %s\n", format) 55 fmt.Fprintf(os.Stderr, format, v...) 56 } 57 58 func main() { 59 // Setting the name of the app for managedFields in the Kubernetes client. 60 // It is set here to the full name of "helm" so that renaming of helm to 61 // another name (e.g., helm2 or helm3) does not change the name of the 62 // manager as picked up by the automated name detection. 63 kube.ManagedFieldsManager = "helm" 64 65 actionConfig := new(action.Configuration) 66 cmd, err := NewRootCmd(actionConfig, os.Stdout, os.Args[1:]) 67 if err != nil { 68 warning("%+v", err) 69 os.Exit(1) 70 } 71 72 // run when each command's execute method is called 73 cobra.OnInitialize(func() { 74 helmDriver := os.Getenv("HELM_DRIVER") 75 if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), helmDriver, debug); err != nil { 76 log.Fatal(err) 77 } 78 if helmDriver == "memory" { 79 loadReleasesInMemory(actionConfig) 80 } 81 }) 82 83 if err := cmd.Execute(); err != nil { 84 debug("%+v", err) 85 switch e := err.(type) { 86 case pluginError: 87 os.Exit(e.code) 88 default: 89 os.Exit(1) 90 } 91 } 92 } 93 94 // This function loads releases into the memory storage if the 95 // environment variable is properly set. 96 func loadReleasesInMemory(actionConfig *action.Configuration) { 97 filePaths := strings.Split(os.Getenv("HELM_MEMORY_DRIVER_DATA"), ":") 98 if len(filePaths) == 0 { 99 return 100 } 101 102 store := actionConfig.Releases 103 mem, ok := store.Driver.(*driver.Memory) 104 if !ok { 105 // For an unexpected reason we are not dealing with the memory storage driver. 106 return 107 } 108 109 actionConfig.KubeClient = &kubefake.PrintingKubeClient{Out: io.Discard} 110 111 for _, path := range filePaths { 112 b, err := os.ReadFile(path) 113 if err != nil { 114 log.Fatal("Unable to read memory driver data", err) 115 } 116 117 releases := []*release.Release{} 118 if err := yaml.Unmarshal(b, &releases); err != nil { 119 log.Fatal("Unable to unmarshal memory driver data: ", err) 120 } 121 122 for _, rel := range releases { 123 if err := store.Create(rel); err != nil { 124 log.Fatal(err) 125 } 126 } 127 } 128 // Must reset namespace to the proper one 129 mem.SetNamespace(settings.Namespace()) 130 }