From 2e12787ae56b871c7247aeaf14b8e4c468a48bcf Mon Sep 17 00:00:00 2001 From: Alexander Kanevskiy Date: Thu, 17 Aug 2017 14:18:58 +0300 Subject: [PATCH] kubeadm: preflight check for enabled swap Recent versions of kubelet require special flags if runned on the system with enabled swap. Thus, remind user about either disabling swap or add appropriate flag to kubelet settings --- cmd/kubeadm/app/preflight/checks.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 1e766db4dbd..62c3a8c997d 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -431,6 +431,32 @@ func (kubever KubernetesVersionCheck) Check() (warnings, errors []error) { return nil, nil } +// SwapCheck warns if swap is enabled +type SwapCheck struct{} + +func (swc SwapCheck) Check() (warnings, errors []error) { + f, err := os.Open("/proc/swaps") + if err != nil { + // /proc/swaps not available, thus no reasons to warn + return nil, nil + } + defer f.Close() + var buf []string + scanner := bufio.NewScanner(f) + for scanner.Scan() { + buf = append(buf, scanner.Text()) + } + if err := scanner.Err(); err != nil { + return nil, []error{fmt.Errorf("error parsing /proc/swaps: %v", err)} + } + + if len(buf) > 1 { + return []error{fmt.Errorf("Running with swap on is not supported. Please disable swap or set kubelet's --fail-swap-on flag to false.")}, nil + } + + return nil, nil +} + type etcdVersionResponse struct { Etcdserver string `json:"etcdserver"` Etcdcluster string `json:"etcdcluster"` @@ -586,6 +612,7 @@ func RunInitMasterChecks(cfg *kubeadmapi.MasterConfiguration) error { DirAvailableCheck{Path: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)}, DirAvailableCheck{Path: "/var/lib/kubelet"}, FileContentCheck{Path: bridgenf, Content: []byte{'1'}}, + SwapCheck{}, InPathCheck{executable: "ip", mandatory: true}, InPathCheck{executable: "iptables", mandatory: true}, InPathCheck{executable: "mount", mandatory: true}, @@ -646,6 +673,7 @@ func RunJoinNodeChecks(cfg *kubeadmapi.NodeConfiguration) error { FileAvailableCheck{Path: cfg.CACertPath}, FileAvailableCheck{Path: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)}, FileContentCheck{Path: bridgenf, Content: []byte{'1'}}, + SwapCheck{}, InPathCheck{executable: "ip", mandatory: true}, InPathCheck{executable: "iptables", mandatory: true}, InPathCheck{executable: "mount", mandatory: true},