Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conformance: fetch Gateway in GatewayMustHaveLatestConditions loop #2203

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions conformance/tests/gateway-modify-listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ var GatewayModifyListeners = suite.ConformanceTest{

namespaces := []string{"gateway-conformance-infra"}
kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)

// verify that the implementation is tracking the most recent resource changes
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)
Copy link
Member

@sunjayBhatia sunjayBhatia Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move this after the namespace ready check but before fetching the "original" gateway? (similar for the other tests that have similar logic)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like original is only being used for spec fields, so shouldn't matter too much one way or another, but I'm fine changing the order for readability.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah good point, the original Generation field shouldnt change since the resource shouldnt be updated from when we create it, so I'll defer to you if you think it makes the code more readable to fetch the Gateway after checking the status

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


original := &v1beta1.Gateway{}
err := s.Client.Get(ctx, gwNN, original)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

// verify that the implementation is tracking the most recent resource changes
kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, original)

all := v1beta1.NamespacesFromAll

mutate := original.DeepCopy()
Expand All @@ -78,9 +79,6 @@ var GatewayModifyListeners = suite.ConformanceTest{

// Ensure the generation and observedGeneration sync up
kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)
updated := &v1beta1.Gateway{}
err = s.Client.Get(ctx, gwNN, updated)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

listeners := []v1beta1.ListenerStatus{
{
Expand Down Expand Up @@ -114,7 +112,11 @@ var GatewayModifyListeners = suite.ConformanceTest{
kubernetes.GatewayStatusMustHaveListeners(t, s.Client, s.TimeoutConfig, gwNN, listeners)

// verify that the implementation continues to keep up to date with the resource changes we've been making
kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, updated)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)

updated := &v1beta1.Gateway{}
err = s.Client.Get(ctx, gwNN, updated)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update")
})
Expand All @@ -126,13 +128,14 @@ var GatewayModifyListeners = suite.ConformanceTest{

namespaces := []string{"gateway-conformance-infra"}
kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)

// verify that the implementation is tracking the most recent resource changes
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)

original := &v1beta1.Gateway{}
err := s.Client.Get(ctx, gwNN, original)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

// verify that the implementation is tracking the most recent resource changes
kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, original)

mutate := original.DeepCopy()
require.Equalf(t, 2, len(mutate.Spec.Listeners), "the gateway must have 2 listeners")

Expand All @@ -150,9 +153,6 @@ var GatewayModifyListeners = suite.ConformanceTest{

// Ensure the generation and observedGeneration sync up
kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)
updated := &v1beta1.Gateway{}
err = s.Client.Get(ctx, gwNN, updated)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

listeners := []v1beta1.ListenerStatus{
{
Expand All @@ -173,7 +173,11 @@ var GatewayModifyListeners = suite.ConformanceTest{
kubernetes.GatewayStatusMustHaveListeners(t, s.Client, s.TimeoutConfig, gwNN, listeners)

// verify that the implementation continues to keep up to date with the resource changes we've been making
kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, updated)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)

updated := &v1beta1.Gateway{}
err = s.Client.Get(ctx, gwNN, updated)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update")
})
Expand Down
12 changes: 6 additions & 6 deletions conformance/tests/gateway-observed-generation-bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ var GatewayObservedGenerationBump = suite.ConformanceTest{
namespaces := []string{"gateway-conformance-infra"}
kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)

// Sanity check
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)

original := &v1beta1.Gateway{}
err := s.Client.Get(ctx, gwNN, original)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

// Sanity check
kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, original)

all := v1beta1.NamespacesFromAll

mutate := original.DeepCopy()
Expand All @@ -79,13 +79,13 @@ var GatewayObservedGenerationBump = suite.ConformanceTest{
// Ensure the generation and observedGeneration sync up
kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)

// Sanity check
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)

updated := &v1beta1.Gateway{}
err = s.Client.Get(ctx, gwNN, updated)
require.NoErrorf(t, err, "error getting Gateway: %v", err)

// Sanity check
kubernetes.GatewayMustHaveLatestConditions(t, s.TimeoutConfig, updated)

require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update")
})
},
Expand Down
30 changes: 2 additions & 28 deletions conformance/tests/gateway-routability.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@ package tests

