Skip to content

Commit c6975a7

Browse files
author
Xianglin Gao
committed
add test case for kubeadm memory check
Signed-off-by: Xianglin Gao <[email protected]>
1 parent e5bb66f commit c6975a7

File tree

9 files changed

+61
-15
lines changed

9 files changed

+61
-15
lines changed

cmd/kubeadm/app/constants/constants.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ const (
351351
ControlPlaneNumCPU = 2
352352

353353
// ControlPlaneMem is the number of megabytes of memory required on the control-plane
354-
ControlPlaneMem = 2 * 1024
354+
// Below that amount of RAM running a stable control plane would be difficult.
355+
ControlPlaneMem = 1700
355356

356357
// KubeadmCertsSecret specifies in what Secret in the kube-system namespace the certificates should be stored
357358
KubeadmCertsSecret = "kubeadm-certs"

cmd/kubeadm/app/preflight/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ go_library(
2828
"//staging/src/k8s.io/apimachinery/pkg/util/version:go_default_library",
2929
"//staging/src/k8s.io/component-base/version:go_default_library",
3030
"//vendor/github.com/PuerkitoBio/purell:go_default_library",
31-
"//vendor/github.com/pbnjay/memory:go_default_library",
3231
"//vendor/github.com/pkg/errors:go_default_library",
3332
"//vendor/k8s.io/klog/v2:go_default_library",
3433
"//vendor/k8s.io/system-validators/validators:go_default_library",

cmd/kubeadm/app/preflight/checks.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"time"
3636

3737
"github.com/PuerkitoBio/purell"
38-
"github.com/pbnjay/memory"
3938
"github.com/pkg/errors"
4039
netutil "k8s.io/apimachinery/pkg/util/net"
4140
"k8s.io/apimachinery/pkg/util/sets"
@@ -880,15 +879,6 @@ func (MemCheck) Name() string {
880879
return "Mem"
881880
}
882881

883-
// Check number of memory required by kubeadm
884-
func (mc MemCheck) Check() (warnings, errorList []error) {
885-
actual := memory.TotalMemory() / 1024 / 1024 // TotalMemory returns bytes; convert to MB
886-
if actual < mc.Mem {
887-
errorList = append(errorList, errors.Errorf("the system RAM (%d MB) is less than the minimum %d MB", actual, mc.Mem))
888-
}
889-
return warnings, errorList
890-
}
891-
892882
// RunInitNodeChecks executes all individual, applicable to control-plane node checks.
893883
// The boolean flag 'isSecondaryControlPlane' controls whether we are running checks in a --join-control-plane scenario.
894884
// 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
904894
manifestsDir := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)
905895
checks := []Checker{
906896
NumCPUCheck{NumCPU: kubeadmconstants.ControlPlaneNumCPU},
897+
// Linux only
898+
// TODO: support other OS, if control-plane is supported on it.
907899
MemCheck{Mem: kubeadmconstants.ControlPlaneMem},
908900
KubernetesVersionCheck{KubernetesVersion: cfg.KubernetesVersion, KubeadmVersion: kubeadmversion.Get().GitVersion},
909901
FirewalldCheck{ports: []int{int(cfg.LocalAPIEndpoint.BindPort), kubeadmconstants.KubeletPort}},

cmd/kubeadm/app/preflight/checks_darwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ package preflight
2525
func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) {
2626
return nil, nil
2727
}
28+
29+
// Check number of memory required by kubeadm
30+
// No-op for Darwin (MacOS).
31+
func (mc MemCheck) Check() (warnings, errorList []error) {
32+
return nil, nil
33+
}

cmd/kubeadm/app/preflight/checks_linux.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ limitations under the License.
1919
package preflight
2020

2121
import (
22+
"syscall"
23+
2224
"github.com/pkg/errors"
2325
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
2426
"k8s.io/utils/exec"
@@ -40,3 +42,19 @@ func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) {
4042
}
4143
return nil, nil
4244
}
45+
46+
// Check number of memory required by kubeadm
47+
func (mc MemCheck) Check() (warnings, errorList []error) {
48+
info := syscall.Sysinfo_t{}
49+
err := syscall.Sysinfo(&info)
50+
if err != nil {
51+
errorList = append(errorList, errors.Wrapf(err, "failed to get system info"))
52+
}
53+
54+
// Totalram returns bytes; convert to MB
55+
actual := uint64(info.Totalram) / 1024 / 1024
56+
if actual < mc.Mem {
57+
errorList = append(errorList, errors.Errorf("the system RAM (%d MB) is less than the minimum %d MB", actual, mc.Mem))
58+
}
59+
return warnings, errorList
60+
}

