/
proxystatus.go
185 lines (165 loc) · 6.19 KB
/
proxystatus.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
// 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 proxystatus
import (
"context"
"fmt"
"io"
"os"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
"github.com/spf13/cobra"
"istio.io/istio/istioctl/pkg/cli"
"istio.io/istio/istioctl/pkg/clioptions"
"istio.io/istio/istioctl/pkg/completion"
"istio.io/istio/istioctl/pkg/multixds"
"istio.io/istio/istioctl/pkg/util/ambient"
"istio.io/istio/istioctl/pkg/writer/compare"
"istio.io/istio/istioctl/pkg/writer/pilot"
pilotxds "istio.io/istio/pilot/pkg/xds"
"istio.io/istio/pkg/log"
)
var configDumpFile string
func readConfigFile(filename string) ([]byte, error) {
file := os.Stdin
if filename != "-" {
var err error
file, err = os.Open(filename)
if err != nil {
return nil, err
}
}
defer func() {
if err := file.Close(); err != nil {
log.Errorf("failed to close %s: %s", filename, err)
}
}()
data, err := io.ReadAll(file)
if err != nil {
return nil, err
}
return data, nil
}
func StableXdsStatusCommand(ctx cli.Context) *cobra.Command {
cmd := XdsStatusCommand(ctx)
unstableFlags := []string{"xds-via-agents", "xds-via-agents-limit"}
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
for _, flag := range unstableFlags {
if cmd.PersistentFlags().Changed(flag) {
return fmt.Errorf("--%s is experimental. Use `istioctl experimental ps --%s`", flag, flag)
}
}
return nil
}
for _, flag := range unstableFlags {
_ = cmd.PersistentFlags().MarkHidden(flag)
}
return cmd
}
func XdsStatusCommand(ctx cli.Context) *cobra.Command {
var opts clioptions.ControlPlaneOptions
var centralOpts clioptions.CentralControlPlaneOptions
var multiXdsOpts multixds.Options
statusCmd := &cobra.Command{
Use: "proxy-status [<type>/]<name>[.<namespace>]",
Short: "Retrieves the synchronization status of each Envoy in the mesh",
Long: `
Retrieves last sent and last acknowledged xDS sync from Istiod to each Envoy in the mesh
`,
Example: ` # Retrieve sync status for all Envoys in a mesh
istioctl proxy-status
# Retrieve sync status for Envoys in a specific namespace
istioctl proxy-status --namespace foo
# Retrieve sync diff for a single Envoy and Istiod
istioctl proxy-status istio-egressgateway-59585c5b9c-ndc59.istio-system
# SECURITY OPTIONS
# Retrieve proxy status information directly from the control plane, using token security
# (This is the usual way to get the proxy-status with an out-of-cluster control plane.)
istioctl ps --xds-address istio.cloudprovider.example.com:15012
# Retrieve proxy status information via Kubernetes config, using token security
# (This is the usual way to get the proxy-status with an in-cluster control plane.)
istioctl proxy-status
# Retrieve proxy status 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 ps --xds-address istio.example.com:15012 --cert-dir ~/.istio-certs
# Retrieve proxy status 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 ps --xds-label istio.io/rev=default
`,
Aliases: []string{"ps"},
RunE: func(c *cobra.Command, args []string) error {
kubeClient, err := ctx.CLIClientWithRevision(opts.Revision)
if err != nil {
return err
}
multiXdsOpts.MessageWriter = c.OutOrStdout()
if len(args) > 0 {
podName, ns, err := ctx.InferPodInfoFromTypedResource(args[0], ctx.Namespace())
if err != nil {
return err
}
if ambient.IsZtunnelPod(kubeClient, podName, ns) {
_, _ = fmt.Fprintf(c.OutOrStdout(),
"Sync diff is not available for ztunnel pod %s.%s\n", podName, ns)
return nil
}
var envoyDump []byte
if configDumpFile != "" {
envoyDump, err = readConfigFile(configDumpFile)
} else {
path := "config_dump"
envoyDump, err = kubeClient.EnvoyDo(context.TODO(), podName, ns, "GET", path)
}
if err != nil {
return fmt.Errorf("could not contact sidecar: %w", err)
}
xdsRequest := discovery.DiscoveryRequest{
ResourceNames: []string{fmt.Sprintf("%s.%s", podName, ns)},
TypeUrl: pilotxds.TypeDebugConfigDump,
}
xdsResponses, err := multixds.FirstRequestAndProcessXds(&xdsRequest, centralOpts, ctx.IstioNamespace(), "", "", kubeClient, multiXdsOpts)
if err != nil {
return err
}
c, err := compare.NewXdsComparator(c.OutOrStdout(), xdsResponses, envoyDump)
if err != nil {
return err
}
return c.Diff()
}
xdsRequest := discovery.DiscoveryRequest{
TypeUrl: pilotxds.TypeDebugSyncronization,
}
xdsResponses, err := multixds.AllRequestAndProcessXds(&xdsRequest, centralOpts, ctx.IstioNamespace(), "", "", kubeClient, multiXdsOpts)
if err != nil {
return err
}
sw := pilot.XdsStatusWriter{
Writer: c.OutOrStdout(),
Namespace: ctx.Namespace(),
}
return sw.PrintAll(xdsResponses)
},
ValidArgsFunction: completion.ValidPodsNameArgs(ctx),
}
opts.AttachControlPlaneFlags(statusCmd)
centralOpts.AttachControlPlaneFlags(statusCmd)
statusCmd.PersistentFlags().StringVarP(&configDumpFile, "file", "f", "",
"Envoy config dump JSON file")
statusCmd.PersistentFlags().BoolVar(&multiXdsOpts.XdsViaAgents, "xds-via-agents", false,
"Access Istiod via the tap service of each agent")
statusCmd.PersistentFlags().IntVar(&multiXdsOpts.XdsViaAgentsLimit, "xds-via-agents-limit", 100,
"Maximum number of pods being visited by istioctl when `xds-via-agent` flag is true."+
"To iterate all the agent pods without limit, set to 0")
return statusCmd
}