Skip to content

Commit 5bb793f

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

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

pkg/reconciler/ingress/ingress.go

Lines changed: 27 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,11 @@ func (c *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress
102105

103106
routesReady := true
104107

108+
// 望ましいHTTPRoute名(ルールごとにLongestHostを採用)を収集
109+
desiredRouteNames := sets.New[string]()
105110
for _, rule := range ing.Spec.Rules {
111+
desiredRouteNames.Insert(resources.LongestHost(rule.Hosts))
112+
106113
httproute, probeTargets, err := c.reconcileHTTPRoute(ctx, ingressHash, ing, &rule)
107114
if err != nil {
108115
return err
@@ -123,6 +130,26 @@ func (c *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress
123130
}
124131
}
125132

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