// Reconcile reads that state of the cluster for a Build object and makes changes based on the state read
// and what is in the Build.Spec
Reconcile reads that state of the cluster for a Build object and makes changes based on the state read
and what is in the Build.Spec
// Reconcile reads that state of the cluster for a Build object and makes changes based on the state read // and what is in the Build . Spec
func (r *ReconcileBuild) Reconcile(request reconcile.Request) (reconcile.Result, error) {
	ctx, cancel := context.WithTimeout(r.ctx, r.config.CtxTimeOut)
	defer cancel()
	ctxlog.Debug(ctx, "start reconciling Build", namespace, request.Namespace, name, request.Name)
	b := &build.Build{}
	err := r.client.Get(ctx, request.NamespacedName, b)
	if err != nil && !apierrors.IsNotFound(err) {
		return reconcile.Result{}, err
	} else if apierrors.IsNotFound(err) {
		ctxlog.Debug(ctx, "finish reconciling build. build was not found", namespace, request.Namespace, name, request.Name)
		return reconcile.Result{}, nil
	}
	if err := r.validateBuildRunOwnerReferences(ctx, b); err != nil {
		ctxlog.Info(ctx, "unexpected error during ownership reference validation", namespace, request.Namespace, name, request.Name, "error", err)
	}
	b.Status.Registered = corev1.ConditionFalse
	b.Status.Reason = succeedStatus
	var secretNames []string
	if b.Spec.Output.SecretRef != nil && b.Spec.Output.SecretRef.Name != "" {
		secretNames = append(secretNames, b.Spec.Output.SecretRef.Name)
	}
	if b.Spec.Source.SecretRef != nil && b.Spec.Source.SecretRef.Name != "" {
		secretNames = append(secretNames, b.Spec.Source.SecretRef.Name)
	}
	if b.Spec.BuilderImage != nil && b.Spec.BuilderImage.SecretRef != nil && b.Spec.BuilderImage.SecretRef.Name != "" {
		secretNames = append(secretNames, b.Spec.BuilderImage.SecretRef.Name)
	}
	if len(secretNames) > 0 {
		if err := r.validateSecrets(ctx, secretNames, b.Namespace); err != nil {
			b.Status.Reason = err.Error()
			if updateErr := r.client.Status().Update(ctx, b); updateErr != nil {
				return reconcile.Result{}, fmt.Errorf("errors: %v %v", err, updateErr)
			}
			return reconcile.Result{}, nil
		}
	}
	if b.Spec.StrategyRef != nil {
		if err := r.validateStrategyRef(ctx, b.Spec.StrategyRef, b.Namespace); err != nil {
			b.Status.Reason = err.Error()
			updateErr := r.client.Status().Update(ctx, b)
			return reconcile.Result{}, fmt.Errorf("errors: %v %v", err, updateErr)
		}
		ctxlog.Info(ctx, "buildStrategy found", namespace, b.Namespace, name, b.Name, "strategy", b.Spec.StrategyRef.Name)
	}
	if utils.IsRuntimeDefined(b) {
		if err := r.validateRuntime(b.Spec.Runtime); err != nil {
			ctxlog.Error(ctx, err, "failed validating runtime attributes", "Build", b.Name)
			b.Status.Reason = err.Error()
			updateErr := r.client.Status().Update(ctx, b)
			return reconcile.Result{}, fmt.Errorf("errors: %v %v", err, updateErr)
		}
	}
	b.Status.Registered = corev1.ConditionTrue
	err = r.client.Status().Update(ctx, b)
	if err != nil {
		return reconcile.Result{}, err
	}
	buildmetrics.BuildCountInc(b.Spec.StrategyRef.Name)
	ctxlog.Debug(ctx, "finishing reconciling Build", namespace, request.Namespace, name, request.Name)
	return reconcile.Result{}, nil
}
func ( r * ReconcileBuild ) Reconcile ( request reconcile . Request ) ( reconcile . Result , error ) { ctx , cancel := context . WithTimeout ( r . ctx , r . config . CtxTimeOut ) defer cancel ( ) ctxlog . Debug ( ctx , " " , namespace , request . Namespace , name , request . Name ) b := & build . Build { } err := r . client . Get ( ctx , request . NamespacedName , b ) if err != nil && ! apierrors . IsNotFound ( err ) { return reconcile . Result { } , err } else if apierrors . IsNotFound ( err ) { ctxlog . Debug ( ctx , " " , namespace , request . Namespace , name , request . Name ) return reconcile . Result { } , nil } if err := r . validateBuildRunOwnerReferences ( ctx , b ) ; err != nil { ctxlog . Info ( ctx , " " , namespace , request . Namespace , name , request . Name , " " , err ) } b . Status . Registered = corev1 . ConditionFalse b . Status . Reason = succeedStatus var secretNames [ ] string if b . Spec . Output . SecretRef != nil && b . Spec . Output . SecretRef . Name != " " { secretNames = append ( secretNames , b . Spec . Output . SecretRef . Name ) } if b . Spec . Source . SecretRef != nil && b . Spec . Source . SecretRef . Name != " " { secretNames = append ( secretNames , b . Spec . Source . SecretRef . Name ) } if b . Spec . BuilderImage != nil && b . Spec . BuilderImage . SecretRef != nil && b . Spec . BuilderImage . SecretRef . Name != " " { secretNames = append ( secretNames , b . Spec . BuilderImage . SecretRef . Name ) } if len ( secretNames ) > 0 { if err := r . validateSecrets ( ctx , secretNames , b . Namespace ) ; err != nil { b . Status . Reason = err . Error ( ) if updateErr := r . client . Status ( ) . Update ( ctx , b ) ; updateErr != nil { return reconcile . Result { } , fmt . Errorf ( " " , err , updateErr ) } return reconcile . Result { } , nil } } if b . Spec . StrategyRef != nil { if err := r . validateStrategyRef ( ctx , b . Spec . StrategyRef , b . Namespace ) ; err != nil { b . Status . Reason = err . Error ( ) updateErr := r . client . Status ( ) . Update ( ctx , b ) return reconcile . Result { } , fmt . Errorf ( " " , err , updateErr ) } ctxlog . Info ( ctx , " " , namespace , b . Namespace , name , b . Name , " " , b . Spec . StrategyRef . Name ) } if utils . IsRuntimeDefined ( b ) { if err := r . validateRuntime ( b . Spec . Runtime ) ; err != nil { ctxlog . Error ( ctx , err , " " , " " , b . Name ) b . Status . Reason = err . Error ( ) updateErr := r . client . Status ( ) . Update ( ctx , b ) return reconcile . Result { } , fmt . Errorf ( " " , err , updateErr ) } } b . Status . Registered = corev1 . ConditionTrue err = r . client . Status ( ) . Update ( ctx , b ) if err != nil { return reconcile . Result { } , err } buildmetrics . BuildCountInc ( b . Spec . StrategyRef . Name ) ctxlog . Debug ( ctx , " " , namespace , request . Namespace , name , request . Name ) return reconcile . Result { } , nil }
Reconcile reads that state of the cluster for a Build object and makes changes based on the state read
and what is in the Build.Spec
Reconcile reads that state of the cluster for a Build object and makes changes based on the state read and what is in the Build . Spec
// Set the ctx to be Background, as the top-level context for incoming requests. // we do not want to bail out here if the owerreference validation fails, we ignore this error on purpose // In case we just created the Build, we want the Build reconcile logic to continue, in order to // validate the Build references ( e.g secrets, strategies ) // Populate the status struct with default values // Validate if the referenced secrets exist in the namespace // return an error in case of transient failure, and expect the next // reconciliation to be able to update the Status of the object // The Secret Resource watcher will Reconcile again once the missing // secret is created, therefore no need to return an error and enter on an infinite // reconciliation // Validate if the build strategy is defined // validate if "spec.runtime" attributes are valid // Increase Build count in metrics