Skip to content

Commit

Permalink
[Conformance]: Adds GatewayClass SupportedVersion Test
Browse files Browse the repository at this point in the history
Adds a test to ensure implementations conform to the GatewayClass
SupportedVersion status condition.

Fixes kubernetes-sigs#3367

Signed-off-by: Daneyon Hansen <[email protected]>
  • Loading branch information
danehans committed Sep 27, 2024
1 parent 9f7dd62 commit 0b0c265
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 18 deletions.
103 changes: 103 additions & 0 deletions conformance/tests/gatewayclass-supported-version-condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tests

import (
"context"
"testing"

"github.com/stretchr/testify/require"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
"sigs.k8s.io/gateway-api/pkg/consts"
"sigs.k8s.io/gateway-api/pkg/features"
)

func init() {
ConformanceTests = append(ConformanceTests, GatewayClassSupportedVersionCondition)
}

var GatewayClassSupportedVersionCondition = suite.ConformanceTest{
ShortName: "GatewayClassSupportedVersionCondition",
Features: []features.FeatureName{
features.SupportGateway,
},
Description: "A GatewayClass should set the SupportedVersion condition based on the presence and version of Gateway API CRDs in the cluster",
Manifests: []string{"tests/gatewayclass-supported-version-condition.yaml"},
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
gwc := types.NamespacedName{Name: "gatewayclass-supported-version-condition"}

t.Run("SupportedVersion condition should be set correctly", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.DefaultTestTimeout)
defer cancel()

// Ensure GatewayClass conditions are as expected before proceeding
kubernetes.GWCMustHaveAcceptedConditionTrue(t, s.Client, s.TimeoutConfig, gwc.Name)
kubernetes.GWCMustHaveSupportedVersionConditionAny(t, s.Client, s.TimeoutConfig, gwc.Name)

// Retrieve the GatewayClass CRD
crd := &apiextv1.CustomResourceDefinition{}
crdName := types.NamespacedName{Name: "gatewayclasses.gateway.networking.k8s.io"}
err := s.Client.Get(ctx, crdName, crd)
require.NoErrorf(t, err, "error getting GatewayClass CRD: %v", err)

if crd.Annotations != nil {
// Remove the bundle version annotation if it exists
if _, exists := crd.Annotations[consts.BundleVersionAnnotation]; !exists {
t.Fatalf("Annotation %q does not exist on CRD %s", consts.BundleVersionAnnotation, crdName)
}
delete(crd.Annotations, consts.BundleVersionAnnotation)
if err := s.Client.Update(ctx, crd); err != nil {
t.Fatalf("Failed to update CRD %s: %v", crdName, err)
}
}

// Ensure the SupportedVersion status condition is false
kubernetes.GWCMustHaveSupportedVersionConditionFalse(t, s.Client, s.TimeoutConfig, gwc.Name)

// Add the bundle version annotation
crd.Annotations[consts.BundleVersionAnnotation] = consts.BundleVersion
if err := s.Client.Update(ctx, crd); err != nil {
t.Fatalf("Failed to update CRD %s: %v", crdName, err)
}

// Ensure the SupportedVersion status condition is true
kubernetes.GWCMustHaveSupportedVersionConditionTrue(t, s.Client, s.TimeoutConfig, gwc.Name)

// Set the bundle version annotation to an unsupported version
crd.Annotations[consts.BundleVersionAnnotation] = "v0.0.0"
if err := s.Client.Update(ctx, crd); err != nil {
t.Fatalf("Failed to update CRD %s: %v", crdName, err)
}

// Ensure the SupportedVersion is false
kubernetes.GWCMustHaveSupportedVersionConditionFalse(t, s.Client, s.TimeoutConfig, gwc.Name)

// Add the bundle version annotation back
crd.Annotations[consts.BundleVersionAnnotation] = consts.BundleVersion
if err := s.Client.Update(ctx, crd); err != nil {
t.Fatalf("Failed to update CRD %s: %v", crdName, err)
}

// Ensure the SupportedVersion is true
kubernetes.GWCMustHaveSupportedVersionConditionTrue(t, s.Client, s.TimeoutConfig, gwc.Name)
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: gatewayclass-supported-version-condition
spec:
controllerName: "{GATEWAY_CONTROLLER_NAME}"
8 changes: 8 additions & 0 deletions conformance/utils/config/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type TimeoutConfig struct {
// Max value for conformant implementation: None
GWCMustBeAccepted time.Duration

// GWCMustBeSupportedVersion represents the maximum time for a GatewayClass to have a SupportedVersion condition set to true.
// Max value for conformant implementation: None
GWCMustBeSupportedVersion time.Duration

// HTTPRouteMustNotHaveParents represents the maximum time for an HTTPRoute to have either no parents or a single parent that is not accepted.
// Max value for conformant implementation: None
HTTPRouteMustNotHaveParents time.Duration
Expand Down Expand Up @@ -115,6 +119,7 @@ func DefaultTimeoutConfig() TimeoutConfig {
GatewayStatusMustHaveListeners: 60 * time.Second,
GatewayListenersMustHaveConditions: 60 * time.Second,
GWCMustBeAccepted: 180 * time.Second,
GWCMustBeSupportedVersion: 180 * time.Second,
HTTPRouteMustNotHaveParents: 60 * time.Second,
HTTPRouteMustHaveCondition: 60 * time.Second,
TLSRouteMustHaveCondition: 60 * time.Second,
Expand Down Expand Up @@ -155,6 +160,9 @@ func SetupTimeoutConfig(timeoutConfig *TimeoutConfig) {
if timeoutConfig.GWCMustBeAccepted == 0 {
timeoutConfig.GWCMustBeAccepted = defaultTimeoutConfig.GWCMustBeAccepted
}
if timeoutConfig.GWCMustBeSupportedVersion == 0 {
timeoutConfig.GWCMustBeSupportedVersion = defaultTimeoutConfig.GWCMustBeSupportedVersion
}
if timeoutConfig.HTTPRouteMustNotHaveParents == 0 {
timeoutConfig.HTTPRouteMustNotHaveParents = defaultTimeoutConfig.HTTPRouteMustNotHaveParents
}
Expand Down
90 changes: 90 additions & 0 deletions conformance/utils/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,26 @@ func GWCMustHaveAcceptedConditionTrue(t *testing.T, c client.Client, timeoutConf
return gwcMustBeAccepted(t, c, timeoutConfig, gwcName, string(metav1.ConditionTrue))
}

// GWCMustHaveSupportedVersionConditionTrue waits until the specified GatewayClass has a SupportedVersion condition set with a status value equal to True.
func GWCMustHaveSupportedVersionConditionTrue(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwcName string) string {
return gwcMustBeSupportedVersion(t, c, timeoutConfig, gwcName, string(metav1.ConditionTrue))
}

// GWCMustHaveSupportedVersionConditionFalse waits until the specified GatewayClass has a SupportedVersion condition set with a status value equal to False.
func GWCMustHaveSupportedVersionConditionFalse(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwcName string) string {
return gwcMustBeSupportedVersion(t, c, timeoutConfig, gwcName, string(metav1.ConditionFalse))
}

// GWCMustHaveAcceptedConditionAny waits until the specified GatewayClass has an Accepted condition set with a status set to any value.
func GWCMustHaveAcceptedConditionAny(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwcName string) string {
return gwcMustBeAccepted(t, c, timeoutConfig, gwcName, "")
}

// GWCMustHaveSupportedVersionConditionAny waits until the specified GatewayClass has a SupportedVersion condition set to any value.
func GWCMustHaveSupportedVersionConditionAny(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwcName string) string {
return gwcMustBeSupportedVersion(t, c, timeoutConfig, gwcName, "")
}

// gwcMustBeAccepted waits until the specified GatewayClass has an Accepted
// condition set. Passing an empty status string means that any value
// will be accepted. It also returns the ControllerName for the GatewayClass.
Expand Down Expand Up @@ -112,6 +127,35 @@ func gwcMustBeAccepted(t *testing.T, c client.Client, timeoutConfig config.Timeo
return controllerName
}

// gwcMustBeSupportedVersion waits until the specified GatewayClass has a SupportedVersion condition set.
// Passing an empty status string means that any value will be accepted. It also returns the ControllerName
// for the GatewayClass. This will cause the test to halt if the specified timeout is exceeded.
func gwcMustBeSupportedVersion(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwcName, expectedStatus string) string {
t.Helper()

var controllerName string
waitErr := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, timeoutConfig.GWCMustBeSupportedVersion, true, func(ctx context.Context) (bool, error) {
gwc := &gatewayv1.GatewayClass{}
err := c.Get(ctx, types.NamespacedName{Name: gwcName}, gwc)
if err != nil {
return false, fmt.Errorf("error fetching GatewayClass: %w", err)
}

controllerName = string(gwc.Spec.ControllerName)

if err := ConditionsHaveLatestObservedGeneration(gwc, gwc.Status.Conditions); err != nil {
tlog.Log(t, "GatewayClass", err)
return false, nil
}

// Passing an empty string as the Reason means that any Reason will do.
return findConditionInList(t, gwc.Status.Conditions, "SupportedVersion", expectedStatus, ""), nil
})
require.NoErrorf(t, waitErr, "error waiting for %s GatewayClass to have SupportedVersion condition to be set: %v", gwcName, waitErr)

return controllerName
}

// GatewayMustHaveLatestConditions waits until the specified Gateway has
// all conditions updated with the latest observed generation.
func GatewayMustHaveLatestConditions(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwNN types.NamespacedName) {
Expand Down Expand Up @@ -249,6 +293,52 @@ func NamespacesMustBeReady(t *testing.T, c client.Client, timeoutConfig config.T
require.NoErrorf(t, waitErr, "error waiting for %s namespaces to be ready", strings.Join(namespaces, ", "))
}

// GatewayClassMustHaveCondition checks that the supplied GatewayClass has the supplied Condition,
// halting after the specified timeout is exceeded.
func GatewayClassMustHaveCondition(
t *testing.T,
client client.Client,
timeoutConfig config.TimeoutConfig,
gcName string,
expectedCondition metav1.Condition,
) {
t.Helper()

waitErr := wait.PollUntilContextTimeout(
context.Background(),
1*time.Second,
timeoutConfig.GWCMustBeSupportedVersion,
true,
func(ctx context.Context) (bool, error) {
gc := &gatewayv1.GatewayClass{}
gcNN := types.NamespacedName{
Name: gcName,
}
err := client.Get(ctx, gcNN, gc)
if err != nil {
return false, fmt.Errorf("error fetching GatewayClass: %w", err)
}

if err := ConditionsHaveLatestObservedGeneration(gc, gc.Status.Conditions); err != nil {
return false, err
}

if findConditionInList(t,
gc.Status.Conditions,
expectedCondition.Type,
string(expectedCondition.Status),
expectedCondition.Reason,
) {
return true, nil
}

return false, nil
},
)

require.NoErrorf(t, waitErr, "error waiting for GatewayClass status to have a Condition matching expectations")
}

// GatewayMustHaveCondition checks that the supplied Gateway has the supplied Condition,
// halting after the specified timeout is exceeded.
func GatewayMustHaveCondition(
Expand Down
18 changes: 9 additions & 9 deletions pkg/test/cel/grpcroute_experimental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
rules []gatewayv1.GRPCRouteRule
}{
{
name: "GRPCRoute - Invalid because both percent and fraction are specified",
name: "GRPCRoute - Invalid because both percent and fraction are specified",
wantErrors: []string{"Only one of percent or fraction may be specified in HTTPRequestMirrorFilter"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Expand All @@ -67,15 +67,15 @@ func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
},
Percent: &percent,
Fraction: &gatewayv1.Fraction{
Numerator: 83,
Numerator: 83,
Denominator: &denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Invalid fraction - numerator greater than denominator",
name: "GRPCRoute - Invalid fraction - numerator greater than denominator",
wantErrors: []string{"numerator must be less than or equal to denominator"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Expand All @@ -86,15 +86,15 @@ func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: 1001,
Numerator: 1001,
Denominator: &denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Invalid fraction - denominator is 0",
name: "GRPCRoute - Invalid fraction - denominator is 0",
wantErrors: []string{"spec.rules[0].filters[0].requestMirror.fraction.denominator in body should be greater than or equal to 1"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Expand All @@ -105,15 +105,15 @@ func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: 0,
Numerator: 0,
Denominator: &bad_denominator,
},
},
}},
}},
},
{
name: "GRPCRoute - Invalid fraction - numerator is negative",
name: "GRPCRoute - Invalid fraction - numerator is negative",
wantErrors: []string{"spec.rules[0].filters[0].requestMirror.fraction.numerator in body should be greater than or equal to 0"},
rules: []gatewayv1.GRPCRouteRule{{
Filters: []gatewayv1.GRPCRouteFilter{{
Expand All @@ -124,7 +124,7 @@ func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: -1,
Numerator: -1,
Denominator: &denominator,
},
},
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestGRPCRequestMirrorFilterExperimental(t *testing.T) {
Port: ptrTo(gatewayv1.PortNumber(8081)),
},
Fraction: &gatewayv1.Fraction{
Numerator: 83,
Numerator: 83,
Denominator: &denominator,
},
},
Expand Down
Loading

0 comments on commit 0b0c265

Please sign in to comment.