func()

in controllers/etcdadmconfig_controller.go [113:210]


func (r *EtcdadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, rerr error) {
	log := r.Log.WithValues("etcdadmconfig", req.Name, "namespace", req.Namespace)

	// Lookup the etcdadm config
	etcdadmConfig := &etcdbootstrapv1.EtcdadmConfig{}
	if err := r.Client.Get(ctx, req.NamespacedName, etcdadmConfig); err != nil {
		if apierrors.IsNotFound(err) {
			return ctrl.Result{}, nil
		}
		log.Error(err, "Failed to get etcdadm config")
		return ctrl.Result{}, err
	}

	// Look up the Machine associated with this EtcdadmConfig resource
	machine, err := util.GetOwnerMachine(ctx, r.Client, etcdadmConfig.ObjectMeta)
	if err != nil {
		if apierrors.IsNotFound(err) {
			// could not find owning machine, reconcile when owner is set
			return ctrl.Result{}, nil
		}
		log.Error(err, "could not get owner machine for the EtcdadmConfig")
		return ctrl.Result{}, err
	}
	if machine == nil {
		log.Info("Waiting for Machine Controller to set OwnerRef on the EtcdadmConfig")
		return ctrl.Result{}, nil
	}
	log = log.WithValues("machine-name", machine.Name)
	// Lookup the cluster the machine is associated with
	cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
	if err != nil {
		if errors.Cause(err) == util.ErrNoCluster {
			log.Info("Machine does not belong to a cluster yet, waiting until its part of a cluster")
			return ctrl.Result{}, nil
		}
		if apierrors.IsNotFound(err) {
			log.Info("Cluster does not exist yet, waiting until it is created")
			return ctrl.Result{}, nil
		}
		log.Error(err, "could not get cluster by machine metadata")
		return ctrl.Result{}, err
	}

	if annotations.IsPaused(cluster, etcdadmConfig) {
		log.Info("Reconciliation is paused for this object")
		return ctrl.Result{}, nil
	}
	// Initialize the patch helper.
	patchHelper, err := patch.NewHelper(etcdadmConfig, r.Client)
	if err != nil {
		return ctrl.Result{}, err
	}

	// Attempt to Patch the EtcdadmConfig object and status after each reconciliation if no error occurs.
	defer func() {
		// always update the readyCondition; the summary is represented using the "1 of x completed" notation.
		conditions.SetSummary(etcdadmConfig,
			conditions.WithConditions(
				etcdbootstrapv1.DataSecretAvailableCondition,
			),
		)
		// Patch ObservedGeneration only if the reconciliation completed successfully
		patchOpts := []patch.Option{}
		if rerr == nil {
			patchOpts = append(patchOpts, patch.WithStatusObservedGeneration{})
		}
		if err := patchHelper.Patch(ctx, etcdadmConfig, patchOpts...); err != nil {
			log.Error(err, "Failed to patch etcdadmConfig")
			if rerr == nil {
				rerr = err
			}
		}
	}()

	if etcdadmConfig.Status.Ready {
		return ctrl.Result{}, nil
	}

	scope := Scope{
		Logger:  log,
		Config:  etcdadmConfig,
		Cluster: cluster,
		Machine: machine,
	}

	if !conditions.IsTrue(cluster, clusterv1.ManagedExternalEtcdClusterInitializedCondition) {
		return r.initializeEtcd(ctx, &scope)
	}
	// Unlock any locks that might have been set during init process
	r.EtcdadmInitLock.Unlock(ctx, cluster)

	res, err := r.joinEtcd(ctx, &scope)
	if err != nil {
		return res, err
	}

	return ctrl.Result{}, nil
}