Skip to content

Commit 8deb79b

Browse files
committed
fix:
1 parent d052ca2 commit 8deb79b

File tree

4 files changed

+83
-58
lines changed

4 files changed

+83
-58
lines changed

test/e2e/framework/assertion.go

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
)
3535

3636
func GatewayClassMustHaveCondition(t testing.TestingT, cli client.Client, timeout time.Duration, gcNN types.NamespacedName, condition metav1.Condition) {
37-
err := PollUntilGatewayClassMustHaveStatus(cli, timeout, gcNN, func(gc gatewayv1.GatewayClass) bool {
38-
if err := kubernetes.ConditionsHaveLatestObservedGeneration(&gc, gc.Status.Conditions); err != nil {
37+
err := PollUntilGatewayClassMustHaveStatus(cli, timeout, gcNN, func(gc *gatewayv1.GatewayClass) bool {
38+
if err := kubernetes.ConditionsHaveLatestObservedGeneration(gc, gc.Status.Conditions); err != nil {
3939
log.Printf("GatewayClass %s %v", gcNN, err)
4040
return false
4141
}
@@ -48,63 +48,51 @@ func GatewayClassMustHaveCondition(t testing.TestingT, cli client.Client, timeou
4848
require.NoError(t, err, "waiting for GatewayClass to have condition %+v", condition)
4949
}
5050

51-
func PollUntilGatewayClassMustHaveStatus(cli client.Client, timeout time.Duration, gcNN types.NamespacedName, f func(gc gatewayv1.GatewayClass) bool) error {
51+
func PollUntilGatewayClassMustHaveStatus(cli client.Client, timeout time.Duration, gcNN types.NamespacedName, f func(gc *gatewayv1.GatewayClass) bool) error {
5252
if err := gatewayv1.Install(cli.Scheme()); err != nil {
5353
return err
5454
}
55-
return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) {
56-
var gc gatewayv1.GatewayClass
57-
if err := cli.Get(ctx, gcNN, &gc); err != nil {
58-
return false, errors.Wrapf(err, "failed to get GatewayClass %s", gcNN)
59-
}
60-
return f(gc), nil
61-
})
55+
return genericPollResource(new(gatewayv1.GatewayClass), cli, timeout, gcNN, f)
6256
}
6357

6458
func GatewayMustHaveCondition(t testing.TestingT, cli client.Client, timeout time.Duration, gwNN types.NamespacedName, condition metav1.Condition) {
65-
err := PollUntilGatewayHaveStatus(cli, timeout, gwNN, func(gw gatewayv1.Gateway) bool {
66-
if err := kubernetes.ConditionsHaveLatestObservedGeneration(&gw, gw.Status.Conditions); err != nil {
59+
err := PollUntilGatewayHaveStatus(cli, timeout, gwNN, func(gw *gatewayv1.Gateway) bool {
60+
if err := kubernetes.ConditionsHaveLatestObservedGeneration(gw, gw.Status.Conditions); err != nil {
6761
log.Printf("Gateway %s %v", gwNN, err)
6862
return false
6963
}
7064
if findConditionInList(gw.Status.Conditions, condition) {
71-
log.Printf("found condition %v in list [%v]", condition, gw.Status.Conditions)
65+
log.Printf("found condition %v in list %v", condition, gw.Status.Conditions)
7266
return true
7367
} else {
74-
log.Printf("not found condition %v in list [%v]", condition, gw.Status.Conditions)
68+
log.Printf("NOT FOUND condition %v in %v", condition, gw.Status.Conditions)
7569
return false
7670
}
7771
})
7872
require.NoError(t, err, "waiting for Gateway to have condition %+v", condition)
7973
}
8074

81-
func PollUntilGatewayHaveStatus(cli client.Client, timeout time.Duration, gwNN types.NamespacedName, f func(gateway gatewayv1.Gateway) bool) error {
75+
func PollUntilGatewayHaveStatus(cli client.Client, timeout time.Duration, gwNN types.NamespacedName, f func(gateway *gatewayv1.Gateway) bool) error {
8276
if err := gatewayv1.Install(cli.Scheme()); err != nil {
8377
return err
8478
}
85-
return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) {
86-
var gw gatewayv1.Gateway
87-
if err := cli.Get(ctx, gwNN, &gw); err != nil {
88-
return false, errors.Wrapf(err, "failed to get Gateway %s", gwNN)
89-
}
90-
return f(gw), nil
91-
})
79+
return genericPollResource(new(gatewayv1.Gateway), cli, timeout, gwNN, f)
9280
}
9381

9482
func HTTPRouteMustHaveCondition(t testing.TestingT, cli client.Client, timeout time.Duration, refNN, hrNN types.NamespacedName, condition metav1.Condition) {
95-
err := PollUntilHTTPRouteHaveStatus(cli, timeout, hrNN, func(hr gatewayv1.HTTPRoute) bool {
83+
err := PollUntilHTTPRouteHaveStatus(cli, timeout, hrNN, func(hr *gatewayv1.HTTPRoute) bool {
9684
for _, parent := range hr.Status.Parents {
97-
if err := kubernetes.ConditionsHaveLatestObservedGeneration(&hr, parent.Conditions); err != nil {
85+
if err := kubernetes.ConditionsHaveLatestObservedGeneration(hr, parent.Conditions); err != nil {
9886
log.Printf("HTTPRoute %s (parentRef=%v) %v", hrNN, parentRefToString(parent.ParentRef), err)
9987
return false
10088
}
10189
if (refNN.Name == "" || parent.ParentRef.Name == gatewayv1.ObjectName(refNN.Name)) &&
10290
(refNN.Namespace == "" || (parent.ParentRef.Namespace != nil && string(*parent.ParentRef.Namespace) == refNN.Namespace)) {
10391
if findConditionInList(parent.Conditions, condition) {
104-
log.Printf("found condition %v in list [%v] for %s reference %s", condition, parent.Conditions, hrNN, refNN)
92+
log.Printf("found condition %v in %v for %s reference %s", condition, parent.Conditions, hrNN, refNN)
10593
return true
10694
} else {
107-
log.Printf("found condition %v in list [%v] for %s reference %s", condition, parent.Conditions, hrNN, refNN)
95+
log.Printf("NOT FOUND condition %v in %v for %s reference %s", condition, parent.Conditions, hrNN, refNN)
10896
}
10997
}
11098
}
@@ -113,35 +101,29 @@ func HTTPRouteMustHaveCondition(t testing.TestingT, cli client.Client, timeout t
113101
require.NoError(t, err, "error waiting for HTTPRoute status to have a Condition matching %+v", condition)
114102
}
115103

116-
func PollUntilHTTPRouteHaveStatus(cli client.Client, timeout time.Duration, hrNN types.NamespacedName, f func(route gatewayv1.HTTPRoute) bool) error {
104+
func PollUntilHTTPRouteHaveStatus(cli client.Client, timeout time.Duration, hrNN types.NamespacedName, f func(route *gatewayv1.HTTPRoute) bool) error {
117105
if err := gatewayv1.Install(cli.Scheme()); err != nil {
118106
return err
119107
}
120-
return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) {
121-
var httpRoute gatewayv1.HTTPRoute
122-
if err := cli.Get(ctx, hrNN, &httpRoute); err != nil {
123-
return false, errors.Wrapf(err, "failed to get HTTPRoute %s", hrNN)
124-
}
125-
return f(httpRoute), nil
126-
})
108+
return genericPollResource(new(gatewayv1.HTTPRoute), cli, timeout, hrNN, f)
127109
}
128110

129111
func HTTPRoutePolicyMustHaveCondition(t testing.TestingT, client client.Client, timeout time.Duration, refNN, hrpNN types.NamespacedName,
130112
condition metav1.Condition) {
131-
err := PollUntilHTTPRoutePolicyHaveStatus(client, timeout, hrpNN, func(httpRoutePolicy v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool {
132-
for _, ancestor := range status.Ancestors {
133-
if err := kubernetes.ConditionsHaveLatestObservedGeneration(&httpRoutePolicy, ancestor.Conditions); err != nil {
113+
err := PollUntilHTTPRoutePolicyHaveStatus(client, timeout, hrpNN, func(httpRoutePolicy *v1alpha1.HTTPRoutePolicy) bool {
114+
for _, ancestor := range httpRoutePolicy.Status.Ancestors {
115+
if err := kubernetes.ConditionsHaveLatestObservedGeneration(httpRoutePolicy, ancestor.Conditions); err != nil {
134116
log.Printf("HTTPRoutePolicy %s (parentRef=%v) %v", hrpNN, parentRefToString(ancestor.AncestorRef), err)
135117
return false
136118
}
137119

138120
if ancestor.AncestorRef.Name == gatewayv1.ObjectName(refNN.Name) &&
139121
(refNN.Namespace == "" || (ancestor.AncestorRef.Namespace != nil && string(*ancestor.AncestorRef.Namespace) == refNN.Namespace)) {
140122
if findConditionInList(ancestor.Conditions, condition) {
141-
log.Printf("found condition %v in list [%v] for %s reference %s", condition, ancestor.Conditions, hrpNN, refNN)
123+
log.Printf("found condition %v in list %v for %s reference %s", condition, ancestor.Conditions, hrpNN, refNN)
142124
return true
143125
} else {
144-
log.Printf("not found condition %v in list [%v] for %s reference %s", condition, ancestor.Conditions, hrpNN, refNN)
126+
log.Printf("NOT FOUND condition %v in %v for %s reference %s", condition, ancestor.Conditions, hrpNN, refNN)
145127
}
146128
}
147129
}
@@ -151,16 +133,12 @@ func HTTPRoutePolicyMustHaveCondition(t testing.TestingT, client client.Client,
151133
require.NoError(t, err, "error waiting for HTTPRoutePolicy %s status to have a Condition matching %+v", hrpNN, condition)
152134
}
153135

154-
func PollUntilHTTPRoutePolicyHaveStatus(client client.Client, timeout time.Duration, hrpNN types.NamespacedName,
155-
f func(httpRoutePolicy v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool) error {
156-
_ = v1alpha1.AddToScheme(client.Scheme())
157-
return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) {
158-
var httpRoutePolicy v1alpha1.HTTPRoutePolicy
159-
if err = client.Get(ctx, hrpNN, &httpRoutePolicy); err != nil {
160-
return false, errors.Wrapf(err, "error fetching HTTPRoutePolicy %s", hrpNN)
161-
}
162-
return f(httpRoutePolicy, httpRoutePolicy.Status), nil
163-
})
136+
func PollUntilHTTPRoutePolicyHaveStatus(cli client.Client, timeout time.Duration, hrpNN types.NamespacedName,
137+
f func(httpRoutePolicy *v1alpha1.HTTPRoutePolicy) bool) error {
138+
if err := v1alpha1.AddToScheme(cli.Scheme()); err != nil {
139+
return err
140+
}
141+
return genericPollResource(new(v1alpha1.HTTPRoutePolicy), cli, timeout, hrpNN, f)
164142
}
165143

166144
func parentRefToString(p gatewayv1.ParentReference) string {
@@ -180,3 +158,12 @@ func findConditionInList(conditions []metav1.Condition, expected metav1.Conditio
180158
(expected.Message == "" || strings.Contains(item.Message, expected.Message))
181159
})
182160
}
161+
162+
func genericPollResource[Obj client.Object](obj Obj, cli client.Client, timeout time.Duration, nn types.NamespacedName, predicate func(Obj) bool) error {
163+
return wait.PollUntilContextTimeout(context.Background(), time.Second, timeout, true, func(ctx context.Context) (done bool, err error) {
164+
if err := cli.Get(ctx, nn, obj); err != nil {
165+
return false, errors.Wrapf(err, "error fetching Object %s", nn)
166+
}
167+
return predicate(obj), nil
168+
})
169+
}

test/e2e/gatewayapi/httproute.go

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/pkg/errors"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/types"
29+
"k8s.io/utils/ptr"
2930
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3031
"sigs.k8s.io/gateway-api/apis/v1alpha2"
3132

@@ -1046,8 +1047,8 @@ spec:
10461047
s.Logf(message)
10471048

10481049
err = framework.PollUntilHTTPRoutePolicyHaveStatus(s.K8sClient, 8*time.Second, types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
1049-
func(_ v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool {
1050-
return len(status.Ancestors) == 0
1050+
func(hrp *v1alpha1.HTTPRoutePolicy) bool {
1051+
return len(hrp.Status.Ancestors) == 0
10511052
},
10521053
)
10531054
Expect(err).NotTo(HaveOccurred(), "HTPRoutePolicy.Status should has no ancestor")
@@ -1320,7 +1321,11 @@ spec:
13201321

13211322
It("HTTPRoute RequestRedirect", func() {
13221323
By("create HTTPRoute")
1323-
s.ApplyHTTPRoute(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin"}, httpsRedirectByHeaders)
1324+
s.ApplyHTTPRoute(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin"}, httpsRedirectByHeaders, func(_ context.Context) (done bool, err error) {
1325+
return s.NewAPISIXClient().GET("/headers").
1326+
WithHeader("Host", "httpbin.example").
1327+
Expect().Raw().Header.Get("Location") == "https://httpbin.example:9443/headers", nil
1328+
})
13241329

13251330
s.NewAPISIXClient().GET("/headers").
13261331
WithHeader("Host", "httpbin.example").
@@ -1329,7 +1334,11 @@ spec:
13291334
Header("Location").IsEqual("https://httpbin.example:9443/headers")
13301335

13311336
By("update HTTPRoute")
1332-
s.ApplyHTTPRoute(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin"}, hostnameRedirectByHeaders)
1337+
s.ApplyHTTPRoute(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin"}, hostnameRedirectByHeaders, func(_ context.Context) (done bool, err error) {
1338+
return s.NewAPISIXClient().GET("/headers").
1339+
WithHeader("Host", "httpbin.example").
1340+
Expect().Raw().Header.Get("Location") == "http://httpbin.org/headers", nil
1341+
})
13331342

13341343
s.NewAPISIXClient().GET("/headers").
13351344
WithHeader("Host", "httpbin.example").
@@ -1339,7 +1348,7 @@ spec:
13391348
})
13401349

13411350
It("HTTPRoute RequestMirror", func() {
1342-
echoRoute := `
1351+
const echoService = `
13431352
apiVersion: apps/v1
13441353
kind: Deployment
13451354
metadata:
@@ -1372,7 +1381,9 @@ spec:
13721381
port: 80
13731382
protocol: TCP
13741383
targetPort: 8080
1375-
---
1384+
`
1385+
1386+
const echoRoute = `
13761387
apiVersion: gateway.networking.k8s.io/v1
13771388
kind: HTTPRoute
13781389
metadata:
@@ -1397,13 +1408,22 @@ spec:
13971408
- name: httpbin-service-e2e-test
13981409
port: 80
13991410
`
1411+
// apply echo server Deployment and Service
1412+
err := s.CreateResourceFromString(echoService)
1413+
Expect(err).NotTo(HaveOccurred(), "create echo service")
1414+
err = framework.WaitPodsAvailable(s.GinkgoT, s.KubectlOpts(), metav1.ListOptions{
1415+
LabelSelector: "app=echo",
1416+
TimeoutSeconds: ptr.To(int64(10)),
1417+
})
1418+
Expect(err).NotTo(HaveOccurred(), "wait for echo available")
1419+
14001420
s.ApplyHTTPRoute(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin"}, echoRoute, func(_ context.Context) (done bool, err error) {
14011421
return s.NewAPISIXClient().GET("/headers").
14021422
WithHeader("Host", "httpbin.example").
14031423
Expect().Raw().StatusCode == http.StatusOK, nil
14041424
})
14051425

1406-
time.Sleep(time.Second * 6)
1426+
time.Sleep(time.Second)
14071427

14081428
echoLogs := s.GetDeploymentLogs("echo")
14091429
Expect(echoLogs).To(ContainSubstring("GET /headers"))
@@ -1565,6 +1585,20 @@ spec:
15651585

15661586
s.ApplyHTTPRoute(types.NamespacedName{Namespace: s.Namespace(), Name: "httpbin"}, oneWeight)
15671587

1588+
var cnt int
1589+
for cnt < 5 {
1590+
body := s.NewAPISIXClient().GET("/get").
1591+
WithHeader("Host", "httpbin.example").
1592+
Expect().
1593+
Status(http.StatusOK).
1594+
Body().Raw()
1595+
if strings.Contains(body, "Hello") {
1596+
cnt = 0
1597+
} else {
1598+
cnt++
1599+
}
1600+
}
1601+
15681602
hitNginxCnt = 0
15691603
hitHttpbinCnt = 0
15701604
for i := 0; i < 100; i++ {

test/e2e/ingress/ingress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,8 @@ spec:
742742

743743
err = framework.PollUntilHTTPRoutePolicyHaveStatus(s.K8sClient, 8*time.Second,
744744
types.NamespacedName{Namespace: s.Namespace(), Name: "http-route-policy-0"},
745-
func(_ v1alpha1.HTTPRoutePolicy, status v1alpha1.PolicyStatus) bool {
746-
return len(status.Ancestors) == 0
745+
func(hrp *v1alpha1.HTTPRoutePolicy) bool {
746+
return len(hrp.Status.Ancestors) == 0
747747
},
748748
)
749749
Expect(err).NotTo(HaveOccurred(), "expected HTTPRoutePolicy.Status has no Ancestor")

test/e2e/scaffold/scaffold.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,3 +812,7 @@ func (s *Scaffold) GetGatewayGroupHTTPSEndpoint(gatewayGroupID string) (string,
812812
func (s *Scaffold) CurrentGatewayGroupID() string {
813813
return s.gatewaygroupid
814814
}
815+
816+
func (s *Scaffold) KubectlOpts() *k8s.KubectlOptions {
817+
return s.kubectlOptions
818+
}

0 commit comments

Comments
 (0)