kubeadm: add temporary --self-hosted flag.

This commit is contained in:
Paulo Pires 2017-01-20 16:27:34 +00:00
parent c80c0275da
commit 724ce6a8a5
No known key found for this signature in database
GPG Key ID: F3F6ED5C522EAA71
2 changed files with 23 additions and 137 deletions

View File

@ -21,13 +21,11 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"path" "path"
"strconv"
"github.com/renstrom/dedent" "github.com/renstrom/dedent"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
netutil "k8s.io/apimachinery/pkg/util/net"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1" kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation" "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
@ -54,9 +52,6 @@ var (
kubeadm join --discovery %s kubeadm join --discovery %s
`) `)
deploymentStaticPod = "static-pods"
deploymentSelfHosted = "self-hosted"
deploymentTypes = []string{deploymentStaticPod, deploymentSelfHosted}
) )
// NewCmdInit returns "kubeadm init" command. // NewCmdInit returns "kubeadm init" command.
@ -68,14 +63,14 @@ func NewCmdInit(out io.Writer) *cobra.Command {
var cfgPath string var cfgPath string
var skipPreFlight bool var skipPreFlight bool
var deploymentType string // static pods, self-hosted, etc. var selfHosted bool
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "init", Use: "init",
Short: "Run this in order to set up the Kubernetes master", Short: "Run this in order to set up the Kubernetes master",
Run: func(cmd *cobra.Command, args []string) { 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(err)
kubeadmutil.CheckErr(Validate(i.Cfg())) kubeadmutil.CheckErr(i.Validate())
kubeadmutil.CheckErr(i.Run(out)) kubeadmutil.CheckErr(i.Run(out))
}, },
} }
@ -118,7 +113,7 @@ func NewCmdInit(out io.Writer) *cobra.Command {
cmd.PersistentFlags().BoolVar( cmd.PersistentFlags().BoolVar(
&skipPreFlight, "skip-preflight-checks", skipPreFlight, &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( 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", "The discovery method kubeadm will use for connecting nodes to the master",
) )
cmd.PersistentFlags().StringVar( cmd.PersistentFlags().BoolVar(
&deploymentType, "deployment", deploymentType, &selfHosted, "self-hosted", selfHosted,
fmt.Sprintf("specify a deployment type from %v", deploymentTypes), "Enable self-hosted control plane",
) )
return cmd 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.") 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 // Try to start the kubelet service in case it's inactive
preflight.TryStartKubelet() preflight.TryStartKubelet()
// Warn about the limitations with the current cloudprovider solution. // Warn about the limitations with the current cloudprovider solution.
if cfg.CloudProvider != "" { if cfg.CloudProvider != "" {
fmt.Println("WARNING: For cloudprovider integrations to work --cloud-provider must be set for all kubelets in the cluster.") 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)") fmt.Println("\t(/etc/systemd/system/kubelet.service.d/10-kubeadm.conf should be edited for this purpose)")
} }
var deploymentTypeValid bool return &Init{cfg: cfg, selfHosted: selfHosted}, nil
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
} }
func Validate(cfg *kubeadmapi.MasterConfiguration) error { type Init struct {
return validation.ValidateMasterConfiguration(cfg).ToAggregate() cfg *kubeadmapi.MasterConfiguration
selfHosted bool
} }
// Init structs define implementations of the cluster setup for each supported // Validate validates configuration passed to "kubeadm init"
// delpoyment type. func (i *Init) Validate() error {
type Init interface { return validation.ValidateMasterConfiguration(i.cfg).ToAggregate()
Cfg() *kubeadmapi.MasterConfiguration
Run(out io.Writer) error
}
type StaticPodInit struct {
cfg *kubeadmapi.MasterConfiguration
}
func (spi *StaticPodInit) Cfg() *kubeadmapi.MasterConfiguration {
return spi.cfg
} }
// Run executes master node provisioning, including certificates, needed static pod manifests, etc. // 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 // PHASE 1: Generate certificates
caCert, err := certphase.CreatePKIAssets(i.cfg, kubeadmapi.GlobalEnvParams.HostPKIPath) 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 { // Is deployment type self-hosted?
return err if i.selfHosted {
} // Temporary control plane is up, now we create our self hosted control
// plane components and remove the static manifests:
fmt.Fprintf(out, initDoneMsgf, generateJoinArgs(i.cfg)) fmt.Println("[init] Creating self-hosted control plane...")
return nil if err := kubemaster.CreateSelfHostedControlPlane(i.cfg, client); err != 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 {
return err return err
} }
} }

View File

@ -542,6 +542,7 @@ seccomp-profile-root
secondary-node-eviction-rate secondary-node-eviction-rate
secret-name secret-name
secure-port secure-port
self-hosted
serialize-image-pulls serialize-image-pulls
server-start-timeout server-start-timeout
service-account-key-file service-account-key-file