func()

in pkg/controllers/hub/trafficmanagerprofile/controller.go [89:129]


func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	name := req.NamespacedName
	profileKRef := klog.KRef(name.Namespace, name.Name)

	startTime := time.Now()
	klog.V(2).InfoS("Reconciliation starts", "trafficManagerProfile", profileKRef)
	defer func() {
		latency := time.Since(startTime).Milliseconds()
		klog.V(2).InfoS("Reconciliation ends", "trafficManagerProfile", profileKRef, "latency", latency)
	}()

	profile := &fleetnetv1beta1.TrafficManagerProfile{}
	if err := r.Client.Get(ctx, name, profile); err != nil {
		if apierrors.IsNotFound(err) {
			klog.V(2).InfoS("Ignoring NotFound trafficManagerProfile", "trafficManagerProfile", profileKRef)
			return ctrl.Result{}, nil
		}
		klog.ErrorS(err, "Failed to get trafficManagerProfile", "trafficManagerProfile", profileKRef)
		return ctrl.Result{}, controller.NewAPIServerError(true, err)
	}

	if !profile.ObjectMeta.DeletionTimestamp.IsZero() {
		// TODO: handle the deletion when backends are still attached to the profile
		return r.handleDelete(ctx, profile)
	}

	// register metrics finalizer
	if !controllerutil.ContainsFinalizer(profile, objectmeta.MetricsFinalizer) {
		controllerutil.AddFinalizer(profile, objectmeta.MetricsFinalizer)
		if err := r.Update(ctx, profile); err != nil {
			klog.ErrorS(err, "Failed to add trafficManagerProfile metrics finalizer", "trafficManagerProfile", profileKRef)
			return ctrl.Result{}, err
		}
	}

	defer emitTrafficManagerProfileStatusMetric(profile)

	// TODO: replace the following with defaulter wehbook
	defaulter.SetDefaultsTrafficManagerProfile(profile)
	return r.handleUpdate(ctx, profile)
}