diff --git a/controllers/httproute_controller.go b/controllers/httproute_controller.go index 85c7a39..35f97b4 100644 --- a/controllers/httproute_controller.go +++ b/controllers/httproute_controller.go @@ -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 diff --git a/controllers/httproute_controller_test.go b/controllers/httproute_controller_test.go index 9c26f10..acbc0dc 100644 --- a/controllers/httproute_controller_test.go +++ b/controllers/httproute_controller_test.go @@ -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) @@ -200,6 +203,11 @@ 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) @@ -207,8 +215,8 @@ func TestHTTPRouteWatcher_AnnotationRemoved_CleansUpFinalizer(t *testing.T) { 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 --- @@ -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{ @@ -381,8 +401,11 @@ func TestHTTPRouteWatcher_ErrorsAndAuth(t *testing.T) { if !controllerutil.ContainsFinalizer(¤t, 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]) } } })