Skip to content

Commit

Permalink
kubeadm: check required number of CPUs on master
Browse files Browse the repository at this point in the history
Implemented preflight check to ensure that number of CPUs
on the master node is not less than required.
  • Loading branch information
bart0sh committed Oct 20, 2018
1 parent 2dc9acc commit d230b24
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/kubeadm/app/constants/constants.go
Expand Up @@ -323,6 +323,9 @@ const (

// DefaultAPIServerBindAddress is the default bind address for the API Server
DefaultAPIServerBindAddress = "0.0.0.0"

// MasterNumCPU is the number of CPUs required on master
MasterNumCPU = 2
)

var (
Expand Down
20 changes: 20 additions & 0 deletions cmd/kubeadm/app/preflight/checks.go
Expand Up @@ -845,6 +845,25 @@ func (ipc ImagePullCheck) Check() (warnings, errors []error) {
return warnings, errors
}

// NumCPUCheck checks if current number of CPUs is not less than required
type NumCPUCheck struct {
NumCPU int
}

// Name returns the label for NumCPUCheck
func (NumCPUCheck) Name() string {
return "NumCPU"
}

// Check number of CPUs required by kubeadm
func (ncc NumCPUCheck) Check() (warnings, errors []error) {
numCPU := runtime.NumCPU()
if numCPU < ncc.NumCPU {
errors = append(errors, fmt.Errorf("the number of available CPUs %d is less than the required %d", numCPU, ncc.NumCPU))
}
return warnings, errors
}

// RunInitMasterChecks executes all individual, applicable to Master node checks.
func RunInitMasterChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfiguration, ignorePreflightErrors sets.String) error {
// First, check if we're root separately from the other preflight checks and fail fast
Expand All @@ -854,6 +873,7 @@ func RunInitMasterChecks(execer utilsexec.Interface, cfg *kubeadmapi.InitConfigu

manifestsDir := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName)
checks := []Checker{
NumCPUCheck{NumCPU: kubeadmconstants.MasterNumCPU},
KubernetesVersionCheck{KubernetesVersion: cfg.KubernetesVersion, KubeadmVersion: kubeadmversion.Get().GitVersion},
FirewalldCheck{ports: []int{int(cfg.APIEndpoint.BindPort), 10250}},
PortOpenCheck{port: int(cfg.APIEndpoint.BindPort)},
Expand Down
21 changes: 21 additions & 0 deletions cmd/kubeadm/app/preflight/checks_test.go
Expand Up @@ -777,3 +777,24 @@ func TestImagePullCheck(t *testing.T) {
t.Fatalf("expected 2 errors but got %d: %q", len(errors), errors)
}
}

func TestNumCPUCheck(t *testing.T) {
var tests = []struct {
numCPU int
numErrors int
numWarnings int
}{
{0, 0, 0},
{999999999, 1, 0},
}

for _, rt := range tests {
warnings, errors := NumCPUCheck{NumCPU: rt.numCPU}.Check()
if len(warnings) != rt.numWarnings {
t.Errorf("expected %d warning(s) but got %d: %q", rt.numWarnings, len(warnings), warnings)
}
if len(errors) != rt.numErrors {
t.Errorf("expected %d warning(s) but got %d: %q", rt.numErrors, len(errors), errors)
}
}
}

0 comments on commit d230b24

Please sign in to comment.