github.com/lrills/helm@v2.8.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 } 59 60 func newVersionCmd(c helm.Interface, out io.Writer) *cobra.Command { 61 version := &versionCmd{ 62 client: c, 63 out: out, 64 } 65 66 cmd := &cobra.Command{ 67 Use: "version", 68 Short: "print the client/server version information", 69 Long: versionDesc, 70 RunE: func(cmd *cobra.Command, args []string) error { 71 // If neither is explicitly set, show both. 72 if !version.showClient && !version.showServer { 73 version.showClient, version.showServer = true, true 74 } 75 if version.showServer { 76 // We do this manually instead of in PreRun because we only 77 // need a tunnel if server version is requested. 78 setupConnection(cmd, args) 79 } 80 version.client = ensureHelmClient(version.client) 81 return version.run() 82 }, 83 } 84 f := cmd.Flags() 85 f.BoolVarP(&version.showClient, "client", "c", false, "client version only") 86 f.BoolVarP(&version.showServer, "server", "s", false, "server version only") 87 f.BoolVar(&version.short, "short", false, "print the version number") 88 89 return cmd 90 } 91 92 func (v *versionCmd) run() error { 93 if v.showClient { 94 cv := version.GetVersionProto() 95 fmt.Fprintf(v.out, "Client: %s\n", formatVersion(cv, v.short)) 96 } 97 98 if !v.showServer { 99 return nil 100 } 101 102 if settings.Debug { 103 k8sVersion, err := getK8sVersion() 104 if err != nil { 105 return err 106 } 107 fmt.Fprintf(v.out, "Kubernetes: %#v\n", k8sVersion) 108 } 109 110 resp, err := v.client.GetVersion() 111 if err != nil { 112 if grpc.Code(err) == codes.Unimplemented { 113 return errors.New("server is too old to know its version") 114 } 115 debug("%s", err) 116 return errors.New("cannot connect to Tiller") 117 } 118 fmt.Fprintf(v.out, "Server: %s\n", formatVersion(resp.Version, v.short)) 119 return nil 120 } 121 122 func getK8sVersion() (*apiVersion.Info, error) { 123 var v *apiVersion.Info 124 _, client, err := getKubeClient(settings.KubeContext) 125 if err != nil { 126 return v, err 127 } 128 v, err = client.Discovery().ServerVersion() 129 return v, err 130 } 131 132 func formatVersion(v *pb.Version, short bool) string { 133 if short { 134 return fmt.Sprintf("%s+g%s", v.SemVer, v.GitCommit[:7]) 135 } 136 return fmt.Sprintf("%#v", v) 137 }