// Reconcile is the main logic for EnvBinding controller
Reconcile is the main logic for EnvBinding controller
// Reconcile is the main logic for EnvBinding controller
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	ctx, cancel := common2.NewReconcileContext(ctx)
	defer cancel()
	klog.InfoS("Reconcile EnvBinding", "envbinding", klog.KRef(req.Namespace, req.Name))
	envBinding := new(v1alpha1.EnvBinding)
	if err := r.Client.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: req.Name}, envBinding); err != nil {
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}
	if envBinding.DeletionTimestamp != nil {
		return ctrl.Result{}, nil
	}
	baseApp, err := util.RawExtension2Application(envBinding.Spec.AppTemplate.RawExtension)
	if err != nil {
		klog.ErrorS(err, "Failed to parse AppTemplate of EnvBinding")
		r.record.Event(envBinding, event.Warning("Failed to parse AppTemplate of EnvBinding", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	var engine ClusterManagerEngine
	switch envBinding.Spec.Engine {
	case v1alpha1.OCMEngine:
		engine = NewOCMEngine(r.Client, baseApp.Name, baseApp.Namespace)
	default:
		engine = NewOCMEngine(r.Client, baseApp.Name, baseApp.Namespace)
	}
	envBinding.Status.Phase = v1alpha1.EnvBindingPrepare
	if err = engine.Prepare(ctx, envBinding.Spec.Envs); err != nil {
		klog.ErrorS(err, "Failed to prepare the pre-work for cluster scheduling")
		r.record.Event(envBinding, event.Warning("Failed to prepare the pre-work for cluster scheduling", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	envBinding.Status.Phase = v1alpha1.EnvBindingRendering
	envBindApps, err := CreateEnvBindApps(envBinding, baseApp)
	if err != nil {
		klog.ErrorS(err, "Failed to patch the parameters for application in different envs")
		r.record.Event(envBinding, event.Warning("Failed to patch the parameters for application in different envs", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	appParser := appfile.NewApplicationParser(r.Client, r.dm, r.pd)
	if err = RenderEnvBindApps(ctx, envBindApps, appParser); err != nil {
		klog.ErrorS(err, "Failed to render the application for different envs")
		r.record.Event(envBinding, event.Warning("Failed to render the application for different envs", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	if err = AssembleEnvBindApps(envBindApps); err != nil {
		klog.ErrorS(err, "Failed to assemble resources for application in different envs")
		r.record.Event(envBinding, event.Warning("Failed to assemble resources for application in different envs", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	envBinding.Status.Phase = v1alpha1.EnvBindingScheduling
	if err = engine.Schedule(ctx, envBindApps); err != nil {
		klog.ErrorS(err, "Failed to schedule resource of applications in different envs")
		r.record.Event(envBinding, event.Warning("Failed to schedule resource of applications in different envs", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	if err = StoreManifest2ConfigMap(ctx, r.Client, envBinding, envBindApps); err != nil {
		klog.ErrorS(err, "Failed to store manifest of different envs to configmap")
		r.record.Event(envBinding, event.Warning("Failed to store manifest of different envs to configmap", err))
		return r.endWithNegativeCondition(ctx, envBinding, condition.ReconcileError(err))
	}
	envBinding.Status.Phase = v1alpha1.EnvBindingFinished
	envBinding.Status.ClusterDecisions = engine.GetClusterDecisions()
	if err = r.Client.Status().Patch(ctx, envBinding, client.Merge); err != nil {
		klog.ErrorS(err, "Failed to update status")
		r.record.Event(envBinding, event.Warning("Failed to update status", err))
		return ctrl.Result{}, util.EndReconcileWithNegativeCondition(ctx, r, envBinding, condition.ReconcileError(err))
	}
	return ctrl.Result{}, nil
}
func ( r * Reconciler ) Reconcile ( ctx context . Context , req ctrl . Request ) ( ctrl . Result , error ) { ctx , cancel := common2 . NewReconcileContext ( ctx ) defer cancel ( ) klog . InfoS ( " " , " " , klog . KRef ( req . Namespace , req . Name ) ) envBinding := new ( v1alpha1 . EnvBinding ) if err := r . Client . Get ( ctx , client . ObjectKey { Namespace : req . Namespace , Name : req . Name } , envBinding ) ; err != nil { return ctrl . Result { } , client . IgnoreNotFound ( err ) } if envBinding . DeletionTimestamp != nil { return ctrl . Result { } , nil } baseApp , err := util . RawExtension2Application ( envBinding . Spec . AppTemplate . RawExtension ) if err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } var engine ClusterManagerEngine switch envBinding . Spec . Engine { case v1alpha1 . OCMEngine : engine = NewOCMEngine ( r . Client , baseApp . Name , baseApp . Namespace ) default : engine = NewOCMEngine ( r . Client , baseApp . Name , baseApp . Namespace ) } envBinding . Status . Phase = v1alpha1 . EnvBindingPrepare if err = engine . Prepare ( ctx , envBinding . Spec . Envs ) ; err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } envBinding . Status . Phase = v1alpha1 . EnvBindingRendering envBindApps , err := CreateEnvBindApps ( envBinding , baseApp ) if err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } appParser := appfile . NewApplicationParser ( r . Client , r . dm , r . pd ) if err = RenderEnvBindApps ( ctx , envBindApps , appParser ) ; err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } if err = AssembleEnvBindApps ( envBindApps ) ; err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } envBinding . Status . Phase = v1alpha1 . EnvBindingScheduling if err = engine . Schedule ( ctx , envBindApps ) ; err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } if err = StoreManifest2ConfigMap ( ctx , r . Client , envBinding , envBindApps ) ; err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return r . endWithNegativeCondition ( ctx , envBinding , condition . ReconcileError ( err ) ) } envBinding . Status . Phase = v1alpha1 . EnvBindingFinished envBinding . Status . ClusterDecisions = engine . GetClusterDecisions ( ) if err = r . Client . Status ( ) . Patch ( ctx , envBinding , client . Merge ) ; err != nil { klog . ErrorS ( err , " " ) r . record . Event ( envBinding , event . Warning ( " " , err ) ) return ctrl . Result { } , util . EndReconcileWithNegativeCondition ( ctx , r , envBinding , condition . ReconcileError ( err ) ) } return ctrl . Result { } , nil }
Reconcile is the main logic for EnvBinding controller
Reconcile is the main logic for EnvBinding controller
// this is a placeholder for finalizer here in the future // 1. prepare the pre-work for cluster scheduling // 2. patch the component parameters for application in different envs // 3. render applications for different envs // 4. assemble resources for applications in different envs // 5. schedule resource of applications in different envs // 6. store manifest of different envs to configmap