diff --git a/internal/gatewayapi/resource.go b/internal/gatewayapi/resource.go index 0cea818a637..691e61b6a06 100644 --- a/internal/gatewayapi/resource.go +++ b/internal/gatewayapi/resource.go @@ -21,6 +21,20 @@ import ( type XdsIRMap map[string]*ir.Xds type InfraIRMap map[string]*ir.Infra +// ControllerResources holds all the GatewayAPI resources per GatewayClass +type ControllerResources []*Resources + +// DeepCopy creates a new ControllerResources. +// It is handwritten since the tooling was unable to copy into a new slice +func (in ControllerResources) DeepCopy() *ControllerResources { + if in == nil { + return nil + } + out := make(ControllerResources, len(in)) + copy(out, in) + return &out +} + // Resources holds the Gateway API and related // resources that the translators needs as inputs. // +k8s:deepcopy-gen=true diff --git a/internal/gatewayapi/runner/runner.go b/internal/gatewayapi/runner/runner.go index a0ae3d07634..a9cc6e555c6 100644 --- a/internal/gatewayapi/runner/runner.go +++ b/internal/gatewayapi/runner/runner.go @@ -49,7 +49,7 @@ func (r *Runner) Start(ctx context.Context) (err error) { func (r *Runner) subscribeAndTranslate(ctx context.Context) { message.HandleSubscription(message.Metadata{Runner: string(v1alpha1.LogComponentGatewayAPIRunner), Message: "provider-resources"}, r.ProviderResources.GatewayAPIResources.Subscribe(ctx), - func(update message.Update[string, *[]*gatewayapi.Resources], errChan chan error) { + func(update message.Update[string, *gatewayapi.ControllerResources], errChan chan error) { r.Logger.Info("received an update") val := update.Value // There is only 1 key which is the controller name diff --git a/internal/message/types.go b/internal/message/types.go index 5034cad1f89..2d7fdfc2c6e 100644 --- a/internal/message/types.go +++ b/internal/message/types.go @@ -21,7 +21,7 @@ import ( type ProviderResources struct { // GatewayAPIResources is a map from a GatewayClass name to // a group of gateway API and other related resources. - GatewayAPIResources watchable.Map[string, *[]*gatewayapi.Resources] + GatewayAPIResources watchable.Map[string, *gatewayapi.ControllerResources] // GatewayAPIStatuses is a group of gateway api // resource statuses maps. @@ -43,6 +43,16 @@ func (p *ProviderResources) GetResources() []*gatewayapi.Resources { return nil } +func (p *ProviderResources) GetResourcesByGatewayClass(name string) *gatewayapi.Resources { + for _, r := range p.GetResources() { + if r != nil && r.GatewayClass != nil && r.GatewayClass.Name == name { + return r + } + } + + return nil +} + func (p *ProviderResources) GetResourcesKey() string { if p.GatewayAPIResources.Len() == 0 { return "" diff --git a/internal/provider/kubernetes/controller.go b/internal/provider/kubernetes/controller.go index aca1f7c6aa0..1d700daf034 100644 --- a/internal/provider/kubernetes/controller.go +++ b/internal/provider/kubernetes/controller.go @@ -169,7 +169,7 @@ func (r *gatewayAPIReconciler) Reconcile(ctx context.Context, _ reconcile.Reques return reconcile.Result{}, nil } - gwcResources := make([]*gatewayapi.Resources, 0, len(acceptedGCs)) + gwcResources := make(gatewayapi.ControllerResources, 0, len(acceptedGCs)) for _, acceptedGC := range acceptedGCs { // Initialize resource types. acceptedGC := acceptedGC diff --git a/internal/provider/kubernetes/kubernetes_test.go b/internal/provider/kubernetes/kubernetes_test.go index fc4cc7a39a0..350818cb182 100644 --- a/internal/provider/kubernetes/kubernetes_test.go +++ b/internal/provider/kubernetes/kubernetes_test.go @@ -366,10 +366,8 @@ func testGatewayScheduledStatus(ctx context.Context, t *testing.T, provider *Pro return cli.Get(ctx, key, gw) == nil }, defaultWait, defaultTick) - gatewayClassResources, _ := resources.GatewayAPIResources.Load(egv1a1.GatewayControllerName) - assert.NotNil(t, gatewayClassResources) - - res := (*gatewayClassResources)[gc.Name] + res := resources.GetResourcesByGatewayClass(gc.Name) + assert.NotNil(t, res) // Only check if the spec is equal // The watchable map will not store a resource // with an updated status if the spec has not changed @@ -903,10 +901,8 @@ func testHTTPRoute(ctx context.Context, t *testing.T, provider *Provider, resour return ok && len(res.HTTPRoutes) != 0 }, defaultWait, defaultTick) - gatewayClassResources, _ := resources.GatewayAPIResources.Load(egv1a1.GatewayControllerName) - assert.NotNil(t, gatewayClassResources) - - res := (*gatewayClassResources)[gc.Name] + res := resources.GetResourcesByGatewayClass(gc.Name) + assert.NotNil(t, res) assert.Equal(t, &testCase.route, res.HTTPRoutes[0]) // Ensure the HTTPRoute Namespace is in the Namespace resource map. @@ -1054,10 +1050,8 @@ func testTLSRoute(ctx context.Context, t *testing.T, provider *Provider, resourc return ok && len(res.TLSRoutes) != 0 }, defaultWait, defaultTick) - gatewayClassResources, _ := resources.GatewayAPIResources.Load(egv1a1.GatewayControllerName) - assert.NotNil(t, gatewayClassResources) - - res, _ := (*gatewayClassResources)[gc.Name] + res := resources.GetResourcesByGatewayClass(gc.Name) + assert.NotNil(t, res) assert.Equal(t, &testCase.route, res.TLSRoutes[0]) // Ensure the HTTPRoute Namespace is in the Namespace resource map. @@ -1593,13 +1587,8 @@ func TestNamespaceSelectorProvider(t *testing.T) { } func waitUntilGatewayClassResourcesAreReady(resources *message.ProviderResources, gatewayClassName string) (*gatewayapi.Resources, bool) { - gatewayClassResources, ok := resources.GatewayAPIResources.Load(egv1a1.GatewayControllerName) - if !ok { - return nil, false - } - - res, ok := (*gatewayClassResources)[gatewayClassName] - if !ok { + res := resources.GetResourcesByGatewayClass(gatewayClassName) + if res == nil { return nil, false }