From 95797f13ca5e224daaf5daa01c3a96da12415f01 Mon Sep 17 00:00:00 2001 From: hangaoshuai Date: Fri, 22 Jun 2018 15:20:18 +0800 Subject: [PATCH] fixtodo: Move these kubelet start/stop functions to phases/kubelet --- cmd/kubeadm/app/cmd/init.go | 4 +- cmd/kubeadm/app/cmd/join.go | 4 +- cmd/kubeadm/app/phases/kubelet/BUILD | 2 + cmd/kubeadm/app/phases/kubelet/kubelet.go | 63 +++++++++++++++++++++++ cmd/kubeadm/app/preflight/checks.go | 41 --------------- 5 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 cmd/kubeadm/app/phases/kubelet/kubelet.go diff --git a/cmd/kubeadm/app/cmd/init.go b/cmd/kubeadm/app/cmd/init.go index 5467b59956e..a72a0e015b4 100644 --- a/cmd/kubeadm/app/cmd/init.go +++ b/cmd/kubeadm/app/cmd/init.go @@ -297,7 +297,7 @@ func (i *Init) Run(out io.Writer) error { // Try to stop the kubelet service so no race conditions occur when configuring it if !i.dryRun { glog.V(1).Infof("Stopping the kubelet") - preflight.TryStopKubelet() + kubeletphase.TryStopKubelet() } // Write env file with flags for the kubelet to use. We do not need to write the --register-with-taints for the master, @@ -315,7 +315,7 @@ func (i *Init) Run(out io.Writer) error { if !i.dryRun { // Try to start the kubelet service in case it's inactive glog.V(1).Infof("Starting the kubelet") - preflight.TryStartKubelet() + kubeletphase.TryStartKubelet() } // certsDirToWriteTo is gonna equal cfg.CertificatesDir in the normal case, but gonna be a temp directory if dryrunning diff --git a/cmd/kubeadm/app/cmd/join.go b/cmd/kubeadm/app/cmd/join.go index 20ccfb4f048..35253e6eb99 100644 --- a/cmd/kubeadm/app/cmd/join.go +++ b/cmd/kubeadm/app/cmd/join.go @@ -281,7 +281,7 @@ func (j *Join) Run(out io.Writer) error { // Configure the kubelet. In this short timeframe, kubeadm is trying to stop/restart the kubelet // Try to stop the kubelet service so no race conditions occur when configuring it glog.V(1).Infof("Stopping the kubelet") - preflight.TryStopKubelet() + kubeletphase.TryStopKubelet() // Write the configuration for the kubelet (using the bootstrap token credentials) to disk so the kubelet can start if err := kubeletphase.DownloadConfig(bootstrapClient, kubeletVersion, kubeadmconstants.KubeletRunDirectory); err != nil { @@ -295,7 +295,7 @@ func (j *Join) Run(out io.Writer) error { // Try to start the kubelet service in case it's inactive glog.V(1).Infof("Starting the kubelet") - preflight.TryStartKubelet() + kubeletphase.TryStartKubelet() // Now the kubelet will perform the TLS Bootstrap, transforming /etc/kubernetes/bootstrap-kubelet.conf to /etc/kubernetes/kubelet.conf // Wait for the kubelet to create the /etc/kubernetes/kubelet.conf KubeConfig file. If this process diff --git a/cmd/kubeadm/app/phases/kubelet/BUILD b/cmd/kubeadm/app/phases/kubelet/BUILD index ef5848e5a9e..d33eef8e7b9 100644 --- a/cmd/kubeadm/app/phases/kubelet/BUILD +++ b/cmd/kubeadm/app/phases/kubelet/BUILD @@ -6,6 +6,7 @@ go_library( "config.go", "dynamic.go", "flags.go", + "kubelet.go", ], importpath = "k8s.io/kubernetes/cmd/kubeadm/app/phases/kubelet", visibility = ["//visibility:public"], @@ -19,6 +20,7 @@ go_library( "//pkg/apis/rbac/v1:go_default_library", "//pkg/kubelet/apis/kubeletconfig/scheme:go_default_library", "//pkg/kubelet/apis/kubeletconfig/v1beta1:go_default_library", + "//pkg/util/initsystem:go_default_library", "//pkg/util/node:go_default_library", "//pkg/util/procfs:go_default_library", "//pkg/util/version:go_default_library", diff --git a/cmd/kubeadm/app/phases/kubelet/kubelet.go b/cmd/kubeadm/app/phases/kubelet/kubelet.go new file mode 100644 index 00000000000..76a29139917 --- /dev/null +++ b/cmd/kubeadm/app/phases/kubelet/kubelet.go @@ -0,0 +1,63 @@ +/* +Copyright 2018 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 kubelet + +import ( + "fmt" + + "k8s.io/kubernetes/pkg/util/initsystem" +) + +// TryStartKubelet attempts to bring up kubelet service +func TryStartKubelet() { + // If we notice that the kubelet service is inactive, try to start it + initSystem, err := initsystem.GetInitSystem() + if err != nil { + fmt.Println("[preflight] no supported init system detected, won't make sure the kubelet is running properly.") + return + } + + if !initSystem.ServiceExists("kubelet") { + fmt.Println("[preflight] couldn't detect a kubelet service, can't make sure the kubelet is running properly.") + } + + fmt.Println("[preflight] Activating the kubelet service") + // This runs "systemctl daemon-reload && systemctl restart kubelet" + if err := initSystem.ServiceRestart("kubelet"); err != nil { + fmt.Printf("[preflight] WARNING: unable to start the kubelet service: [%v]\n", err) + fmt.Printf("[preflight] please ensure kubelet is reloaded and running manually.\n") + } +} + +// TryStopKubelet attempts to bring down the kubelet service momentarily +func TryStopKubelet() { + // If we notice that the kubelet service is inactive, try to start it + initSystem, err := initsystem.GetInitSystem() + if err != nil { + fmt.Println("[preflight] no supported init system detected, won't make sure the kubelet not running for a short period of time while setting up configuration for it.") + return + } + + if !initSystem.ServiceExists("kubelet") { + fmt.Println("[preflight] couldn't detect a kubelet service, can't make sure the kubelet not running for a short period of time while setting up configuration for it.") + } + + // This runs "systemctl daemon-reload && systemctl stop kubelet" + if err := initSystem.ServiceStop("kubelet"); err != nil { + fmt.Printf("[preflight] WARNING: unable to stop the kubelet service momentarily: [%v]\n", err) + } +} diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 28d98e6ebbb..9eb799719fd 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -1051,47 +1051,6 @@ func RunChecks(checks []Checker, ww io.Writer, ignorePreflightErrors sets.String return nil } -// TryStartKubelet attempts to bring up kubelet service -// TODO: Move these kubelet start/stop functions to some other place, e.g. phases/kubelet -func TryStartKubelet() { - // If we notice that the kubelet service is inactive, try to start it - initSystem, err := initsystem.GetInitSystem() - if err != nil { - fmt.Println("[preflight] no supported init system detected, won't make sure the kubelet is running properly.") - return - } - - if !initSystem.ServiceExists("kubelet") { - fmt.Println("[preflight] couldn't detect a kubelet service, can't make sure the kubelet is running properly.") - } - - fmt.Println("[preflight] Activating the kubelet service") - // This runs "systemctl daemon-reload && systemctl restart kubelet" - if err := initSystem.ServiceRestart("kubelet"); err != nil { - fmt.Printf("[preflight] WARNING: unable to start the kubelet service: [%v]\n", err) - fmt.Printf("[preflight] please ensure kubelet is reloaded and running manually.\n") - } -} - -// TryStopKubelet attempts to bring down the kubelet service momentarily -func TryStopKubelet() { - // If we notice that the kubelet service is inactive, try to start it - initSystem, err := initsystem.GetInitSystem() - if err != nil { - fmt.Println("[preflight] no supported init system detected, won't make sure the kubelet not running for a short period of time while setting up configuration for it.") - return - } - - if !initSystem.ServiceExists("kubelet") { - fmt.Println("[preflight] couldn't detect a kubelet service, can't make sure the kubelet not running for a short period of time while setting up configuration for it.") - } - - // This runs "systemctl daemon-reload && systemctl stop kubelet" - if err := initSystem.ServiceStop("kubelet"); err != nil { - fmt.Printf("[preflight] WARNING: unable to stop the kubelet service momentarily: [%v]\n", err) - } -} - // setHasItemOrAll is helper function that return true if item is present in the set (case insensitive) or special key 'all' is present func setHasItemOrAll(s sets.String, item string) bool { if s.Has("all") || s.Has(strings.ToLower(item)) {