|
| 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 rootfs |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "sort" |
| 24 | + "time" |
| 25 | + |
| 26 | + v1 "k8s.io/api/core/v1" |
| 27 | + "k8s.io/apimachinery/pkg/api/resource" |
| 28 | + "k8s.io/apimachinery/pkg/util/sets" |
| 29 | + "k8s.io/client-go/tools/events" |
| 30 | + evictionapi "k8s.io/kubernetes/pkg/kubelet/eviction/api" |
| 31 | + |
| 32 | + apiconsts "github.com/kubewharf/katalyst-api/pkg/consts" |
| 33 | + pluginapi "github.com/kubewharf/katalyst-api/pkg/protocol/evictionplugin/v1alpha1" |
| 34 | + "github.com/kubewharf/katalyst-core/pkg/agent/evictionmanager/plugin" |
| 35 | + "github.com/kubewharf/katalyst-core/pkg/client" |
| 36 | + "github.com/kubewharf/katalyst-core/pkg/config" |
| 37 | + "github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic" |
| 38 | + "github.com/kubewharf/katalyst-core/pkg/config/generic" |
| 39 | + "github.com/kubewharf/katalyst-core/pkg/consts" |
| 40 | + "github.com/kubewharf/katalyst-core/pkg/metaserver" |
| 41 | + "github.com/kubewharf/katalyst-core/pkg/metaserver/agent/metric/helper" |
| 42 | + "github.com/kubewharf/katalyst-core/pkg/metrics" |
| 43 | + "github.com/kubewharf/katalyst-core/pkg/util/general" |
| 44 | + "github.com/kubewharf/katalyst-core/pkg/util/process" |
| 45 | +) |
| 46 | + |
| 47 | +const ( |
| 48 | + EvictionPluginNamePodRootfsOveruse = "rootfs-overuse-eviction-plugin" |
| 49 | + |
| 50 | + minRootfsOveruseThreshold = 20 * 1024 * 1024 * 1024 // 20GB |
| 51 | + minRootfsOverusePercentageThreshold float32 = 0.1 |
| 52 | +) |
| 53 | + |
| 54 | +// PodRootfsOveruseEvictionPlugin implements the EvictPlugin interface. |
| 55 | +type PodRootfsOveruseEvictionPlugin struct { |
| 56 | + *process.StopControl |
| 57 | + pluginName string |
| 58 | + dynamicConfig *dynamic.DynamicAgentConfiguration |
| 59 | + metaServer *metaserver.MetaServer |
| 60 | + qosConf *generic.QoSConfiguration |
| 61 | + emitter metrics.MetricEmitter |
| 62 | +} |
| 63 | + |
| 64 | +func NewPodRootfsOveruseEvictionPlugin(_ *client.GenericClientSet, _ events.EventRecorder, |
| 65 | + metaServer *metaserver.MetaServer, emitter metrics.MetricEmitter, conf *config.Configuration, |
| 66 | +) plugin.EvictionPlugin { |
| 67 | + return &PodRootfsOveruseEvictionPlugin{ |
| 68 | + pluginName: EvictionPluginNamePodRootfsOveruse, |
| 69 | + metaServer: metaServer, |
| 70 | + StopControl: process.NewStopControl(time.Time{}), |
| 71 | + dynamicConfig: conf.DynamicAgentConfiguration, |
| 72 | + qosConf: conf.GenericConfiguration.QoSConfiguration, |
| 73 | + emitter: emitter, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (r *PodRootfsOveruseEvictionPlugin) Name() string { |
| 78 | + if r == nil { |
| 79 | + return "" |
| 80 | + } |
| 81 | + |
| 82 | + return r.pluginName |
| 83 | +} |
| 84 | + |
| 85 | +func (r *PodRootfsOveruseEvictionPlugin) Start() {} |
| 86 | + |
| 87 | +func (r *PodRootfsOveruseEvictionPlugin) ThresholdMet(_ context.Context) (*pluginapi.ThresholdMetResponse, error) { |
| 88 | + return &pluginapi.ThresholdMetResponse{ |
| 89 | + MetType: pluginapi.ThresholdMetType_NOT_MET, |
| 90 | + }, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (r *PodRootfsOveruseEvictionPlugin) GetTopEvictionPods(_ context.Context, _ *pluginapi.GetTopEvictionPodsRequest) (*pluginapi.GetTopEvictionPodsResponse, error) { |
| 94 | + return &pluginapi.GetTopEvictionPodsResponse{}, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (r *PodRootfsOveruseEvictionPlugin) GetEvictPods(_ context.Context, request *pluginapi.GetEvictPodsRequest) (*pluginapi.GetEvictPodsResponse, error) { |
| 98 | + if request == nil { |
| 99 | + return nil, fmt.Errorf("GetEvictPods got nil request") |
| 100 | + } |
| 101 | + |
| 102 | + rootfsEvictionConfig := r.dynamicConfig.GetDynamicConfiguration().RootfsPressureEvictionConfiguration |
| 103 | + if !rootfsEvictionConfig.EnableRootfsOveruseEviction { |
| 104 | + return &pluginapi.GetEvictPodsResponse{}, nil |
| 105 | + } |
| 106 | + |
| 107 | + if len(request.ActivePods) == 0 { |
| 108 | + return &pluginapi.GetEvictPodsResponse{}, nil |
| 109 | + } |
| 110 | + |
| 111 | + filterPods := r.filterPods(request.ActivePods) |
| 112 | + |
| 113 | + var usageItemList podUsageList |
| 114 | + for _, pod := range filterPods { |
| 115 | + threshold := r.getRootfsOveruseThreshold(pod) |
| 116 | + if threshold == nil { |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + used, capacity, err := r.getPodRootfsUsed(pod) |
| 121 | + if err != nil { |
| 122 | + general.Warningf("Failed to get pod rootfs usage for %s: %q", pod.UID, err) |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + if rootfsOveruseThresholdMet(used, capacity, threshold) { |
| 127 | + podUsageItem := podUsageItem{ |
| 128 | + pod: pod, |
| 129 | + usage: used, |
| 130 | + capacity: capacity, |
| 131 | + threshold: getThresholdValue(threshold, capacity), |
| 132 | + } |
| 133 | + usageItemList = append(usageItemList, podUsageItem) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if len(usageItemList) == 0 { |
| 138 | + return &pluginapi.GetEvictPodsResponse{}, nil |
| 139 | + } |
| 140 | + |
| 141 | + sort.Sort(usageItemList) |
| 142 | + result := make([]*pluginapi.EvictPod, 0) |
| 143 | + deletionOptions := &pluginapi.DeletionOptions{ |
| 144 | + GracePeriodSeconds: rootfsEvictionConfig.GracePeriod, |
| 145 | + } |
| 146 | + for i := 0; i < rootfsEvictionConfig.RootfsOveruseEvictionCount && i < len(usageItemList); i++ { |
| 147 | + item := usageItemList[i] |
| 148 | + evictPod := &pluginapi.EvictPod{ |
| 149 | + Pod: item.pod, |
| 150 | + Reason: fmt.Sprintf("rootfs overuse threshold met, used: %d, threshold: %d", item.usage, item.threshold), |
| 151 | + } |
| 152 | + if deletionOptions.GracePeriodSeconds > 0 { |
| 153 | + evictPod.DeletionOptions = deletionOptions |
| 154 | + } |
| 155 | + result = append(result, evictPod) |
| 156 | + general.InfoS("rootfs overuse threshold met", "pod", item.pod.Name, "used", item.usage, "threshold", item.threshold) |
| 157 | + } |
| 158 | + return &pluginapi.GetEvictPodsResponse{EvictPods: result}, nil |
| 159 | +} |
| 160 | + |
| 161 | +func (r *PodRootfsOveruseEvictionPlugin) getPodRootfsUsed(pod *v1.Pod) (int64, int64, error) { |
| 162 | + podRootfsUsed, err := helper.GetPodMetric(r.metaServer.MetricsFetcher, r.emitter, pod, consts.MetricsContainerRootfsUsed, -1) |
| 163 | + if err != nil { |
| 164 | + return 0, 0, err |
| 165 | + } |
| 166 | + |
| 167 | + rootfsCapacity, err := helper.GetNodeMetric(r.metaServer.MetricsFetcher, r.emitter, consts.MetricsImageFsCapacity) |
| 168 | + if err != nil { |
| 169 | + return 0, 0, err |
| 170 | + } |
| 171 | + |
| 172 | + if rootfsCapacity < 1 { |
| 173 | + return 0, 0, errors.New("invalid rootfs capacity") |
| 174 | + } |
| 175 | + |
| 176 | + return int64(podRootfsUsed), int64(rootfsCapacity), nil |
| 177 | +} |
| 178 | + |
| 179 | +func (r *PodRootfsOveruseEvictionPlugin) getRootfsOveruseThreshold(pod *v1.Pod) *evictionapi.ThresholdValue { |
| 180 | + qosLevel, err := r.qosConf.GetQoSLevelForPod(pod) |
| 181 | + if err != nil { |
| 182 | + return nil |
| 183 | + } |
| 184 | + |
| 185 | + switch qosLevel { |
| 186 | + case string(apiconsts.QoSLevelSharedCores): |
| 187 | + return getRootfsOveruseThreshold(r.dynamicConfig.GetDynamicConfiguration().RootfsPressureEvictionConfiguration.SharedQoSRootfsOveruseThreshold) |
| 188 | + case string(apiconsts.QoSLevelReclaimedCores): |
| 189 | + return getRootfsOveruseThreshold(r.dynamicConfig.GetDynamicConfiguration().RootfsPressureEvictionConfiguration.ReclaimedQoSRootfsOveruseThreshold) |
| 190 | + default: |
| 191 | + } |
| 192 | + return nil |
| 193 | +} |
| 194 | + |
| 195 | +func (r *PodRootfsOveruseEvictionPlugin) filterPods(pods []*v1.Pod) []*v1.Pod { |
| 196 | + supportedQoSLevels := sets.NewString(r.dynamicConfig.GetDynamicConfiguration().RootfsPressureEvictionConfiguration.RootfsOveruseEvictionSupportedQoSLevels...) |
| 197 | + sharedQoSNamespaceFilter := sets.NewString(r.dynamicConfig.GetDynamicConfiguration().RootfsPressureEvictionConfiguration.SharedQoSNamespaceFilter...) |
| 198 | + |
| 199 | + filteredPods := make([]*v1.Pod, 0, len(pods)) |
| 200 | + for _, pod := range pods { |
| 201 | + qosLevel, err := r.qosConf.GetQoSLevelForPod(pod) |
| 202 | + if err != nil { |
| 203 | + continue |
| 204 | + } |
| 205 | + if !supportedQoSLevels.Has(qosLevel) { |
| 206 | + continue |
| 207 | + } |
| 208 | + if qosLevel == apiconsts.PodAnnotationQoSLevelSharedCores && !sharedQoSNamespaceFilter.Has(pod.Namespace) { |
| 209 | + continue |
| 210 | + } |
| 211 | + filteredPods = append(filteredPods, pod) |
| 212 | + } |
| 213 | + return filteredPods |
| 214 | +} |
| 215 | + |
| 216 | +func rootfsOveruseThresholdMet(used, capacity int64, threshold *evictionapi.ThresholdValue) bool { |
| 217 | + if threshold == nil { |
| 218 | + return false |
| 219 | + } |
| 220 | + if thresholdValue := getThresholdValue(threshold, capacity); used > thresholdValue { |
| 221 | + return true |
| 222 | + } |
| 223 | + return false |
| 224 | +} |
| 225 | + |
| 226 | +func getThresholdValue(threshold *evictionapi.ThresholdValue, capacity int64) int64 { |
| 227 | + if threshold == nil { |
| 228 | + return 0 |
| 229 | + } |
| 230 | + if threshold.Quantity != nil { |
| 231 | + return threshold.Quantity.Value() |
| 232 | + } |
| 233 | + if threshold.Percentage > 0 && threshold.Percentage < 1 { |
| 234 | + return int64(float32(capacity) * threshold.Percentage) |
| 235 | + } |
| 236 | + return 0 |
| 237 | +} |
| 238 | + |
| 239 | +func getRootfsOveruseThreshold(threshold *evictionapi.ThresholdValue) *evictionapi.ThresholdValue { |
| 240 | + if threshold == nil { |
| 241 | + return nil |
| 242 | + } |
| 243 | + if threshold.Quantity != nil && threshold.Quantity.Value() < minRootfsOveruseThreshold { |
| 244 | + return &evictionapi.ThresholdValue{ |
| 245 | + Quantity: resource.NewQuantity(minRootfsOveruseThreshold, resource.BinarySI), |
| 246 | + } |
| 247 | + } |
| 248 | + if threshold.Percentage > 0 && threshold.Percentage < 1 && threshold.Percentage < minRootfsOverusePercentageThreshold { |
| 249 | + return &evictionapi.ThresholdValue{ |
| 250 | + Percentage: minRootfsOverusePercentageThreshold, |
| 251 | + } |
| 252 | + } |
| 253 | + return threshold |
| 254 | +} |
0 commit comments