Skip to content

Commit

Permalink
Set kubeadm version as the default version in phase command.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangpengzhao committed Aug 8, 2018
1 parent db9545e commit 5cf9291
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 59 deletions.
7 changes: 7 additions & 0 deletions cmd/kubeadm/app/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
kubeadmapiv1alpha2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha2"
kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3"
phaseutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
Expand Down Expand Up @@ -368,6 +369,12 @@ func RunConfigView(out io.Writer, client clientset.Interface) error {

// uploadConfiguration handles the uploading of the configuration internally
func uploadConfiguration(client clientset.Interface, cfgPath string, defaultcfg *kubeadmapiv1alpha3.InitConfiguration) error {
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := phaseutil.SetKubernetesVersion(client, defaultcfg)
if err != nil {
return err
}

// Default both statically and dynamically, convert to internal API type, and validate everything
// First argument is unset here as we shouldn't load a config file from disk
Expand Down
18 changes: 8 additions & 10 deletions cmd/kubeadm/app/cmd/phases/bootstraptoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ func NewCmdBootstrapToken() *cobra.Command {

// NewSubCmdBootstrapTokenAll returns the Cobra command for running the token all sub-phase
func NewSubCmdBootstrapTokenAll(kubeConfigFile *string) *cobra.Command {
cfg := &kubeadmapiv1alpha3.InitConfiguration{
// KubernetesVersion is not used by bootstrap-token, but we set this explicitly to avoid
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
KubernetesVersion: "v1.10.0",
}
cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)
Expand Down Expand Up @@ -174,11 +170,7 @@ func NewSubCmdBootstrapTokenAll(kubeConfigFile *string) *cobra.Command {

// NewSubCmdBootstrapToken returns the Cobra command for running the create token phase
func NewSubCmdBootstrapToken(kubeConfigFile *string) *cobra.Command {
cfg := &kubeadmapiv1alpha3.InitConfiguration{
// KubernetesVersion is not used by bootstrap-token, but we set this explicitly to avoid
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
KubernetesVersion: "v1.10.0",
}
cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)
Expand Down Expand Up @@ -307,6 +299,12 @@ func addGenericFlags(flagSet *pflag.FlagSet, cfgPath *string, skipTokenPrint *bo
}

func createBootstrapToken(kubeConfigFile string, client clientset.Interface, cfgPath string, cfg *kubeadmapiv1alpha3.InitConfiguration, skipTokenPrint bool) error {
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := SetKubernetesVersion(client, cfg)
if err != nil {
return err
}

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
Expand Down
23 changes: 14 additions & 9 deletions cmd/kubeadm/app/cmd/phases/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,6 @@ func getCertsSubCommands(defaultKubernetesVersion string) []*cobra.Command {

cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// This is used for unit testing only...
// If we wouldn't set this to something, the code would dynamically look up the version from the internet
// By setting this explicitly for tests workarounds that
if defaultKubernetesVersion != "" {
cfg.KubernetesVersion = defaultKubernetesVersion
}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)

Expand Down Expand Up @@ -252,7 +245,7 @@ func getCertsSubCommands(defaultKubernetesVersion string) []*cobra.Command {
Short: properties.short,
Long: properties.long,
Example: properties.examples,
Run: runCmdFunc(properties.cmdFunc, &cfgPath, cfg),
Run: runCmdFunc(properties.cmdFunc, &cfgPath, cfg, defaultKubernetesVersion),
}

// Add flags to the command
Expand All @@ -272,7 +265,7 @@ func getCertsSubCommands(defaultKubernetesVersion string) []*cobra.Command {
}

// runCmdFunc creates a cobra.Command Run function, by composing the call to the given cmdFunc with necessary additional steps (e.g preparation of input parameters)
func runCmdFunc(cmdFunc func(cfg *kubeadmapi.InitConfiguration) error, cfgPath *string, cfg *kubeadmapiv1alpha3.InitConfiguration) func(cmd *cobra.Command, args []string) {
func runCmdFunc(cmdFunc func(cfg *kubeadmapi.InitConfiguration) error, cfgPath *string, cfg *kubeadmapiv1alpha3.InitConfiguration, defaultKubernetesVersion string) func(cmd *cobra.Command, args []string) {

// the following statement build a closure that wraps a call to a cmdFunc, binding
// the function itself with the specific parameters of each sub command.
Expand All @@ -284,6 +277,18 @@ func runCmdFunc(cmdFunc func(cfg *kubeadmapi.InitConfiguration) error, cfgPath *
kubeadmutil.CheckErr(err)
}

// This is used for unit testing only...
// If we wouldn't set this to something, the code would dynamically look up the version from the internet
// By setting this explicitly for tests workarounds that
if defaultKubernetesVersion != "" {
cfg.KubernetesVersion = defaultKubernetesVersion
} else {
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := SetKubernetesVersion(nil, cfg)
kubeadmutil.CheckErr(err)
}

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(*cfgPath, cfg)
kubeadmutil.CheckErr(err)
Expand Down
9 changes: 1 addition & 8 deletions cmd/kubeadm/app/cmd/phases/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ func getEtcdSubCommands(outDir, defaultKubernetesVersion string) []*cobra.Comman

cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// This is used for unit testing only...
// If we wouldn't set this to something, the code would dynamically look up the version from the internet
// By setting this explicitly for tests workarounds that
if defaultKubernetesVersion != "" {
cfg.KubernetesVersion = defaultKubernetesVersion
}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)

Expand All @@ -96,7 +89,7 @@ func getEtcdSubCommands(outDir, defaultKubernetesVersion string) []*cobra.Comman
Short: properties.short,
Long: properties.long,
Example: properties.examples,
Run: runCmdPhase(properties.cmdFunc, &outDir, &cfgPath, cfg),
Run: runCmdPhase(properties.cmdFunc, &outDir, &cfgPath, cfg, defaultKubernetesVersion),
}

// Add flags to the command
Expand Down
9 changes: 1 addition & 8 deletions cmd/kubeadm/app/cmd/phases/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ func getKubeConfigSubCommands(out io.Writer, outDir, defaultKubernetesVersion st

cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// This is used for unit testing only...
// If we wouldn't set this to something, the code would dynamically look up the version from the internet
// By setting this explicitly for tests workarounds that
if defaultKubernetesVersion != "" {
cfg.KubernetesVersion = defaultKubernetesVersion
}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)

Expand Down Expand Up @@ -172,7 +165,7 @@ func getKubeConfigSubCommands(out io.Writer, outDir, defaultKubernetesVersion st
Short: properties.short,
Long: properties.long,
Example: properties.examples,
Run: runCmdPhase(properties.cmdFunc, &outDir, &cfgPath, cfg),
Run: runCmdPhase(properties.cmdFunc, &outDir, &cfgPath, cfg, defaultKubernetesVersion),
}

// Add flags to the command
Expand Down
16 changes: 14 additions & 2 deletions cmd/kubeadm/app/cmd/phases/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func NewCmdKubeletConfig() *cobra.Command {

// NewCmdKubeletConfigUpload calls cobra.Command for uploading dynamic kubelet configuration
func NewCmdKubeletConfigUpload() *cobra.Command {
cfg := &kubeadmapiv1alpha3.InitConfiguration{}
var cfgPath string
kubeConfigFile := constants.GetAdminKubeConfigPath()

Expand All @@ -194,8 +195,13 @@ func NewCmdKubeletConfigUpload() *cobra.Command {
kubeadmutil.CheckErr(fmt.Errorf("The --config argument is required"))
}

// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := SetKubernetesVersion(nil, cfg)
kubeadmutil.CheckErr(err)

// This call returns the ready-to-use configuration based on the configuration file
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, &kubeadmapiv1alpha3.InitConfiguration{})
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
kubeadmutil.CheckErr(err)

client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
Expand Down Expand Up @@ -248,6 +254,7 @@ func getKubeletVersion(kubeletVersionStr string) (*version.Version, error) {

// NewCmdKubeletConfigWriteToDisk calls cobra.Command for writing init kubelet configuration
func NewCmdKubeletConfigWriteToDisk() *cobra.Command {
cfg := &kubeadmapiv1alpha3.InitConfiguration{}
var cfgPath string
cmd := &cobra.Command{
Use: "write-to-disk",
Expand All @@ -259,8 +266,13 @@ func NewCmdKubeletConfigWriteToDisk() *cobra.Command {
kubeadmutil.CheckErr(fmt.Errorf("The --config argument is required"))
}

// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := SetKubernetesVersion(nil, cfg)
kubeadmutil.CheckErr(err)

// This call returns the ready-to-use configuration based on the configuration file
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, &kubeadmapiv1alpha3.InitConfiguration{})
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
kubeadmutil.CheckErr(err)

err = kubeletphase.WriteConfigToDisk(internalcfg.ComponentConfigs.Kubelet, constants.KubeletRunDirectory)
Expand Down
18 changes: 9 additions & 9 deletions cmd/kubeadm/app/cmd/phases/markmaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ var (

// NewCmdMarkMaster returns the Cobra command for running the mark-master phase
func NewCmdMarkMaster() *cobra.Command {

cfg := &kubeadmapiv1alpha3.InitConfiguration{
// KubernetesVersion is not used by mark master, but we set this explicitly to avoid
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
KubernetesVersion: "v1.10.0",
}
cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)
Expand All @@ -68,11 +63,16 @@ func NewCmdMarkMaster() *cobra.Command {
kubeadmutil.CheckErr(err)
}

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
kubeadmutil.CheckErr(err)

client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err = SetKubernetesVersion(client, cfg)
kubeadmutil.CheckErr(err)

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
kubeadmutil.CheckErr(err)

err = markmasterphase.MarkMaster(client, internalcfg.NodeRegistration.Name, internalcfg.NodeRegistration.Taints)
Expand Down
13 changes: 9 additions & 4 deletions cmd/kubeadm/app/cmd/phases/selfhosting.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ func getSelfhostingSubCommand() *cobra.Command {
kubeadmutil.CheckErr(err)
}

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
kubeadmutil.CheckErr(err)

// Gets the kubernetes client
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
kubeadmutil.CheckErr(err)

// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err = SetKubernetesVersion(client, cfg)
kubeadmutil.CheckErr(err)

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
kubeadmutil.CheckErr(err)

// Converts the Static Pod-hosted control plane into a self-hosted one
waiter := apiclient.NewKubeWaiter(client, 2*time.Minute, os.Stdout)
err = selfhosting.CreateSelfHostedControlPlane(constants.GetStaticPodDirectory(), constants.KubernetesDir, internalcfg, client, waiter, false)
Expand Down
10 changes: 8 additions & 2 deletions cmd/kubeadm/app/cmd/phases/uploadconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ var (

// NewCmdUploadConfig returns the Cobra command for running the uploadconfig phase
func NewCmdUploadConfig() *cobra.Command {
cfg := &kubeadmapiv1alpha3.InitConfiguration{}
var cfgPath, kubeConfigFile string

cmd := &cobra.Command{
Use: "upload-config",
Short: "Uploads the currently used configuration for kubeadm to a ConfigMap",
Expand All @@ -62,8 +64,12 @@ func NewCmdUploadConfig() *cobra.Command {
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
kubeadmutil.CheckErr(err)

defaultcfg := &kubeadmapiv1alpha3.InitConfiguration{}
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, defaultcfg)
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err = SetKubernetesVersion(client, cfg)
kubeadmutil.CheckErr(err)

internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
kubeadmutil.CheckErr(err)

err = uploadconfig.UploadConfiguration(internalcfg, client)
Expand Down
33 changes: 32 additions & 1 deletion cmd/kubeadm/app/cmd/phases/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ limitations under the License.
package phases

import (
"os"

"github.com/spf13/cobra"

clientset "k8s.io/client-go/kubernetes"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
)

// runCmdPhase creates a cobra.Command Run function, by composing the call to the given cmdFunc with necessary additional steps (e.g preparation of input parameters)
func runCmdPhase(cmdFunc func(outDir string, cfg *kubeadmapi.InitConfiguration) error, outDir, cfgPath *string, cfg *kubeadmapiv1alpha3.InitConfiguration) func(cmd *cobra.Command, args []string) {
func runCmdPhase(cmdFunc func(outDir string, cfg *kubeadmapi.InitConfiguration) error, outDir, cfgPath *string, cfg *kubeadmapiv1alpha3.InitConfiguration, defaultKubernetesVersion string) func(cmd *cobra.Command, args []string) {

// the following statement build a closure that wraps a call to a cmdFunc, binding
// the function itself with the specific parameters of each sub command.
Expand All @@ -39,6 +43,18 @@ func runCmdPhase(cmdFunc func(outDir string, cfg *kubeadmapi.InitConfiguration)
kubeadmutil.CheckErr(err)
}

// This is used for unit testing only...
// If we wouldn't set this to something, the code would dynamically look up the version from the internet
// By setting this explicitly for tests workarounds that
if defaultKubernetesVersion != "" {
cfg.KubernetesVersion = defaultKubernetesVersion
} else {
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := SetKubernetesVersion(nil, cfg)
kubeadmutil.CheckErr(err)
}

// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(*cfgPath, cfg)
kubeadmutil.CheckErr(err)
Expand All @@ -48,3 +64,18 @@ func runCmdPhase(cmdFunc func(outDir string, cfg *kubeadmapi.InitConfiguration)
kubeadmutil.CheckErr(err)
}
}

// SetKubernetesVersion gets current Kubeadm version and sets it as Kubernetes version of master configuration if not set.
func SetKubernetesVersion(client clientset.Interface, cfg *kubeadmapiv1alpha3.InitConfiguration) error {
if cfg.KubernetesVersion != "" {
return nil
}

kubeadmVer, _, err := upgrade.NewKubeVersionGetter(client, os.Stdout).KubeadmVersion()
if err != nil {
return err
}
cfg.KubernetesVersion = kubeadmVer

return nil
}
13 changes: 8 additions & 5 deletions cmd/kubeadm/app/cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3"
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation"
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
phaseutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases"
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
tokenphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/node"
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
Expand Down Expand Up @@ -89,11 +90,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
tokenCmd.PersistentFlags().BoolVar(&dryRun,
"dry-run", dryRun, "Whether to enable dry-run mode or not")

cfg := &kubeadmapiv1alpha3.InitConfiguration{
// KubernetesVersion is not used by bootstrap-token, but we set this explicitly to avoid
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
KubernetesVersion: "v1.10.0",
}
cfg := &kubeadmapiv1alpha3.InitConfiguration{}

// Default values for the cobra help text
kubeadmscheme.Scheme.Default(cfg)
Expand Down Expand Up @@ -215,6 +212,12 @@ func NewCmdTokenGenerate(out io.Writer) *cobra.Command {

// RunCreateToken generates a new bootstrap token and stores it as a secret on the server.
func RunCreateToken(out io.Writer, client clientset.Interface, cfgPath string, cfg *kubeadmapiv1alpha3.InitConfiguration, printJoinCommand bool, kubeConfigFile string) error {
// KubernetesVersion is not used, but we set it explicitly to avoid the lookup
// of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
err := phaseutil.SetKubernetesVersion(client, cfg)
if err != nil {
return err
}
// This call returns the ready-to-use configuration based on the configuration file that might or might not exist and the default cfg populated by flags
glog.V(1).Infoln("[token] loading configurations")
internalcfg, err := configutil.ConfigFileAndDefaultsToInternalConfig(cfgPath, cfg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/cmd/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestRunCreateToken(t *testing.T) {

cfg := &kubeadmapiv1alpha3.InitConfiguration{

// KubernetesVersion is not used by bootstrap-token, but we set this explicitly to avoid
// KubernetesVersion is not used, but we set this explicitly to avoid
// the lookup of the version from the internet when executing ConfigFileAndDefaultsToInternalConfig
KubernetesVersion: "v1.10.0",
BootstrapTokens: []kubeadmapiv1alpha3.BootstrapToken{
Expand Down

0 comments on commit 5cf9291

Please sign in to comment.