func controlPlaneJoin()

in projects/aws/bottlerocket-bootstrap/pkg/kubeadm/controlplane_join.go [14:106]


func controlPlaneJoin() error {
	err := setHostName(kubeadmJoinFile)
	if err != nil {
		return errors.Wrap(err, "Error replacing hostname on kubeadm")
	}

	cmd := exec.Command(kubeadmBinary, utils.LogVerbosity, "join", "phase", "control-plane-prepare", "all", "--config", kubeadmJoinFile)
	if err := cmd.Run(); err != nil {
		return errors.Wrapf(err, "Error running command: %v", cmd)
	}

	cmd = exec.Command(kubeadmBinary, utils.LogVerbosity, "join", "phase", "kubelet-start", "--config", kubeadmJoinFile)
	if err := cmd.Start(); err != nil {
		return errors.Wrapf(err, "Error running command: %v", cmd)
	}

	err = killCmdAfterJoinFilesGeneration(cmd)
	if err != nil {
		return errors.Wrap(err, "Error waiting for worker join files")
	}

	dns, err := getDNSFromJoinConfig("/var/lib/kubelet/config.yaml")
	if err != nil {
		return errors.Wrap(err, "Error getting api server")
	}

	apiServer, token, err := getBootstrapFromJoinConfig(kubeadmJoinFile)
	if err != nil {
		return errors.Wrap(err, "Error getting api server")
	}

	// Get CA
	b64CA, err := getEncodedCA()
	if err != nil {
		return errors.Wrap(err, "Error reading the ca data")
	}

	cmd = exec.Command(utils.ApiclientBinary, "set",
		"kubernetes.api-server="+apiServer,
		"kubernetes.cluster-certificate="+b64CA,
		"kubernetes.cluster-dns-ip="+dns,
		"kubernetes.bootstrap-token="+token,
		"kubernetes.authentication-mode=tls",
		"kubernetes.standalone-mode=false")
	if err := cmd.Run(); err != nil {
		return errors.Wrapf(err, "Error running command: %v", cmd)
	}

	err = waitForActiveKubelet()
	if err != nil {
		return errors.Wrap(err, "Error waiting for kubelet to come up")
	}

	if isEtcdExternal, err := isClusterWithExternalEtcd(kubeconfigPath); err != nil {
		return err
	} else if !isEtcdExternal {
		if err := joinLocalEtcd(); err != nil {
			return err
		}	
	}

	// Migrate all static pods from this host-container to the bottlerocket host using the apiclient
	// now that etcd manifest is also created
	podDefinitions, err := utils.EnableStaticPods("/etc/kubernetes/manifests")
	if err != nil {
		return errors.Wrap(err, "Error enabling static pods")
	}

	// Now that etcd is up and running, check for other pod liveness
	err = utils.WaitForPods(podDefinitions)
	if err != nil {
		return errors.Wrapf(err, "Error waiting for static pods to be up")
	}

	// Wait for Kubernetes API server to come up.
	err = utils.WaitFor200("https://localhost:6443/healthz", 30*time.Second)
	if err != nil {
		return err
	}

	err = utils.WaitFor200(string(apiServer)+"/healthz", 30*time.Second)
	if err != nil {
		return err
	}

	// finish kubeadm
	cmd = exec.Command(kubeadmBinary, utils.LogVerbosity, "join", "--skip-phases", "preflight,control-plane-prepare,kubelet-start,control-plane-join/etcd",
		"--config", kubeadmJoinFile)
	if err := cmd.Run(); err != nil {
		return errors.Wrapf(err, "Error running command: %v", cmd)
	}
	return nil
}