forked from istio/istio
/
main.go
191 lines (153 loc) · 4.96 KB
/
main.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
// Copyright 2017 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 main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"k8s.io/api/core/v1"
// import all known client auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
"istio.io/istio/istioctl/cmd/istioctl/gendeployment"
"istio.io/istio/istioctl/pkg/install"
"istio.io/istio/istioctl/pkg/validate"
"istio.io/istio/pilot/pkg/serviceregistry/kube"
"istio.io/istio/pkg/cmd"
"istio.io/istio/pkg/collateral"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/version"
)
const (
kubePlatform = "kube"
)
var (
platform string
kubeconfig string
configContext string
namespace string
istioNamespace string
defaultNamespace string
// input file name
file string
// output format (yaml or short)
outputFormat string
// Create a kubernetes.ExecClient (or mockExecClient)
clientExecFactory = newExecClient
loggingOptions = log.DefaultOptions()
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: istioPersistentPreRunE,
}
experimentalCmd = &cobra.Command{
Use: "experimental",
Short: "Experimental commands that may be modified or deprecated",
}
)
func istioPersistentPreRunE(c *cobra.Command, args []string) error {
if err := log.Configure(loggingOptions); err != nil {
return err
}
defaultNamespace = getDefaultNamespace(kubeconfig)
return nil
}
func init() {
rootCmd.PersistentFlags().StringVarP(&platform, "platform", "p", kubePlatform,
"Istio host platform")
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", kube.IstioNamespace,
"Istio system namespace")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", v1.NamespaceAll,
"Config namespace")
// Attach the Istio logging options to the command.
loggingOptions.AttachCobraFlags(rootCmd)
cmd.AddFlags(rootCmd)
rootCmd.AddCommand(version.CobraCommandWithOptions(version.CobraOptions{GetRemoteVersion: getRemoteInfo}))
rootCmd.AddCommand(gendeployment.Command(&istioNamespace))
experimentalCmd.AddCommand(install.NewVerifyCommand())
experimentalCmd.AddCommand(Rbac())
rootCmd.AddCommand(experimentalCmd)
rootCmd.AddCommand(collateral.CobraCommand(rootCmd, &doc.GenManHeader{
Title: "Istio Control",
Section: "istioctl CLI",
Manual: "Istio Control",
}))
// Deprecated commands
rootCmd.AddCommand(postCmd)
rootCmd.AddCommand(putCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(deleteCmd)
rootCmd.AddCommand(contextCmd)
rootCmd.AddCommand(validate.NewValidateCommand())
}
func getRemoteInfo() (*version.MeshInfo, error) {
kubeClient, err := clientExecFactory(kubeconfig, configContext)
if err != nil {
return nil, err
}
return kubeClient.GetIstioVersions(istioNamespace)
}
func main() {
if platform != kubePlatform {
log.Warnf("Platform '%s' not supported.", platform)
}
if err := rootCmd.Execute(); err != nil {
os.Exit(-1)
}
}
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
}
context, ok := config.Contexts[config.CurrentContext]
if !ok {
return v1.NamespaceDefault
}
if context.Namespace == "" {
return v1.NamespaceDefault
}
return context.Namespace
}
func handleNamespaces(objectNamespace string) (string, error) {
if objectNamespace != "" && namespace != "" && namespace != objectNamespace {
return "", fmt.Errorf(`the namespace from the provided object "%s" does `+
`not match the namespace "%s". You must pass '--namespace=%s' to perform `+
`this operation`, objectNamespace, namespace, objectNamespace)
}
if namespace != "" {
return namespace, nil
}
if objectNamespace != "" {
return objectNamespace, nil
}
return defaultNamespace, nil
}