github.com/valdemarpavesi/helm@v2.9.1+incompatible/cmd/helm/version.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 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 if version.showServer { 77 // We do this manually instead of in PreRun because we only 78 // need a tunnel if server version is requested. 79 setupConnection() 80 } 81 version.client = ensureHelmClient(version.client) 82 return version.run() 83 }, 84 } 85 f := cmd.Flags() 86 f.BoolVarP(&version.showClient, "client", "c", false, "client version only") 87 f.BoolVarP(&version.showServer, "server", "s", false, "server version only") 88 f.BoolVar(&version.short, "short", false, "print the version number") 89 f.StringVar(&version.template, "template", "", "template for version string format") 90 91 return cmd 92 } 93 94 func (v *versionCmd) run() error { 95 // Store map data for template rendering 96 data := map[string]interface{}{} 97 98 if v.showClient { 99 cv := version.GetVersionProto() 100 if v.template != "" { 101 data["Client"] = cv 102 } else { 103 fmt.Fprintf(v.out, "Client: %s\n", formatVersion(cv, v.short)) 104 } 105 } 106 107 if !v.showServer { 108 return tpl(v.template, data, v.out) 109 } 110 111 if settings.Debug { 112 k8sVersion, err := getK8sVersion() 113 if err != nil { 114 return err 115 } 116 fmt.Fprintf(v.out, "Kubernetes: %#v\n", k8sVersion) 117 } 118 119 resp, err := v.client.GetVersion() 120 if err != nil { 121 if grpc.Code(err) == codes.Unimplemented { 122 return errors.New("server is too old to know its version") 123 } 124 debug("%s", err) 125 return errors.New("cannot connect to Tiller") 126 } 127 128 if v.template != "" { 129 data["Server"] = resp.Version 130 } else { 131 fmt.Fprintf(v.out, "Server: %s\n", formatVersion(resp.Version, v.short)) 132 } 133 return tpl(v.template, data, v.out) 134 } 135 136 func getK8sVersion() (*apiVersion.Info, error) { 137 var v *apiVersion.Info 138 _, client, err := getKubeClient(settings.KubeContext) 139 if err != nil { 140 return v, err 141 } 142 v, err = client.Discovery().ServerVersion() 143 return v, err 144 } 145 146 func formatVersion(v *pb.Version, short bool) string { 147 if short { 148 return fmt.Sprintf("%s+g%s", v.SemVer, v.GitCommit[:7]) 149 } 150 return fmt.Sprintf("%#v", v) 151 }