// Reconcile is the main logic of appDeployment controller
Reconcile is the main logic of appDeployment controller
// Reconcile is the main logic of appDeployment controller
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (res reconcile.Result, retErr error) {
	ctx, cancel := common2.NewReconcileContext(ctx)
	defer cancel()
	appDeployment := &oamcore.AppDeployment{}
	startTime := time.Now()
	defer func() {
		if retErr == nil {
			if res.Requeue || res.RequeueAfter > 0 {
				klog.InfoS("Finished reconciling appDeployment", "controller request", req, "time spent", time.Since(startTime), "result", res)
			} else {
				klog.InfoS("Finished reconcile appDeployment", "controller  request", req, "time spent", time.Since(startTime))
			}
		} else {
			klog.Errorf("Failed to reconcile appDeployment %s: %v", req, retErr)
		}
	}()
	if err := r.Client.Get(ctx, req.NamespacedName, appDeployment); err != nil {
		if apierrors.IsNotFound(err) {
			klog.InfoS("appDeployment does not exist", "appDeployment", klog.KRef(req.Namespace, req.Name))
		}
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}
	klog.InfoS("Start to reconcile", "appDeployment", klog.KObj(appDeployment))
	if !appDeployment.DeletionTimestamp.IsZero() {
		err := r.handleFinalizer(ctx, appDeployment)
		return ctrl.Result{}, err
	}
	if !slice.ContainsString(appDeployment.ObjectMeta.Finalizers, appDeploymentFinalizer, nil) {
		appDeployment.ObjectMeta.Finalizers = append(appDeployment.ObjectMeta.Finalizers, appDeploymentFinalizer)
		if err := r.Client.Update(context.Background(), appDeployment); err != nil {
			return ctrl.Result{}, err
		}
	}
	diff := r.calculateDiff(appDeployment)
	if !diff.Empty() {
		if appDeployment.Status.Phase != oamcore.PhaseRolling {
			appDeployment.Status.Phase = oamcore.PhaseRolling
			if err := r.updateStatus(ctx, appDeployment); err != nil {
				return ctrl.Result{}, err
			}
		}
		if err := r.deleteRevisions(ctx, appDeployment, diff.Del); err != nil {
			return ctrl.Result{}, err
		}
		if err := r.applyRevisions(ctx, appDeployment, diff.Mod); err != nil {
			return ctrl.Result{}, err
		}
		if err := r.applyRevisions(ctx, appDeployment, diff.Add); err != nil {
			return ctrl.Result{}, err
		}
	}
	appDeployment.Status.Phase = oamcore.PhaseCompleted
	appDeployment.Status.Placement = makePlacement(
		append(append(diff.Add, diff.Mod...), diff.Unchanged...),
	)
	if appDeployment.Spec.Traffic != nil {
		if err := r.applyTraffic(ctx, appDeployment); err != nil {
			return ctrl.Result{}, err
		}
	}
	return ctrl.Result{}, r.updateStatus(ctx, appDeployment)
}
func ( r * Reconciler ) Reconcile ( ctx context . Context , req ctrl . Request ) ( res reconcile . Result , retErr error ) { ctx , cancel := common2 . NewReconcileContext ( ctx ) defer cancel ( ) appDeployment := & oamcore . AppDeployment { } startTime := time . Now ( ) defer func ( ) { if retErr == nil { if res . Requeue || res . RequeueAfter > 0 { klog . InfoS ( " " , " " , req , " " , time . Since ( startTime ) , " " , res ) } else { klog . InfoS ( " " , " " , req , " " , time . Since ( startTime ) ) } } else { klog . Errorf ( " " , req , retErr ) } } ( ) if err := r . Client . Get ( ctx , req . NamespacedName , appDeployment ) ; err != nil { if apierrors . IsNotFound ( err ) { klog . InfoS ( " " , " " , klog . KRef ( req . Namespace , req . Name ) ) } return ctrl . Result { } , client . IgnoreNotFound ( err ) } klog . InfoS ( " " , " " , klog . KObj ( appDeployment ) ) if ! appDeployment . DeletionTimestamp . IsZero ( ) { err := r . handleFinalizer ( ctx , appDeployment ) return ctrl . Result { } , err } if ! slice . ContainsString ( appDeployment . ObjectMeta . Finalizers , appDeploymentFinalizer , nil ) { appDeployment . ObjectMeta . Finalizers = append ( appDeployment . ObjectMeta . Finalizers , appDeploymentFinalizer ) if err := r . Client . Update ( context . Background ( ) , appDeployment ) ; err != nil { return ctrl . Result { } , err } } diff := r . calculateDiff ( appDeployment ) if ! diff . Empty ( ) { if appDeployment . Status . Phase != oamcore . PhaseRolling { appDeployment . Status . Phase = oamcore . PhaseRolling if err := r . updateStatus ( ctx , appDeployment ) ; err != nil { return ctrl . Result { } , err } } if err := r . deleteRevisions ( ctx , appDeployment , diff . Del ) ; err != nil { return ctrl . Result { } , err } if err := r . applyRevisions ( ctx , appDeployment , diff . Mod ) ; err != nil { return ctrl . Result { } , err } if err := r . applyRevisions ( ctx , appDeployment , diff . Add ) ; err != nil { return ctrl . Result { } , err } } appDeployment . Status . Phase = oamcore . PhaseCompleted appDeployment . Status . Placement = makePlacement ( append ( append ( diff . Add , diff . Mod ... ) , diff . Unchanged ... ) , ) if appDeployment . Spec . Traffic != nil { if err := r . applyTraffic ( ctx , appDeployment ) ; err != nil { return ctrl . Result { } , err } } return ctrl . Result { } , r . updateStatus ( ctx , appDeployment ) }
Reconcile is the main logic of appDeployment controller
Reconcile is the main logic of appDeployment controller
// The object is not being deleted, so if it does not have our finalizer, // then lets add the finalizer and update the object. This is equivalent // registering our finalizer.