Skip to content

Commit e5bb66f

Browse files
author
Xianglin Gao
committed
Add a preflight check that the control-plane node has at least 2GB RAM
Signed-off-by: Xianglin Gao <[email protected]>
1 parent 43fbe17 commit e5bb66f

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

cmd/kubeadm/app/constants/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ const (
350350
// ControlPlaneNumCPU is the number of CPUs required on control-plane
351351
ControlPlaneNumCPU = 2
352352

353+
// ControlPlaneMem is the number of megabytes of memory required on the control-plane
354+
ControlPlaneMem = 2 * 1024
355+
353356
// KubeadmCertsSecret specifies in what Secret in the kube-system namespace the certificates should be stored
354357
KubeadmCertsSecret = "kubeadm-certs"
355358

cmd/kubeadm/app/preflight/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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",
3132
"//vendor/github.com/pkg/errors:go_default_library",
3233
"//vendor/k8s.io/klog/v2:go_default_library",
3334
"//vendor/k8s.io/system-validators/validators:go_default_library",

cmd/kubeadm/app/preflight/checks.go

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

3737
"github.com/PuerkitoBio/purell"
38+
"github.com/pbnjay/memory"
3839
"github.com/pkg/errors"
3940
netutil "k8s.io/apimachinery/pkg/util/net"
4041
"k8s.io/apimachinery/pkg/util/sets"
@@ -869,6 +870,25 @@ func (ncc NumCPUCheck) Check() (warnings, errorList []error) {
869870
return warnings, errorList
870871
}
871872

873+
// MemCheck checks if the number of megabytes of memory is not less than required
874+
type MemCheck struct {
875+
Mem uint64
876+
}
877+
878+
// Name returns the label for memory
879+
func (MemCheck) Name() string {
880+
return "Mem"
881+
}
882+
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+
872892
// RunInitNodeChecks executes all individual, applicable to control-plane node checks.
873893
// The boolean flag 'isSecondaryControlPlane' controls whether we are running checks in a --join-control-plane scenario.
874894
// The boolean flag 'downloadCerts' controls whether we should skip checks on certificates because we are downloading them.
@@ -884,6 +904,7 @@ func RunInitNodeChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfigura
884904
manifestsDir := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)
885905
checks := []Checker{
886906
NumCPUCheck{NumCPU: kubeadmconstants.ControlPlaneNumCPU},
907+
MemCheck{Mem: kubeadmconstants.ControlPlaneMem},
887908
KubernetesVersionCheck{KubernetesVersion: cfg.KubernetesVersion, KubeadmVersion: kubeadmversion.Get().GitVersion},
888909
FirewalldCheck{ports: []int{int(cfg.LocalAPIEndpoint.BindPort), kubeadmconstants.KubeletPort}},
889910
PortOpenCheck{port: int(cfg.LocalAPIEndpoint.BindPort)},

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ 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
8283
github.com/pkg/errors v0.9.1
8384
github.com/pmezard/go-difflib v1.0.0
8485
github.com/prometheus/client_golang v1.7.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ 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=
379381
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
380382
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
381383
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=

0 commit comments

Comments
 (0)