import (
"context"
"fmt"
"regexp"
"testing"
"time"

"github.com/stretchr/testify/require"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/gateway-api/apis/v1beta1"
"sigs.k8s.io/gateway-api/conformance/utils/config"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
)
Expand Down Expand Up @@ -107,7 +102,7 @@ var GatewayUnsupportedRoutability = suite.ConformanceTest{
Description: "A Gateway should set Accepted condition to False when it doesn't support a routability",
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
gwn := types.NamespacedName{Name: "gateway-broken-routability", Namespace: "gateway-conformance-infra"}
GatewayMustHaveLatestConditions(t, s.TimeoutConfig, s.Client, gwn)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwn)

ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout)
defer cancel()
Expand Down Expand Up @@ -164,7 +159,7 @@ var GatewayUnsupportedRoutabilityMutation = suite.ConformanceTest{
}
require.NoErrorf(t, err, "error updating Gateway: %v", err)

GatewayMustHaveLatestConditions(t, s.TimeoutConfig, s.Client, gwn)
kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwn)

err = s.Client.Get(ctx, gwn, gw)
require.NoErrorf(t, err, "error getting Gateway: %v", err)
Expand Down Expand Up @@ -303,24 +298,3 @@ func validateAddresses(t *testing.T, addrs []v1beta1.GatewayStatusAddress, allow
}
}
}

func GatewayMustHaveLatestConditions(t *testing.T, timeoutConfig config.TimeoutConfig, c client.Client, gwName types.NamespacedName) {
t.Helper()
waitErr := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, timeoutConfig.LatestObservedGenerationSet, true, func(ctx context.Context) (bool, error) {
gw := &v1beta1.Gateway{}
err := c.Get(ctx, gwName, gw)
if err != nil {
t.Logf("error fetching Gateway: %v", err)
return false, fmt.Errorf("error fetching Gateway: %w", err)
}

if err := kubernetes.ConditionsHaveLatestObservedGeneration(gw, gw.Status.Conditions); err != nil {
t.Logf("Gateway %s/%s latest conditions not set yet: %v", gw.Namespace, gw.Name, err)
return false, nil
}

return true, nil
})

require.NoErrorf(t, waitErr, "error waiting for %s Gateway to have Latest ObservedGeneration to be set: %v", gwName, waitErr)
}
16 changes: 11 additions & 5 deletions conformance/utils/kubernetes/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,26 @@ func gwcMustBeAccepted(t *testing.T, c client.Client, timeoutConfig config.Timeo
}

// GatewayMustHaveLatestConditions waits until the specified Gateway has
// the latest conditions to set.
func GatewayMustHaveLatestConditions(t *testing.T, timeoutConfig config.TimeoutConfig, gw *v1beta1.Gateway) {
// all conditions updated with the latest observed generation.
func GatewayMustHaveLatestConditions(t *testing.T, c client.Client, timeoutConfig config.TimeoutConfig, gwNN types.NamespacedName) {
t.Helper()

waitErr := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, timeoutConfig.LatestObservedGenerationSet, true, func(_ context.Context) (bool, error) {
waitErr := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, timeoutConfig.LatestObservedGenerationSet, true, func(ctx context.Context) (bool, error) {
gw := &v1beta1.Gateway{}
err := c.Get(ctx, gwNN, gw)
if err != nil {
return false, fmt.Errorf("error fetching Gateway: %w", err)
}

if err := ConditionsHaveLatestObservedGeneration(gw, gw.Status.Conditions); err != nil {
t.Logf("Gateway %s/%s latest conditions not set yet: %v", gw.Namespace, gw.Name, err)
t.Logf("Gateway %s latest conditions not set yet: %v", gwNN.String(), err)
return false, nil
}

return true, nil
})

require.NoErrorf(t, waitErr, "error waiting for %s Gateway to have Latest ObservedGeneration to be set: %v", gw.Name, waitErr)
require.NoErrorf(t, waitErr, "error waiting for Gateway %s to have Latest ObservedGeneration to be set: %v", gwNN.String(), waitErr)
}

// GatewayClassMustHaveLatestConditions will fail the test if there are
Expand Down