Skip to content

Commit 804d644

Browse files
authored
BackendTLSPolicy conformance tests for observedGeneration bump (#3997)
* BackendTLSPolicy conformance tests for observedGeneration bump Signed-off-by: Norwin Schnyder <[email protected]> * Apply PR feedback Signed-off-by: Norwin Schnyder <[email protected]> --------- Signed-off-by: Norwin Schnyder <[email protected]>
1 parent 02e4952 commit 804d644

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tests
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/types"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
"sigs.k8s.io/gateway-api/apis/v1alpha2"
29+
"sigs.k8s.io/gateway-api/apis/v1alpha3"
30+
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
31+
"sigs.k8s.io/gateway-api/conformance/utils/suite"
32+
"sigs.k8s.io/gateway-api/pkg/features"
33+
)
34+
35+
func init() {
36+
ConformanceTests = append(ConformanceTests, BackendTLSPolicyObservedGenerationBump)
37+
}
38+
39+
var BackendTLSPolicyObservedGenerationBump = suite.ConformanceTest{
40+
ShortName: "BackendTLSPolicyObservedGenerationBump",
41+
Description: "A BackendTLSPolicy in the gateway-conformance-infra namespace should update the observedGeneration in all of it's Status.Conditions after an update to the spec",
42+
Features: []features.FeatureName{
43+
features.SupportGateway,
44+
features.SupportHTTPRoute,
45+
features.SupportBackendTLSPolicy,
46+
},
47+
Manifests: []string{"tests/backendtlspolicy-observed-generation-bump.yaml"},
48+
Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
49+
ns := "gateway-conformance-infra"
50+
policyNN := types.NamespacedName{Name: "observed-generation-bump", Namespace: ns}
51+
gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns}
52+
53+
t.Run("observedGeneration should increment", func(t *testing.T) {
54+
ctx, cancel := context.WithTimeout(context.Background(), suite.TimeoutConfig.LatestObservedGenerationSet)
55+
defer cancel()
56+
57+
namespaces := []string{"gateway-conformance-infra"}
58+
kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces)
59+
60+
original := &v1alpha3.BackendTLSPolicy{}
61+
err := suite.Client.Get(ctx, policyNN, original)
62+
require.NoError(t, err, "error getting HTTPRoute")
63+
64+
// Sanity check
65+
kubernetes.BackendTLSPolicyMustHaveLatestConditions(t, original)
66+
67+
mutate := original.DeepCopy()
68+
mutate.Spec.Validation.Hostname = "foo.example.com"
69+
err = suite.Client.Patch(ctx, mutate, client.MergeFrom(original))
70+
require.NoError(t, err, "error patching the BackendTLSPolicy")
71+
72+
kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, metav1.Condition{
73+
Type: string(v1alpha2.PolicyConditionAccepted),
74+
Status: metav1.ConditionTrue,
75+
Reason: "", // any reason
76+
})
77+
78+
updated := &v1alpha3.BackendTLSPolicy{}
79+
err = suite.Client.Get(ctx, policyNN, updated)
80+
require.NoError(t, err, "error getting BackendTLSPolicy")
81+
82+
// Sanity check
83+
kubernetes.BackendTLSPolicyMustHaveLatestConditions(t, updated)
84+
85+
require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update")
86+
})
87+
},
88+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apiVersion: gateway.networking.k8s.io/v1
2+
kind: HTTPRoute
3+
metadata:
4+
name: backendtlspolicy-observed-generation-bump
5+
namespace: gateway-conformance-infra
6+
spec:
7+
parentRefs:
8+
- name: same-namespace
9+
rules:
10+
- backendRefs:
11+
- name: observed-generation-bump-test
12+
port: 443
13+
---
14+
apiVersion: v1
15+
kind: Service
16+
metadata:
17+
name: observed-generation-bump-test
18+
namespace: gateway-conformance-infra
19+
spec:
20+
selector:
21+
app: observed-generation-bump-test
22+
ports:
23+
- name: "https"
24+
protocol: TCP
25+
port: 443
26+
targetPort: 8443
27+
---
28+
apiVersion: gateway.networking.k8s.io/v1alpha3
29+
kind: BackendTLSPolicy
30+
metadata:
31+
name: observed-generation-bump
32+
namespace: gateway-conformance-infra
33+
spec:
34+
targetRefs:
35+
- group: ""
36+
kind: Service
37+
name: "observed-generation-bump-test"
38+
sectionName: "https"
39+
validation:
40+
caCertificateRefs:
41+
- group: ""
42+
kind: ConfigMap
43+
# This ConfigMap is generated dynamically by the test suite.
44+
name: "backend-tls-checks-certificate"
45+
hostname: "abc.example.com"

conformance/utils/kubernetes/helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,15 @@ func BackendTLSPolicyMustHaveCondition(t *testing.T, client client.Client, timeo
10261026

10271027
require.NoErrorf(t, waitErr, "error waiting for BackendTLSPolicy status to have a Condition %v", condition)
10281028
}
1029+
1030+
// BackendTLSPolicyMustHaveLatestConditions will fail the test if there are
1031+
// conditions that were not updated
1032+
func BackendTLSPolicyMustHaveLatestConditions(t *testing.T, r *v1alpha3.BackendTLSPolicy) {
1033+
t.Helper()
1034+
1035+
for _, ancestor := range r.Status.Ancestors {
1036+
if err := ConditionsHaveLatestObservedGeneration(r, ancestor.Conditions); err != nil {
1037+
tlog.Fatalf(t, "BackendTLSPolicy(controller=%v, ancestorRef=%#v) %v", ancestor.ControllerName, parentRefToString(ancestor.AncestorRef), err)
1038+
}
1039+
}
1040+
}

0 commit comments

Comments
 (0)