-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
version.go
242 lines (224 loc) · 8.33 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright Istio 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 version
import (
"context"
"encoding/json"
"fmt"
"os"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
statusv3 "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"google.golang.org/protobuf/types/known/structpb"
"istio.io/istio/istioctl/pkg/cli"
"istio.io/istio/istioctl/pkg/clioptions"
"istio.io/istio/istioctl/pkg/multixds"
"istio.io/istio/operator/cmd/mesh"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/xds"
"istio.io/istio/pkg/proxy"
istioVersion "istio.io/istio/pkg/version"
)
func NewVersionCommand(ctx cli.Context) *cobra.Command {
profileCmd := mesh.ProfileCmd(ctx)
var opts clioptions.ControlPlaneOptions
versionCmd := istioVersion.CobraCommandWithOptions(istioVersion.CobraOptions{
GetRemoteVersion: getRemoteInfoWrapper(ctx, &profileCmd, &opts),
GetProxyVersions: getProxyInfoWrapper(ctx, &opts),
})
opts.AttachControlPlaneFlags(versionCmd)
versionCmd.Flags().VisitAll(func(flag *pflag.Flag) {
if flag.Name == "short" {
err := flag.Value.Set("true")
if err != nil {
fmt.Fprintf(os.Stdout, "set flag %q as true failed due to error %v", flag.Name, err)
}
}
if flag.Name == "remote" {
err := flag.Value.Set("true")
if err != nil {
fmt.Fprintf(os.Stdout, "set flag %q as true failed due to error %v", flag.Name, err)
}
}
})
return versionCmd
}
func getRemoteInfo(ctx cli.Context, opts clioptions.ControlPlaneOptions) (*istioVersion.MeshInfo, error) {
kubeClient, err := ctx.CLIClientWithRevision(opts.Revision)
if err != nil {
return nil, err
}
return kubeClient.GetIstioVersions(context.TODO(), ctx.IstioNamespace())
}
func getRemoteInfoWrapper(ctx cli.Context, pc **cobra.Command, opts *clioptions.ControlPlaneOptions) func() (*istioVersion.MeshInfo, error) {
return func() (*istioVersion.MeshInfo, error) {
remInfo, err := getRemoteInfo(ctx, *opts)
if err != nil {
fmt.Fprintf((*pc).OutOrStderr(), "%v\n", err)
// Return nil so that the client version is printed
return nil, nil
}
if remInfo == nil {
fmt.Fprintf((*pc).OutOrStderr(), "Istio is not present in the cluster with namespace %q\n", ctx.IstioNamespace())
}
return remInfo, err
}
}
func getProxyInfoWrapper(ctx cli.Context, opts *clioptions.ControlPlaneOptions) func() (*[]istioVersion.ProxyInfo, error) {
return func() (*[]istioVersion.ProxyInfo, error) {
client, err := ctx.CLIClientWithRevision(opts.Revision)
if err != nil {
return nil, err
}
return proxy.GetProxyInfo(client, ctx.IstioNamespace())
}
}
// XdsVersionCommand gets the Control Plane and Sidecar versions via XDS
func XdsVersionCommand(ctx cli.Context) *cobra.Command {
var opts clioptions.ControlPlaneOptions
var centralOpts clioptions.CentralControlPlaneOptions
var xdsResponses *discovery.DiscoveryResponse
versionCmd := istioVersion.CobraCommandWithOptions(istioVersion.CobraOptions{
GetRemoteVersion: xdsRemoteVersionWrapper(ctx, &opts, ¢ralOpts, &xdsResponses),
GetProxyVersions: xdsProxyVersionWrapper(&xdsResponses),
})
opts.AttachControlPlaneFlags(versionCmd)
centralOpts.AttachControlPlaneFlags(versionCmd)
versionCmd.Args = func(c *cobra.Command, args []string) error {
if err := cobra.NoArgs(c, args); err != nil {
return err
}
if err := centralOpts.ValidateControlPlaneFlags(); err != nil {
return err
}
return nil
}
versionCmd.Example = ` # Retrieve version information directly from the control plane, using token security
# (This is the usual way to get the control plane version with an out-of-cluster control plane.)
istioctl x version --xds-address istio.cloudprovider.example.com:15012
# Retrieve version information via Kubernetes config, using token security
# (This is the usual way to get the control plane version with an in-cluster control plane.)
istioctl x version
# Retrieve version information directly from the control plane, using RSA certificate security
# (Certificates must be obtained before this step. The --cert-dir flag lets istioctl bypass the Kubernetes API server.)
istioctl x version --xds-address istio.example.com:15012 --cert-dir ~/.istio-certs
# Retrieve version information via XDS from specific control plane in multi-control plane in-cluster configuration
# (Select a specific control plane in an in-cluster canary Istio configuration.)
istioctl x version --xds-label istio.io/rev=default
`
versionCmd.Flags().VisitAll(func(flag *pflag.Flag) {
if flag.Name == "short" {
err := flag.Value.Set("true")
if err != nil {
fmt.Fprintf(os.Stdout, "set flag %q as true failed due to error %v", flag.Name, err)
}
}
if flag.Name == "remote" {
err := flag.Value.Set("true")
if err != nil {
fmt.Fprintf(os.Stdout, "set flag %q as true failed due to error %v", flag.Name, err)
}
}
})
return versionCmd
}
// xdsRemoteVersionWrapper uses outXDS to share the XDS response with xdsProxyVersionWrapper.
// (Screwy API on istioVersion.CobraCommandWithOptions)
// nolint: lll
func xdsRemoteVersionWrapper(ctx cli.Context, opts *clioptions.ControlPlaneOptions, centralOpts *clioptions.CentralControlPlaneOptions, outXDS **discovery.DiscoveryResponse) func() (*istioVersion.MeshInfo, error) {
return func() (*istioVersion.MeshInfo, error) {
xdsRequest := discovery.DiscoveryRequest{
TypeUrl: xds.TypeDebugSyncronization,
}
kubeClient, err := ctx.CLIClientWithRevision(opts.Revision)
if err != nil {
return nil, err
}
xdsResponse, err := multixds.RequestAndProcessXds(&xdsRequest, *centralOpts, ctx.IstioNamespace(), kubeClient)
if err != nil {
return nil, err
}
*outXDS = xdsResponse
if xdsResponse.ControlPlane == nil {
return &istioVersion.MeshInfo{
istioVersion.ServerInfo{
Component: "MISSING CP ID",
Info: istioVersion.BuildInfo{
Version: "MISSING CP ID",
},
Revision: "MISSING CP ID",
},
}, nil
}
cpID := xds.IstioControlPlaneInstance{}
err = json.Unmarshal([]byte(xdsResponse.ControlPlane.Identifier), &cpID)
if err != nil {
return nil, fmt.Errorf("could not parse CP Identifier: %w", err)
}
return &istioVersion.MeshInfo{
istioVersion.ServerInfo{
Component: cpID.Component,
Info: cpID.Info,
Revision: opts.Revision,
},
}, nil
}
}
func xdsProxyVersionWrapper(xdsResponse **discovery.DiscoveryResponse) func() (*[]istioVersion.ProxyInfo, error) {
return func() (*[]istioVersion.ProxyInfo, error) {
pi := []istioVersion.ProxyInfo{}
for _, resource := range (*xdsResponse).Resources {
switch resource.TypeUrl {
case "type.googleapis.com/envoy.config.core.v3.Node":
node := core.Node{}
err := resource.UnmarshalTo(&node)
if err != nil {
return nil, fmt.Errorf("could not unmarshal Node: %w", err)
}
meta, err := model.ParseMetadata(node.Metadata)
if err != nil || meta.ProxyConfig == nil {
// Skip non-sidecars (e.g. istioctl queries)
continue
}
pi = append(pi, istioVersion.ProxyInfo{
ID: node.Id,
IstioVersion: getIstioVersionFromXdsMetadata(node.Metadata),
})
case "type.googleapis.com/envoy.service.status.v3.ClientConfig":
cc := statusv3.ClientConfig{}
err := resource.UnmarshalTo(&cc)
if err != nil {
return nil, fmt.Errorf("could not unmarshal Node: %w", err)
}
node := cc.Node
pi = append(pi, istioVersion.ProxyInfo{
ID: node.Id,
IstioVersion: getIstioVersionFromXdsMetadata(node.Metadata),
})
default:
return nil, fmt.Errorf("unexpected resource type %q", resource.TypeUrl)
}
}
return &pi, nil
}
}
func getIstioVersionFromXdsMetadata(metadata *structpb.Struct) string {
meta, err := model.ParseMetadata(metadata)
if err != nil {
return "unknown sidecar version"
}
return meta.IstioVersion
}