func()

in controllers/etcdadmconfig_controller.go [212:286]


func (r *EtcdadmConfigReconciler) initializeEtcd(ctx context.Context, scope *Scope) (_ ctrl.Result, rerr error) {
	log := r.Log
	// acquire the init lock so that only the first machine configured as etcd node gets processed here
	// if not the first, requeue
	if !r.EtcdadmInitLock.Lock(ctx, scope.Cluster, scope.Machine) {
		log.Info("An etcd node is already being initialized, requeing until etcd plane is ready")
		return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
	}

	defer func() {
		if rerr != nil {
			r.EtcdadmInitLock.Unlock(ctx, scope.Cluster)
		}
	}()
	log.Info("Creating cloudinit for the init etcd plane")

	CACertKeyPair := etcdCACertKeyPair()
	rerr = CACertKeyPair.LookupOrGenerate(
		ctx,
		r.Client,
		util.ObjectKey(scope.Cluster),
		*metav1.NewControllerRef(scope.Config, etcdbootstrapv1.GroupVersion.WithKind("EtcdadmConfig")),
	)

	initInput := userdata.EtcdPlaneInput{
		BaseUserData: userdata.BaseUserData{
			Users:              scope.Config.Spec.Users,
			PreEtcdadmCommands: scope.Config.Spec.PreEtcdadmCommands,
			NTP:                scope.Config.Spec.NTP,
			Hostname:           scope.Machine.Name,
		},
		Certificates: CACertKeyPair,
	}

	// grab user pass for registry mirror
	if scope.Config.Spec.RegistryMirror != nil {
		username, password, err := r.resolveRegistryCredentials(ctx, scope.Config)
		if err != nil {
			log.Info("Cannot find secret for registry credentials, proceeding without registry credentials")
		} else {
			initInput.RegistryMirrorCredentials.Username = string(username)
			initInput.RegistryMirrorCredentials.Password = string(password)
		}
	}

	// only do this if etcdadm not baked in image
	if !scope.Config.Spec.EtcdadmBuiltin {
		if len(scope.Config.Spec.EtcdadmInstallCommands) > 0 {
			initInput.PreEtcdadmCommands = append(initInput.PreEtcdadmCommands, scope.Config.Spec.EtcdadmInstallCommands...)
		} else {
			initInput.PreEtcdadmCommands = append(initInput.PreEtcdadmCommands, defaultEtcdadmInstallCommands...)
		}
	}

	var bootstrapData []byte
	var err error

	switch scope.Config.Spec.Format {
	case etcdbootstrapv1.Bottlerocket:
		bootstrapData, err = bottlerocket.NewInitEtcdPlane(&initInput, scope.Config.Spec, log)
	default:
		initInput.PreEtcdadmCommands = append(initInput.PreEtcdadmCommands, stopKubeletCommand)
		bootstrapData, err = cloudinit.NewInitEtcdPlane(&initInput, scope.Config.Spec)
	}
	if err != nil {
		log.Error(err, "Failed to generate cloud init for initializing etcd plane")
		return ctrl.Result{}, err
	}

	if err := r.storeBootstrapData(ctx, scope.Config, bootstrapData, scope.Cluster.Name); err != nil {
		log.Error(err, "Failed to store bootstrap data")
		return ctrl.Result{}, err
	}
	return ctrl.Result{}, nil
}