github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/util/resource.go (about) 1 /* 2 Copyright 2022 The Katalyst Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "fmt" 21 22 "github.com/kubewharf/katalyst-core/pkg/consts" 23 24 v1 "k8s.io/api/core/v1" 25 pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1" 26 ) 27 28 func IsRequestFullCPU(pod *v1.Pod) bool { 29 for _, initContainer := range pod.Spec.InitContainers { 30 for resName, resInfo := range initContainer.Resources.Requests { 31 if resName == v1.ResourceCPU { 32 if resInfo.MilliValue()%1000 != 0 { 33 return false 34 } 35 } 36 } 37 } 38 for _, container := range pod.Spec.Containers { 39 for resName, resInfo := range container.Resources.Requests { 40 if resName == v1.ResourceCPU { 41 if resInfo.MilliValue()%1000 != 0 { 42 return false 43 } 44 } 45 } 46 } 47 return true 48 } 49 50 func GetPodEffectiveRequest(pod *v1.Pod) v1.ResourceList { 51 resources := make(v1.ResourceList) 52 53 for _, container := range pod.Spec.Containers { 54 for name, quantity := range container.Resources.Requests { 55 if q, ok := resources[name]; ok { 56 quantity.Add(q) 57 } 58 resources[name] = quantity 59 } 60 } 61 for _, container := range pod.Spec.InitContainers { 62 for name, quantity := range container.Resources.Requests { 63 if q, ok := resources[name]; ok && quantity.Cmp(q) <= 0 { 64 continue 65 } 66 resources[name] = quantity 67 } 68 } 69 70 return resources 71 } 72 73 func GetContainerTypeAndIndex(pod *v1.Pod, container *v1.Container) (containerType pluginapi.ContainerType, containerIndex uint64, err error) { 74 if pod == nil || container == nil { 75 err = fmt.Errorf("got nil pod: %v or container: %v", pod, container) 76 return 77 } 78 79 foundContainer := false 80 81 for i, initContainer := range pod.Spec.InitContainers { 82 if container.Name == initContainer.Name { 83 foundContainer = true 84 containerType = pluginapi.ContainerType_INIT 85 containerIndex = uint64(i) 86 break 87 } 88 } 89 90 if !foundContainer { 91 mainContainerName := pod.Annotations[consts.MainContainerNameAnnotationKey] 92 93 if mainContainerName == "" && len(pod.Spec.Containers) > 0 { 94 mainContainerName = pod.Spec.Containers[0].Name 95 } 96 97 for i, appContainer := range pod.Spec.Containers { 98 if container.Name == appContainer.Name { 99 foundContainer = true 100 101 if container.Name == mainContainerName { 102 containerType = pluginapi.ContainerType_MAIN 103 } else { 104 containerType = pluginapi.ContainerType_SIDECAR 105 } 106 107 containerIndex = uint64(i) 108 break 109 } 110 } 111 } 112 113 if !foundContainer { 114 err = fmt.Errorf("GetContainerTypeAndIndex doesn't find container: %s in pod: %s/%s", container.Name, pod.Namespace, pod.Name) 115 } 116 117 return 118 }