/
root.go
312 lines (268 loc) · 10 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
// 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"
"fmt"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"istio.io/istio/istioctl/pkg/admin"
"istio.io/istio/istioctl/pkg/analyze"
"istio.io/istio/istioctl/pkg/authz"
"istio.io/istio/istioctl/pkg/checkinject"
"istio.io/istio/istioctl/pkg/cli"
"istio.io/istio/istioctl/pkg/completion"
"istio.io/istio/istioctl/pkg/config"
"istio.io/istio/istioctl/pkg/dashboard"
"istio.io/istio/istioctl/pkg/describe"
"istio.io/istio/istioctl/pkg/injector"
"istio.io/istio/istioctl/pkg/internaldebug"
"istio.io/istio/istioctl/pkg/kubeinject"
"istio.io/istio/istioctl/pkg/metrics"
"istio.io/istio/istioctl/pkg/multicluster"
"istio.io/istio/istioctl/pkg/precheck"
"istio.io/istio/istioctl/pkg/proxyconfig"
"istio.io/istio/istioctl/pkg/proxystatus"
"istio.io/istio/istioctl/pkg/root"
"istio.io/istio/istioctl/pkg/tag"
"istio.io/istio/istioctl/pkg/util"
"istio.io/istio/istioctl/pkg/validate"
"istio.io/istio/istioctl/pkg/version"
"istio.io/istio/istioctl/pkg/wait"
"istio.io/istio/istioctl/pkg/waypoint"
"istio.io/istio/istioctl/pkg/workload"
"istio.io/istio/istioctl/pkg/ztunnelconfig"
"istio.io/istio/operator/cmd/mesh"
"istio.io/istio/pkg/cmd"
"istio.io/istio/pkg/collateral"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/log"
"istio.io/istio/tools/bug-report/pkg/bugreport"
)
const (
// Location to read istioctl defaults from
defaultIstioctlConfig = "$HOME/.istioctl/config.yaml"
)
const (
FlagCharts = "charts"
)
// ConfigAndEnvProcessing uses spf13/viper for overriding CLI parameters
func ConfigAndEnvProcessing() error {
configPath := filepath.Dir(root.IstioConfig)
baseName := filepath.Base(root.IstioConfig)
configType := filepath.Ext(root.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 root.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,
PersistentPreRunE: ConfigureLogging,
Long: `Istio configuration command line utility for service operators to
debug and diagnose their Istio mesh.
`,
}
rootCmd.SetArgs(args)
flags := rootCmd.PersistentFlags()
rootOptions := cli.AddRootFlags(flags)
ctx := cli.NewCLIContext(rootOptions)
_ = rootCmd.RegisterFlagCompletionFunc(cli.FlagIstioNamespace, func(
cmd *cobra.Command, args []string, toComplete string,
) ([]string, cobra.ShellCompDirective) {
return completion.ValidNamespaceArgs(cmd, ctx, args, toComplete)
})
_ = rootCmd.RegisterFlagCompletionFunc(cli.FlagNamespace, func(
cmd *cobra.Command, args []string, toComplete string,
) ([]string, cobra.ShellCompDirective) {
return completion.ValidNamespaceArgs(cmd, ctx, args, toComplete)
})
// Attach the Istio logging options to the command.
root.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 := kubeinject.InjectCommand(ctx)
hideInheritedFlags(kubeInjectCmd, cli.FlagNamespace)
rootCmd.AddCommand(kubeInjectCmd)
experimentalCmd := &cobra.Command{
Use: "experimental",
Aliases: []string{"x", "exp"},
Short: "Experimental commands that may be modified or deprecated",
}
xdsBasedTroubleshooting := []*cobra.Command{
// TODO(hanxiaop): I think experimental version still has issues, so we keep the old version for now.
version.XdsVersionCommand(ctx),
// TODO(hanxiaop): this is kept for some releases in case someone is using it.
proxystatus.XdsStatusCommand(ctx),
}
troubleshootingCommands := []*cobra.Command{
version.NewVersionCommand(ctx),
proxystatus.StableXdsStatusCommand(ctx),
}
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 troubleshootingCommands {
debugCmdAttachmentPoint.AddCommand(c)
}
rootCmd.AddCommand(experimentalCmd)
rootCmd.AddCommand(proxyconfig.ProxyConfig(ctx))
rootCmd.AddCommand(admin.Cmd(ctx))
experimentalCmd.AddCommand(injector.Cmd(ctx))
rootCmd.AddCommand(mesh.NewVerifyCommand(ctx))
rootCmd.AddCommand(mesh.UninstallCmd(ctx))
experimentalCmd.AddCommand(authz.AuthZ(ctx))
rootCmd.AddCommand(seeExperimentalCmd("authz"))
experimentalCmd.AddCommand(metrics.Cmd(ctx))
experimentalCmd.AddCommand(describe.Cmd(ctx))
experimentalCmd.AddCommand(wait.Cmd(ctx))
experimentalCmd.AddCommand(config.Cmd())
experimentalCmd.AddCommand(workload.Cmd(ctx))
experimentalCmd.AddCommand(internaldebug.DebugCommand(ctx))
experimentalCmd.AddCommand(precheck.Cmd(ctx))
experimentalCmd.AddCommand(proxyconfig.StatsConfigCmd(ctx))
experimentalCmd.AddCommand(checkinject.Cmd(ctx))
experimentalCmd.AddCommand(waypoint.Cmd(ctx))
experimentalCmd.AddCommand(ztunnelconfig.ZtunnelConfig(ctx))
analyzeCmd := analyze.Analyze(ctx)
hideInheritedFlags(analyzeCmd, cli.FlagIstioNamespace)
rootCmd.AddCommand(analyzeCmd)
dashboardCmd := dashboard.Dashboard(ctx)
hideInheritedFlags(dashboardCmd, cli.FlagNamespace, cli.FlagIstioNamespace)
rootCmd.AddCommand(dashboardCmd)
manifestCmd := mesh.ManifestCmd(ctx)
hideInheritedFlags(manifestCmd, cli.FlagNamespace, cli.FlagIstioNamespace, FlagCharts)
rootCmd.AddCommand(manifestCmd)
operatorCmd := mesh.OperatorCmd(ctx)
hideInheritedFlags(operatorCmd, cli.FlagNamespace, cli.FlagIstioNamespace, FlagCharts)
rootCmd.AddCommand(operatorCmd)
installCmd := mesh.InstallCmd(ctx)
hideInheritedFlags(installCmd, cli.FlagNamespace, cli.FlagIstioNamespace, FlagCharts)
rootCmd.AddCommand(installCmd)
profileCmd := mesh.ProfileCmd(ctx)
hideInheritedFlags(profileCmd, cli.FlagNamespace, cli.FlagIstioNamespace, FlagCharts)
rootCmd.AddCommand(profileCmd)
upgradeCmd := mesh.UpgradeCmd(ctx)
hideInheritedFlags(upgradeCmd, cli.FlagNamespace, cli.FlagIstioNamespace, FlagCharts)
rootCmd.AddCommand(upgradeCmd)
bugReportCmd := bugreport.Cmd(ctx, root.LoggingOptions)
hideInheritedFlags(bugReportCmd, cli.FlagNamespace, cli.FlagIstioNamespace)
rootCmd.AddCommand(bugReportCmd)
tagCmd := tag.TagCommand(ctx)
hideInheritedFlags(tag.TagCommand(ctx), cli.FlagNamespace, cli.FlagIstioNamespace, FlagCharts)
rootCmd.AddCommand(tagCmd)
// leave the multicluster commands in x for backwards compat
rootCmd.AddCommand(multicluster.NewCreateRemoteSecretCommand(ctx))
rootCmd.AddCommand(proxyconfig.ClustersCommand(ctx))
rootCmd.AddCommand(collateral.CobraCommand(rootCmd, collateral.Metadata{
Title: "Istio Control",
Section: "istioctl CLI",
Manual: "Istio Control",
}))
validateCmd := validate.NewValidateCommand(ctx)
hideInheritedFlags(validateCmd, "kubeconfig")
rootCmd.AddCommand(validateCmd)
rootCmd.AddCommand(optionsCommand(rootCmd))
// BFS applies 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 util.CommandParseError{Err: 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 {
return log.Configure(root.LoggingOptions)
}
// seeExperimentalCmd is used for commands that have been around for a release but not graduated from
// 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)
},
}
}