forked from kubernetes-sigs/gateway-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.go
385 lines (324 loc) · 13.2 KB
/
apply.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Copyright 2022 The Kubernetes 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 kubernetes
import (
"bytes"
"context"
"embed"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/gateway-api/apis/v1beta1"
"sigs.k8s.io/gateway-api/conformance/utils/config"
)
// Applier prepares manifests depending on the available options and applies
// them to the Kubernetes cluster.
type Applier struct {
NamespaceLabels map[string]string
NamespaceAnnotations map[string]string
// GatewayClass will be used as the spec.gatewayClassName when applying Gateway resources
GatewayClass string
// ControllerName will be used as the spec.controllerName when applying GatewayClass resources
ControllerName string
// FS is the filesystem to use when reading manifests.
FS embed.FS
// UsableNetworkAddresses is a list of addresses that are expected to be
// supported AND usable for Gateways in the underlying implementation.
UsableNetworkAddresses []v1beta1.GatewayAddress
// UnusableNetworkAddresses is a list of addresses that are expected to be
// supported, but not usable for Gateways in the underlying implementation.
UnusableNetworkAddresses []v1beta1.GatewayAddress
}
// prepareGateway adjusts the gatewayClassName.
func (a Applier) prepareGateway(t *testing.T, uObj *unstructured.Unstructured) {
ns := uObj.GetNamespace()
name := uObj.GetName()
err := unstructured.SetNestedField(uObj.Object, a.GatewayClass, "spec", "gatewayClassName")
require.NoErrorf(t, err, "error setting `spec.gatewayClassName` on Gateway %s/%s", ns, name)
rawSpec, hasSpec, err := unstructured.NestedFieldCopy(uObj.Object, "spec")
require.NoError(t, err, "error retrieving spec.addresses to verify if any static addresses were present on Gateway resource %s/%s", ns, name)
require.True(t, hasSpec)
rawSpecMap, ok := rawSpec.(map[string]interface{})
require.True(t, ok, "expected gw spec received %T", rawSpec)
gwspec := &v1beta1.GatewaySpec{}
require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(rawSpecMap, gwspec))
// for tests which have placeholders for static gateway addresses we will
// inject real addresses from the address pools the caller provided.
if len(gwspec.Addresses) > 0 {
// this is a hack because we don't have any other great way to inject custom
// values into the test YAML at the time of writing: Gateways that include
// addresses with the following values:
//
// * PLACEHOLDER_USABLE_ADDRS
// * PLACEHOLDER_UNUSABLE_ADDRS
//
// indicate that they expect the caller of the test suite to have provided
// relevant addresses (usable, or unusable ones) in the test suite, and those
// addresses will be injected into the Gateway and the placeholders removed.
//
// A special "test/fake-invalid-type" can be provided as well in the test to
// explicitly trigger a failure to support a type. If an implementation ever
// comes along actually trying to support that type, I'm going to be very
// cranky.
//
// Note: I would really love to find a better way to do this kind of
// thing in the future.
var overlayUsable, overlayUnusable bool
var specialAddrs []v1beta1.GatewayAddress
for _, addr := range gwspec.Addresses {
switch addr.Value {
case "PLACEHOLDER_USABLE_ADDRS":
overlayUsable = true
case "PLACEHOLDER_UNUSABLE_ADDRS":
overlayUnusable = true
}
if addr.Type != nil && *addr.Type == "test/fake-invalid-type" {
specialAddrs = append(specialAddrs, addr)
}
}
var primOverlayAddrs []interface{}
if len(specialAddrs) > 0 {
t.Logf("the test provides %d special addresses that will be kept", len(specialAddrs))
primOverlayAddrs = append(primOverlayAddrs, convertGatewayAddrsToPrimitives(specialAddrs)...)
}
if overlayUnusable {
t.Logf("address pool of %d unusable addresses will be overlaid", len(a.UnusableNetworkAddresses))
primOverlayAddrs = append(primOverlayAddrs, convertGatewayAddrsToPrimitives(a.UnusableNetworkAddresses)...)
}
if overlayUsable {
t.Logf("address pool of %d usable addresses will be overlaid", len(a.UsableNetworkAddresses))
primOverlayAddrs = append(primOverlayAddrs, convertGatewayAddrsToPrimitives(a.UsableNetworkAddresses)...)
}
err = unstructured.SetNestedSlice(uObj.Object, primOverlayAddrs, "spec", "addresses")
require.NoError(t, err, "could not overlay static addresses on Gateway %s/%s", ns, name)
}
}
// prepareGatewayClass adjust the spec.controllerName on the resource
func (a Applier) prepareGatewayClass(t *testing.T, uObj *unstructured.Unstructured) {
err := unstructured.SetNestedField(uObj.Object, a.ControllerName, "spec", "controllerName")
require.NoErrorf(t, err, "error setting `spec.controllerName` on %s GatewayClass resource", uObj.GetName())
}
// prepareNamespace adjusts the Namespace labels.
func (a Applier) prepareNamespace(t *testing.T, uObj *unstructured.Unstructured) {
labels, _, err := unstructured.NestedStringMap(uObj.Object, "metadata", "labels")
require.NoErrorf(t, err, "error getting labels on Namespace %s", uObj.GetName())
for k, v := range a.NamespaceLabels {
if labels == nil {
labels = map[string]string{}
}
labels[k] = v
}
// SetNestedStringMap converts nil to an empty map
if labels != nil {
err = unstructured.SetNestedStringMap(uObj.Object, labels, "metadata", "labels")
}
require.NoErrorf(t, err, "error setting labels on Namespace %s", uObj.GetName())
annotations, _, err := unstructured.NestedStringMap(uObj.Object, "metadata", "annotations")
require.NoErrorf(t, err, "error getting annotations on Namespace %s", uObj.GetName())
for k, v := range a.NamespaceAnnotations {
if annotations == nil {
annotations = map[string]string{}
}
annotations[k] = v
}
// SetNestedStringMap converts nil to an empty map
if annotations != nil {
err = unstructured.SetNestedStringMap(uObj.Object, annotations, "metadata", "annotations")
}
require.NoErrorf(t, err, "error setting annotations on Namespace %s", uObj.GetName())
}
// prepareResources uses the options from an Applier to tweak resources given by
// a set of manifests.
func (a Applier) prepareResources(t *testing.T, decoder *yaml.YAMLOrJSONDecoder) ([]unstructured.Unstructured, error) {
var resources []unstructured.Unstructured
for {
uObj := unstructured.Unstructured{}
if err := decoder.Decode(&uObj); err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, err
}
if len(uObj.Object) == 0 {
continue
}
if uObj.GetKind() == "GatewayClass" {
a.prepareGatewayClass(t, &uObj)
}
if uObj.GetKind() == "Gateway" {
a.prepareGateway(t, &uObj)
}
if uObj.GetKind() == "Namespace" && uObj.GetObjectKind().GroupVersionKind().Group == "" {
a.prepareNamespace(t, &uObj)
}
resources = append(resources, uObj)
}
return resources, nil
}
func (a Applier) MustApplyObjectsWithCleanup(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, resources []client.Object, cleanup bool) {
for _, resource := range resources {
resource := resource
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.CreateTimeout)
defer cancel()
// Check if the resource exists
existingResource := resource.DeepCopyObject().(client.Object)
err := c.Get(ctx, types.NamespacedName{Name: resource.GetName(), Namespace: resource.GetNamespace()}, existingResource)
if err != nil {
if !apierrors.IsNotFound(err) {
require.NoError(t, err, "error getting resource")
}
t.Logf("Creating %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
err = c.Create(ctx, resource)
require.NoError(t, err, "error creating resource")
if cleanup {
t.Cleanup(func() {
ctx, cancel = context.WithTimeout(context.Background(), timeoutConfig.DeleteTimeout)
defer cancel()
t.Logf("Deleting %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
err = c.Delete(ctx, resource)
require.NoErrorf(t, err, "error deleting resource")
})
}
continue
}
// Resource exists, update it
t.Logf("Updating %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
resource.SetResourceVersion(existingResource.GetResourceVersion())
err = c.Update(ctx, resource)
require.NoErrorf(t, err, "error updating resource")
if cleanup {
t.Cleanup(func() {
ctx, cancel = context.WithTimeout(context.Background(), timeoutConfig.DeleteTimeout)
defer cancel()
t.Logf("Deleting %s %s", resource.GetName(), resource.GetObjectKind().GroupVersionKind().Kind)
err = c.Delete(ctx, resource)
require.NoErrorf(t, err, "error deleting resource")
})
}
}
}
// MustApplyWithCleanup creates or updates Kubernetes resources defined with the
// provided YAML file and registers a cleanup function for resources it created.
// Note that this does not remove resources that already existed in the cluster.
func (a Applier) MustApplyWithCleanup(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, location string, cleanup bool) {
data, err := getContentsFromPathOrURL(a.FS, location, timeoutConfig)
require.NoError(t, err)
decoder := yaml.NewYAMLOrJSONDecoder(data, 4096)
resources, err := a.prepareResources(t, decoder)
if err != nil {
t.Logf("manifest: %s", data.String())
require.NoErrorf(t, err, "error parsing manifest")
}
for i := range resources {
uObj := &resources[i]
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.CreateTimeout)
defer cancel()
namespacedName := types.NamespacedName{Namespace: uObj.GetNamespace(), Name: uObj.GetName()}
fetchedObj := uObj.DeepCopy()
err := c.Get(ctx, namespacedName, fetchedObj)
if err != nil {
if !apierrors.IsNotFound(err) {
require.NoErrorf(t, err, "error getting resource")
}
t.Logf("Creating %s %s", uObj.GetName(), uObj.GetKind())
err = c.Create(ctx, uObj)
require.NoErrorf(t, err, "error creating resource")
if cleanup {
t.Cleanup(func() {
ctx, cancel = context.WithTimeout(context.Background(), timeoutConfig.DeleteTimeout)
defer cancel()
t.Logf("Deleting %s %s", uObj.GetName(), uObj.GetKind())
err = c.Delete(ctx, uObj)
if !apierrors.IsNotFound(err) {
require.NoErrorf(t, err, "error deleting resource")
}
})
}
continue
}
uObj.SetResourceVersion(fetchedObj.GetResourceVersion())
t.Logf("Updating %s %s", uObj.GetName(), uObj.GetKind())
err = c.Update(ctx, uObj)
if cleanup {
t.Cleanup(func() {
ctx, cancel = context.WithTimeout(context.Background(), timeoutConfig.DeleteTimeout)
defer cancel()
t.Logf("Deleting %s %s", uObj.GetName(), uObj.GetKind())
err = c.Delete(ctx, uObj)
if !apierrors.IsNotFound(err) {
require.NoErrorf(t, err, "error deleting resource")
}
})
}
require.NoErrorf(t, err, "error updating resource")
}
}
// getContentsFromPathOrURL takes a string that can either be a local file
// path or an https:// URL to YAML manifests and provides the contents.
func getContentsFromPathOrURL(fs embed.FS, location string, timeoutConfig config.TimeoutConfig) (*bytes.Buffer, error) {
if strings.HasPrefix(location, "http://") {
return nil, fmt.Errorf("data can't be retrieved from %s: http is not supported, use https", location)
} else if strings.HasPrefix(location, "https://") {
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.ManifestFetchTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, location, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
manifests := new(bytes.Buffer)
count, err := manifests.ReadFrom(resp.Body)
if err != nil {
return nil, err
}
if resp.ContentLength != -1 && count != resp.ContentLength {
return nil, fmt.Errorf("received %d bytes from %s, expected %d", count, location, resp.ContentLength)
}
return manifests, nil
}
b, err := fs.ReadFile(location)
if err != nil {
return nil, err
}
return bytes.NewBuffer(b), nil
}
// convertGatewayAddrsToPrimitives converts a slice of Gateway addresses
// to a slice of primitive types and then returns them as a []interface{} so that
// they can be applied back to an unstructured Gateway.
func convertGatewayAddrsToPrimitives(gwaddrs []v1beta1.GatewayAddress) (raw []interface{}) {
for _, addr := range gwaddrs {
addrType := string(v1beta1.IPAddressType)
if addr.Type != nil {
addrType = string(*addr.Type)
}
raw = append(raw, map[string]interface{}{
"type": addrType,
"value": addr.Value,
})
}
return
}