Skip to content
Merged
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
9 changes: 9 additions & 0 deletions controllers/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ func (r *HTTPRouteWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctr
// If annotation was removed, clean up the finalizer
if route.Annotations[triggerAnnotation] != "true" {
if controllerutil.ContainsFinalizer(&route, finalizerName) {
payload := WebhookPayload{
Event: eventDeleted,
Name: route.Name,
Namespace: route.Namespace,
}
if err := r.callExternalAPI(ctx, payload); err != nil {
return ctrl.Result{}, err
}
controllerutil.RemoveFinalizer(&route, finalizerName)
delete(route.Annotations, checksumAnnotation)
return ctrl.Result{}, r.Patch(ctx, &route, client.MergeFrom(base))
}
return ctrl.Result{}, nil
Expand Down
31 changes: 27 additions & 4 deletions controllers/httproute_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func TestHTTPRouteWatcher_AnnotationRemoved_CleansUpFinalizer(t *testing.T) {
Name: "untracked-route",
Namespace: "default",
Finalizers: []string{finalizerName},
Annotations: map[string]string{
checksumAnnotation: "some-previous-hash",
},
}

env := newTestEnv(t, route)
Expand All @@ -200,15 +203,20 @@ func TestHTTPRouteWatcher_AnnotationRemoved_CleansUpFinalizer(t *testing.T) {
t.Fatalf("reconcile failed: %v", err)
}

events := env.recordedEvents()
if len(events) != 1 || events[0].Event != eventDeleted {
t.Fatalf("expected 1 DELETED event on unregister, got: %+v", events)
}

var updated gatewayv1.HTTPRoute
if err := env.client.Get(ctx, req.NamespacedName, &updated); err != nil {
t.Fatalf("failed to fetch updated route: %v", err)
}
if controllerutil.ContainsFinalizer(&updated, finalizerName) {
t.Fatalf("expected finalizer %q to be removed, still present: %v", finalizerName, updated.Finalizers)
}
if len(env.recordedEvents()) != 0 {
t.Fatalf("expected 0 API calls when annotation is missing, got: %+v", env.recordedEvents())
if _, exists := updated.Annotations[checksumAnnotation]; exists {
t.Fatalf("expected %q annotation to be removed", checksumAnnotation)
}

// --- TEST: noop when neither annotation nor finalizer are present ---
Expand Down Expand Up @@ -260,6 +268,18 @@ func TestHTTPRouteWatcher_ErrorsAndAuth(t *testing.T) {
wantErr: true,
wantFinalizerKept: true,
},
{
name: "API 500 on unregister returns error and keeps finalizer",
route: &gatewayv1.HTTPRoute{
Name: "fail-unregister",
Namespace: "default",
Finalizers: []string{finalizerName},
Annotations: map[string]string{checksumAnnotation: "existing-checksum"},
},
apiStatusCode: http.StatusInternalServerError,
wantErr: true,
wantFinalizerKept: true,
},
{
name: "invalid external URL returns request error",
route: &gatewayv1.HTTPRoute{
Expand Down Expand Up @@ -381,8 +401,11 @@ func TestHTTPRouteWatcher_ErrorsAndAuth(t *testing.T) {
if !controllerutil.ContainsFinalizer(&current, finalizerName) {
t.Fatalf("expected finalizer %q to remain, got: %v", finalizerName, current.Finalizers)
}
if tc.wantErr && current.Annotations[checksumAnnotation] != "" {
t.Fatalf("expected checksumAnnotation not to be stamped on failed API call, got %q", current.Annotations[checksumAnnotation])
if tc.wantErr && tc.route.Annotations[checksumAnnotation] == "" && current.Annotations[checksumAnnotation] != "" {
t.Fatalf("expected %q not to be stamped on failed API call, got %q", checksumAnnotation, current.Annotations[checksumAnnotation])
}
if tc.wantErr && tc.route.Annotations[checksumAnnotation] != "" && current.Annotations[checksumAnnotation] != tc.route.Annotations[checksumAnnotation] {
t.Fatalf("expected %q to be preserved on failed unregister, got %q", checksumAnnotation, current.Annotations[checksumAnnotation])
}
}
})
Expand Down
Loading