func()

in internal/controller/authproxyworkload_controller.go [228:272]


func (r *AuthProxyWorkloadReconciler) doCreateUpdate(ctx context.Context, l logr.Logger, resource *cloudsqlapi.AuthProxyWorkload) (ctrl.Result, error) {
	orig := resource.DeepCopy()
	var err error
	// State 0: The reconcile loop for a single AuthProxyWorkload resource begins
	// when an AuthProxyWorkload resource is created, modified, or deleted in the k8s api
	// or when that AuthProxyWorkload resource is requeued for another reconcile loop.

	if !controllerutil.ContainsFinalizer(resource, finalizerName) {
		// State 1.1: This is a brand new thing that doesn't have a finalizer.
		// Add the finalizer and requeue for another run through the reconcile loop
		return r.applyFinalizer(ctx, l, resource)
	}

	// find all workloads that relate to this AuthProxyWorkload resource
	allWorkloads, err := r.updateWorkloadStatus(ctx, resource)
	if err != nil {
		// State 1.2 - unable to read workloads, abort and try again after a delay.
		return requeueWithDelay, err
	}

	// State 2: If workload reconcile has not yet started, then start it.

	// State 2.1: When there are no workloads, then mark this as "UpToDate" true,
	// do not requeue.
	if len(allWorkloads) == 0 {
		return r.reconcileResult(ctx, l, resource, orig, cloudsqlapi.ReasonNoWorkloadsFound, "No workload updates needed", true)
	}

	// State 3.*: Workloads already exist. Some may need to be updated to roll out
	// changes.
	outOfDateCount, err := r.updateWorkloadAnnotations(ctx, resource, allWorkloads)
	if err != nil {
		return requeueNow, err
	}

	// State 3.2 Successfully updated all workload PodTemplateSpec annotations, requeue
	if outOfDateCount > 0 {
		message := fmt.Sprintf("Reconciled %d matching workloads. %d workloads need updates", len(allWorkloads), outOfDateCount)
		return r.reconcileResult(ctx, l, resource, orig, cloudsqlapi.ReasonWorkloadNeedsUpdate, message, false)
	}

	// State 3.3 Workload PodTemplateSpec annotations are all up to date
	message := fmt.Sprintf("Reconciled %d matching workloads complete", len(allWorkloads))
	return r.reconcileResult(ctx, l, resource, orig, cloudsqlapi.ReasonFinishedReconcile, message, true)
}