1// Copyright 2019 Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bootstrap
16
17import (
18	"time"
19
20	meshconfig "istio.io/api/mesh/v1alpha1"
21	"istio.io/pkg/ctrlz"
22	"istio.io/pkg/env"
23
24	"istio.io/istio/pilot/pkg/features"
25	kubecontroller "istio.io/istio/pilot/pkg/serviceregistry/kube/controller"
26	"istio.io/istio/pkg/config/constants"
27	istiokeepalive "istio.io/istio/pkg/keepalive"
28)
29
30// MeshArgs provide configuration options for the mesh. If ConfigFile is provided, an attempt will be made to
31// load the mesh from the file. Otherwise, a default mesh will be used with optional overrides.
32type MeshArgs struct {
33	ConfigFile string
34	// Used for test
35	MixerAddress string
36}
37
38// ConfigArgs provide configuration options for the configuration controller. If FileDir is set, that directory will
39// be monitored for CRD yaml files and will update the controller as those files change (This is used for testing
40// purposes). Otherwise, a CRD client is created based on the configuration.
41type ConfigArgs struct {
42	ControllerOptions          kubecontroller.Options
43	ClusterRegistriesNamespace string
44	KubeConfig                 string
45	FileDir                    string
46
47	// DistributionTracking control
48	DistributionCacheRetention time.Duration
49
50	DisableInstallCRDs bool
51
52	// DistributionTracking control
53	DistributionTrackingEnabled bool
54}
55
56// ConsulArgs provides configuration for the Consul service registry.
57type ConsulArgs struct {
58	ServerURL string
59}
60
61// ServiceArgs provides the composite configuration for all service registries in the system.
62type ServiceArgs struct {
63	Registries []string
64	Consul     ConsulArgs
65}
66
67// PilotArgs provides all of the configuration parameters for the Pilot discovery service.
68type PilotArgs struct {
69	DiscoveryOptions   DiscoveryServiceOptions
70	InjectionOptions   InjectionOptions
71	PodName            string
72	Namespace          string
73	Revision           string
74	ServiceAccountName string
75	Mesh               MeshArgs
76	Config             ConfigArgs
77	Service            ServiceArgs
78	MeshConfig         *meshconfig.MeshConfig
79	NetworksConfigFile string
80	CtrlZOptions       *ctrlz.Options
81	Plugins            []string
82	MCPOptions         MCPOptions
83	KeepaliveOptions   *istiokeepalive.Options
84	// ForceStop is set as true when used for testing to make the server stop quickly
85	ForceStop bool
86	// Optional TLS configuration
87	TLSOptions TLSOptions
88}
89
90// DiscoveryServiceOptions contains options for create a new discovery
91// service instance.
92type DiscoveryServiceOptions struct {
93	// The listening address for HTTP (debug). If the port in the address is empty or "0" (as in "127.0.0.1:" or "[::1]:0")
94	// a port number is automatically chosen.
95	HTTPAddr string
96
97	// The listening address for HTTPS (webhooks). If the port in the address is empty or "0" (as in "127.0.0.1:" or "[::1]:0")
98	// a port number is automatically chosen.
99	HTTPSAddr string
100
101	// The listening address for GRPC. If the port in the address is empty or "0" (as in "127.0.0.1:" or "[::1]:0")
102	// a port number is automatically chosen.
103	GrpcAddr string
104
105	// The listening address for the monitoring port. If the port in the address is empty or "0" (as in "127.0.0.1:" or "[::1]:0")
106	// a port number is automatically chosen.
107	MonitoringAddr string
108
109	EnableProfiling bool
110}
111
112type InjectionOptions struct {
113	// Directory of injection related config files.
114	InjectionDirectory string
115}
116
117type MCPOptions struct {
118	MaxMessageSize        int
119	InitialWindowSize     int
120	InitialConnWindowSize int
121}
122
123// Optional TLS parameters for the server.
124type TLSOptions struct {
125	CaCertFile string
126	CertFile   string
127	KeyFile    string
128}
129
130var PodNamespaceVar = env.RegisterStringVar("POD_NAMESPACE", "istio-system", "")
131var podNameVar = env.RegisterStringVar("POD_NAME", "", "")
132var serviceAccountVar = env.RegisterStringVar("SERVICE_ACCOUNT", "", "")
133
134// RevisionVar is the value of the Istio control plane revision, e.g. "canary",
135// and is the value used by the "istio.io/rev" label.
136var RevisionVar = env.RegisterStringVar("REVISION", "", "")
137
138// NewPilotArgs constructs pilotArgs with default values.
139func NewPilotArgs(initFuncs ...func(*PilotArgs)) *PilotArgs {
140	p := &PilotArgs{}
141
142	// Apply Default Values.
143	p.applyDefaults()
144
145	// Apply custom initialization functions.
146	for _, fn := range initFuncs {
147		fn(p)
148	}
149
150	// Set the ClusterRegistries namespace based on the selected namespace.
151	if p.Namespace != "" {
152		p.Config.ClusterRegistriesNamespace = p.Namespace
153	} else {
154		p.Config.ClusterRegistriesNamespace = constants.IstioSystemNamespace
155	}
156
157	return p
158}
159
160// Apply default value to PilotArgs
161func (p *PilotArgs) applyDefaults() {
162	p.Namespace = PodNamespaceVar.Get()
163	p.PodName = podNameVar.Get()
164	p.ServiceAccountName = serviceAccountVar.Get()
165	p.Revision = RevisionVar.Get()
166	p.KeepaliveOptions = istiokeepalive.DefaultOption()
167	p.Config.DistributionTrackingEnabled = features.EnableDistributionTracking
168	p.Config.DistributionCacheRetention = features.DistributionHistoryRetention
169}
170