func()

in pkg/controllers/hub/trafficmanagerprofile/controller.go [131:169]


func (r *Reconciler) handleDelete(ctx context.Context, profile *fleetnetv1beta1.TrafficManagerProfile) (ctrl.Result, error) {
	profileKObj := klog.KObj(profile)
	needUpdate := false
	// The profile is being deleted
	if controllerutil.ContainsFinalizer(profile, objectmeta.MetricsFinalizer) {
		klog.V(2).InfoS("TrafficManagerProfile is being deleted and cleaning up its metrics", "trafficManagerProfile", profileKObj)
		// The controller registers profile finalizer only before creating atm profile to avoid the deletion stuck for the 403 error.
		// We use a separate finalizer to clean up the metrics for the profile.
		trafficManagerProfileStatusLastTimestampSeconds.DeletePartialMatch(prometheus.Labels{"namespace": profile.GetNamespace(), "name": profile.GetName()})
		controllerutil.RemoveFinalizer(profile, objectmeta.MetricsFinalizer)
		needUpdate = true
	}

	if controllerutil.ContainsFinalizer(profile, objectmeta.TrafficManagerProfileFinalizer) {
		atmProfileName := generateAzureTrafficManagerProfileNameFunc(profile)
		klog.V(2).InfoS("Deleting Azure Traffic Manager profile", "trafficManagerProfile", profileKObj, "atmProfileName", atmProfileName)
		if _, err := r.ProfilesClient.Delete(ctx, profile.Spec.ResourceGroup, atmProfileName, nil); err != nil {
			if !azureerrors.IsNotFound(err) {
				klog.ErrorS(err, "Failed to delete Azure Traffic Manager profile", "trafficManagerProfile", profileKObj, "atmProfileName", atmProfileName)
				return ctrl.Result{}, err
			}
		}
		klog.V(2).InfoS("Deleted Azure Traffic Manager profile", "trafficManagerProfile", profileKObj, "atmProfileName", atmProfileName)
		controllerutil.RemoveFinalizer(profile, objectmeta.TrafficManagerProfileFinalizer)
		needUpdate = true
	}

	if !needUpdate {
		klog.V(2).InfoS("No need to remove finalizer", "trafficManagerProfile", profileKObj)
		return ctrl.Result{}, nil
	}

	if err := r.Client.Update(ctx, profile); err != nil {
		klog.ErrorS(err, "Failed to remove trafficManagerProfile finalizers", "trafficManagerProfile", profileKObj)
		return ctrl.Result{}, controller.NewUpdateIgnoreConflictError(err)
	}
	klog.V(2).InfoS("Removed trafficManagerProfile finalizers", "trafficManagerProfile", profileKObj)
	return ctrl.Result{}, nil
}