add test case for kubeadm memory check

Signed-off-by: Xianglin Gao <xianglin.gxl@alibaba-inc.com>
This commit is contained in:
Xianglin Gao 2020-07-20 23:59:57 +08:00
parent e5bb66f899
commit c6975a7750
9 changed files with 61 additions and 15 deletions

View File

@ -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"

View File

@ -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",

View File

@ -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}},

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}
})
}
}

View File

@ -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
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=