From 19c91a59ab709eee4ff7a6beb923c2576e8c420f Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Mon, 3 Jun 2019 13:16:08 -0700 Subject: [PATCH] Iterate through thresholds in managerImpl#synchronize --- pkg/kubelet/eviction/eviction_manager.go | 6 +-- pkg/kubelet/eviction/helpers.go | 11 +++++ pkg/kubelet/eviction/helpers_test.go | 59 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/pkg/kubelet/eviction/eviction_manager.go b/pkg/kubelet/eviction/eviction_manager.go index a1ab99869ab..a471383d7ff 100644 --- a/pkg/kubelet/eviction/eviction_manager.go +++ b/pkg/kubelet/eviction/eviction_manager.go @@ -324,10 +324,8 @@ func (m *managerImpl) synchronize(diskInfoProvider DiskInfoProvider, podFunc Act // rank the thresholds by eviction priority sort.Sort(byEvictionPriority(thresholds)) - thresholdToReclaim := thresholds[0] - resourceToReclaim, found := signalToResource[thresholdToReclaim.Signal] - if !found { - klog.V(3).Infof("eviction manager: threshold %s was crossed, but reclaim is not implemented for this threshold.", thresholdToReclaim.Signal) + thresholdToReclaim, resourceToReclaim, foundAny := getReclaimableThreshold(thresholds) + if !foundAny { return nil } klog.Warningf("eviction manager: attempting to reclaim %v", resourceToReclaim) diff --git a/pkg/kubelet/eviction/helpers.go b/pkg/kubelet/eviction/helpers.go index 50279fd1977..01152eac705 100644 --- a/pkg/kubelet/eviction/helpers.go +++ b/pkg/kubelet/eviction/helpers.go @@ -98,6 +98,17 @@ func validSignal(signal evictionapi.Signal) bool { return found } +// getReclaimableThreshold finds the threshold and resource to reclaim +func getReclaimableThreshold(thresholds []evictionapi.Threshold) (evictionapi.Threshold, v1.ResourceName, bool) { + for _, thresholdToReclaim := range thresholds { + if resourceToReclaim, ok := signalToResource[thresholdToReclaim.Signal]; ok { + return thresholdToReclaim, resourceToReclaim, true + } + klog.V(3).Infof("eviction manager: threshold %s was crossed, but reclaim is not implemented for this threshold.", thresholdToReclaim.Signal) + } + return evictionapi.Threshold{}, "", false +} + // ParseThresholdConfig parses the flags for thresholds. func ParseThresholdConfig(allocatableConfig []string, evictionHard, evictionSoft, evictionSoftGracePeriod, evictionMinimumReclaim map[string]string) ([]evictionapi.Threshold, error) { results := []evictionapi.Threshold{} diff --git a/pkg/kubelet/eviction/helpers_test.go b/pkg/kubelet/eviction/helpers_test.go index c10a4e6e23e..81a3c451cd6 100644 --- a/pkg/kubelet/eviction/helpers_test.go +++ b/pkg/kubelet/eviction/helpers_test.go @@ -40,6 +40,65 @@ func quantityMustParse(value string) *resource.Quantity { return &q } +func TestGetReclaimableThreshold(t *testing.T) { + testCases := map[string]struct { + thresholds []evictionapi.Threshold + }{ + "": { + thresholds: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalAllocatableMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + { + Signal: evictionapi.SignalImageFsAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("2Gi"), + }, + }, + { + Signal: evictionapi.SignalNodeFsAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("100Mi"), + }, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("1Gi"), + }, + }, + }, + }, + } + for testName, testCase := range testCases { + sort.Sort(byEvictionPriority(testCase.thresholds)) + _, resource, ok := getReclaimableThreshold(testCase.thresholds) + print(resource) + if !ok { + t.Errorf("Didn't find reclaimable threshold, test: %v", testName) + } + } +} + func TestParseThresholdConfig(t *testing.T) { gracePeriod, _ := time.ParseDuration("30s") testCases := map[string]struct {