-
Notifications
You must be signed in to change notification settings - Fork 89
/
resources.go
258 lines (209 loc) · 8.82 KB
/
resources.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
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 resource
import (
"context"
"fmt"
"sort"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1"
"github.com/kubewharf/katalyst-core/pkg/metaserver"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/general"
"github.com/kubewharf/katalyst-core/pkg/util/native"
)
const (
MetricsNamePodCount = "pod_count"
MetricsNamePodResource = "pod_resource"
MetricsNameGetResourceEmpty = "get_resource_empty"
)
type ResourcesGetter func(ctx context.Context) (v1.ResourceList, error)
type ThresholdGetter func(resourceName v1.ResourceName) *float64
type GracePeriodGetter func() int64
// ResourcesEvictionPlugin implements EvictPlugin interface it trigger
// pod eviction logic based on the tolerance of resources.
type ResourcesEvictionPlugin struct {
// emitter is used to emit metrics.
emitter metrics.MetricEmitter
// thresholdGetter is used to get the threshold of resources.
thresholdGetter ThresholdGetter
resourcesGetter ResourcesGetter
deletionGracePeriodGetter GracePeriodGetter
thresholdMetToleranceDurationGetter GracePeriodGetter
skipZeroQuantityResourceNames sets.String
podFilter func(pod *v1.Pod) (bool, error)
pluginName string
metaServer *metaserver.MetaServer
}
func NewResourcesEvictionPlugin(pluginName string, metaServer *metaserver.MetaServer,
emitter metrics.MetricEmitter, resourcesGetter ResourcesGetter, thresholdGetter ThresholdGetter,
deletionGracePeriodGetter GracePeriodGetter, thresholdMetToleranceDurationGetter GracePeriodGetter,
skipZeroQuantityResourceNames sets.String,
podFilter func(pod *v1.Pod) (bool, error),
) *ResourcesEvictionPlugin {
// use the given threshold to override the default configurations
plugin := &ResourcesEvictionPlugin{
pluginName: pluginName,
emitter: emitter,
metaServer: metaServer,
resourcesGetter: resourcesGetter,
thresholdGetter: thresholdGetter,
deletionGracePeriodGetter: deletionGracePeriodGetter,
thresholdMetToleranceDurationGetter: thresholdMetToleranceDurationGetter,
skipZeroQuantityResourceNames: skipZeroQuantityResourceNames,
podFilter: podFilter,
}
return plugin
}
func (b *ResourcesEvictionPlugin) Name() string {
if b == nil {
return ""
}
return b.pluginName
}
func (b *ResourcesEvictionPlugin) Start() {
return
}
// ThresholdMet evict pods when the beset effort resources usage is greater than
// the supply (after considering toleration).
func (b *ResourcesEvictionPlugin) ThresholdMet(ctx context.Context) (*pluginapi.ThresholdMetResponse, error) {
activePods, err := b.metaServer.GetPodList(ctx, native.PodIsActive)
if err != nil {
errMsg := fmt.Sprintf("failed to list pods from metaServer: %v", err)
klog.Errorf("[%s] %s", b.pluginName, errMsg)
return nil, fmt.Errorf(errMsg)
}
filteredPods := native.FilterPods(activePods, b.podFilter)
klog.Infof("[%s] total %d filtered pods out-of %d running pods", b.pluginName, len(filteredPods), len(activePods))
_ = b.emitter.StoreInt64(MetricsNamePodCount, int64(len(filteredPods)), metrics.MetricTypeNameRaw)
if len(filteredPods) == 0 {
return &pluginapi.ThresholdMetResponse{
MetType: pluginapi.ThresholdMetType_NOT_MET,
}, nil
}
allocatable, err := b.resourcesGetter(ctx)
if err != nil {
errMsg := fmt.Sprintf("failed to get resources: %v", err)
klog.Errorf("[%s] %s", b.pluginName, errMsg)
return nil, fmt.Errorf(errMsg)
}
native.EmitResourceMetrics(MetricsNamePodResource, allocatable, map[string]string{
"type": "allocatable",
}, b.emitter)
// allocatable resources in cnr status is empty and it's a abnormal case.
// to avoid evict mistakenly, we return threshold not met, and emit metrics.
// [TODO] we should consider refining it when actually finding cases.
if allocatable == nil || len(allocatable) == 0 {
_ = b.emitter.StoreInt64(MetricsNameGetResourceEmpty, 1, metrics.MetricTypeNameCount, metrics.MetricTag{
Key: "pluginName", Val: b.pluginName,
})
return &pluginapi.ThresholdMetResponse{
MetType: pluginapi.ThresholdMetType_NOT_MET,
}, nil
}
// use requests (rather than limits) as used resource
usedResources := make(v1.ResourceList)
for _, pod := range filteredPods {
resources := native.SumUpPodRequestResources(pod)
usedResources = native.AddResources(usedResources, resources)
native.EmitResourceMetrics(MetricsNamePodResource, resources, map[string]string{
"pluginName": b.pluginName,
"namespace": pod.Namespace,
"name": pod.Name,
"type": "pod",
}, b.emitter)
}
klog.Infof("[%s] resources: allocatable %+v usedResources %+v", b.pluginName, allocatable, usedResources)
native.EmitResourceMetrics(MetricsNamePodResource, usedResources, map[string]string{
"pluginName": b.pluginName,
"type": "used",
}, b.emitter)
for resourceName, usedQuantity := range usedResources {
totalQuantity, ok := allocatable[resourceName]
if !ok {
klog.Warningf("[%s] used resource: %s doesn't exist in allocatable", b.pluginName, resourceName)
continue
}
total := float64((&totalQuantity).Value())
if total <= 0 && b.skipZeroQuantityResourceNames.Has(string(resourceName)) {
klog.Warningf("[%s] skip resource: %s with total: %.2f", b.pluginName, total)
continue
}
used := float64((&usedQuantity).Value())
// get resource threshold (i.e. tolerance) for each resource
// if nil, eviction will not be triggered.
thresholdRate := b.thresholdGetter(resourceName)
if thresholdRate == nil {
continue
}
thresholdValue := *thresholdRate * total
klog.Infof("[%s] resources %v: total %v, used %v, thresholdRate %v, thresholdValue: %v", b.pluginName,
resourceName, total, used, *thresholdRate, thresholdValue)
exceededValue := thresholdValue - used
if exceededValue < 0 {
klog.Infof("[%s] resources %v exceeded: total %v, used %v, thresholdRate %v, thresholdValue: %v", b.pluginName,
resourceName, total, used, *thresholdRate, thresholdValue)
return &pluginapi.ThresholdMetResponse{
ThresholdValue: thresholdValue,
ObservedValue: used,
ThresholdOperator: pluginapi.ThresholdOperator_GREATER_THAN,
MetType: pluginapi.ThresholdMetType_HARD_MET,
EvictionScope: string(resourceName),
GracePeriodSeconds: b.thresholdMetToleranceDurationGetter(),
}, nil
}
}
return &pluginapi.ThresholdMetResponse{
MetType: pluginapi.ThresholdMetType_NOT_MET,
}, nil
}
func (b *ResourcesEvictionPlugin) GetTopEvictionPods(_ context.Context, request *pluginapi.GetTopEvictionPodsRequest) (*pluginapi.GetTopEvictionPodsResponse, error) {
if request == nil {
return nil, fmt.Errorf("GetTopEvictionPods got nil request")
}
if len(request.ActivePods) == 0 {
klog.Warningf("[%s] GetTopEvictionPods got empty active pods list", b.pluginName)
return &pluginapi.GetTopEvictionPodsResponse{}, nil
}
activeFilteredPods := native.FilterPods(request.ActivePods, b.podFilter)
sort.Slice(activeFilteredPods, func(i, j int) bool {
valueI, valueJ := int64(0), int64(0)
resourceI, resourceJ := native.SumUpPodRequestResources(activeFilteredPods[i]), native.SumUpPodRequestResources(activeFilteredPods[j])
if quantity, ok := resourceI[v1.ResourceName(request.EvictionScope)]; ok {
valueI = (&quantity).Value()
}
if quantity, ok := resourceJ[v1.ResourceName(request.EvictionScope)]; ok {
valueJ = (&quantity).Value()
}
return valueI > valueJ
})
retLen := general.MinUInt64(request.TopN, uint64(len(activeFilteredPods)))
var deletionOptions *pluginapi.DeletionOptions
if gracePeriod := b.deletionGracePeriodGetter(); gracePeriod > 0 {
deletionOptions = &pluginapi.DeletionOptions{
GracePeriodSeconds: gracePeriod,
}
}
return &pluginapi.GetTopEvictionPodsResponse{
TargetPods: activeFilteredPods[:retLen],
DeletionOptions: deletionOptions,
}, nil
}
func (b *ResourcesEvictionPlugin) GetEvictPods(_ context.Context, request *pluginapi.GetEvictPodsRequest) (*pluginapi.GetEvictPodsResponse, error) {
if request == nil {
return nil, fmt.Errorf("GetEvictPods got nil request")
}
return &pluginapi.GetEvictPodsResponse{}, nil
}