forked from istio/istio
/
workload.go
206 lines (175 loc) · 5.54 KB
/
workload.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
// Copyright 2019 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 native
import (
"errors"
"fmt"
"github.com/hashicorp/go-multierror"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/test/application"
appEcho "istio.io/istio/pkg/test/application/echo"
"istio.io/istio/pkg/test/envoy"
"istio.io/istio/pkg/test/framework/components/echo"
"istio.io/istio/pkg/test/framework/components/echo/common"
"istio.io/istio/pkg/test/framework/components/environment/native"
"istio.io/istio/pkg/test/framework/components/pilot"
"istio.io/istio/pkg/test/framework/resource"
)
const (
envoyLogLovel = envoy.LogLevelWarning
)
var _ echo.Workload = &workload{}
type workload struct {
*appEcho.Client
discoveryFilter discoveryFilter
app application.Application
sidecar *sidecar
}
func newWorkload(ctx resource.Context, cfg *echo.Config) (w *workload, err error) {
env := ctx.Environment().(*native.Environment)
w = &workload{}
defer func() {
if err != nil {
_ = w.Close()
}
}()
// Convert the configured ports for the echo application. Ignore any specified port numbers.
appPorts := make(model.PortList, 0, len(cfg.Ports))
for _, p := range cfg.Ports {
appPorts = append(appPorts, &model.Port{
Name: p.Name,
Protocol: p.Protocol,
})
}
appFactory := &appEcho.Factory{
Ports: appPorts,
Version: cfg.Version,
}
// Create and start the Echo application
w.app, err = appFactory.NewApplication(application.DefaultDialer)
if err != nil {
return nil, err
}
if cfg.Sidecar {
// Using a sidecar Envoy proxy. Need to wire up a custom discovery filter and start Envoy...
if cfg.Galley == nil {
return nil, fmt.Errorf("galley must be provided when running echo with a sidecar")
}
if cfg.Pilot == nil {
return nil, fmt.Errorf("pilot must be provided when running echo with a sidecar")
}
// Get Pilot's discovery address.
pilotDiscoveryAddress := cfg.Pilot.(pilot.Native).GetDiscoveryAddress()
// Create the discovery filter that modifies the XDS from Pilot to allow the application run
// run natively.
w.discoveryFilter, err = newDiscoverFilter(pilotDiscoveryAddress.String(), env.PortManager)
if err != nil {
return nil, err
}
// Create a temp directory for the Envoy boostrap YAML file.
tmpDir, err := ctx.CreateTmpDirectory("echo_" + cfg.Service)
if err != nil {
return nil, err
}
// Create an start a new sidecar.
if w.sidecar, err = newSidecar(sidecarConfig{
discoveryAddress: w.discoveryFilter.GetDiscoveryAddress(),
portManager: env.PortManager,
outDir: tmpDir,
domain: env.Domain,
service: cfg.Service,
envoyLogLevel: envoyLogLovel,
namespace: cfg.Namespace.Name(),
servicePorts: w.app.GetPorts(),
}); err != nil {
return nil, err
}
// Apply the service config to Galley.
svcCfg := serviceConfig{
service: cfg.Service,
ns: cfg.Namespace,
domain: env.Domain,
version: cfg.Version,
ports: w.sidecar.GetPorts(),
locality: cfg.Locality,
}
if _, err = svcCfg.applyTo(cfg.Galley); err != nil {
return nil, err
}
// Update the ports in the configuration to reflect the port mapping between Envoy and the Application.
cfg.Ports = w.sidecar.GetPorts()
} else {
// No sidecar case is simple: just use the application ports directly ...
// Update the configuration with the ports assigned by the application.
cfg.Ports = cfg.Ports[:0]
for _, p := range w.app.GetPorts() {
cfg.Ports = append(cfg.Ports, echo.Port{
Name: p.Name,
Protocol: p.Protocol,
ServicePort: p.Port,
InstancePort: p.Port,
})
}
}
// Get the GRPC port.
var grpcPort uint16
for _, p := range cfg.Ports {
if p.Protocol == model.ProtocolGRPC {
grpcPort = uint16(p.InstancePort)
break
}
}
// Create the client for sending forward requests.
if grpcPort == 0 {
// This should never happen, since we're manually adding a GRPC port if one doesn't exist.
return nil, errors.New("unable to find grpc port for application")
}
w.Client, err = appEcho.NewClient(fmt.Sprintf("%s:%d", localhost, grpcPort))
if err != nil {
return nil, err
}
return w, nil
}
func (w *workload) Address() string {
return localhost
}
func (w *workload) Sidecar() echo.Sidecar {
return w.sidecar
}
func (w *workload) Call(opts *echo.CallOptions) (appEcho.ParsedResponses, error) {
// Override the Host.
opts.Host = localhost
portSelector := common.IdentityOutboundPortSelector
if w.discoveryFilter != nil {
// Use the discovery filter as the outbound port selector to force outbound
// requests to go through Envoy.
portSelector = w.discoveryFilter.GetBoundOutboundListenerPort
}
return common.CallEcho(w.Client, opts, portSelector)
}
func (w *workload) Close() (err error) {
if w.Client != nil {
err = multierror.Append(err, w.Client.Close()).ErrorOrNil()
}
if w.sidecar != nil {
w.sidecar.Stop()
}
if w.discoveryFilter != nil {
w.discoveryFilter.Stop()
}
if w.app != nil {
err = multierror.Append(err, w.app.Close()).ErrorOrNil()
}
return
}