-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathhttproute_controller_test.go
More file actions
235 lines (200 loc) · 7.38 KB
/
httproute_controller_test.go
File metadata and controls
235 lines (200 loc) · 7.38 KB
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
package gateway
import (
"time"
testutils "github.com/ngrok/ngrok-operator/internal/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)
var _ = Describe("HTTPRoute controller", Ordered, func() {
const (
ManagedControllerName = ControllerName
UnmanagedControllerName = "k8s.io/some-other-controller"
timeout = 10 * time.Second
duration = 10 * time.Second
interval = 250 * time.Millisecond
)
var (
gatewayClass *gatewayv1.GatewayClass
route *gatewayv1.HTTPRoute
)
When("the gateway class is managed by us", Ordered, func() {
BeforeAll(func(ctx SpecContext) {
gatewayClass = testutils.NewGatewayClass(true)
CreateGatewayClassAndWaitForAcceptance(ctx, gatewayClass, timeout, interval)
})
AfterAll(func(ctx SpecContext) {
DeleteAllGatewayClasses(ctx, timeout, interval)
})
When("the parent ref is a gateway", func() {
var (
gw *gatewayv1.Gateway
)
BeforeEach(func(ctx SpecContext) {
gw = newGateway(gatewayClass)
CreateGatewayAndWaitForAcceptance(ctx, gw, timeout, interval)
route = &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: testutils.RandomName("httproute"),
Namespace: "default",
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{
{
Name: gatewayv1.ObjectName(gw.Name),
},
},
},
Rules: []gatewayv1.HTTPRouteRule{
{
Matches: []gatewayv1.HTTPRouteMatch{
{
Path: &gatewayv1.HTTPPathMatch{},
},
},
},
},
},
}
})
AfterEach(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, gw))).To(Succeed())
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, route))).To(Succeed())
})
JustBeforeEach(func(ctx SpecContext) {
Expect(k8sClient.Create(ctx, route)).To(Succeed())
})
When("the gateway exists", func() {
It("Should accept the HTTPRoute", func(ctx SpecContext) {
Eventually(func(g Gomega) {
obj := &gatewayv1.HTTPRoute{}
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(route), obj)).To(Succeed())
g.Expect(obj.Status.Parents).To(HaveLen(1))
parent := obj.Status.Parents[0]
g.Expect(parent.ParentRef.Name).To(Equal(gatewayv1.ObjectName(gw.Name)))
g.Expect(parent.ControllerName).To(Equal(ManagedControllerName))
g.Expect(parent.Conditions).To(HaveLen(1))
cond := meta.FindStatusCondition(parent.Conditions, string(gatewayv1.RouteConditionAccepted))
g.Expect(cond).ToNot(BeNil())
g.Expect(cond.Status).To(Equal(metav1.ConditionTrue))
g.Expect(cond.Reason).To(Equal(string(gatewayv1.RouteReasonAccepted)))
}, timeout, interval).Should(Succeed())
})
})
When("the gateway does not exist", func() {
BeforeEach(func(ctx SpecContext) {
DeleteGatewayAndWaitForDeletion(ctx, gw, timeout, interval)
})
It("Should not write any status since the Gateway no longer exists and ownership cannot be confirmed", func(ctx SpecContext) {
// Per the Gateway API spec, a controller must NOT write status entries for
// parentRefs it cannot confirm ownership of. When the Gateway doesn't exist,
// ownership cannot be determined, so the ngrok controller should not write
// any status entries (including NoMatchingParent).
Consistently(func(g Gomega) {
obj := &gatewayv1.HTTPRoute{}
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(route), obj)).To(Succeed())
g.Expect(obj.Status.Parents).To(BeEmpty())
g.Expect(obj.Finalizers).NotTo(ContainElement("k8s.ngrok.com/finalizer"))
}, duration, interval).Should(Succeed())
})
})
})
When("the parent ref is an unsupported type", func() {
var (
service *v1.Service
)
BeforeEach(func(ctx SpecContext) {
service = testutils.NewTestServiceV1(testutils.RandomName("svc"), "default")
Expect(k8sClient.Create(ctx, service)).To(Succeed())
route = &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: testutils.RandomName("httproute"),
Namespace: "default",
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{
{
Kind: ptr.To(gatewayv1.Kind("Service")),
Group: ptr.To(gatewayv1.Group("")),
Namespace: ptr.To(gatewayv1.Namespace(service.Namespace)),
Name: gatewayv1.ObjectName(service.Name),
},
},
},
},
}
})
JustBeforeEach(func(ctx SpecContext) {
Expect(k8sClient.Create(ctx, route)).To(Succeed())
})
It("Should not write any status or finalizer since the route does not reference an ngrok Gateway", func(ctx SpecContext) {
Consistently(func(g Gomega) {
obj := &gatewayv1.HTTPRoute{}
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(route), obj)).To(Succeed())
g.Expect(obj.Status.Parents).To(BeEmpty())
g.Expect(obj.Finalizers).NotTo(ContainElement("k8s.ngrok.com/finalizer"))
}, duration, interval).Should(Succeed())
})
})
})
When("the gateway class is NOT managed by us", Ordered, func() {
var (
unmanagedGatewayClass *gatewayv1.GatewayClass
)
BeforeAll(func(ctx SpecContext) {
unmanagedGatewayClass = testutils.NewGatewayClass(false)
Expect(k8sClient.Create(ctx, unmanagedGatewayClass)).To(Succeed())
})
AfterAll(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, unmanagedGatewayClass))).To(Succeed())
})
When("an HTTPRoute references a Gateway with an unmanaged GatewayClass", func() {
var (
unmanagedGateway *gatewayv1.Gateway
)
BeforeEach(func(ctx SpecContext) {
unmanagedGateway = newGateway(unmanagedGatewayClass)
Expect(k8sClient.Create(ctx, unmanagedGateway)).To(Succeed())
route = &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: testutils.RandomName("httproute"),
Namespace: "default",
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{
{
Name: gatewayv1.ObjectName(unmanagedGateway.Name),
},
},
},
},
}
Expect(k8sClient.Create(ctx, route)).To(Succeed())
})
AfterEach(func(ctx SpecContext) {
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, unmanagedGateway))).To(Succeed())
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, route))).To(Succeed())
})
It("Should not add a finalizer or write status to the HTTPRoute", func(ctx SpecContext) {
Consistently(func(g Gomega) {
obj := &gatewayv1.HTTPRoute{}
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(route), obj)).To(Succeed())
// The ngrok controller must not add its finalizer to routes it does not own
g.Expect(obj.Finalizers).NotTo(ContainElement("k8s.ngrok.com/finalizer"))
// The ngrok controller must not write any .status.parents entries for routes it does not own
for _, parent := range obj.Status.Parents {
g.Expect(parent.ControllerName).NotTo(Equal(UnmanagedControllerName))
}
}, duration, interval).Should(Succeed())
})
})
})
})