Skip to content

Commit 3de6775

Browse files
committed
Adjust and add e2e tests for status cleanup
Signed-off-by: y-rabie <[email protected]>
1 parent 183cfa3 commit 3de6775

15 files changed

+548
-42
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: gateway.envoyproxy.io/v1alpha1
2+
kind: BackendTrafficPolicy
3+
metadata:
4+
name: backendtrafficpolicy-multiple-ancestors-same-gwc
5+
namespace: gateway-conformance-infra
6+
spec:
7+
targetRefs:
8+
- group: gateway.networking.k8s.io
9+
kind: Gateway
10+
name: same-namespace
11+
# This gateway (and its gatewayclass) is created through `status-cleanup-gateway-different-gwc.yaml` manifest.
12+
- group: gateway.networking.k8s.io
13+
kind: Gateway
14+
name: gateway-2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: gateway.envoyproxy.io/v1alpha1
2+
kind: BackendTrafficPolicy
3+
metadata:
4+
name: backendtrafficpolicy-multiple-ancestors-same-gwc
5+
namespace: gateway-conformance-infra
6+
spec:
7+
targetRefs:
8+
- group: gateway.networking.k8s.io
9+
kind: Gateway
10+
name: same-namespace
11+
- group: gateway.networking.k8s.io
12+
kind: Gateway
13+
name: all-namespaces
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: gateway.envoyproxy.io/v1alpha1
2+
kind: BackendTrafficPolicy
3+
metadata:
4+
name: backendtrafficpolicy-multiple-ancestors-same-gwc
5+
namespace: gateway-conformance-infra
6+
spec:
7+
targetRefs:
8+
- group: gateway.networking.k8s.io
9+
kind: Gateway
10+
name: non-existent-ancestor
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: gateway.envoyproxy.io/v1alpha1
2+
kind: BackendTrafficPolicy
3+
metadata:
4+
name: backendtrafficpolicy-multiple-ancestors-same-gwc
5+
namespace: gateway-conformance-infra
6+
spec:
7+
targetRefs:
8+
- group: gateway.networking.k8s.io
9+
kind: Gateway
10+
name: same-namespace
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: gateway.networking.k8s.io/v1beta1
2+
kind: Gateway
3+
metadata:
4+
name: gateway-1
5+
namespace: gateway-conformance-infra
6+
spec:
7+
gatewayClassName: "{GATEWAY_CLASS_NAME}"
8+
listeners:
9+
- name: foo
10+
protocol: TCP
11+
port: 8080
12+
allowedRoutes:
13+
kinds:
14+
- kind: TCPRoute
15+
---
16+
apiVersion: gateway.networking.k8s.io/v1alpha2
17+
kind: TCPRoute
18+
metadata:
19+
name: tcp-route-status-cleanup
20+
namespace: gateway-conformance-infra
21+
spec:
22+
parentRefs:
23+
- name: gateway-1
24+
sectionName: foo
25+
# This gateway (and its gatewayclass) is created through `status-cleanup-gateway-different-gwc.yaml` manifest.
26+
- name: gateway-2
27+
sectionName: foo
28+
rules:
29+
- backendRefs:
30+
- name: infra-backend-v1
31+
port: 8080
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apiVersion: gateway.networking.k8s.io/v1beta1
2+
kind: Gateway
3+
metadata:
4+
name: gateway-1
5+
namespace: gateway-conformance-infra
6+
spec:
7+
gatewayClassName: "{GATEWAY_CLASS_NAME}"
8+
listeners:
9+
- name: foo
10+
protocol: TCP
11+
port: 8080
12+
allowedRoutes:
13+
kinds:
14+
- kind: TCPRoute
15+
---
16+
apiVersion: gateway.networking.k8s.io/v1beta1
17+
kind: Gateway
18+
metadata:
19+
name: gateway-2
20+
namespace: gateway-conformance-infra
21+
spec:
22+
gatewayClassName: "{GATEWAY_CLASS_NAME}"
23+
listeners:
24+
- name: foo
25+
protocol: TCP
26+
port: 8080
27+
allowedRoutes:
28+
kinds:
29+
- kind: TCPRoute
30+
---
31+
apiVersion: gateway.networking.k8s.io/v1alpha2
32+
kind: TCPRoute
33+
metadata:
34+
name: tcp-route-status-cleanup
35+
namespace: gateway-conformance-infra
36+
spec:
37+
parentRefs:
38+
- name: gateway-1
39+
sectionName: foo
40+
- name: gateway-2
41+
sectionName: foo
42+
rules:
43+
- backendRefs:
44+
- name: infra-backend-v1
45+
port: 8080
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: gateway.networking.k8s.io/v1alpha2
2+
kind: TCPRoute
3+
metadata:
4+
name: tcp-route-status-cleanup
5+
namespace: gateway-conformance-infra
6+
spec:
7+
parentRefs:
8+
- name: gateway-1
9+
sectionName: foo
10+
rules:
11+
- backendRefs:
12+
- name: infra-backend-v1
13+
port: 8080
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: GatewayClass
3+
metadata:
4+
name: status-cleanup
5+
spec:
6+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
7+
---
8+
apiVersion: gateway.networking.k8s.io/v1beta1
9+
kind: Gateway
10+
metadata:
11+
name: gateway-2
12+
namespace: gateway-conformance-infra
13+
spec:
14+
gatewayClassName: status-cleanup
15+
listeners:
16+
- name: foo
17+
protocol: TCP
18+
port: 8080
19+
allowedRoutes:
20+
kinds:
21+
- kind: TCPRoute
22+
infrastructure:
23+
parametersRef:
24+
group: gateway.envoyproxy.io
25+
kind: EnvoyProxy
26+
name: status-cleanup
27+
---
28+
apiVersion: gateway.envoyproxy.io/v1alpha1
29+
kind: EnvoyProxy
30+
metadata:
31+
name: status-cleanup
32+
namespace: gateway-conformance-infra
33+
spec:
34+
ipFamily: IPv4
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright Envoy Gateway Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
// The full text of the Apache license is available in the LICENSE file at
4+
// the root of the repo.
5+
6+
//go:build e2e
7+
8+
package tests
9+
10+
import (
11+
"context"
12+
"reflect"
13+
"testing"
14+
"time"
15+
16+
"github.com/stretchr/testify/require"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
"k8s.io/apimachinery/pkg/types"
19+
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
20+
"sigs.k8s.io/gateway-api/conformance/utils/suite"
21+
22+
egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
23+
"github.com/envoyproxy/gateway/internal/gatewayapi"
24+
"github.com/envoyproxy/gateway/internal/gatewayapi/resource"
25+
)
26+
27+
func init() {
28+
ConformanceTests = append(ConformanceTests, PolicyStatusCleanupSameGatewayClass, PolicyStatusCleanupMultipleGatewayClasses)
29+
}
30+
31+
var PolicyStatusCleanupSameGatewayClass = suite.ConformanceTest{
32+
ShortName: "PolicyStatusCleanupSameGatewayClass",
33+
Description: "Testing Policy Status Cleanup With Ancestors of The Same GatewayClass",
34+
Manifests: []string{"testdata/policy-status-cleanup-multiple-ancestors-same-gwc.yaml"},
35+
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
36+
t.Run("PolicyStatusCleanup", func(t *testing.T) {
37+
ns := "gateway-conformance-infra"
38+
gw1NN, gw2NN := types.NamespacedName{Name: "same-namespace", Namespace: ns}, types.NamespacedName{Name: "all-namespaces", Namespace: ns}
39+
40+
policyNamespacedName := types.NamespacedName{Name: "backendtrafficpolicy-multiple-ancestors-same-gwc", Namespace: ns}
41+
42+
// Check the policy has two ancestors in its status.
43+
BackendTrafficPolicyMustBeAcceptedByAllAncestors(t,
44+
suite.Client,
45+
policyNamespacedName,
46+
suite.ControllerName,
47+
gwapiv1.ParentReference{
48+
Group: gatewayapi.GroupPtr(gwapiv1.GroupName),
49+
Kind: gatewayapi.KindPtr(resource.KindGateway),
50+
Namespace: gatewayapi.NamespacePtr(gw1NN.Namespace),
51+
Name: gwapiv1.ObjectName(gw1NN.Name),
52+
},
53+
gwapiv1.ParentReference{
54+
Group: gatewayapi.GroupPtr(gwapiv1.GroupName),
55+
Kind: gatewayapi.KindPtr(resource.KindGateway),
56+
Namespace: gatewayapi.NamespacePtr(gw2NN.Namespace),
57+
Name: gwapiv1.ObjectName(gw2NN.Name),
58+
},
59+
)
60+
61+
// Change the policy to have a single ancestor, and check its status.
62+
suite.Applier.MustApplyWithCleanup(t, suite.Client, suite.TimeoutConfig, "testdata/policy-status-cleanup-single-ancestor.yaml", false)
63+
BackendTrafficPolicyMustBeAcceptedByAllAncestors(t,
64+
suite.Client,
65+
policyNamespacedName,
66+
suite.ControllerName,
67+
gwapiv1.ParentReference{
68+
Group: gatewayapi.GroupPtr(gwapiv1.GroupName),
69+
Kind: gatewayapi.KindPtr(resource.KindGateway),
70+
Namespace: gatewayapi.NamespacePtr(gw1NN.Namespace),
71+
Name: gwapiv1.ObjectName(gw1NN.Name),
72+
},
73+
)
74+
75+
// Change the policy to have a single ancestor that doesn't exist, and check its status to be empty.
76+
suite.Applier.MustApplyWithCleanup(t, suite.Client, suite.TimeoutConfig, "testdata/policy-status-cleanup-no-ancestor.yaml", false)
77+
BackendTrafficPolicyMustBeAcceptedByAllAncestors(t,
78+
suite.Client,
79+
policyNamespacedName,
80+
suite.ControllerName,
81+
[]gwapiv1.ParentReference{}...,
82+
)
83+
84+
// Update the policy status to artificially simulate another controller, and make sure it persists.
85+
policy := &egv1a1.BackendTrafficPolicy{}
86+
err := suite.Client.Get(context.Background(), policyNamespacedName, policy)
87+
require.NoErrorf(t, err, "error getting BackendTrafficPolicy %s", policyNamespacedName.String())
88+
desiredStatusAncestors := []gwapiv1.PolicyAncestorStatus{
89+
{
90+
AncestorRef: gwapiv1.ParentReference{
91+
Name: "other-gateway",
92+
},
93+
ControllerName: "istio.io/gateway-controller",
94+
Conditions: []metav1.Condition{
95+
{
96+
Type: string(gwapiv1.PolicyConditionAccepted),
97+
Status: metav1.ConditionFalse,
98+
LastTransitionTime: metav1.NewTime(time.Now()),
99+
Reason: string(gwapiv1.PolicyConditionAccepted),
100+
},
101+
},
102+
},
103+
}
104+
policy.Status.Ancestors = desiredStatusAncestors
105+
err = suite.Client.Status().Update(context.Background(), policy)
106+
require.NoErrorf(t, err, "error updating BackendTrafficPolicy status %s", policyNamespacedName.String())
107+
108+
// To be extra sure, do the check 3 times with 1 second intervals.
109+
for range 3 {
110+
err := suite.Client.Get(context.Background(), policyNamespacedName, policy)
111+
require.NoErrorf(t, err, "error getting BackendTrafficPolicy %s", policyNamespacedName.String())
112+
reflect.DeepEqual(policy.Status.Ancestors, desiredStatusAncestors)
113+
time.Sleep(time.Second)
114+
}
115+
})
116+
},
117+
}
118+
119+
var PolicyStatusCleanupMultipleGatewayClasses = suite.ConformanceTest{
120+
ShortName: "PolicyStatusCleanupMultipleGatewayClasses",
121+
Description: "Testing Policy Status Cleanup With Ancestors of Multiple GatewayClasses",
122+
Manifests: []string{"testdata/policy-status-cleanup-multiple-ancestors-multiple-gwcs.yaml"},
123+
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
124+
t.Run("PolicyStatusCleanup", func(t *testing.T) {
125+
// Create the second gateway of a different gatewayclass, which the backendtrafficpolicy is already attached to.
126+
prevGwc := suite.Applier.GatewayClass
127+
suite.Applier.GatewayClass = "status-cleanup"
128+
suite.Applier.MustApplyWithCleanup(t, suite.Client, suite.TimeoutConfig, "testdata/status-cleanup-gateway-different-gwc.yaml", true)
129+
suite.Applier.GatewayClass = prevGwc
130+
131+
ns := "gateway-conformance-infra"
132+
gw1NN, gw2NN := types.NamespacedName{Name: "same-namespace", Namespace: ns}, types.NamespacedName{Name: "gateway-2", Namespace: ns}
133+
134+
// Check the policy has two ancestors in its status.
135+
BackendTrafficPolicyMustBeAcceptedByAllAncestors(t,
136+
suite.Client,
137+
types.NamespacedName{Name: "backendtrafficpolicy-multiple-ancestors-same-gwc", Namespace: ns},
138+
suite.ControllerName,
139+
gwapiv1.ParentReference{
140+
Group: gatewayapi.GroupPtr(gwapiv1.GroupName),
141+
Kind: gatewayapi.KindPtr(resource.KindGateway),
142+
Namespace: gatewayapi.NamespacePtr(gw1NN.Namespace),
143+
Name: gwapiv1.ObjectName(gw1NN.Name),
144+
},
145+
gwapiv1.ParentReference{
146+
Group: gatewayapi.GroupPtr(gwapiv1.GroupName),
147+
Kind: gatewayapi.KindPtr(resource.KindGateway),
148+
Namespace: gatewayapi.NamespacePtr(gw2NN.Namespace),
149+
Name: gwapiv1.ObjectName(gw2NN.Name),
150+
},
151+
)
152+
153+
// Change the policy to have a single ancestor, and check its status.
154+
suite.Applier.MustApplyWithCleanup(t, suite.Client, suite.TimeoutConfig, "testdata/policy-status-cleanup-single-ancestor.yaml", false)
155+
BackendTrafficPolicyMustBeAcceptedByAllAncestors(t,
156+
suite.Client,
157+
types.NamespacedName{Name: "backendtrafficpolicy-multiple-ancestors-same-gwc", Namespace: ns},
158+
suite.ControllerName,
159+
gwapiv1.ParentReference{
160+
Group: gatewayapi.GroupPtr(gwapiv1.GroupName),
161+
Kind: gatewayapi.KindPtr(resource.KindGateway),
162+
Namespace: gatewayapi.NamespacePtr(gw1NN.Namespace),
163+
Name: gwapiv1.ObjectName(gw1NN.Name),
164+
},
165+
)
166+
})
167+
},
168+
}

0 commit comments

Comments
 (0)