forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.go
More file actions
158 lines (137 loc) · 4.38 KB
/
version.go
File metadata and controls
158 lines (137 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"sigs.k8s.io/yaml"
"github.com/spf13/cobra"
"k8s.io/kops"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
versionLong = templates.LongDesc(i18n.T(`
Print the kOps version and git SHA.`))
versionExample = templates.Examples(i18n.T(`
kops version`))
versionShort = i18n.T(`Print the kOps version information.`)
)
// NewCmdVersion builds a cobra command for the kops version command
func NewCmdVersion(f *util.Factory, out io.Writer) *cobra.Command {
options := &VersionOptions{}
cmd := &cobra.Command{
Use: "version",
Short: versionShort,
Long: versionLong,
Example: versionExample,
Args: rootCommand.clusterNameArgsAllowNoCluster(&options.ClusterName),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
return RunVersion(f, out, options)
},
}
cmd.Flags().BoolVar(&options.short, "short", options.short, "only print the main kOps version. Useful for scripting.")
cmd.Flags().BoolVar(&options.server, "server", options.server, "show the kOps version that made the last change to the state store.")
cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "One of 'yaml' or 'json'.")
return cmd
}
type VersionOptions struct {
short bool
server bool
ClusterName string
Output string
}
// Version is a struct for version information
type Version struct {
ClientVersion *kops.Info `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"`
ServerVersion string `json:"serverVersion,omitempty" yaml:"serverVersion,omitempty"`
}
// RunVersion implements the version command logic
func RunVersion(f *util.Factory, out io.Writer, options *VersionOptions) error {
var versionInfo Version
clientVersion := kops.Get()
versionInfo.ClientVersion = &clientVersion
if options.server {
versionInfo.ServerVersion = serverVersion(f, options)
}
switch options.Output {
case "":
if options.short {
_, err := fmt.Fprintf(out, "%s\n", versionInfo.ClientVersion.Version)
if err != nil {
return err
}
if options.server {
_, err := fmt.Fprintf(out, "%s\n", versionInfo.ServerVersion)
return err
}
return nil
} else {
client := versionInfo.ClientVersion.Version
if versionInfo.ClientVersion.GitVersion != "" {
client += " (git-" + versionInfo.ClientVersion.GitVersion + ")"
}
{
_, err := fmt.Fprintf(out, "Client Version: %s\n", client)
if err != nil {
return err
}
}
if options.server {
_, err := fmt.Fprintf(out, "Last applied server version: %s\n", versionInfo.ServerVersion)
return err
}
return nil
}
case OutputYaml:
marshalled, err := yaml.Marshal(&versionInfo)
if err != nil {
return err
}
_, err = fmt.Fprintln(out, string(marshalled))
return err
case OutputJSON:
marshalled, err := json.MarshalIndent(&versionInfo, "", " ")
if err != nil {
return err
}
_, err = fmt.Fprintln(out, string(marshalled))
return err
default:
return fmt.Errorf("VersionOptions were not validated: --output=%q should have been rejected", options.Output)
}
}
func serverVersion(f *util.Factory, options *VersionOptions) string {
if options.ClusterName == "" {
return "No cluster selected"
}
ctx := context.Background()
cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return "could not fetch cluster"
}
configBase, err := f.VFSContext().BuildVfsPath(cluster.Spec.ConfigStore.Base)
if err != nil {
return "could not talk to vfs"
}
kopsVersionUpdatedBytes, err := configBase.Join(registry.PathKopsVersionUpdated).ReadFile(ctx)
if err != nil {
return "could get cluster version"
}
return string(kopsVersionUpdatedBytes)
}