Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubeadm: check required number of CPUs on master #70048

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/kubeadm/app/constants/constants.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we gravitate towards using subtests with t.Run for new code, but not critical here.

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