k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubelet/app/init_windows.go (about) 1 //go:build windows 2 // +build windows 3 4 /* 5 Copyright 2018 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package app 21 22 import ( 23 "fmt" 24 "unsafe" 25 26 "golang.org/x/sys/windows" 27 "k8s.io/klog/v2" 28 29 "k8s.io/kubernetes/pkg/windows/service" 30 ) 31 32 const ( 33 serviceName = "kubelet" 34 ) 35 36 // getPriorityValue returns the value associated with a Windows process priorityClass 37 // Ref: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/setpriority-method-in-class-win32-process 38 func getPriorityValue(priorityClassName string) uint32 { 39 var priorityClassMap = map[string]uint32{ 40 "IDLE_PRIORITY_CLASS": uint32(windows.IDLE_PRIORITY_CLASS), 41 "BELOW_NORMAL_PRIORITY_CLASS": uint32(windows.BELOW_NORMAL_PRIORITY_CLASS), 42 "NORMAL_PRIORITY_CLASS": uint32(windows.NORMAL_PRIORITY_CLASS), 43 "ABOVE_NORMAL_PRIORITY_CLASS": uint32(windows.ABOVE_NORMAL_PRIORITY_CLASS), 44 "HIGH_PRIORITY_CLASS": uint32(windows.HIGH_PRIORITY_CLASS), 45 "REALTIME_PRIORITY_CLASS": uint32(windows.REALTIME_PRIORITY_CLASS), 46 } 47 return priorityClassMap[priorityClassName] 48 } 49 50 // createWindowsJobObject creates a new Job Object 51 // (https://docs.microsoft.com/en-us/windows/win32/procthread/job-objects), 52 // and specifies the priority class for the job object to the specified value. 53 // A job object is used here so that any spawned processes such as powershell or 54 // wmic are created at the specified thread priority class. 55 // Running kubelet with above normal / high priority can help improve 56 // responsiveness on machines with high CPU utilization. 57 func createWindowsJobObject(pc uint32) (windows.Handle, error) { 58 job, err := windows.CreateJobObject(nil, nil) 59 if err != nil { 60 return windows.InvalidHandle, fmt.Errorf("windows.CreateJobObject failed: %w", err) 61 } 62 limitInfo := windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{ 63 LimitFlags: windows.JOB_OBJECT_LIMIT_PRIORITY_CLASS, 64 PriorityClass: pc, 65 } 66 if _, err := windows.SetInformationJobObject( 67 job, 68 windows.JobObjectBasicLimitInformation, 69 uintptr(unsafe.Pointer(&limitInfo)), 70 uint32(unsafe.Sizeof(limitInfo))); err != nil { 71 return windows.InvalidHandle, fmt.Errorf("windows.SetInformationJobObject failed: %w", err) 72 } 73 return job, nil 74 } 75 76 func initForOS(windowsService bool, windowsPriorityClass string) error { 77 priority := getPriorityValue(windowsPriorityClass) 78 if priority == 0 { 79 return fmt.Errorf("unknown priority class %s, valid ones are available at "+ 80 "https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities", windowsPriorityClass) 81 } 82 klog.InfoS("Creating a Windows job object and adding kubelet process to it", "windowsPriorityClass", windowsPriorityClass) 83 job, err := createWindowsJobObject(priority) 84 if err != nil { 85 return err 86 } 87 if err := windows.AssignProcessToJobObject(job, windows.CurrentProcess()); err != nil { 88 return fmt.Errorf("windows.AssignProcessToJobObject failed: %w", err) 89 } 90 91 if windowsService { 92 return service.InitService(serviceName) 93 } 94 return nil 95 }