mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
[kubelet] Allow priority to be set for kubelet process on Windows
This commit is contained in:
parent
ad6a2af7d8
commit
f4c2dcd030
@ -18,6 +18,6 @@ limitations under the License.
|
|||||||
|
|
||||||
package app
|
package app
|
||||||
|
|
||||||
func initForOS(service bool) error {
|
func initForOS(service bool, priorityClass string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,10 @@ limitations under the License.
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
"k8s.io/kubernetes/pkg/windows/service"
|
"k8s.io/kubernetes/pkg/windows/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,7 +30,33 @@ const (
|
|||||||
serviceName = "kubelet"
|
serviceName = "kubelet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initForOS(windowsService bool) error {
|
// getPriorityValue returns the value associated with a Windows process priorityClass
|
||||||
|
// Ref: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/setpriority-method-in-class-win32-process
|
||||||
|
func getPriorityValue(priorityClassName string) uint32 {
|
||||||
|
var priorityClassMap = map[string]uint32{
|
||||||
|
"IDLE_PRIORITY_CLASS": uint32(64),
|
||||||
|
"BELOW_NORMAL_PRIORITY_CLASS": uint32(16384),
|
||||||
|
"NORMAL_PRIORITY_CLASS": uint32(32),
|
||||||
|
"ABOVE_NORMAL_PRIORITY_CLASS": uint32(32768),
|
||||||
|
"HIGH_PRIORITY_CLASS": uint32(128),
|
||||||
|
"REALTIME_PRIORITY_CLASS": uint32(256),
|
||||||
|
}
|
||||||
|
return priorityClassMap[priorityClassName]
|
||||||
|
}
|
||||||
|
|
||||||
|
func initForOS(windowsService bool, windowsPriorityClass string) error {
|
||||||
|
priority := getPriorityValue(windowsPriorityClass)
|
||||||
|
if priority == 0 {
|
||||||
|
return fmt.Errorf("unknown priority class %s, valid ones are available at "+
|
||||||
|
"https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities", windowsPriorityClass)
|
||||||
|
}
|
||||||
|
kubeletProcessHandle := windows.CurrentProcess()
|
||||||
|
// Set the priority of the kubelet process to given priority
|
||||||
|
klog.Infof("Setting the priority of kubelet process to %s", windowsPriorityClass)
|
||||||
|
if err := windows.SetPriorityClass(kubeletProcessHandle, priority); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if windowsService {
|
if windowsService {
|
||||||
return service.InitService(serviceName)
|
return service.InitService(serviceName)
|
||||||
}
|
}
|
||||||
|
44
cmd/kubelet/app/init_windows_test.go
Normal file
44
cmd/kubelet/app/init_windows_test.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestIsValidPriorityClass(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
description string
|
||||||
|
priorityClassName string
|
||||||
|
expectedPriorityValue uint32
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
description: "Invalid Priority Class",
|
||||||
|
priorityClassName: "myPriorityClass",
|
||||||
|
expectedPriorityValue: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "Valid Priority Class",
|
||||||
|
priorityClassName: "IDLE_PRIORITY_CLASS",
|
||||||
|
expectedPriorityValue: uint32(64),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range testCases {
|
||||||
|
actualPriorityValue := getPriorityValue(test.priorityClassName)
|
||||||
|
if test.expectedPriorityValue != actualPriorityValue {
|
||||||
|
t.Fatalf("unexpected error for %s", test.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -111,6 +111,13 @@ type KubeletFlags struct {
|
|||||||
// Its corresponding flag only gets registered in Windows builds.
|
// Its corresponding flag only gets registered in Windows builds.
|
||||||
WindowsService bool
|
WindowsService bool
|
||||||
|
|
||||||
|
// WindowsPriorityClass sets the priority class associated with the Kubelet process
|
||||||
|
// Its corresponding flag only gets registered in Windows builds
|
||||||
|
// The default priority class associated with any process in Windows is NORMAL_PRIORITY_CLASS. Keeping it as is
|
||||||
|
// to maintain backwards compatibility.
|
||||||
|
// Source: https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
|
||||||
|
WindowsPriorityClass string
|
||||||
|
|
||||||
// remoteRuntimeEndpoint is the endpoint of remote runtime service
|
// remoteRuntimeEndpoint is the endpoint of remote runtime service
|
||||||
RemoteRuntimeEndpoint string
|
RemoteRuntimeEndpoint string
|
||||||
// remoteImageEndpoint is the endpoint of remote image service
|
// remoteImageEndpoint is the endpoint of remote image service
|
||||||
|
@ -24,4 +24,10 @@ import (
|
|||||||
|
|
||||||
func (f *KubeletFlags) addOSFlags(fs *pflag.FlagSet) {
|
func (f *KubeletFlags) addOSFlags(fs *pflag.FlagSet) {
|
||||||
fs.BoolVar(&f.WindowsService, "windows-service", f.WindowsService, "Enable Windows Service Control Manager API integration")
|
fs.BoolVar(&f.WindowsService, "windows-service", f.WindowsService, "Enable Windows Service Control Manager API integration")
|
||||||
|
// The default priority class associated with any process in Windows is NORMAL_PRIORITY_CLASS. Keeping it as is
|
||||||
|
// to maintain backwards compatibility.
|
||||||
|
// Source: https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
|
||||||
|
fs.StringVar(&f.WindowsPriorityClass, "windows-priorityclass", "NORMAL_PRIORITY_CLASS",
|
||||||
|
"Set the PriorityClass associated with kubelet process, the default ones are available at "+
|
||||||
|
"https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities")
|
||||||
}
|
}
|
||||||
|
@ -414,7 +414,7 @@ func Run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend
|
|||||||
logOption.Apply()
|
logOption.Apply()
|
||||||
// To help debugging, immediately log version
|
// To help debugging, immediately log version
|
||||||
klog.Infof("Version: %+v", version.Get())
|
klog.Infof("Version: %+v", version.Get())
|
||||||
if err := initForOS(s.KubeletFlags.WindowsService); err != nil {
|
if err := initForOS(s.KubeletFlags.WindowsService, s.KubeletFlags.WindowsPriorityClass); err != nil {
|
||||||
return fmt.Errorf("failed OS init: %v", err)
|
return fmt.Errorf("failed OS init: %v", err)
|
||||||
}
|
}
|
||||||
if err := run(ctx, s, kubeDeps, featureGate); err != nil {
|
if err := run(ctx, s, kubeDeps, featureGate); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user