|
| 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