-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
ingress.go
277 lines (244 loc) · 8.01 KB
/
ingress.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
271
272
273
274
275
276
277
// 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 istio
import (
"context"
"fmt"
"io"
"net"
"net/netip"
"strconv"
"time"
"k8s.io/apimachinery/pkg/types"
"istio.io/istio/pkg/http/headers"
"istio.io/istio/pkg/test"
"istio.io/istio/pkg/test/echo/common/scheme"
"istio.io/istio/pkg/test/framework/components/cluster"
"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/kube"
"istio.io/istio/pkg/test/framework/components/istio/ingress"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/scopes"
"istio.io/istio/pkg/test/util/retry"
)
const (
defaultIngressIstioNameLabel = "ingressgateway"
defaultIngressIstioLabel = "istio=" + defaultIngressIstioNameLabel
defaultIngressServiceName = "istio-" + defaultIngressIstioNameLabel
discoveryPort = 15012
)
var (
getAddressTimeout = retry.Timeout(3 * time.Minute)
getAddressDelay = retry.BackoffDelay(500 * time.Millisecond)
_ ingress.Instance = &ingressImpl{}
_ io.Closer = &ingressImpl{}
)
type ingressConfig struct {
// Service is the kubernetes Service name for the cluster
Service types.NamespacedName
// LabelSelector is the value for the label on the ingress kubernetes objects
LabelSelector string
// Cluster to be used in a multicluster environment
Cluster cluster.Cluster
}
func newIngress(ctx resource.Context, cfg ingressConfig) (i ingress.Instance) {
if cfg.LabelSelector == "" {
cfg.LabelSelector = defaultIngressIstioLabel
}
c := &ingressImpl{
service: cfg.Service,
labelSelector: cfg.LabelSelector,
env: ctx.Environment().(*kube.Environment),
cluster: ctx.Clusters().GetOrDefault(cfg.Cluster),
caller: common.NewCaller(),
}
return c
}
type ingressImpl struct {
service types.NamespacedName
labelSelector string
env *kube.Environment
cluster cluster.Cluster
caller *common.Caller
}
func (c *ingressImpl) Close() error {
return c.caller.Close()
}
// getAddressesInner returns the external addresses for the given port. When we don't have support for LoadBalancer,
// the returned list will contain will have the externally reachable NodePort address and port.
func (c *ingressImpl) getAddressesInner(port int) ([]string, []int, error) {
attempts := 0
remoteAddrs, err := retry.UntilComplete(func() (addrs any, completed bool, err error) {
attempts++
addrs, completed, err = getRemoteServiceAddresses(c.env.Settings(), c.cluster, c.service.Namespace, c.labelSelector, c.service.Name, port)
if err != nil && attempts > 1 {
// Log if we fail more than once to avoid test appearing to hang
// LB provision be slow, so timeout here needs to be long we should give context
scopes.Framework.Warnf("failed to get address for port %v: %v", port, err)
}
return
}, getAddressTimeout, getAddressDelay)
var anyRemoteAddrs []interface{}
// Perform type assertion and construct a new slice of `any`
anyRemoteAddrs, _ = remoteAddrs.([]any)
if err != nil {
return nil, nil, err
}
var addrs []string
var ports []int
for _, addr := range anyRemoteAddrs {
switch v := addr.(type) {
case string:
host, portStr, err := net.SplitHostPort(v)
if err != nil {
return nil, nil, err
}
mappedPort, err := strconv.Atoi(portStr)
if err != nil {
return nil, nil, err
}
addrs = append(addrs, host)
ports = append(ports, mappedPort)
case netip.AddrPort:
addrs = append(addrs, v.Addr().String())
ports = append(ports, int(v.Port()))
}
}
if len(addrs) > 0 {
return addrs, ports, nil
}
return nil, nil, fmt.Errorf("failed to get address for port %v", port)
}
// AddressForPort returns the externally reachable host and port of the component for the given port.
func (c *ingressImpl) AddressesForPort(port int) ([]string, []int) {
addrs, ports, err := c.getAddressesInner(port)
if err != nil {
scopes.Framework.Error(err)
return nil, nil
}
return addrs, ports
}
func (c *ingressImpl) Cluster() cluster.Cluster {
return c.cluster
}
// HTTPAddresses returns the externally reachable HTTP hosts and port (80) of the component.
func (c *ingressImpl) HTTPAddresses() ([]string, []int) {
return c.AddressesForPort(80)
}
// TCPAddresses returns the externally reachable TCP hosts and port (31400) of the component.
func (c *ingressImpl) TCPAddresses() ([]string, []int) {
return c.AddressesForPort(31400)
}
// HTTPSAddresses returns the externally reachable TCP hosts and port (443) of the component.
func (c *ingressImpl) HTTPSAddresses() ([]string, []int) {
return c.AddressesForPort(443)
}
// DiscoveryAddresses returns the externally reachable discovery addresses (15012) of the component.
func (c *ingressImpl) DiscoveryAddresses() []netip.AddrPort {
hosts, ports := c.AddressesForPort(discoveryPort)
var addrs []netip.AddrPort
if hosts == nil {
return []netip.AddrPort{{}}
}
for i, host := range hosts {
ip, err := netip.ParseAddr(host)
if err != nil {
return []netip.AddrPort{}
}
addrs = append(addrs, netip.AddrPortFrom(ip, uint16(ports[i])))
}
return addrs
}
func (c *ingressImpl) Call(options echo.CallOptions) (echo.CallResult, error) {
return c.callEcho(options)
}
func (c *ingressImpl) CallOrFail(t test.Failer, options echo.CallOptions) echo.CallResult {
t.Helper()
resp, err := c.Call(options)
if err != nil {
t.Fatal(err)
}
return resp
}
func (c *ingressImpl) callEcho(opts echo.CallOptions) (echo.CallResult, error) {
var (
addr string
port int
)
opts = opts.DeepCopy()
var addrs []string
var ports []int
if opts.Port.ServicePort == 0 {
s, err := c.schemeFor(opts)
if err != nil {
return echo.CallResult{}, err
}
opts.Scheme = s
// Default port based on protocol
switch s {
case scheme.HTTP:
addrs, ports = c.HTTPAddresses()
case scheme.HTTPS:
addrs, ports = c.HTTPSAddresses()
case scheme.TCP:
addrs, ports = c.TCPAddresses()
default:
return echo.CallResult{}, fmt.Errorf("ingress: scheme %v not supported. Options: %v+", s, opts)
}
} else {
addrs, ports = c.AddressesForPort(opts.Port.ServicePort)
}
if addrs == nil || ports == nil {
scopes.Framework.Warnf("failed to get host and port for %s/%d", opts.Port.Protocol, opts.Port.ServicePort)
}
addr = addrs[0]
port = ports[0]
// Even if they set ServicePort, when load balancer is disabled, we may need to switch to NodePort, so replace it.
opts.Port.ServicePort = port
if opts.HTTP.Headers == nil {
opts.HTTP.Headers = map[string][]string{}
}
if host := opts.GetHost(); len(host) > 0 {
opts.HTTP.Headers.Set(headers.Host, host)
}
// Default address based on port
opts.Address = addr
if len(c.cluster.HTTPProxy()) > 0 && !c.cluster.ProxyKubectlOnly() {
opts.HTTP.HTTPProxy = c.cluster.HTTPProxy()
}
return c.caller.CallEcho(c, opts)
}
func (c *ingressImpl) schemeFor(opts echo.CallOptions) (scheme.Instance, error) {
if opts.Scheme == "" && opts.Port.Protocol == "" {
return "", fmt.Errorf("must provide either protocol or scheme")
}
if opts.Scheme != "" {
return opts.Scheme, nil
}
return opts.Port.Scheme()
}
func (c *ingressImpl) PodID(i int) (string, error) {
pods, err := c.env.Clusters().Default().PodsForSelector(context.TODO(), c.service.Namespace, c.labelSelector)
if err != nil {
return "", fmt.Errorf("unable to get ingressImpl gateway stats: %v", err)
}
if i < 0 || i >= len(pods.Items) {
return "", fmt.Errorf("pod index out of boundary (%d): %d", len(pods.Items), i)
}
return pods.Items[i].Name, nil
}
func (c *ingressImpl) Namespace() string {
return c.service.Namespace
}