cmd/kubeadm/app/preflight/checks_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"io/ioutil"
23+
"runtime"
2324
"strings"
2425
"testing"
2526

@@ -849,3 +850,29 @@ func TestNumCPUCheck(t *testing.T) {
849850
})
850851
}
851852
}
853+
854+
func TestMemCheck(t *testing.T) {
855+
// skip this test, if OS in not Linux, since it will ONLY pass on Linux.
856+
if runtime.GOOS != "linux" {
857+
t.Skip("unsupported OS for memory check test ")
858+
}
859+
860+
var tests = []struct {
861+
minimum uint64
862+
expectedErrors int
863+
}{
864+
{0, 0},
865+
{9999999999999999, 1},
866+
}
867+
868+
for _, rt := range tests {
869+
t.Run(fmt.Sprintf("MemoryCheck{%d}", rt.minimum), func(t *testing.T) {
870+
warnings, errors := MemCheck{Mem: rt.minimum}.Check()
871+
if len(warnings) > 0 {
872+
t.Errorf("expected 0 warnings but got %d: %q", len(warnings), warnings)
873+
} else if len(errors) != rt.expectedErrors {
874+
t.Errorf("expected %d error(s) but got %d: %q", rt.expectedErrors, len(errors), errors)
875+
}
876+
})
877+
}
878+
}

cmd/kubeadm/app/preflight/checks_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ func (ipuc IsPrivilegedUserCheck) Check() (warnings, errorList []error) {
5454
func (idsc IsDockerSystemdCheck) Check() (warnings, errorList []error) {
5555
return nil, nil
5656
}
57+
58+
// Check number of memory required by kubeadm
59+
// No-op for Windows.
60+
func (mc MemCheck) Check() (warnings, errorList []error) {
61+
return nil, nil
62+
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ require (
7979
github.com/opencontainers/go-digest v1.0.0-rc1
8080
github.com/opencontainers/runc v1.0.0-rc91.0.20200707015106-819fcc687efb
8181
github.com/opencontainers/selinux v1.5.2
82-
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4
8382
github.com/pkg/errors v0.9.1
8483
github.com/pmezard/go-difflib v1.0.0
8584
github.com/prometheus/client_golang v1.7.1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ github.com/opencontainers/runtime-spec v1.0.3-0.20200520003142-237cc4f519e2 h1:9
376376
github.com/opencontainers/runtime-spec v1.0.3-0.20200520003142-237cc4f519e2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
377377
github.com/opencontainers/selinux v1.5.2 h1:F6DgIsjgBIcDksLW4D5RG9bXok6oqZ3nvMwj4ZoFu/Q=
378378
github.com/opencontainers/selinux v1.5.2/go.mod h1:yTcKuYAh6R95iDpefGLQaPaRwJFwyzAJufJyiTt7s0g=
379-
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4 h1:MfIUBZ1bz7TgvQLVa/yPJZOGeKEgs6eTKUjz3zB4B+U=
380-
github.com/pbnjay/memory v0.0.0-20190104145345-974d429e7ae4/go.mod h1:RMU2gJXhratVxBDTFeOdNhd540tG57lt9FIUV0YLvIQ=
381379
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
382380
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
383381
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=

0 commit comments

Comments
 (0)