forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
191 lines (162 loc) · 5.65 KB
/
version.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/openservicemesh/osm/pkg/constants"
httpserverconstants "github.com/openservicemesh/osm/pkg/httpserver/constants"
"github.com/openservicemesh/osm/pkg/version"
)
const versionHelp = `
This command prints the OSM CLI and remote mesh version information
`
type versionCmd struct {
out io.Writer
namespace string
clientOnly bool
versionOnly bool
config *rest.Config
clientset kubernetes.Interface
remoteVersion remoteVersionGetter
}
type remoteVersionGetter interface {
proxyGetMeshVersion(pod string, namespace string, clientset kubernetes.Interface) (*version.Info, error)
}
type remoteVersion struct{}
type remoteVersionInfo struct {
meshName string
namespace string
version *version.Info
}
type versionInfo struct {
cliVersionInfo *version.Info
remoteVersionInfoList []*remoteVersionInfo
}
func newVersionCmd(out io.Writer) *cobra.Command {
versionCmd := &versionCmd{
out: out,
}
cmd := &cobra.Command{
Use: "version",
Short: "osm cli and mesh version",
Long: versionHelp,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var verInfo versionInfo
var multiError *multierror.Error
versionCmd.remoteVersion = &remoteVersion{}
cliVersionInfo := version.GetInfo()
verInfo.cliVersionInfo = &cliVersionInfo
fmt.Fprintf(versionCmd.out, "CLI Version: %#v\n", *verInfo.cliVersionInfo)
if versionCmd.clientOnly {
return nil
}
if err := versionCmd.setKubeClientset(); err != nil {
return err
}
meshInfoList, err := getMeshInfoList(versionCmd.config, versionCmd.clientset)
if err != nil {
return errors.Wrapf(err, "unable to list meshes within the cluster")
}
for _, m := range meshInfoList {
versionCmd.namespace = m.namespace
meshVer, err := versionCmd.getMeshVersion()
if err != nil {
multiError = multierror.Append(multiError, errors.Wrap(err, fmt.Sprintf("Failed to get mesh version for mesh %s in namespace %s", m.name, m.namespace)))
}
verInfo.remoteVersionInfoList = append(verInfo.remoteVersionInfoList, meshVer)
}
w := newTabWriter(versionCmd.out)
fmt.Fprint(w, versionCmd.outputPrettyVersionInfo(verInfo.remoteVersionInfoList))
_ = w.Flush()
if !settings.IsManaged() && !versionCmd.versionOnly {
latestReleaseVersion, err := getLatestReleaseVersion()
if err != nil {
multiError = multierror.Append(multiError, errors.Wrapf(err, "Failed to get latest release information"))
} else if err := outputLatestReleaseVersion(versionCmd.out, latestReleaseVersion, cliVersionInfo.Version); err != nil {
multiError = multierror.Append(multiError, errors.Wrapf(err, "Failed to output latest release information"))
}
}
return multiError.ErrorOrNil()
},
}
f := cmd.Flags()
f.BoolVar(&versionCmd.clientOnly, "client-only", false, "only show the OSM CLI version")
f.BoolVar(&versionCmd.versionOnly, "version-only", false, "only show the OSM version information. Hide warnings and upgrade notifications")
return cmd
}
func (v *versionCmd) setKubeClientset() error {
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return errors.Wrap(err, "Error fetching kubeconfig")
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "Could not access Kubernetes cluster, check kubeconfig")
}
v.clientset = clientset
return nil
}
func (v *versionCmd) getMeshVersion() (*remoteVersionInfo, error) {
var version *version.Info
var versionInfo *remoteVersionInfo
controllerPods, err := getControllerPods(v.clientset, v.namespace)
if err != nil {
return nil, err
}
if len(controllerPods.Items) == 0 {
return &remoteVersionInfo{}, nil
}
controllerPod := controllerPods.Items[0]
version, err = v.remoteVersion.proxyGetMeshVersion(controllerPod.Name, v.namespace, v.clientset)
if err != nil {
return nil, err
}
versionInfo = &remoteVersionInfo{
meshName: controllerPod.Labels[constants.OSMAppInstanceLabelKey],
namespace: v.namespace,
version: version,
}
return versionInfo, nil
}
func (r *remoteVersion) proxyGetMeshVersion(pod string, namespace string, clientset kubernetes.Interface) (*version.Info, error) {
resp, err := clientset.CoreV1().Pods(namespace).ProxyGet("", pod, strconv.Itoa(constants.OSMHTTPServerPort), httpserverconstants.VersionPath, nil).DoRaw(context.TODO())
if err != nil {
return nil, errors.Wrapf(err, "Error retrieving mesh version from pod [%s] in namespace [%s]", pod, namespace)
}
if len(resp) == 0 {
return nil, errors.Errorf("Empty response received from pod [%s] in namespace [%s]", pod, namespace)
}
versionInfo := &version.Info{}
err = json.Unmarshal(resp, versionInfo)
if err != nil {
return nil, errors.Wrapf(err, "Error unmarshalling retrieved mesh version from pod [%s] in namespace [%s]", pod, namespace)
}
return versionInfo, nil
}
func (v *versionCmd) outputPrettyVersionInfo(remoteVerList []*remoteVersionInfo) string {
if len(remoteVerList) == 0 {
return "Unable to find OSM control plane in the cluster\n"
}
table := "\nMESH NAME\tMESH NAMESPACE\tVERSION\tGIT COMMIT\tBUILD DATE\n"
for _, remoteVersionInfo := range remoteVerList {
if remoteVersionInfo != nil && remoteVersionInfo.meshName != "" {
table += fmt.Sprintf(
"%s\t%s\t%s\t%s\t%s\n",
remoteVersionInfo.meshName,
remoteVersionInfo.namespace,
remoteVersionInfo.version.Version,
remoteVersionInfo.version.GitCommit,
remoteVersionInfo.version.BuildDate,
)
}
}
return table
}