github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/cmd/helm/version.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 18 19 import ( 20 "errors" 21 "fmt" 22 "io" 23 24 "github.com/spf13/cobra" 25 "google.golang.org/grpc" 26 "google.golang.org/grpc/codes" 27 28 apiVersion "k8s.io/apimachinery/pkg/version" 29 "k8s.io/helm/pkg/helm" 30 pb "k8s.io/helm/pkg/proto/hapi/version" 31 "k8s.io/helm/pkg/version" 32 ) 33 34 const versionDesc = ` 35 Show the client and server versions for Helm and tiller. 36 37 This will print a representation of the client and server versions of Helm and 38 Tiller. The output will look something like this: 39 40 Client: &version.Version{SemVer:"v2.0.0", GitCommit:"ff52399e51bb880526e9cd0ed8386f6433b74da1", GitTreeState:"clean"} 41 Server: &version.Version{SemVer:"v2.0.0", GitCommit:"b0c113dfb9f612a9add796549da66c0d294508a3", GitTreeState:"clean"} 42 43 - SemVer is the semantic version of the release. 44 - GitCommit is the SHA for the commit that this version was built from. 45 - GitTreeState is "clean" if there are no local code changes when this binary was 46 built, and "dirty" if the binary was built from locally modified code. 47 48 To print just the client version, use '--client'. To print just the server version, 49 use '--server'. 50 ` 51 52 type versionCmd struct { 53 out io.Writer 54 client helm.Interface 55 showClient bool 56 showServer bool 57 short bool 58 template string 59 } 60 61 func newVersionCmd(c helm.Interface, out io.Writer) *cobra.Command { 62 version := &versionCmd{ 63 client: c, 64 out: out, 65 } 66 67 cmd := &cobra.Command{ 68 Use: "version", 69 Short: "Print the client/server version information", 70 Long: versionDesc, 71 RunE: func(cmd *cobra.Command, args []string) error { 72 // If neither is explicitly set, show both. 73 if !version.showClient && !version.showServer { 74 version.showClient, version.showServer = true, true 75 } 76 return version.run() 77 }, 78 } 79 f := cmd.Flags() 80 settings.AddFlagsTLS(f) 81 f.BoolVarP(&version.showClient, "client", "c", false, "Client version only") 82 f.BoolVarP(&version.showServer, "server", "s", false, "Server version only") 83 f.BoolVar(&version.short, "short", false, "Print the version number") 84 f.StringVar(&version.template, "template", "", "Template for version string format") 85 86 // set defaults from environment 87 settings.InitTLS(f) 88 89 return cmd 90 } 91 92 func (v *versionCmd) run() error { 93 // Store map data for template rendering 94 data := map[string]interface{}{} 95 96 if v.showClient { 97 cv := version.GetVersionProto() 98 if v.template != "" { 99 data["Client"] = cv 100 } else { 101 fmt.Fprintf(v.out, "Client: %s\n", formatVersion(cv, v.short)) 102 } 103 } 104 105 if !v.showServer { 106 return tpl(v.template, data, v.out) 107 } 108 109 // We do this manually instead of in PreRun because we only 110 // need a tunnel if server version is requested. 111 if err := setupConnection(); err != nil { 112 return err 113 } 114 v.client = ensureHelmClient(v.client) 115 116 if settings.Debug { 117 k8sVersion, err := getK8sVersion() 118 if err != nil { 119 return err 120 } 121 fmt.Fprintf(v.out, "Kubernetes: %#v\n", k8sVersion) 122 } 123 resp, err := v.client.GetVersion() 124 if err != nil { 125 if grpc.Code(err) == codes.Unimplemented { 126 return errors.New("server is too old to know its version") 127 } 128 debug("%s", err) 129 return errors.New("cannot connect to Tiller") 130 } 131 132 if v.template != "" { 133 data["Server"] = resp.Version 134 } else { 135 fmt.Fprintf(v.out, "Server: %s\n", formatVersion(resp.Version, v.short)) 136 } 137 return tpl(v.template, data, v.out) 138 } 139 140 func getK8sVersion() (*apiVersion.Info, error) { 141 var v *apiVersion.Info 142 _, client, err := getKubeClient(settings.KubeContext, settings.KubeConfig) 143 if err != nil { 144 return v, err 145 } 146 v, err = client.Discovery().ServerVersion() 147 return v, err 148 } 149 150 func formatVersion(v *pb.Version, short bool) string { 151 if short && v.GitCommit != "" { 152 return fmt.Sprintf("%s+g%s", v.SemVer, v.GitCommit[:7]) 153 } 154 return fmt.Sprintf("&version.Version{SemVer:\"%s\", GitCommit:\"%s\", GitTreeState:\"%s\"}", v.SemVer, v.GitCommit, v.GitTreeState) 155 }