mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-10-31 05:40:42 +00:00 
			
		
		
		
	kubeadm: add temporary --self-hosted flag.
This commit is contained in:
		| @@ -21,13 +21,11 @@ import ( | ||||
| 	"io" | ||||
| 	"io/ioutil" | ||||
| 	"path" | ||||
| 	"strconv" | ||||
|  | ||||
| 	"github.com/renstrom/dedent" | ||||
| 	"github.com/spf13/cobra" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	netutil "k8s.io/apimachinery/pkg/util/net" | ||||
| 	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" | ||||
| 	kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1" | ||||
| 	"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation" | ||||
| @@ -54,9 +52,6 @@ var ( | ||||
|  | ||||
| 		kubeadm join --discovery %s | ||||
| 		`) | ||||
| 	deploymentStaticPod  = "static-pods" | ||||
| 	deploymentSelfHosted = "self-hosted" | ||||
| 	deploymentTypes      = []string{deploymentStaticPod, deploymentSelfHosted} | ||||
| ) | ||||
|  | ||||
| // NewCmdInit returns "kubeadm init" command. | ||||
| @@ -68,14 +63,14 @@ func NewCmdInit(out io.Writer) *cobra.Command { | ||||
|  | ||||
| 	var cfgPath string | ||||
| 	var skipPreFlight bool | ||||
| 	var deploymentType string // static pods, self-hosted, etc. | ||||
| 	var selfHosted bool | ||||
| 	cmd := &cobra.Command{ | ||||
| 		Use:   "init", | ||||
| 		Short: "Run this in order to set up the Kubernetes master", | ||||
| 		Run: func(cmd *cobra.Command, args []string) { | ||||
| 			i, err := NewInit(cfgPath, &cfg, skipPreFlight, deploymentType) | ||||
| 			i, err := NewInit(cfgPath, &cfg, skipPreFlight, selfHosted) | ||||
| 			kubeadmutil.CheckErr(err) | ||||
| 			kubeadmutil.CheckErr(Validate(i.Cfg())) | ||||
| 			kubeadmutil.CheckErr(i.Validate()) | ||||
| 			kubeadmutil.CheckErr(i.Run(out)) | ||||
| 		}, | ||||
| 	} | ||||
| @@ -118,7 +113,7 @@ func NewCmdInit(out io.Writer) *cobra.Command { | ||||
|  | ||||
| 	cmd.PersistentFlags().BoolVar( | ||||
| 		&skipPreFlight, "skip-preflight-checks", skipPreFlight, | ||||
| 		"skip preflight checks normally run before modifying the system", | ||||
| 		"Skip preflight checks normally run before modifying the system", | ||||
| 	) | ||||
|  | ||||
| 	cmd.PersistentFlags().Var( | ||||
| @@ -126,15 +121,15 @@ func NewCmdInit(out io.Writer) *cobra.Command { | ||||
| 		"The discovery method kubeadm will use for connecting nodes to the master", | ||||
| 	) | ||||
|  | ||||
| 	cmd.PersistentFlags().StringVar( | ||||
| 		&deploymentType, "deployment", deploymentType, | ||||
| 		fmt.Sprintf("specify a deployment type from %v", deploymentTypes), | ||||
| 	cmd.PersistentFlags().BoolVar( | ||||
| 		&selfHosted, "self-hosted", selfHosted, | ||||
| 		"Enable self-hosted control plane", | ||||
| 	) | ||||
|  | ||||
| 	return cmd | ||||
| } | ||||
|  | ||||
| func NewInit(cfgPath string, cfg *kubeadmapi.MasterConfiguration, skipPreFlight bool, deploymentType string) (Init, error) { | ||||
| func NewInit(cfgPath string, cfg *kubeadmapi.MasterConfiguration, skipPreFlight bool, selfHosted bool) (*Init, error) { | ||||
|  | ||||
| 	fmt.Println("[kubeadm] WARNING: kubeadm is in alpha, please do not use it for production clusters.") | ||||
|  | ||||
| @@ -173,52 +168,27 @@ func NewInit(cfgPath string, cfg *kubeadmapi.MasterConfiguration, skipPreFlight | ||||
| 	// Try to start the kubelet service in case it's inactive | ||||
| 	preflight.TryStartKubelet() | ||||
|  | ||||
|  | ||||
| 	// Warn about the limitations with the current cloudprovider solution. | ||||
| 	if cfg.CloudProvider != "" { | ||||
| 		fmt.Println("WARNING: For cloudprovider integrations to work --cloud-provider must be set for all kubelets in the cluster.") | ||||
| 		fmt.Println("\t(/etc/systemd/system/kubelet.service.d/10-kubeadm.conf should be edited for this purpose)") | ||||
| 	} | ||||
|  | ||||
| 	var deploymentTypeValid bool | ||||
| 	for _, supportedDT := range deploymentTypes { | ||||
| 		if deploymentType == supportedDT { | ||||
| 			deploymentTypeValid = true | ||||
| 		} | ||||
| 	} | ||||
| 	if !deploymentTypeValid { | ||||
| 		return nil, fmt.Errorf("%s is not a valid deployment type, you can use any of %v or leave unset to accept the default", deploymentType, deploymentTypes) | ||||
| 	} | ||||
| 	if deploymentType == deploymentSelfHosted { | ||||
| 		fmt.Println("[init] Creating self-hosted Kubernetes deployment...") | ||||
| 		return &SelfHostedInit{cfg: cfg}, nil | ||||
| 	} | ||||
|  | ||||
| 	fmt.Println("[init] Creating static pod Kubernetes deployment...") | ||||
| 	return &StaticPodInit{cfg: cfg}, nil | ||||
| 	return &Init{cfg: cfg, selfHosted: selfHosted}, nil | ||||
| } | ||||
|  | ||||
| func Validate(cfg *kubeadmapi.MasterConfiguration) error { | ||||
| 	return validation.ValidateMasterConfiguration(cfg).ToAggregate() | ||||
| type Init struct { | ||||
| 	cfg        *kubeadmapi.MasterConfiguration | ||||
| 	selfHosted bool | ||||
| } | ||||
|  | ||||
| // Init structs define implementations of the cluster setup for each supported | ||||
| // delpoyment type. | ||||
| type Init interface { | ||||
| 	Cfg() *kubeadmapi.MasterConfiguration | ||||
| 	Run(out io.Writer) error | ||||
| } | ||||
|  | ||||
| type StaticPodInit struct { | ||||
| 	cfg *kubeadmapi.MasterConfiguration | ||||
| } | ||||
|  | ||||
| func (spi *StaticPodInit) Cfg() *kubeadmapi.MasterConfiguration { | ||||
| 	return spi.cfg | ||||
| // Validate validates configuration passed to "kubeadm init" | ||||
| func (i *Init) Validate() error { | ||||
| 	return validation.ValidateMasterConfiguration(i.cfg).ToAggregate() | ||||
| } | ||||
|  | ||||
| // Run executes master node provisioning, including certificates, needed static pod manifests, etc. | ||||
| func (i *StaticPodInit) Run(out io.Writer) error { | ||||
| func (i *Init) Run(out io.Writer) error { | ||||
|  | ||||
| 	// PHASE 1: Generate certificates | ||||
| 	caCert, err := certphase.CreatePKIAssets(i.cfg, kubeadmapi.GlobalEnvParams.HostPKIPath) | ||||
| @@ -287,97 +257,12 @@ func (i *StaticPodInit) Run(out io.Writer) error { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if err := kubemaster.CreateEssentialAddons(i.cfg, client); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	fmt.Fprintf(out, initDoneMsgf, generateJoinArgs(i.cfg)) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // SelfHostedInit initializes a self-hosted cluster. | ||||
| type SelfHostedInit struct { | ||||
| 	cfg *kubeadmapi.MasterConfiguration | ||||
| } | ||||
|  | ||||
| func (spi *SelfHostedInit) Cfg() *kubeadmapi.MasterConfiguration { | ||||
| 	return spi.cfg | ||||
| } | ||||
|  | ||||
| // Run executes master node provisioning, including certificates, needed pod manifests, etc. | ||||
| func (i *SelfHostedInit) Run(out io.Writer) error { | ||||
| 	// Validate token if any, otherwise generate | ||||
| 	if i.cfg.Discovery.Token != nil { | ||||
| 		if i.cfg.Discovery.Token.ID != "" && i.cfg.Discovery.Token.Secret != "" { | ||||
| 			fmt.Printf("[token-discovery] A token has been provided, validating [%s]\n", kubeadmutil.BearerToken(i.cfg.Discovery.Token)) | ||||
| 			if valid, err := kubeadmutil.ValidateToken(i.cfg.Discovery.Token); valid == false { | ||||
| 				return err | ||||
| 			} | ||||
| 		} else { | ||||
| 			fmt.Println("[token-discovery] A token has not been provided, generating one") | ||||
| 			if err := kubeadmutil.GenerateToken(i.cfg.Discovery.Token); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// Make sure there is at least one address | ||||
| 		if len(i.cfg.Discovery.Token.Addresses) == 0 { | ||||
| 			ip, err := netutil.ChooseHostInterface() | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			i.cfg.Discovery.Token.Addresses = []string{ip.String() + ":" + strconv.Itoa(kubeadmapiext.DefaultDiscoveryBindPort)} | ||||
| 		} | ||||
|  | ||||
| 		if err := kubemaster.CreateTokenAuthFile(kubeadmutil.BearerToken(i.cfg.Discovery.Token)); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// PHASE 1: Generate certificates | ||||
| 	caCert, err := certphase.CreatePKIAssets(i.cfg, kubeadmapi.GlobalEnvParams.HostPKIPath) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// PHASE 2: Generate kubeconfig files for the admin and the kubelet | ||||
|  | ||||
| 	// TODO this is not great, but there is only one address we can use here | ||||
| 	// so we'll pick the first one, there is much of chance to have an empty | ||||
| 	// slice by the time this gets called | ||||
| 	masterEndpoint := fmt.Sprintf("https://%s:%d", i.cfg.API.AdvertiseAddresses[0], i.cfg.API.Port) | ||||
| 	err = kubeconfigphase.CreateAdminAndKubeletKubeConfig(masterEndpoint, kubeadmapi.GlobalEnvParams.HostPKIPath, kubeadmapi.GlobalEnvParams.KubernetesDir) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// Phase 3: Bootstrap the control plane | ||||
| 	if err := kubemaster.WriteStaticPodManifests(i.cfg); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	client, err := kubemaster.CreateClientAndWaitForAPI(path.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeconfigphase.AdminKubeConfigFileName)) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err := kubemaster.UpdateMasterRoleLabelsAndTaints(client, false); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// Temporary control plane is up, now we create our self hosted control | ||||
| 	// plane components and remove the static manifests: | ||||
| 	fmt.Println("[init] Creating self-hosted control plane...") | ||||
| 	if err := kubemaster.CreateSelfHostedControlPlane(i.cfg, client); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if i.cfg.Discovery.Token != nil { | ||||
| 		fmt.Printf("[token-discovery] Using token: %s\n", kubeadmutil.BearerToken(i.cfg.Discovery.Token)) | ||||
| 		if err := kubemaster.CreateDiscoveryDeploymentAndSecret(i.cfg, client, caCert); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		if err := kubeadmutil.UpdateOrCreateToken(client, i.cfg.Discovery.Token, kubeadmutil.DefaultTokenDuration); err != nil { | ||||
| 	// Is deployment type self-hosted? | ||||
| 	if i.selfHosted { | ||||
| 		// Temporary control plane is up, now we create our self hosted control | ||||
| 		// plane components and remove the static manifests: | ||||
| 		fmt.Println("[init] Creating self-hosted control plane...") | ||||
| 		if err := kubemaster.CreateSelfHostedControlPlane(i.cfg, client); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
| @@ -542,6 +542,7 @@ seccomp-profile-root | ||||
| secondary-node-eviction-rate | ||||
| secret-name | ||||
| secure-port | ||||
| self-hosted | ||||
| serialize-image-pulls | ||||
| server-start-timeout | ||||
| service-account-key-file | ||||
|   | ||||
		Reference in New Issue
	
	Block a user