Skip to content
Open
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
5 changes: 5 additions & 0 deletions pkg/kthena-router/controller/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"fmt"
"reflect"
"sync/atomic"
"time"

Expand Down Expand Up @@ -187,6 +188,10 @@ func (c *HTTPRouteController) syncHandler(key string) error {
}
}
if gatewayPending {
oldRoute := c.store.GetHTTPRoute(key)
if oldRoute != nil && !reflect.DeepEqual(oldRoute.Spec.ParentRefs, httpRoute.Spec.ParentRefs) {
_ = c.store.DeleteHTTPRoute(key)
}
return fmt.Errorf("gateway not synced yet")
}
klog.V(4).Infof("Skipping HTTPRoute %s/%s: does not reference kthena-router Gateway", namespace, name)
Expand Down
51 changes: 51 additions & 0 deletions pkg/kthena-router/controller/httproute_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,54 @@ func TestHTTPRouteController_MultipleParentRefs_FirstPending(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, store.GetHTTPRoute(ns+"/route-multi"))
}

func TestHTTPRouteController_SyncHandler_DeletesOldRouteWhenNewGatewayPending(t *testing.T) {
gatewayClient := gatewayfake.NewSimpleClientset()
gatewayInformerFactory := gatewayinformers.NewSharedInformerFactory(gatewayClient, 0)
store := datastore.New()

ctx := context.Background()
ns := "default"
kind := gatewayv1.Kind("Gateway")

oldRoute := &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: "route"},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{
{Kind: &kind, Name: gatewayv1.ObjectName("gateway-a")},
},
},
},
}
assert.NoError(t, store.AddOrUpdateHTTPRoute(oldRoute))

gw := &gatewayv1.Gateway{
ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: "gateway-b"},
Spec: gatewayv1.GatewaySpec{
GatewayClassName: gatewayv1.ObjectName(DefaultGatewayClassName),
},
}
_, err := gatewayClient.GatewayV1().Gateways(ns).Create(ctx, gw, metav1.CreateOptions{})
assert.NoError(t, err)

httpRoute := oldRoute.DeepCopy()
httpRoute.Spec.ParentRefs = []gatewayv1.ParentReference{
{Kind: &kind, Name: gatewayv1.ObjectName("gateway-b")},
}
_, err = gatewayClient.GatewayV1().HTTPRoutes(ns).Create(ctx, httpRoute, metav1.CreateOptions{})
assert.NoError(t, err)

ctrl := NewHTTPRouteController(gatewayInformerFactory, store)
stop := make(chan struct{})
defer close(stop)
gatewayInformerFactory.Start(stop)

if !cache.WaitForCacheSync(stop, gatewayInformerFactory.Gateway().V1().HTTPRoutes().Informer().HasSynced, gatewayInformerFactory.Gateway().V1().Gateways().Informer().HasSynced) {
t.Fatal("cache sync timeout")
}

err = ctrl.syncHandler(ns + "/route")
assert.Error(t, err)
assert.Empty(t, store.GetHTTPRoutesByGateway(ns+"/gateway-a"))
}
20 changes: 20 additions & 0 deletions pkg/kthena-router/datastore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1981,9 +1981,29 @@ func (s *store) AddOrUpdateHTTPRoute(httpRoute *gatewayv1.HTTPRoute) error {
key := fmt.Sprintf("%s/%s", httpRoute.Namespace, httpRoute.Name)

s.httpRouteMutex.Lock()
oldRoute := s.httpRoutes[key]
s.httpRoutes[key] = httpRoute

// Update gateway routes mapping
if oldRoute != nil {
for _, parentRef := range oldRoute.Spec.ParentRefs {
if parentRef.Kind != nil && *parentRef.Kind == "Gateway" {
gatewayName := string(parentRef.Name)
gatewayNamespace := oldRoute.Namespace
if parentRef.Namespace != nil {
gatewayNamespace = string(*parentRef.Namespace)
}
gatewayKey := fmt.Sprintf("%s/%s", gatewayNamespace, gatewayName)

if routeSet, exists := s.gatewayRoutes[gatewayKey]; exists {
routeSet.Delete(key)
if routeSet.IsEmpty() {
delete(s.gatewayRoutes, gatewayKey)
}
}
}
}
}
for _, parentRef := range httpRoute.Spec.ParentRefs {
if parentRef.Kind != nil && *parentRef.Kind == "Gateway" {
gatewayName := string(parentRef.Name)
Expand Down
22 changes: 22 additions & 0 deletions pkg/kthena-router/datastore/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,28 @@ func newStore(inspector ...PodRuntimeInspector) *store {
return New(WithPodRuntimeInspector(inspector[0])).(*store)
}

func TestAddOrUpdateHTTPRoute_UpdatesGatewayRoutes(t *testing.T) {
kind := gatewayv1.Kind("Gateway")
s := newStore()

route := &gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "route"},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{{Name: "gateway-a", Kind: &kind}},
},
},
}
assert.NoError(t, s.AddOrUpdateHTTPRoute(route))

route = route.DeepCopy()
route.Spec.ParentRefs = []gatewayv1.ParentReference{{Name: "gateway-b", Kind: &kind}}
assert.NoError(t, s.AddOrUpdateHTTPRoute(route))

assert.Empty(t, s.GetHTTPRoutesByGateway("default/gateway-a"))
assert.Len(t, s.GetHTTPRoutesByGateway("default/gateway-b"), 1)
}

func TestAddOrUpdatePod_MetricsPreservedOnUpdate(t *testing.T) {
sampleCount := uint64(100)
sampleSum := 0.42
Expand Down
Loading