/
root.go
391 lines (329 loc) · 11.8 KB
/
root.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// 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 cmd
import (
"errors"
goflag "flag"
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/viper"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"istio.io/istio/istioctl/pkg/install"
"istio.io/istio/istioctl/pkg/multicluster"
"istio.io/istio/istioctl/pkg/validate"
"istio.io/istio/operator/cmd/mesh"
"istio.io/istio/pkg/cmd"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/tools/bug-report/pkg/bugreport"
"istio.io/pkg/collateral"
"istio.io/pkg/env"
"istio.io/pkg/log"
)
// CommandParseError distinguishes an error parsing istioctl CLI arguments from an error processing
type CommandParseError struct {
e error
}
func (c CommandParseError) Error() string {
return c.e.Error()
}
const (
// Location to read istioctl defaults from
defaultIstioctlConfig = "$HOME/.istioctl/config.yaml"
// ExperimentalMsg indicate active development and not for production use warning.
ExperimentalMsg = `THIS COMMAND IS UNDER ACTIVE DEVELOPMENT AND NOT READY FOR PRODUCTION USE.`
)
var (
// IstioConfig is the name of the istioctl config file (if any)
IstioConfig = env.RegisterStringVar("ISTIOCONFIG", defaultIstioctlConfig,
"Default values for istioctl flags").Get()
kubeconfig string
configContext string
namespace string
istioNamespace string
defaultNamespace string
// Create a kubernetes client (or mockClient) for talking to control plane components
kubeClientWithRevision = newKubeClientWithRevision
// Create a kubernetes.ExecClient (or mock) for talking to data plane components
kubeClient = newKubeClient
loggingOptions = defaultLogOptions()
// scope is for dev logging. Warning: log levels are not set by --log_output_level until command is Run().
scope = log.RegisterScope("cli", "istioctl", 0)
)
func defaultLogOptions() *log.Options {
o := log.DefaultOptions()
// These scopes are, at the default "INFO" level, too chatty for command line use
o.SetOutputLevel("validation", log.ErrorLevel)
o.SetOutputLevel("processing", log.ErrorLevel)
o.SetOutputLevel("source", log.ErrorLevel)
o.SetOutputLevel("analysis", log.WarnLevel)
o.SetOutputLevel("installer", log.WarnLevel)
o.SetOutputLevel("translator", log.WarnLevel)
o.SetOutputLevel("adsc", log.WarnLevel)
o.SetOutputLevel("default", log.WarnLevel)
o.SetOutputLevel("klog", log.WarnLevel)
return o
}
// ConfigAndEnvProcessing uses spf13/viper for overriding CLI parameters
func ConfigAndEnvProcessing() error {
configPath := filepath.Dir(IstioConfig)
baseName := filepath.Base(IstioConfig)
configType := filepath.Ext(IstioConfig)
configName := baseName[0 : len(baseName)-len(configType)]
if configType != "" {
configType = configType[1:]
}
// Allow users to override some variables through $HOME/.istioctl/config.yaml
// and environment variables.
viper.SetEnvPrefix("ISTIOCTL")
viper.AutomaticEnv()
viper.AllowEmptyEnv(true) // So we can say ISTIOCTL_CERT_DIR="" to suppress certs
viper.SetConfigName(configName)
viper.SetConfigType(configType)
viper.AddConfigPath(configPath)
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
err := viper.ReadInConfig()
// Ignore errors reading the configuration unless the file is explicitly customized
if IstioConfig != defaultIstioctlConfig {
return err
}
return nil
}
func init() {
viper.SetDefault("istioNamespace", constants.IstioSystemNamespace)
viper.SetDefault("xds-port", 15012)
}
// GetRootCmd returns the root of the cobra command-tree.
func GetRootCmd(args []string) *cobra.Command {
rootCmd := &cobra.Command{
Use: "istioctl",
Short: "Istio control interface.",
SilenceUsage: true,
DisableAutoGenTag: true,
Long: `Istio configuration command line utility for service operators to
debug and diagnose their Istio mesh.
`,
PersistentPreRunE: configureLogging,
}
rootCmd.SetArgs(args)
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "c", "",
"Kubernetes configuration file")
rootCmd.PersistentFlags().StringVar(&configContext, "context", "",
"The name of the kubeconfig context to use")
rootCmd.PersistentFlags().StringVarP(&istioNamespace, "istioNamespace", "i", viper.GetString("istioNamespace"),
"Istio system namespace")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", v1.NamespaceAll,
"Config namespace")
_ = rootCmd.RegisterFlagCompletionFunc("istioNamespace", validNamespaceArgs)
_ = rootCmd.RegisterFlagCompletionFunc("namespace", validNamespaceArgs)
// Attach the Istio logging options to the command.
loggingOptions.AttachCobraFlags(rootCmd)
hiddenFlags := []string{
"log_as_json", "log_rotate", "log_rotate_max_age", "log_rotate_max_backups",
"log_rotate_max_size", "log_stacktrace_level", "log_target", "log_caller", "log_output_level",
}
for _, opt := range hiddenFlags {
_ = rootCmd.PersistentFlags().MarkHidden(opt)
}
cmd.AddFlags(rootCmd)
kubeInjectCmd := injectCommand()
hideInheritedFlags(kubeInjectCmd, "namespace")
rootCmd.AddCommand(kubeInjectCmd)
experimentalCmd := &cobra.Command{
Use: "experimental",
Aliases: []string{"x", "exp"},
Short: "Experimental commands that may be modified or deprecated",
}
xdsBasedTroubleshooting := []*cobra.Command{
xdsVersionCommand(),
xdsStatusCommand(),
}
debugBasedTroubleshooting := []*cobra.Command{
newVersionCommand(),
statusCommand(),
}
var debugCmdAttachmentPoint *cobra.Command
if viper.GetBool("PREFER-EXPERIMENTAL") {
legacyCmd := &cobra.Command{
Use: "legacy",
Short: "Legacy command variants",
}
rootCmd.AddCommand(legacyCmd)
for _, c := range xdsBasedTroubleshooting {
rootCmd.AddCommand(c)
}
debugCmdAttachmentPoint = legacyCmd
} else {
debugCmdAttachmentPoint = rootCmd
}
for _, c := range xdsBasedTroubleshooting {
experimentalCmd.AddCommand(c)
}
for _, c := range debugBasedTroubleshooting {
debugCmdAttachmentPoint.AddCommand(c)
}
rootCmd.AddCommand(experimentalCmd)
rootCmd.AddCommand(proxyConfig())
rootCmd.AddCommand(adminCmd())
experimentalCmd.AddCommand(injectorCommand())
rootCmd.AddCommand(install.NewVerifyCommand())
experimentalCmd.AddCommand(AuthZ())
rootCmd.AddCommand(seeExperimentalCmd("authz"))
experimentalCmd.AddCommand(uninjectCommand())
experimentalCmd.AddCommand(metricsCmd)
experimentalCmd.AddCommand(describe())
experimentalCmd.AddCommand(addToMeshCmd())
experimentalCmd.AddCommand(removeFromMeshCmd())
experimentalCmd.AddCommand(waitCmd())
experimentalCmd.AddCommand(mesh.UninstallCmd(loggingOptions))
experimentalCmd.AddCommand(configCmd())
experimentalCmd.AddCommand(workloadCommands())
experimentalCmd.AddCommand(revisionCommand())
experimentalCmd.AddCommand(debugCommand())
experimentalCmd.AddCommand(preCheck())
analyzeCmd := Analyze()
hideInheritedFlags(analyzeCmd, "istioNamespace")
rootCmd.AddCommand(analyzeCmd)
dashboardCmd := dashboard()
hideInheritedFlags(dashboardCmd, "namespace", "istioNamespace")
rootCmd.AddCommand(dashboardCmd)
manifestCmd := mesh.ManifestCmd(loggingOptions)
hideInheritedFlags(manifestCmd, "namespace", "istioNamespace", "charts")
rootCmd.AddCommand(manifestCmd)
operatorCmd := mesh.OperatorCmd()
hideInheritedFlags(operatorCmd, "namespace", "istioNamespace", "charts")
rootCmd.AddCommand(operatorCmd)
installCmd := mesh.InstallCmd(loggingOptions)
hideInheritedFlags(installCmd, "namespace", "istioNamespace", "charts")
rootCmd.AddCommand(installCmd)
profileCmd := mesh.ProfileCmd()
hideInheritedFlags(profileCmd, "namespace", "istioNamespace", "charts")
rootCmd.AddCommand(profileCmd)
upgradeCmd := mesh.UpgradeCmd()
hideInheritedFlags(upgradeCmd, "namespace", "istioNamespace", "charts")
rootCmd.AddCommand(upgradeCmd)
bugReportCmd := bugreport.Cmd(loggingOptions)
hideInheritedFlags(bugReportCmd, "namespace", "istioNamespace")
rootCmd.AddCommand(bugReportCmd)
experimentalCmd.AddCommand(multicluster.NewCreateRemoteSecretCommand())
rootCmd.AddCommand(collateral.CobraCommand(rootCmd, &doc.GenManHeader{
Title: "Istio Control",
Section: "istioctl CLI",
Manual: "Istio Control",
}))
validateCmd := validate.NewValidateCommand(&istioNamespace, &namespace)
hideInheritedFlags(validateCmd, "kubeconfig")
rootCmd.AddCommand(validateCmd)
rootCmd.AddCommand(optionsCommand(rootCmd))
// BFS apply the flag error function to all subcommands
seenCommands := make(map[*cobra.Command]bool)
var commandStack []*cobra.Command
commandStack = append(commandStack, rootCmd)
for len(commandStack) > 0 {
n := len(commandStack) - 1
curCmd := commandStack[n]
commandStack = commandStack[:n]
seenCommands[curCmd] = true
for _, command := range curCmd.Commands() {
if !seenCommands[command] {
commandStack = append(commandStack, command)
}
}
curCmd.SetFlagErrorFunc(func(_ *cobra.Command, e error) error {
return CommandParseError{e}
})
}
return rootCmd
}
func hideInheritedFlags(orig *cobra.Command, hidden ...string) {
orig.SetHelpFunc(func(cmd *cobra.Command, args []string) {
for _, hidden := range hidden {
_ = cmd.Flags().MarkHidden(hidden) // nolint: errcheck
}
orig.SetHelpFunc(nil)
orig.HelpFunc()(cmd, args)
})
}
func configureLogging(_ *cobra.Command, _ []string) error {
if err := log.Configure(loggingOptions); err != nil {
return err
}
// --vklog is non zero then KlogScope should be increased.
// klog is a special case.
if klogVerbose() {
log.KlogScope.SetOutputLevel(log.DebugLevel)
}
defaultNamespace = getDefaultNamespace(kubeconfig)
return nil
}
// isKlogVerbose returns true if klog verbosity is non-zero.
// TODO move to istio.io/pkg/log
func klogVerbose() bool {
gf := KlogVerboseFlag()
return gf.Value.String() != "0"
}
// KlogVerboseFlag returns verbose flag from the klog library.
// After parsing it contains the parsed verbosity value.
// TODO move to istio.io/pkg/log
func KlogVerboseFlag() *goflag.Flag {
fs := &goflag.FlagSet{}
klog.InitFlags(fs)
// --v= flag of klog.
return fs.Lookup("v")
}
func getDefaultNamespace(kubeconfig string) string {
configAccess := clientcmd.NewDefaultPathOptions()
if kubeconfig != "" {
// use specified kubeconfig file for the location of the
// config to read
configAccess.GlobalFile = kubeconfig
}
// gets existing kubeconfig or returns new empty config
config, err := configAccess.GetStartingConfig()
if err != nil {
return v1.NamespaceDefault
}
// If a specific context was specified, use that. Otherwise, just use the current context from the kube config.
selectedContext := config.CurrentContext
if configContext != "" {
selectedContext = configContext
}
// Use the namespace associated with the selected context as default, if the context has one
context, ok := config.Contexts[selectedContext]
if !ok {
return v1.NamespaceDefault
}
if context.Namespace == "" {
return v1.NamespaceDefault
}
return context.Namespace
}
// seeExperimentalCmd is used for commands that have been around for a release but not graduated
// Other alternative
// for graduatedCmd see https://github.com/istio/istio/pull/26408
// for softGraduatedCmd see https://github.com/istio/istio/pull/26563
func seeExperimentalCmd(name string) *cobra.Command {
msg := fmt.Sprintf("(%s is experimental. Use `istioctl experimental %s`)", name, name)
return &cobra.Command{
Use: name,
Short: msg,
RunE: func(_ *cobra.Command, _ []string) error {
return errors.New(msg)
},
}
}