-
Notifications
You must be signed in to change notification settings - Fork 294
/
utils.go
331 lines (311 loc) · 8.92 KB
/
utils.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package test
import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
discoveryv1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
)
type GroupKindNamespacedName struct {
Group gwapiv1.Group
Kind gwapiv1.Kind
Namespace gwapiv1.Namespace
Name gwapiv1.ObjectName
}
// GetEnvoyProxy returns an EnvoyProxy object with the provided ns/name.
func GetEnvoyProxy(nsName types.NamespacedName, mergeGateways bool) *egv1a1.EnvoyProxy {
return &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Name: nsName.Name,
Namespace: nsName.Namespace,
},
Spec: egv1a1.EnvoyProxySpec{
MergeGateways: &mergeGateways,
},
}
}
// GetGatewayClass returns a sample GatewayClass.
func GetGatewayClass(name string, controller gwapiv1.GatewayController, envoyProxy *GroupKindNamespacedName) *gwapiv1.GatewayClass {
gwc := &gwapiv1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: gwapiv1.GatewayClassSpec{
ControllerName: controller,
},
}
if envoyProxy != nil {
gwc.Spec.ParametersRef = &gwapiv1.ParametersReference{
Group: envoyProxy.Group,
Kind: envoyProxy.Kind,
Name: string(envoyProxy.Name),
Namespace: &envoyProxy.Namespace,
}
}
return gwc
}
// GetGateway returns a sample Gateway with single listener.
func GetGateway(nsName types.NamespacedName, gwclass string, listenerPort int32) *gwapiv1.Gateway {
return &gwapiv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
Spec: gwapiv1.GatewaySpec{
GatewayClassName: gwapiv1.ObjectName(gwclass),
Listeners: []gwapiv1.Listener{
{
Name: "test",
Port: gwapiv1.PortNumber(listenerPort),
Protocol: gwapiv1.HTTPProtocolType,
},
},
},
}
}
// GetSecureGateway returns a sample Gateway with single TLS listener.
func GetSecureGateway(nsName types.NamespacedName, gwclass string, secretKindNSName GroupKindNamespacedName) *gwapiv1.Gateway {
secureGateway := GetGateway(nsName, gwclass, 8080)
secureGateway.Spec.Listeners[0].TLS = &gwapiv1.GatewayTLSConfig{
Mode: ptr.To(gwapiv1.TLSModeTerminate),
CertificateRefs: []gwapiv1.SecretObjectReference{{
Kind: &secretKindNSName.Kind,
Namespace: &secretKindNSName.Namespace,
Name: secretKindNSName.Name,
}},
}
return secureGateway
}
// GetSecret returns a sample Secret object.
func GetSecret(nsName types.NamespacedName) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
}
}
// GetHTTPRoute returns a sample HTTPRoute with a parent reference.
func GetHTTPRoute(nsName types.NamespacedName, parent string, serviceName types.NamespacedName, port int32) *gwapiv1.HTTPRoute {
return &gwapiv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
Spec: gwapiv1.HTTPRouteSpec{
CommonRouteSpec: gwapiv1.CommonRouteSpec{
ParentRefs: []gwapiv1.ParentReference{
{Name: gwapiv1.ObjectName(parent)},
},
},
Rules: []gwapiv1.HTTPRouteRule{
{
BackendRefs: []gwapiv1.HTTPBackendRef{
{
BackendRef: gwapiv1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{
Name: gwapiv1.ObjectName(serviceName.Name),
Port: ptr.To(gwapiv1.PortNumber(port)),
},
},
},
},
},
},
},
}
}
// GetGRPCRoute returns a sample GRPCRoute with a parent reference.
func GetGRPCRoute(nsName types.NamespacedName, parent string, serviceName types.NamespacedName, port int32) *gwapiv1a2.GRPCRoute {
return &gwapiv1a2.GRPCRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
Spec: gwapiv1a2.GRPCRouteSpec{
CommonRouteSpec: gwapiv1.CommonRouteSpec{
ParentRefs: []gwapiv1.ParentReference{
{Name: gwapiv1.ObjectName(parent)},
},
},
Rules: []gwapiv1a2.GRPCRouteRule{
{
BackendRefs: []gwapiv1a2.GRPCBackendRef{
{
BackendRef: gwapiv1.BackendRef{
BackendObjectReference: gwapiv1.BackendObjectReference{
Name: gwapiv1.ObjectName(serviceName.Name),
Port: ptr.To(gwapiv1.PortNumber(port)),
},
},
},
},
},
},
},
}
}
// GetTLSRoute returns a sample TLSRoute with a parent reference.
func GetTLSRoute(nsName types.NamespacedName, parent string, serviceName types.NamespacedName, port int32) *gwapiv1a2.TLSRoute {
return &gwapiv1a2.TLSRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
Spec: gwapiv1a2.TLSRouteSpec{
CommonRouteSpec: gwapiv1a2.CommonRouteSpec{
ParentRefs: []gwapiv1a2.ParentReference{
{Name: gwapiv1a2.ObjectName(parent)},
},
},
Rules: []gwapiv1a2.TLSRouteRule{
{
BackendRefs: []gwapiv1a2.BackendRef{
{
BackendObjectReference: gwapiv1a2.BackendObjectReference{
Name: gwapiv1a2.ObjectName(serviceName.Name),
Port: ptr.To(gwapiv1.PortNumber(port)),
},
},
},
},
},
},
}
}
// GetTCPRoute returns a sample TCPRoute with a parent reference.
func GetTCPRoute(nsName types.NamespacedName, parent string, serviceName types.NamespacedName, port int32) *gwapiv1a2.TCPRoute {
return &gwapiv1a2.TCPRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
Spec: gwapiv1a2.TCPRouteSpec{
CommonRouteSpec: gwapiv1a2.CommonRouteSpec{
ParentRefs: []gwapiv1a2.ParentReference{
{Name: gwapiv1a2.ObjectName(parent)},
},
},
Rules: []gwapiv1a2.TCPRouteRule{
{
BackendRefs: []gwapiv1a2.BackendRef{
{
BackendObjectReference: gwapiv1a2.BackendObjectReference{
Name: gwapiv1a2.ObjectName(serviceName.Name),
Port: ptr.To(gwapiv1a2.PortNumber(port)),
},
},
},
},
},
},
}
}
// GetUDPRoute returns a sample UDPRoute with a parent reference.
func GetUDPRoute(nsName types.NamespacedName, parent string, serviceName types.NamespacedName, port int32) *gwapiv1a2.UDPRoute {
return &gwapiv1a2.UDPRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
},
Spec: gwapiv1a2.UDPRouteSpec{
CommonRouteSpec: gwapiv1a2.CommonRouteSpec{
ParentRefs: []gwapiv1a2.ParentReference{
{Name: gwapiv1a2.ObjectName(parent)},
},
},
Rules: []gwapiv1a2.UDPRouteRule{
{
BackendRefs: []gwapiv1a2.BackendRef{
{
BackendObjectReference: gwapiv1a2.BackendObjectReference{
Name: gwapiv1a2.ObjectName(serviceName.Name),
Port: ptr.To(gwapiv1a2.PortNumber(port)),
},
},
},
},
},
},
}
}
// GetGatewayDeployment returns a sample Deployment for a Gateway object.
func GetGatewayDeployment(nsName types.NamespacedName, labels map[string]string) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: nsName.Namespace,
Name: nsName.Name,
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "dummy",
Image: "dummy",
Ports: []corev1.ContainerPort{{
ContainerPort: 8080,
}},
}},
},
},
},
}
}
// GetService returns a sample Service with labels and ports.
func GetService(nsName types.NamespacedName, labels map[string]string, ports map[string]int32) *corev1.Service {
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: nsName.Name,
Namespace: nsName.Namespace,
Labels: labels,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{},
},
}
for name, port := range ports {
service.Spec.Ports = append(service.Spec.Ports, corev1.ServicePort{
Name: name,
Port: port,
})
}
return service
}
// GetEndpointSlice returns a sample EndpointSlice.
func GetEndpointSlice(nsName types.NamespacedName, svcName string) *discoveryv1.EndpointSlice {
return &discoveryv1.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Name: nsName.Name,
Namespace: nsName.Namespace,
Labels: map[string]string{discoveryv1.LabelServiceName: svcName},
},
Endpoints: []discoveryv1.Endpoint{
{
Addresses: []string{"10.0.0.1"},
Conditions: discoveryv1.EndpointConditions{
Ready: &[]bool{true}[0],
},
},
},
Ports: []discoveryv1.EndpointPort{
{
Name: &[]string{"dummy"}[0],
Port: &[]int32{8080}[0],
Protocol: &[]corev1.Protocol{corev1.ProtocolTCP}[0],
},
},
}
}