-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathutil.go
More file actions
167 lines (138 loc) · 5.69 KB
/
util.go
File metadata and controls
167 lines (138 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Copyright 2022 The Katalyst 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 commonstate
import (
"encoding/json"
"io/ioutil"
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"
"github.com/kubewharf/katalyst-api/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/qos"
)
// CheckNUMABindingAntiAffinity returns true
// if the AllocationMeta isn't compatible for the annotations of a numa binding candidate
func CheckNUMABindingAntiAffinity(meta *AllocationMeta, annotations map[string]string) bool {
if meta == nil {
return false
} else if len(annotations) == 0 {
return false
}
if !meta.CheckNUMANotShare() &&
!qos.AnnotationsIndicateNUMANotShare(annotations) {
return false
}
if meta.GetQoSLevel() != annotations[consts.PodAnnotationQoSLevelKey] {
return true
}
if meta.CheckSharedNUMABinding() {
// considering isolation, use specified pool instead of actual pool name here
candidateSpecifiedPoolName := GetSpecifiedPoolName(consts.PodAnnotationQoSLevelSharedCores,
annotations[consts.PodAnnotationCPUEnhancementCPUSet])
aiSpecifiedPoolName := meta.GetSpecifiedPoolName()
// shared_cores with numa binding doesn't support two share type pools with same specified name existing at same NUMA
if candidateSpecifiedPoolName != aiSpecifiedPoolName {
return true
}
}
return false
}
func CheckNUMABindingWithAffinity(meta *AllocationMeta, annotations map[string]string) bool {
if meta == nil {
return false
} else if len(annotations) == 0 {
return false
}
// if qos level is same as candidate, filter out this numa
if meta.GetQoSLevel() != annotations[consts.PodAnnotationQoSLevelKey] {
return false
}
if meta.CheckNUMANotShare() ||
qos.AnnotationsIndicateNUMANotShare(annotations) {
return false
}
if meta.CheckNUMABinding() {
// considering isolation, use specified pool instead of actual pool name here
candidateSpecifiedPoolName := GetSpecifiedPoolName(consts.PodAnnotationQoSLevelSharedCores,
annotations[consts.PodAnnotationCPUEnhancementCPUSet])
aiSpecifiedPoolName := meta.GetSpecifiedPoolName()
// shared_cores with numa binding doesn't support two share type pools with same specified name existing at same NUMA
return candidateSpecifiedPoolName == aiSpecifiedPoolName
}
return false
}
// CheckNonCPUAffinityNUMA returns true, if the AllocationMeta indicates that the meta is numa affinity candidate.
func CheckNonCPUAffinityNUMA(meta *AllocationMeta) bool {
if meta == nil {
return false
}
return meta.CheckNUMAAffinity()
}
// CheckNonBindingCPUAffinityNUMA returns true, if the AllocationMeta indicates that the meta is numa affinity candidate.
// Now we consume that different numa affinity cpu pool can share the same numa node.
func CheckNonBindingCPUAffinityNUMA(meta *AllocationMeta, annotations map[string]string) bool {
if meta == nil {
return false
}
if meta.CheckNUMANotShare() || qos.AnnotationsIndicateNUMANotShare(annotations) {
return false
}
return meta.CheckNonBindingNUMAAffinity()
}
// GenerateGenericContainerAllocationMeta generates a generic container's allocation metadata.
// This function populates the AllocationMeta struct using data from the resource request and other parameters.
// Parameters:
// - req: The resource request containing information about the pod, container, and other attributes.
// - ownerPoolName: The name of the pool owning this container.
// - qosLevel: The QoS (Quality of Service) level for the container.
// Returns:
// - A pointer to an AllocationMeta struct filled with relevant data from the request and other inputs.
func GenerateGenericContainerAllocationMeta(req *pluginapi.ResourceRequest, ownerPoolName, qosLevel string) AllocationMeta {
return AllocationMeta{
PodUid: req.PodUid,
PodNamespace: req.PodNamespace,
PodName: req.PodName,
ContainerName: req.ContainerName,
ContainerType: req.ContainerType.String(),
ContainerIndex: req.ContainerIndex,
OwnerPoolName: ownerPoolName,
PodRole: req.PodRole,
PodType: req.PodType,
Labels: general.DeepCopyMap(req.Labels),
Annotations: general.DeepCopyMap(req.Annotations),
QoSLevel: qosLevel,
}
}
// GenerateGenericPoolAllocationMeta generates a generic allocation metadata for a pool.
// This function creates an AllocationMeta where both PodUid and OwnerPoolName are set to the given pool name.
// Parameters:
// - poolName: The name of the pool for which the metadata is generated.
// Returns:
// - A pointer to an AllocationMeta struct with the pool name set for both PodUid and OwnerPoolName.
func GenerateGenericPoolAllocationMeta(poolName string) AllocationMeta {
return AllocationMeta{
PodUid: poolName, // The unique identifier for the pool (reusing poolName).
OwnerPoolName: poolName, // The name of the pool itself.
}
}
func LoadExtraControlKnobConfigs(extraControlKnobConfigAbsPath string) (ExtraControlKnobConfigs, error) {
configBytes, err := ioutil.ReadFile(extraControlKnobConfigAbsPath)
if err != nil {
return nil, err
}
extraControlKnobConfigs := make(ExtraControlKnobConfigs)
err = json.Unmarshal(configBytes, &extraControlKnobConfigs)
if err != nil {
return nil, err
}
return extraControlKnobConfigs, nil
}