generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathgateway-static-addresses.go
177 lines (158 loc) · 7.52 KB
/
gateway-static-addresses.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
/*
Copyright 2023 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 tests
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
v1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
)
func init() {
ConformanceTests = append(ConformanceTests, GatewayStaticAddresses)
}
// GatewayStaticAddresses tests the implementation's support of deploying
// Gateway resources with static addresses, or in other words addresses
// provided via the specification rather than relying on the underlying
// implementation/network to dynamically assign the Gateway an address.
//
// Running this test against your own implementation is currently a little bit
// messy, as at the time of writing we didn't have great ways to provide the
// test suite with things like known good, or known bad addresses to run the
// test with (as we obviously can't determine that for the implementation).
//
// As such, if you're trying to enable this test for yourself and you're getting
// confused about how to provide addresses, you'll actually do that in the
// conformance test suite BEFORE you even set up and run your tests. Make sure
// you populate the following test suite fields:
//
// - suite.UsableNetworkAddresses
// - suite.UnusableNetworkAddresses
//
// With appropriate network addresses for your network environment.
var GatewayStaticAddresses = suite.ConformanceTest{
ShortName: "GatewayStaticAddresses",
Description: "A Gateway in the gateway-conformance-infra namespace should be able to use previously determined addresses.",
Features: []suite.SupportedFeature{
suite.SupportGateway,
suite.SupportGatewayStaticAddresses,
},
Manifests: []string{
"tests/gateway-static-addresses.yaml",
},
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
gwNN := types.NamespacedName{
Name: "gateway-static-addresses",
Namespace: "gateway-conformance-infra",
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
t.Logf("waiting for namespace %s and Gateway %s to be ready for testing", gwNN.Namespace, gwNN.Name)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)
t.Logf("retrieving Gateway %s/%s and noting the provided addresses", gwNN.Namespace, gwNN.Name)
currentGW := &v1.Gateway{}
err := s.Client.Get(ctx, gwNN, currentGW)
require.NoError(t, err, "error getting Gateway: %v", err)
require.Len(t, currentGW.Spec.Addresses, 3, "expected 3 addresses on the Gateway, one invalid, one usable and one unusable. somehow got %d", len(currentGW.Spec.Addresses))
invalidAddress := currentGW.Spec.Addresses[0]
unusableAddress := currentGW.Spec.Addresses[1]
usableAddress := currentGW.Spec.Addresses[2]
t.Logf("verifying that the Gateway %s/%s is NOT accepted due to an address type that the implementation doesn't support", gwNN.Namespace, gwNN.Name)
kubernetes.GatewayMustHaveCondition(t, s.Client, s.TimeoutConfig, gwNN, metav1.Condition{
Type: string(v1.GatewayConditionAccepted),
Status: metav1.ConditionFalse,
Reason: string(v1.GatewayReasonUnsupportedAddress),
})
t.Logf("patching Gateway %s/%s to remove the invalid address %s", gwNN.Namespace, gwNN.Name, invalidAddress.Value)
updatedGW := currentGW.DeepCopy()
updatedGW.Spec.Addresses = filterAddr(currentGW.Spec.Addresses, invalidAddress)
err = s.Client.Patch(ctx, updatedGW, client.MergeFrom(currentGW))
require.NoError(t, err, "failed to patch Gateway: %v", err)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)
t.Logf("verifying that the Gateway %s/%s is now accepted, but is not programmed due to an address that can't be used", gwNN.Namespace, gwNN.Name)
err = s.Client.Get(ctx, gwNN, currentGW)
require.NoError(t, err, "error getting Gateway: %v", err)
kubernetes.GatewayMustHaveCondition(t, s.Client, s.TimeoutConfig, gwNN, metav1.Condition{
Type: string(v1.GatewayConditionAccepted),
Status: metav1.ConditionTrue,
Reason: string(v1.GatewayReasonAccepted),
})
kubernetes.GatewayMustHaveCondition(t, s.Client, s.TimeoutConfig, gwNN, metav1.Condition{
Type: string(v1.GatewayConditionProgrammed),
Status: metav1.ConditionFalse,
Reason: string(v1.GatewayReasonAddressNotUsable),
})
t.Logf("patching Gateway %s/%s to remove the unusable address %s", gwNN.Namespace, gwNN.Name, unusableAddress.Value)
updatedGW = currentGW.DeepCopy()
updatedGW.Spec.Addresses = filterAddr(currentGW.Spec.Addresses, unusableAddress)
err = s.Client.Patch(ctx, updatedGW, client.MergeFrom(currentGW))
require.NoError(t, err, "failed to patch Gateway: %v", err)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)
t.Logf("verifying that the Gateway %s/%s is accepted and programmed with the usable static address %s assigned", gwNN.Namespace, gwNN.Name, usableAddress.Value)
err = s.Client.Get(ctx, gwNN, currentGW)
require.NoError(t, err, "error getting Gateway: %v", err)
kubernetes.GatewayMustHaveCondition(t, s.Client, s.TimeoutConfig, gwNN, metav1.Condition{
Type: string(v1.GatewayConditionAccepted),
Status: metav1.ConditionTrue,
Reason: string(v1.GatewayReasonAccepted),
})
kubernetes.GatewayMustHaveCondition(t, s.Client, s.TimeoutConfig, gwNN, metav1.Condition{
Type: string(v1.GatewayConditionProgrammed),
Status: metav1.ConditionTrue,
Reason: string(v1.GatewayReasonProgrammed),
})
kubernetes.GatewayStatusMustHaveListeners(t, s.Client, s.TimeoutConfig, gwNN, finalExpectedListenerState)
require.Len(t, currentGW.Spec.Addresses, 1, "expected only 1 address left specified on Gateway")
require.Len(t, currentGW.Status.Addresses, 1, "one usable address was provided, so it should be the one reflected in status")
require.Equal(t, usableAddress.Type, currentGW.Status.Addresses[0].Type, "expected address type to match the usable address")
require.Equal(t, usableAddress.Value, currentGW.Status.Addresses[0].Value, "expected usable address to be assigned")
},
}
// -----------------------------------------------------------------------------
// Private Helper Functions
// -----------------------------------------------------------------------------
func filterAddr(addrs []v1.GatewayAddress, filter v1.GatewayAddress) (newAddrs []v1.GatewayAddress) {
for _, addr := range addrs {
if addr.Value != filter.Value {
newAddrs = append(newAddrs, addr)
}
}
return
}
var finalExpectedListenerState = []v1.ListenerStatus{
{
Name: v1.SectionName("http"),
SupportedKinds: []v1.RouteGroupKind{{
Group: (*v1.Group)(&v1.GroupVersion.Group),
Kind: v1.Kind("HTTPRoute"),
}},
Conditions: []metav1.Condition{
{
Type: string(v1.ListenerConditionAccepted),
Status: metav1.ConditionTrue,
Reason: "", // any reason
},
{
Type: string(v1.ListenerConditionResolvedRefs),
Status: metav1.ConditionTrue,
Reason: "", // any reason
},
},
AttachedRoutes: 0,
},
}