diff --git a/cmd/kubeadm/app/cmd/alpha/alpha.go b/cmd/kubeadm/app/cmd/alpha/alpha.go index 06cf9b4279..dbcc722ed2 100644 --- a/cmd/kubeadm/app/cmd/alpha/alpha.go +++ b/cmd/kubeadm/app/cmd/alpha/alpha.go @@ -53,7 +53,6 @@ func newCmdPhase(out io.Writer) *cobra.Command { cmd.AddCommand(phases.NewCmdAddon()) cmd.AddCommand(phases.NewCmdBootstrapToken()) - cmd.AddCommand(phases.NewCmdEtcd()) cmd.AddCommand(phases.NewCmdMarkMaster()) cmd.AddCommand(phases.NewCmdSelfhosting()) cmd.AddCommand(phases.NewCmdUploadConfig()) diff --git a/cmd/kubeadm/app/cmd/init.go b/cmd/kubeadm/app/cmd/init.go index 48bc6c9d68..5e62d27112 100644 --- a/cmd/kubeadm/app/cmd/init.go +++ b/cmd/kubeadm/app/cmd/init.go @@ -48,7 +48,6 @@ import ( clusterinfophase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/clusterinfo" nodebootstraptokenphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/node" certsphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs" - etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd" kubeletphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet" markmasterphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/markmaster" patchnodephase "k8s.io/kubernetes/cmd/kubeadm/app/phases/patchnode" @@ -168,6 +167,7 @@ func NewCmdInit(out io.Writer) *cobra.Command { initRunner.AppendPhase(phases.NewCertsPhase()) initRunner.AppendPhase(phases.NewKubeConfigPhase()) initRunner.AppendPhase(phases.NewControlPlanePhase()) + initRunner.AppendPhase(phases.NewEtcdPhase()) // TODO: add other phases to the runner. // sets the data builder function, that will be used by the runner @@ -434,14 +434,6 @@ func runInit(i *initData, out io.Writer) error { adminKubeConfigPath := filepath.Join(kubeConfigDir, kubeadmconstants.AdminKubeConfigFileName) - // Add etcd static pod spec only if external etcd is not configured - if i.cfg.Etcd.External == nil { - glog.V(1).Infof("[init] no external etcd found. Creating manifest for local etcd static pod") - if err := etcdphase.CreateLocalEtcdStaticPodManifestFile(manifestDir, i.cfg); err != nil { - return errors.Wrap(err, "error creating local etcd static pod manifest file") - } - } - // If we're dry-running, print the generated manifests if err := printFilesIfDryRunning(i.dryRun, manifestDir); err != nil { return errors.Wrap(err, "error printing files on dryrun") diff --git a/cmd/kubeadm/app/cmd/phases/BUILD b/cmd/kubeadm/app/cmd/phases/BUILD index 3c73b3ead1..7226e9a652 100644 --- a/cmd/kubeadm/app/cmd/phases/BUILD +++ b/cmd/kubeadm/app/cmd/phases/BUILD @@ -65,13 +65,11 @@ go_test( name = "go_default_test", srcs = [ "addons_test.go", - "etcd_test.go", "util_test.go", ], embed = [":go_default_library"], deps = [ "//cmd/kubeadm/app/apis/kubeadm/v1beta1:go_default_library", - "//cmd/kubeadm/test:go_default_library", "//cmd/kubeadm/test/cmd:go_default_library", "//pkg/version:go_default_library", ], diff --git a/cmd/kubeadm/app/cmd/phases/etcd.go b/cmd/kubeadm/app/cmd/phases/etcd.go index 911cafbfb0..bd6a2f33c3 100644 --- a/cmd/kubeadm/app/cmd/phases/etcd.go +++ b/cmd/kubeadm/app/cmd/phases/etcd.go @@ -19,84 +19,71 @@ package phases import ( "fmt" - "github.com/spf13/cobra" - + "github.com/golang/glog" + "github.com/pkg/errors" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" - kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme" - kubeadmapiv1beta1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1" - cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util" - kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" + "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow" etcdphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/etcd" "k8s.io/kubernetes/pkg/util/normalizer" ) var ( - etcdLocalLongDesc = fmt.Sprintf(normalizer.LongDesc(` - Generates the static Pod manifest file for a local, single-node etcd instance and saves it to %s file. - `+cmdutil.AlphaDisclaimer), kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, kubeadmconstants.GetStaticPodDirectory())) - etcdLocalExample = normalizer.Examples(` # Generates the static Pod manifest file for etcd, functionally - # equivalent to what generated by kubeadm init. - kubeadm alpha phase etcd local + # equivalent to what is generated by kubeadm init. + kubeadm init phase etcd local - # Generates the static Pod manifest file for etcd. - kubeadm alpha phase etcd local --config masterconfiguration.yaml + # Generates the static Pod manifest file for etcd using options + # read from a configuration file. + kubeadm init phase etcd local --config config.yaml `) ) -// NewCmdEtcd returns main command for Etcd phase -func NewCmdEtcd() *cobra.Command { - cmd := &cobra.Command{ - Use: "etcd", - Short: "Generates static Pod manifest file for etcd.", - Long: cmdutil.MacroCommandLongDescription, - } - - manifestPath := kubeadmconstants.GetStaticPodDirectory() - cmd.AddCommand(getEtcdSubCommands(manifestPath, "")...) - return cmd +type etcdData interface { + Cfg() *kubeadmapi.InitConfiguration + ManifestDir() string } -// getEtcdSubCommands returns sub commands for etcd phase -func getEtcdSubCommands(outDir, defaultKubernetesVersion string) []*cobra.Command { - - cfg := &kubeadmapiv1beta1.InitConfiguration{} - - // Default values for the cobra help text - kubeadmscheme.Scheme.Default(cfg) - - var cfgPath string - var subCmds []*cobra.Command - - properties := struct { - use string - short string - long string - examples string - cmdFunc func(outDir string, cfg *kubeadmapi.InitConfiguration) error - }{ - use: "local", - short: "Generates the static Pod manifest file for a local, single-node etcd instance", - long: etcdLocalLongDesc, - examples: etcdLocalExample, - cmdFunc: etcdphase.CreateLocalEtcdStaticPodManifestFile, +// NewEtcdPhase creates a kubeadm workflow phase that implements handling of etcd. +func NewEtcdPhase() workflow.Phase { + phase := workflow.Phase{ + Name: "etcd", + Short: "Generates static Pod manifest file for local etcd.", + Example: etcdLocalExample, + Phases: []workflow.Phase{ + newEtcdLocalSubPhase(), + }, + } + return phase +} + +func newEtcdLocalSubPhase() workflow.Phase { + phase := workflow.Phase{ + Name: "local", + Short: "Generates the static Pod manifest file for a local, single-node local etcd instance.", + Example: etcdLocalExample, + Run: runEtcdPhaseLocal(), + } + return phase +} + +func runEtcdPhaseLocal() func(c workflow.RunData) error { + return func(c workflow.RunData) error { + data, ok := c.(etcdData) + if !ok { + return errors.New("etcd phase invoked with an invalid data struct") + } + cfg := data.Cfg() + + // Add etcd static pod spec only if external etcd is not configured + if cfg.Etcd.External == nil { + fmt.Printf("[etcd] Creating static Pod manifest for local etcd in %q\n", data.ManifestDir()) + if err := etcdphase.CreateLocalEtcdStaticPodManifestFile(data.ManifestDir(), cfg); err != nil { + return errors.Wrap(err, "error creating local etcd static pod manifest file") + } + } else { + glog.V(1).Infof("[etcd] External etcd mode. Skipping the creation of a manifest for local etcd") + } + return nil } - - // Creates the UX Command - cmd := &cobra.Command{ - Use: properties.use, - Short: properties.short, - Long: properties.long, - Example: properties.examples, - Run: runCmdPhase(properties.cmdFunc, &outDir, &cfgPath, cfg, defaultKubernetesVersion), - } - - // Add flags to the command - cmd.Flags().StringVar(&cfg.CertificatesDir, "cert-dir", cfg.CertificatesDir, `The path where certificates are stored`) - cmd.Flags().StringVar(&cfgPath, "config", cfgPath, "Path to kubeadm config file. WARNING: Usage of a configuration file is experimental") - - subCmds = append(subCmds, cmd) - - return subCmds } diff --git a/cmd/kubeadm/app/cmd/phases/etcd_test.go b/cmd/kubeadm/app/cmd/phases/etcd_test.go deleted file mode 100644 index 640d9ed67b..0000000000 --- a/cmd/kubeadm/app/cmd/phases/etcd_test.go +++ /dev/null @@ -1,85 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package phases - -import ( - "fmt" - "os" - "testing" - - testutil "k8s.io/kubernetes/cmd/kubeadm/test" - cmdtestutil "k8s.io/kubernetes/cmd/kubeadm/test/cmd" -) - -const phaseTestK8sVersion = "v1.11.0" - -func TestEtcdSubCommandsHasFlags(t *testing.T) { - - subCmds := getEtcdSubCommands("", phaseTestK8sVersion) - - commonFlags := []string{ - "cert-dir", - "config", - } - - var tests = []struct { - command string - additionalFlags []string - }{ - { - command: "local", - }, - } - - for _, test := range tests { - expectedFlags := append(commonFlags, test.additionalFlags...) - cmdtestutil.AssertSubCommandHasFlags(t, subCmds, test.command, expectedFlags...) - } -} - -func TestEtcdCreateFilesWithFlags(t *testing.T) { - - var tests = []struct { - command string - additionalFlags []string - expectedFiles []string - }{ - { - command: "local", - expectedFiles: []string{"etcd.yaml"}, - additionalFlags: []string{}, - }, - } - - for _, test := range tests { - - // Create temp folder for the test case - tmpdir := testutil.SetupTempDir(t) - defer os.RemoveAll(tmpdir) - - // Get subcommands working in the temporary directory - subCmds := getEtcdSubCommands(tmpdir, phaseTestK8sVersion) - - // Execute the subcommand - certDirFlag := fmt.Sprintf("--cert-dir=%s", tmpdir) - allFlags := append(test.additionalFlags, certDirFlag) - cmdtestutil.RunSubCommand(t, subCmds, test.command, allFlags...) - - // Checks that requested files are there - testutil.AssertFileExists(t, tmpdir, test.expectedFiles...) - } -} diff --git a/cmd/kubeadm/app/phases/etcd/local.go b/cmd/kubeadm/app/phases/etcd/local.go index 2e3bf6c61c..79e6619023 100644 --- a/cmd/kubeadm/app/phases/etcd/local.go +++ b/cmd/kubeadm/app/phases/etcd/local.go @@ -46,7 +46,6 @@ func CreateLocalEtcdStaticPodManifestFile(manifestDir string, cfg *kubeadmapi.In if cfg.ClusterConfiguration.Etcd.External != nil { return errors.New("etcd static pod manifest cannot be generated for cluster using external etcd") } - glog.V(1).Infoln("creating local etcd static pod manifest file") // gets etcd StaticPodSpec emptyInitialCluster := []etcdutil.Member{} spec := GetEtcdPodSpec(cfg, emptyInitialCluster) @@ -55,7 +54,7 @@ func CreateLocalEtcdStaticPodManifestFile(manifestDir string, cfg *kubeadmapi.In return err } - fmt.Printf("[etcd] Wrote Static Pod manifest for a local etcd instance to %q\n", kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, manifestDir)) + glog.V(1).Infof("[etcd] wrote Static Pod manifest for a local etcd instance to %q\n", kubeadmconstants.GetStaticPodFilepath(kubeadmconstants.Etcd, manifestDir)) return nil } diff --git a/docs/.generated_docs b/docs/.generated_docs index 27089a5a9c..0d06ea8a22 100644 --- a/docs/.generated_docs +++ b/docs/.generated_docs @@ -34,8 +34,6 @@ docs/admin/kubeadm_alpha_phase_bootstrap-token_create.md docs/admin/kubeadm_alpha_phase_bootstrap-token_node.md docs/admin/kubeadm_alpha_phase_bootstrap-token_node_allow-auto-approve.md docs/admin/kubeadm_alpha_phase_bootstrap-token_node_allow-post-csrs.md -docs/admin/kubeadm_alpha_phase_etcd.md -docs/admin/kubeadm_alpha_phase_etcd_local.md docs/admin/kubeadm_alpha_phase_mark-master.md docs/admin/kubeadm_alpha_phase_selfhosting.md docs/admin/kubeadm_alpha_phase_selfhosting_convert-from-staticpods.md @@ -73,6 +71,8 @@ docs/admin/kubeadm_init_phase_control-plane.md docs/admin/kubeadm_init_phase_control-plane_apiserver.md docs/admin/kubeadm_init_phase_control-plane_controller-manager.md docs/admin/kubeadm_init_phase_control-plane_scheduler.md +docs/admin/kubeadm_init_phase_etcd.md +docs/admin/kubeadm_init_phase_etcd_local.md docs/admin/kubeadm_init_phase_kubeconfig.md docs/admin/kubeadm_init_phase_kubeconfig_admin.md docs/admin/kubeadm_init_phase_kubeconfig_controller-manager.md @@ -128,8 +128,6 @@ docs/man/man1/kubeadm-alpha-phase-bootstrap-token-node-allow-auto-approve.1 docs/man/man1/kubeadm-alpha-phase-bootstrap-token-node-allow-post-csrs.1 docs/man/man1/kubeadm-alpha-phase-bootstrap-token-node.1 docs/man/man1/kubeadm-alpha-phase-bootstrap-token.1 -docs/man/man1/kubeadm-alpha-phase-etcd-local.1 -docs/man/man1/kubeadm-alpha-phase-etcd.1 docs/man/man1/kubeadm-alpha-phase-mark-master.1 docs/man/man1/kubeadm-alpha-phase-selfhosting-convert-from-staticpods.1 docs/man/man1/kubeadm-alpha-phase-selfhosting.1 @@ -168,6 +166,8 @@ docs/man/man1/kubeadm-init-phase-control-plane-apiserver.1 docs/man/man1/kubeadm-init-phase-control-plane-controller-manager.1 docs/man/man1/kubeadm-init-phase-control-plane-scheduler.1 docs/man/man1/kubeadm-init-phase-control-plane.1 +docs/man/man1/kubeadm-init-phase-etcd-local.1 +docs/man/man1/kubeadm-init-phase-etcd.1 docs/man/man1/kubeadm-init-phase-kubeconfig-admin.1 docs/man/man1/kubeadm-init-phase-kubeconfig-controller-manager.1 docs/man/man1/kubeadm-init-phase-kubeconfig-kubelet.1 diff --git a/docs/admin/kubeadm_alpha_phase_etcd.md b/docs/admin/kubeadm_init_phase_etcd.md similarity index 100% rename from docs/admin/kubeadm_alpha_phase_etcd.md rename to docs/admin/kubeadm_init_phase_etcd.md diff --git a/docs/admin/kubeadm_alpha_phase_etcd_local.md b/docs/admin/kubeadm_init_phase_etcd_local.md similarity index 100% rename from docs/admin/kubeadm_alpha_phase_etcd_local.md rename to docs/admin/kubeadm_init_phase_etcd_local.md diff --git a/docs/man/man1/kubeadm-alpha-phase-etcd-local.1 b/docs/man/man1/kubeadm-init-phase-etcd-local.1 similarity index 100% rename from docs/man/man1/kubeadm-alpha-phase-etcd-local.1 rename to docs/man/man1/kubeadm-init-phase-etcd-local.1 diff --git a/docs/man/man1/kubeadm-alpha-phase-etcd.1 b/docs/man/man1/kubeadm-init-phase-etcd.1 similarity index 100% rename from docs/man/man1/kubeadm-alpha-phase-etcd.1 rename to docs/man/man1/kubeadm-init-phase-etcd.1