Skip to content

Commit 0b76360

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

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
"time"
2424

2525
corev1 "k8s.io/api/core/v1"
26+
apierrs "k8s.io/apimachinery/pkg/api/errors"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
2830
"k8s.io/apimachinery/pkg/types"
2931
clientgotesting "k8s.io/client-go/testing"
3032
"k8s.io/utils/ptr"
@@ -196,6 +198,83 @@ func TestReconcile(t *testing.T) {
196198
httpRoute(t, ing(withBasicSpec, withGatewayAPIclass), httpRouteReady),
197199
}, servicesAndEndpoints...),
198200
// no extra update
201+
}, {
202+
Name: "prune stale HTTPRoute when rule removed",
203+
Key: "ns/name",
204+
Objects: append([]runtime.Object{
205+
ing(withBasicSpec, withGatewayAPIclass, withFinalizer, makeItReady),
206+
httpRoute(t, ing(withBasicSpec, withGatewayAPIclass), httpRouteReady),
207+
HTTPRoute{
208+
Name: "stale.example.com",
209+
Namespace: "ns",
210+
Hostname: "stale.example.com",
211+
}.Build(),
212+
}, servicesAndEndpoints...),
213+
WantDeletes: []clientgotesting.DeleteActionImpl{
214+
clientgotesting.NewDeleteAction(
215+
schema.GroupVersionResource{
216+
Group: "gateway.networking.k8s.io",
217+
Version: "v1",
218+
Resource: "httproutes",
219+
},
220+
"ns",
221+
"stale.example.com",
222+
),
223+
},
224+
}, {
225+
Name: "prune skips non-owned HTTPRoute",
226+
Key: "ns/name",
227+
Objects: append(func() []runtime.Object {
228+
route := HTTPRoute{
229+
Name: "stale.example.com",
230+
Namespace: "ns",
231+
Hostname: "stale.example.com",
232+
}.Build()
233+
// Remove owner so it is not controlled by this Ingress
234+
route.OwnerReferences = nil
235+
return []runtime.Object{
236+
ing(withBasicSpec, withGatewayAPIclass, withFinalizer, makeItReady),
237+
httpRoute(t, ing(withBasicSpec, withGatewayAPIclass), httpRouteReady),
238+
route,
239+
}
240+
}(), servicesAndEndpoints...),
241+
// No deletes expected
242+
WantDeletes: []clientgotesting.DeleteActionImpl{},
243+
}, {
244+
Name: "prune delete NotFound tolerated",
245+
Key: "ns/name",
246+
WithReactors: []clientgotesting.ReactionFunc{
247+
func(a clientgotesting.Action) (bool, runtime.Object, error) {
248+
if a.GetVerb() == "delete" && a.GetResource().Resource == "httproutes" {
249+
name := a.(clientgotesting.DeleteActionImpl).Name
250+
return true, nil, apierrs.NewNotFound(
251+
schema.GroupResource{Group: "gateway.networking.k8s.io", Resource: "httproutes"},
252+
name,
253+
)
254+
}
255+
return false, nil, nil
256+
},
257+
},
258+
Objects: append([]runtime.Object{
259+
ing(withBasicSpec, withGatewayAPIclass, withFinalizer, makeItReady),
260+
httpRoute(t, ing(withBasicSpec, withGatewayAPIclass), httpRouteReady),
261+
HTTPRoute{
262+
Name: "stale.example.com",
263+
Namespace: "ns",
264+
Hostname: "stale.example.com",
265+
}.Build(),
266+
}, servicesAndEndpoints...),
267+
WantDeletes: []clientgotesting.DeleteActionImpl{
268+
clientgotesting.NewDeleteAction(
269+
schema.GroupVersionResource{
270+
Group: "gateway.networking.k8s.io",
271+
Version: "v1",
272+
Resource: "httproutes",
273+
},
274+
"ns",
275+
"stale.example.com",
276+
),
277+
},
199278
}}
200279

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

0 commit comments

Comments
 (0)