This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
270 lines (241 loc) · 9.57 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
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
// 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 (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/golang/glog"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/duration"
"github.com/spf13/cobra"
proxyconfig "istio.io/api/proxy/v1/config"
"istio.io/istio/pilot/cmd"
"istio.io/istio/pilot/model"
"istio.io/istio/pilot/platform"
"istio.io/istio/pilot/proxy"
"istio.io/istio/pilot/proxy/envoy"
"istio.io/istio/pilot/tools/version"
)
var (
role proxy.Node
serviceregistry platform.ServiceRegistry
// proxy config flags (named identically)
configPath string
binaryPath string
serviceCluster string
availabilityZone string
drainDuration time.Duration
parentShutdownDuration time.Duration
discoveryAddress string
discoveryRefreshDelay time.Duration
zipkinAddress string
connectTimeout time.Duration
statsdUDPAddress string
proxyAdminPort int
controlPlaneAuthPolicy string
customConfigFile string
rootCmd = &cobra.Command{
Use: "agent",
Short: "Istio Pilot agent",
Long: "Istio Pilot provides management plane functionality to the Istio service mesh and Istio Mixer.",
}
proxyCmd = &cobra.Command{
Use: "proxy",
Short: "Envoy proxy agent",
RunE: func(c *cobra.Command, args []string) error {
glog.V(2).Infof("Version %s", version.Line())
role.Type = proxy.Sidecar
if len(args) > 0 {
role.Type = proxy.NodeType(args[0])
}
// set values from registry platform
if role.IPAddress == "" {
if serviceregistry == platform.KubernetesRegistry {
role.IPAddress = os.Getenv("INSTANCE_IP")
} else {
ipAddr := "127.0.0.1"
if ok := proxy.WaitForPrivateNetwork(); ok {
ipAddr = proxy.GetPrivateIP().String()
glog.V(2).Infof("Obtained private IP %v", ipAddr)
}
role.IPAddress = ipAddr
}
}
if role.ID == "" {
if serviceregistry == platform.KubernetesRegistry {
role.ID = os.Getenv("POD_NAME") + "." + os.Getenv("POD_NAMESPACE")
} else if serviceregistry == platform.ConsulRegistry {
role.ID = role.IPAddress + ".service.consul"
} else {
role.ID = role.IPAddress
}
}
pilotDomain := role.Domain
if role.Domain == "" {
if serviceregistry == platform.KubernetesRegistry {
role.Domain = os.Getenv("POD_NAMESPACE") + ".svc.cluster.local"
pilotDomain = "cluster.local"
} else if serviceregistry == platform.ConsulRegistry {
role.Domain = "service.consul"
} else {
role.Domain = ""
}
}
glog.V(2).Infof("Proxy role: %#v", role)
proxyConfig := proxyconfig.ProxyConfig{}
// set all flags
proxyConfig.CustomConfigFile = customConfigFile
proxyConfig.ConfigPath = configPath
proxyConfig.BinaryPath = binaryPath
proxyConfig.ServiceCluster = serviceCluster
proxyConfig.AvailabilityZone = availabilityZone
proxyConfig.DrainDuration = ptypes.DurationProto(drainDuration)
proxyConfig.ParentShutdownDuration = ptypes.DurationProto(parentShutdownDuration)
proxyConfig.DiscoveryAddress = discoveryAddress
proxyConfig.DiscoveryRefreshDelay = ptypes.DurationProto(discoveryRefreshDelay)
proxyConfig.ZipkinAddress = zipkinAddress
proxyConfig.ConnectTimeout = ptypes.DurationProto(connectTimeout)
proxyConfig.StatsdUdpAddress = statsdUDPAddress
proxyConfig.ProxyAdminPort = int32(proxyAdminPort)
var pilotSAN []string
switch controlPlaneAuthPolicy {
case proxyconfig.AuthenticationPolicy_NONE.String():
proxyConfig.ControlPlaneAuthPolicy = proxyconfig.AuthenticationPolicy_NONE
case proxyconfig.AuthenticationPolicy_MUTUAL_TLS.String():
var ns string
proxyConfig.ControlPlaneAuthPolicy = proxyconfig.AuthenticationPolicy_MUTUAL_TLS
if serviceregistry == platform.KubernetesRegistry {
partDiscoveryAddress := strings.Split(discoveryAddress, ":")
discoveryHostname := partDiscoveryAddress[0]
parts := strings.Split(discoveryHostname, ".")
if len(parts) == 1 {
// namespace of pilot is not part of discovery address use
// pod namespace e.g. istio-pilot:15003
ns = os.Getenv("POD_NAMESPACE")
} else {
// namespace is found in the discovery address
// e.g. istio-pilot.istio-system:15003
ns = parts[1]
}
}
pilotSAN = envoy.GetPilotSAN(pilotDomain, ns)
}
// resolve statsd address
if proxyConfig.StatsdUdpAddress != "" {
addr, err := proxy.ResolveAddr(proxyConfig.StatsdUdpAddress)
if err != nil {
return err
}
proxyConfig.StatsdUdpAddress = addr
}
if err := model.ValidateProxyConfig(&proxyConfig); err != nil {
return err
}
if out, err := model.ToYAML(&proxyConfig); err != nil {
glog.V(2).Infof("Failed to serialize to YAML: %v", err)
} else {
glog.V(2).Infof("Effective config: %s", out)
}
certs := []envoy.CertSource{
{
Directory: proxy.AuthCertsPath,
Files: []string{proxy.CertChainFilename, proxy.KeyFilename, proxy.RootCertFilename},
},
}
if role.Type == proxy.Ingress {
certs = append(certs, envoy.CertSource{
Directory: proxy.IngressCertsPath,
Files: []string{proxy.IngressCertFilename, proxy.IngressKeyFilename},
})
}
glog.V(2).Infof("Monitored certs: %#v", certs)
envoyProxy := envoy.NewProxy(proxyConfig, role.ServiceNode())
agent := proxy.NewAgent(envoyProxy, proxy.DefaultRetry)
watcher := envoy.NewWatcher(proxyConfig, agent, role, certs, pilotSAN)
ctx, cancel := context.WithCancel(context.Background())
go watcher.Run(ctx)
stop := make(chan struct{})
cmd.WaitSignal(stop)
<-stop
cancel()
return nil
},
}
)
func timeDuration(dur *duration.Duration) time.Duration {
out, err := ptypes.Duration(dur)
if err != nil {
glog.Warning(err)
}
return out
}
func init() {
proxyCmd.PersistentFlags().StringVar((*string)(&serviceregistry), "serviceregistry",
string(platform.KubernetesRegistry),
fmt.Sprintf("Select the platform for service registry, options are {%s, %s, %s}",
string(platform.KubernetesRegistry), string(platform.ConsulRegistry),
string(platform.EurekaRegistry)))
proxyCmd.PersistentFlags().StringVar(&role.IPAddress, "ip", "",
"Proxy IP address. If not provided uses ${INSTANCE_IP} environment variable.")
proxyCmd.PersistentFlags().StringVar(&role.ID, "id", "",
"Proxy unique ID. If not provided uses ${POD_NAME}.${POD_NAMESPACE} from environment variables")
proxyCmd.PersistentFlags().StringVar(&role.Domain, "domain", "",
"DNS domain suffix. If not provided uses ${POD_NAMESPACE}.svc.cluster.local")
// Flags for proxy configuration
values := proxy.DefaultProxyConfig()
proxyCmd.PersistentFlags().StringVar(&configPath, "configPath", values.ConfigPath,
"Path to the generated configuration file directory")
proxyCmd.PersistentFlags().StringVar(&binaryPath, "binaryPath", values.BinaryPath,
"Path to the proxy binary")
proxyCmd.PersistentFlags().StringVar(&serviceCluster, "serviceCluster", values.ServiceCluster,
"Service cluster")
proxyCmd.PersistentFlags().StringVar(&availabilityZone, "availabilityZone", values.AvailabilityZone,
"Availability zone")
proxyCmd.PersistentFlags().DurationVar(&drainDuration, "drainDuration",
timeDuration(values.DrainDuration),
"The time in seconds that Envoy will drain connections during a hot restart")
proxyCmd.PersistentFlags().DurationVar(&parentShutdownDuration, "parentShutdownDuration",
timeDuration(values.ParentShutdownDuration),
"The time in seconds that Envoy will wait before shutting down the parent process during a hot restart")
proxyCmd.PersistentFlags().StringVar(&discoveryAddress, "discoveryAddress", values.DiscoveryAddress,
"Address of the discovery service exposing xDS (e.g. istio-pilot:8080)")
proxyCmd.PersistentFlags().DurationVar(&discoveryRefreshDelay, "discoveryRefreshDelay",
timeDuration(values.DiscoveryRefreshDelay),
"Polling interval for service discovery (used by EDS, CDS, LDS, but not RDS)")
proxyCmd.PersistentFlags().StringVar(&zipkinAddress, "zipkinAddress", values.ZipkinAddress,
"Address of the Zipkin service (e.g. zipkin:9411)")
proxyCmd.PersistentFlags().DurationVar(&connectTimeout, "connectTimeout",
timeDuration(values.ConnectTimeout),
"Connection timeout used by Envoy for supporting services")
proxyCmd.PersistentFlags().StringVar(&statsdUDPAddress, "statsdUdpAddress", values.StatsdUdpAddress,
"IP Address and Port of a statsd UDP listener (e.g. 10.75.241.127:9125)")
proxyCmd.PersistentFlags().IntVar(&proxyAdminPort, "proxyAdminPort", int(values.ProxyAdminPort),
"Port on which Envoy should listen for administrative commands")
proxyCmd.PersistentFlags().StringVar(&controlPlaneAuthPolicy, "controlPlaneAuthPolicy",
values.ControlPlaneAuthPolicy.String(), "Control Plane Authentication Policy")
proxyCmd.PersistentFlags().StringVar(&customConfigFile, "customConfigFile", values.CustomConfigFile,
"Path to the generated configuration file directory")
cmd.AddFlags(rootCmd)
rootCmd.AddCommand(proxyCmd)
rootCmd.AddCommand(cmd.VersionCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
glog.Error(err)
os.Exit(-1)
}
}