From c6975a77508fef3b03dcaf9f46e5118941a86724 Mon Sep 17 00:00:00 2001 From: Xianglin Gao Date: Mon, 20 Jul 2020 23:59:57 +0800 Subject: [PATCH] add test case for kubeadm memory check Signed-off-by: Xianglin Gao --- cmd/kubeadm/app/constants/constants.go | 3 ++- cmd/kubeadm/app/preflight/BUILD | 1 - cmd/kubeadm/app/preflight/checks.go | 12 ++------- cmd/kubeadm/app/preflight/checks_darwin.go | 6 +++++ cmd/kubeadm/app/preflight/checks_linux.go | 18 ++++++++++++++ cmd/kubeadm/app/preflight/checks_test.go | 27 +++++++++++++++++++++ cmd/kubeadm/app/preflight/checks_windows.go | 6 +++++ go.mod | 1 - go.sum | 2 -- 9 files changed, 61 insertions(+), 15 deletions(-) diff --git a/cmd/kubeadm/app/constants/constants.go b/cmd/kubeadm/app/constants/constants.go index 95d31059cab..5a9b43a7838 100644 --- a/cmd/kubeadm/app/constants/constants.go +++ b/cmd/kubeadm/app/constants/constants.go @@ -351,7 +351,8 @@ const ( ControlPlaneNumCPU = 2 // ControlPlaneMem is the number of megabytes of memory required on the control-plane - ControlPlaneMem = 2 * 1024 + // Below that amount of RAM running a stable control plane would be difficult. + ControlPlaneMem = 1700 // KubeadmCertsSecret specifies in what Secret in the kube-system namespace the certificates should be stored KubeadmCertsSecret = "kubeadm-certs" diff --git a/cmd/kubeadm/app/preflight/BUILD b/cmd/kubeadm/app/preflight/BUILD index 69f951f4b98..49ec4e30f89 100644 --- a/cmd/kubeadm/app/preflight/BUILD +++ b/cmd/kubeadm/app/preflight/BUILD @@ -28,7 +28,6 @@ go_library( "//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library", "//staging/src/k8s.io/component-base/version:go_default_library", "//vendor/github.com/PuerkitoBio/purell:go_default_library", - "//vendor/github.com/pbnjay/memory:go_default_library", "//vendor/github.com/pkg/errors:go_default_library", "//vendor/k8s.io/klog/v2:go_default_library", "//vendor/k8s.io/system-validators/validators:go_default_library", diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 0549d1240b9..cd656189792 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -35,7 +35,6 @@ import ( "time" "github.com/PuerkitoBio/purell" - "github.com/pbnjay/memory" "github.com/pkg/errors" netutil "k8s.io/apimachinery/pkg/util/net" "k8s.io/apimachinery/pkg/util/sets" @@ -880,15 +879,6 @@ func (MemCheck) Name() string { return "Mem" } -// Check number of memory required by kubeadm -func (mc MemCheck) Check() (warnings, errorList []error) { - actual := memory.TotalMemory() / 1024 / 1024 // TotalMemory returns bytes; convert to MB - if actual < mc.Mem { - errorList = append(errorList, errors.Errorf("the system RAM (%d MB) is less than the minimum %d MB", actual, mc.Mem)) - } - return warnings, errorList -} - // RunInitNodeChecks executes all individual, applicable to control-plane node checks. // The boolean flag 'isSecondaryControlPlane' controls whether we are running checks in a --join-control-plane scenario. // The boolean flag 'downloadCerts' controls whether we should skip checks on certificates because we are downloading them. @@ -904,6 +894,8 @@ func RunInitNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfigura manifestsDir := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName) checks := []Checker{ NumCPUCheck{NumCPU: kubeadmconstants.ControlPlaneNumCPU}, + // Linux only + // TODO: support other OS, if control-plane is supported on it. MemCheck{Mem: kubeadmconstants.ControlPlaneMem}, KubernetesVersionCheck{KubernetesVersion: cfg.KubernetesVersion, KubeadmVersion: kubeadmversion.Get().GitVersion}, FirewalldCheck{ports: []int{int(cfg.LocalAPIEndpoint.BindPort), kubeadmconstants.KubeletPort}}, diff --git a/cmd/kubeadm/app/preflight/checks_darwin.go b/cmd/kubeadm/app/preflight/checks_darwin.go index d4cb02628a4..b6508b5ec44 100644 --- a/cmd/kubeadm/app/preflight/checks_darwin.go +++ b/cmd/kubeadm/app/preflight/checks_darwin.go @@ -25,3 +25,9 @@ package preflight func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) { return nil, nil } + +// Check number of memory required by kubeadm +// No-op for Darwin (MacOS). +func (mc MemCheck) Check() (warnings, errorList []error) { + return nil, nil +} diff --git a/cmd/kubeadm/app/preflight/checks_linux.go b/cmd/kubeadm/app/preflight/checks_linux.go index c42363bbbd2..c89d6890136 100644 --- a/cmd/kubeadm/app/preflight/checks_linux.go +++ b/cmd/kubeadm/app/preflight/checks_linux.go @@ -19,6 +19,8 @@ limitations under the License. package preflight import ( + "syscall" + "github.com/pkg/errors" kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util" "k8s.io/utils/exec" @@ -40,3 +42,19 @@ func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) { } return nil, nil } + +// Check number of memory required by kubeadm +func (mc MemCheck) Check() (warnings, errorList []error) { + info := syscall.Sysinfo_t{} + err := syscall.Sysinfo(&info) + if err != nil { + errorList = append(errorList, errors.Wrapf(err, "failed to get system info")) + } + + // Totalram returns bytes; convert to MB + actual := uint64(info.Totalram) / 1024 / 1024 + if actual < mc.Mem { + errorList = append(errorList, errors.Errorf("the system RAM (%d MB) is less than the minimum %d MB", actual, mc.Mem)) + } + return warnings, errorList +} diff --git a/cmd/kubeadm/app/preflight/checks_test.go b/cmd/kubeadm/app/preflight/checks_test.go index 3a6b2f27ab9..aa4f13a7f40 100644 --- a/cmd/kubeadm/app/preflight/checks_test.go +++ b/cmd/kubeadm/app/preflight/checks_test.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "io/ioutil" + "runtime" "strings" "testing" @@ -849,3 +850,29 @@ func TestNumCPUCheck(t *testing.T) { }) } } + +func TestMemCheck(t *testing.T) { + // skip this test, if OS in not Linux, since it will ONLY pass on Linux. + if runtime.GOOS != "linux" { + t.Skip("unsupported OS for memory check test ") + } + + var tests = []struct { + minimum uint64 + expectedErrors int + }{ + {0, 0}, + {9999999999999999, 1}, + } + + for _, rt := range tests { + t.Run(fmt.Sprintf("MemoryCheck{%d}", rt.minimum), func(t *testing.T) { + warnings, errors := MemCheck{Mem: rt.minimum}.Check() + if len(warnings) > 0 { + t.Errorf("expected 0 warnings but got %d: %q", len(warnings), warnings) + } else if len(errors) != rt.expectedErrors { + t.Errorf("expected %d error(s) but got %d: %q", rt.expectedErrors, len(errors), errors) + } + }) + } +} diff --git a/cmd/kubeadm/app/preflight/checks_windows.go b/cmd/kubeadm/app/preflight/checks_windows.go index 6f28081991a..57e5e3b8e05 100644 --- a/cmd/kubeadm/app/preflight/checks_windows.go +++ b/cmd/kubeadm/app/preflight/checks_windows.go @@ -54,3 +54,9 @@ func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) { func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) { return nil, nil } + +// Check number of memory required by kubeadm +// No-op for Windows. +func (mc MemCheck) Check() (warnings, errorList []error) { + return nil, nil +} diff --git a/go.mod b/go.mod index c5381386f88..64ac5d4d164 100644 --- a/go.mod +++ b/go.mod @@ -79,7 +79,6 @@ require ( github.com/opencontainers/go-digest v1.0.0-rc1 github.com/opencontainers/runc v1.0.0-rc91.0.20200707015106-819fcc687efb github.com/opencontainers/selinux v1.5.2 - github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4 github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 github.com/prometheus/client_golang v1.7.1 diff --git a/go.sum b/go.sum index f944d322c3e..f49229589a7 100644 --- a/go.sum +++ b/go.sum @@ -376,8 +376,6 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20200520003142-237cc4f519e2 h1:9 github.com/opencontainers/runtime-spec v1.0.3-0.20200520003142-237cc4f519e2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/selinux v1.5.2 h1:F6DgIsjgBIcDksLW4D5RG9bXok6oqZ3nvMwj4ZoFu/Q= github.com/opencontainers/selinux v1.5.2/go.mod h1:yTcKuYAh6R95iDpefGLQaPaRwJFwyzAJufJyiTt7s0g= -github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4 h1:MfIUBZ1bz7TgvQLVa/yPJZOGeKEgs6eTKUjz3zB4B+U= -github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4/go.mod h1:RMU2gJXhratVxBDTFeOdNhd540tG57lt9FIUV0YLvIQ= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=