/
cmd.go
198 lines (175 loc) · 8.28 KB
/
cmd.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
// 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 app
import (
"fmt"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"istio.io/istio/pilot/pkg/bootstrap"
"istio.io/istio/pilot/pkg/features"
"istio.io/istio/pilot/pkg/serviceregistry/provider"
"istio.io/istio/pkg/cmd"
"istio.io/istio/pkg/collateral"
"istio.io/istio/pkg/config/constants"
"istio.io/istio/pkg/ctrlz"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/version"
)
var (
serverArgs *bootstrap.PilotArgs
loggingOptions = log.DefaultOptions()
)
// NewRootCommand returns the root cobra command of pilot-discovery.
func NewRootCommand() *cobra.Command {
rootCmd := &cobra.Command{
Use: "pilot-discovery",
Short: "Istio Pilot.",
Long: "Istio Pilot provides mesh-wide traffic management, security and policy capabilities in the Istio Service Mesh.",
SilenceUsage: true,
FParseErrWhitelist: cobra.FParseErrWhitelist{
// Allow unknown flags for backward-compatibility.
UnknownFlags: true,
},
PreRunE: func(c *cobra.Command, args []string) error {
cmd.AddFlags(c)
return nil
},
}
discoveryCmd := newDiscoveryCommand()
addFlags(discoveryCmd)
rootCmd.AddCommand(discoveryCmd)
rootCmd.AddCommand(version.CobraCommand())
rootCmd.AddCommand(collateral.CobraCommand(rootCmd, &doc.GenManHeader{
Title: "Istio Pilot Discovery",
Section: "pilot-discovery CLI",
Manual: "Istio Pilot Discovery",
}))
rootCmd.AddCommand(requestCmd)
return rootCmd
}
func newDiscoveryCommand() *cobra.Command {
return &cobra.Command{
Use: "discovery",
Short: "Start Istio proxy discovery service.",
Args: cobra.ExactArgs(0),
FParseErrWhitelist: cobra.FParseErrWhitelist{
// Allow unknown flags for backward-compatibility.
UnknownFlags: true,
},
PreRunE: func(c *cobra.Command, args []string) error {
if err := log.Configure(loggingOptions); err != nil {
return err
}
if err := validateFlags(serverArgs); err != nil {
return err
}
if err := serverArgs.Complete(); err != nil {
return err
}
return nil
},
RunE: func(c *cobra.Command, args []string) error {
cmd.PrintFlags(c.Flags())
// Create the stop channel for all the servers.
stop := make(chan struct{})
// Create the server for the discovery service.
discoveryServer, err := bootstrap.NewServer(serverArgs)
if err != nil {
return fmt.Errorf("failed to create discovery service: %v", err)
}
// Start the server
if err := discoveryServer.Start(stop); err != nil {
return fmt.Errorf("failed to start discovery service: %v", err)
}
cmd.WaitSignal(stop)
// Wait until we shut down. In theory this could block forever; in practice we will get
// forcibly shut down after 30s in Kubernetes.
discoveryServer.WaitUntilCompletion()
return nil
},
}
}
func addFlags(c *cobra.Command) {
serverArgs = bootstrap.NewPilotArgs(func(p *bootstrap.PilotArgs) {
// Set Defaults
p.CtrlZOptions = ctrlz.DefaultOptions()
// TODO replace with mesh config?
p.InjectionOptions = bootstrap.InjectionOptions{
InjectionDirectory: "./var/lib/istio/inject",
}
})
// Process commandline args.
c.PersistentFlags().StringSliceVar(&serverArgs.RegistryOptions.Registries, "registries",
[]string{string(provider.Kubernetes)},
fmt.Sprintf("Comma separated list of platform service registries to read from (choose one or more from {%s, %s})",
provider.Kubernetes, provider.Mock))
c.PersistentFlags().StringVar(&serverArgs.RegistryOptions.ClusterRegistriesNamespace, "clusterRegistriesNamespace",
serverArgs.RegistryOptions.ClusterRegistriesNamespace, "Namespace for ConfigMap which stores clusters configs")
c.PersistentFlags().StringVar(&serverArgs.RegistryOptions.KubeConfig, "kubeconfig", "",
"Use a Kubernetes configuration file instead of in-cluster configuration")
c.PersistentFlags().StringVar(&serverArgs.MeshConfigFile, "meshConfig", "./etc/istio/config/mesh",
"File name for Istio mesh configuration. If not specified, a default mesh will be used.")
c.PersistentFlags().StringVar(&serverArgs.NetworksConfigFile, "networksConfig", "./etc/istio/config/meshNetworks",
"File name for Istio mesh networks configuration. If not specified, a default mesh networks will be used.")
c.PersistentFlags().StringVarP(&serverArgs.Namespace, "namespace", "n", bootstrap.PodNamespace,
"Select a namespace where the controller resides. If not set, uses ${POD_NAMESPACE} environment variable")
c.PersistentFlags().DurationVar(&serverArgs.ShutdownDuration, "shutdownDuration", 10*time.Second,
"Duration the discovery server needs to terminate gracefully")
// RegistryOptions Controller options
c.PersistentFlags().StringVar(&serverArgs.RegistryOptions.FileDir, "configDir", "",
"Directory to watch for updates to config yaml files. If specified, the files will be used as the source of config, rather than a CRD client.")
c.PersistentFlags().StringVar(&serverArgs.RegistryOptions.KubeOptions.DomainSuffix, "domain", constants.DefaultClusterLocalDomain,
"DNS domain suffix")
c.PersistentFlags().StringVar((*string)(&serverArgs.RegistryOptions.KubeOptions.ClusterID), "clusterID", features.ClusterName,
"The ID of the cluster that this Istiod instance resides")
c.PersistentFlags().StringToStringVar(&serverArgs.RegistryOptions.KubeOptions.ClusterAliases, "clusterAliases", map[string]string{},
"Alias names for clusters")
// using address, so it can be configured as localhost:.. (possibly UDS in future)
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.HTTPAddr, "httpAddr", ":8080",
"Discovery service HTTP address")
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.HTTPSAddr, "httpsAddr", ":15017",
"Injection and validation service HTTPS address")
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.GRPCAddr, "grpcAddr", ":15010",
"Discovery service gRPC address")
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.SecureGRPCAddr, "secureGRPCAddr", ":15012",
"Discovery service secured gRPC address")
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.MonitoringAddr, "monitoringAddr", ":15014",
"HTTP address to use for pilot's self-monitoring information")
c.PersistentFlags().BoolVar(&serverArgs.ServerOptions.EnableProfiling, "profile", true,
"Enable profiling via web interface host:port/debug/pprof")
// Use TLS certificates if provided.
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.TLSOptions.CaCertFile, "caCertFile", "",
"File containing the x509 Server CA Certificate")
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.TLSOptions.CertFile, "tlsCertFile", "",
"File containing the x509 Server Certificate")
c.PersistentFlags().StringVar(&serverArgs.ServerOptions.TLSOptions.KeyFile, "tlsKeyFile", "",
"File containing the x509 private key matching --tlsCertFile")
c.PersistentFlags().StringSliceVar(&serverArgs.ServerOptions.TLSOptions.TLSCipherSuites, "tls-cipher-suites", nil,
"Comma-separated list of cipher suites for istiod TLS server. "+
"If omitted, the default Go cipher suites will be used. \n"+
"Preferred values: "+strings.Join(secureTLSCipherNames(), ", ")+". \n"+
"Insecure values: "+strings.Join(insecureTLSCipherNames(), ", ")+".")
c.PersistentFlags().Float32Var(&serverArgs.RegistryOptions.KubeOptions.KubernetesAPIQPS, "kubernetesApiQPS", 80.0,
"Maximum QPS when communicating with the kubernetes API")
c.PersistentFlags().IntVar(&serverArgs.RegistryOptions.KubeOptions.KubernetesAPIBurst, "kubernetesApiBurst", 160,
"Maximum burst for throttle when communicating with the kubernetes API")
// Attach the Istio logging options to the command.
loggingOptions.AttachCobraFlags(c)
// Attach the Istio Ctrlz options to the command.
serverArgs.CtrlZOptions.AttachCobraFlags(c)
// Attach the Istio Keepalive options to the command.
serverArgs.KeepaliveOptions.AttachCobraFlags(c)
}