Skip to content

Commit 907365e

Browse files
committed
🐛 prune stale HTTPRoutes when tags are removed
Signed-off-by: kahirokunn <[email protected]>
1 parent b658b58 commit 907365e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

pkg/reconciler/ingress/ingress.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323

2424
apierrs "k8s.io/apimachinery/pkg/api/errors"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/labels"
27+
"k8s.io/apimachinery/pkg/util/sets"
2628

2729
"knative.dev/net-gateway-api/pkg/reconciler/ingress/config"
30+
"knative.dev/net-gateway-api/pkg/reconciler/ingress/resources"
2831
"knative.dev/net-gateway-api/pkg/status"
2932
"knative.dev/networking/pkg/apis/networking/v1alpha1"
3033
ingressreconciler "knative.dev/networking/pkg/client/injection/reconciler/networking/v1alpha1/ingress"
@@ -102,7 +105,10 @@ func (c *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress
102105

103106
routesReady := true
104107

108+
desiredRouteNames := sets.New[string]()
105109
for _, rule := range ing.Spec.Rules {
110+
desiredRouteNames.Insert(resources.LongestHost(rule.Hosts))
111+
106112
httproute, probeTargets, err := c.reconcileHTTPRoute(ctx, ingressHash, ing, &rule)
107113
if err != nil {
108114
return err
@@ -123,6 +129,26 @@ func (c *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress
123129
}
124130
}
125131

132+
// Delete HTTPRoutes that don't exist in the current Spec (i.e., tags removed and no longer referenced)
133+
{
134+
existingRoutes, err := c.httprouteLister.HTTPRoutes(ing.Namespace).List(labels.Everything())
135+
if err != nil {
136+
return fmt.Errorf("failed to list HTTPRoutes: %w", err)
137+
}
138+
for _, r := range existingRoutes {
139+
// Don't touch routes not owned by this Ingress
140+
if !metav1.IsControlledBy(r, ing) {
141+
continue
142+
}
143+
// Not in the desired set = unnecessary
144+
if !desiredRouteNames.Has(r.Name) {
145+
if err := c.gwapiclient.GatewayV1().HTTPRoutes(r.Namespace).Delete(ctx, r.Name, metav1.DeleteOptions{}); err != nil && !apierrs.IsNotFound(err) {
146+
return fmt.Errorf("failed to delete stale HTTPRoute %s/%s: %w", r.Namespace, r.Name, err)
147+
}
148+
}
149+
}
150+
}
151+
126152
externalIngressTLS := ing.GetIngressTLSForVisibility(v1alpha1.IngressVisibilityExternalIP)
127153
listeners := make([]*gatewayapi.Listener, 0, len(externalIngressTLS))
128154
for _, tls := range externalIngressTLS {

pkg/reconciler/ingress/ingress_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/runtime"
28+
"k8s.io/apimachinery/pkg/runtime/schema"
2829
"k8s.io/apimachinery/pkg/types"
2930
clientgotesting "k8s.io/client-go/testing"
3031
"k8s.io/utils/ptr"
@@ -196,6 +197,29 @@ func TestReconcile(t *testing.T) {
196197
httpRoute(t, ing(withBasicSpec, withGatewayAPIclass), httpRouteReady),
197198
}, servicesAndEndpoints...),
198199
// no extra update
200+
}, {
201+
Name: "prune stale HTTPRoute when rule removed",
202+
Key: "ns/name",
203+
Objects: append([]runtime.Object{
204+
ing(withBasicSpec, withGatewayAPIclass, withFinalizer, makeItReady),
205+
httpRoute(t, ing(withBasicSpec, withGatewayAPIclass), httpRouteReady),
206+
HTTPRoute{
207+
Name: "stale.example.com",
208+
Namespace: "ns",
209+
Hostname: "stale.example.com",
210+
}.Build(),
211+
}, servicesAndEndpoints...),
212+
WantDeletes: []clientgotesting.DeleteActionImpl{
213+
clientgotesting.NewDeleteAction(
214+
schema.GroupVersionResource{
215+
Group: "gateway.networking.k8s.io",
216+
Version: "v1",
217+
Resource: "httproutes",
218+
},
219+
"ns",
220+
"stale.example.com",
221+
),
222+
},
199223
}}
200224

201225
table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {

0 commit comments

Comments
 (0)