diff --git a/pkg/kubelet/eviction/helpers_test.go b/pkg/kubelet/eviction/helpers_test.go index c6a76b0d2e1..aaf648e9374 100644 --- a/pkg/kubelet/eviction/helpers_test.go +++ b/pkg/kubelet/eviction/helpers_test.go @@ -18,6 +18,7 @@ package eviction import ( "fmt" + "k8s.io/apimachinery/pkg/util/diff" "reflect" "sort" "testing" @@ -453,6 +454,187 @@ func TestParseThresholdConfig(t *testing.T) { } } +func TestAddAllocatableThresholds(t *testing.T) { + // About func addAllocatableThresholds, only someone threshold that "Signal" is "memory.available" and "GracePeriod" is 0, + // append this threshold(changed "Signal" to "allocatableMemory.available") to thresholds + testCases := map[string]struct { + thresholds []evictionapi.Threshold + expected []evictionapi.Threshold + }{ + "non-memory-signal": { + thresholds: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalImageFsAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + }, + expected: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalImageFsAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + }, + }, + "memory-signal-with-grace": { + thresholds: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + GracePeriod: 10, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + }, + expected: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + GracePeriod: 10, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + }, + }, + "memory-signal-without-grace": { + thresholds: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + }, + expected: []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"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + }, + }, + "memory-signal-without-grace-two-thresholds": { + thresholds: []evictionapi.Threshold{ + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("150Mi"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("200Mi"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("1Gi"), + }, + }, + }, + expected: []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"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("0"), + }, + }, + { + Signal: evictionapi.SignalAllocatableMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("200Mi"), + }, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("1Gi"), + }, + }, + { + Signal: evictionapi.SignalMemoryAvailable, + Operator: evictionapi.OpLessThan, + Value: evictionapi.ThresholdValue{ + Quantity: quantityMustParse("200Mi"), + }, + GracePeriod: 0, + MinReclaim: &evictionapi.ThresholdValue{ + Quantity: quantityMustParse("1Gi"), + }, + }, + }, + }, + } + for testName, testCase := range testCases { + t.Run(testName, func(t *testing.T) { + if !thresholdsEqual(testCase.expected, addAllocatableThresholds(testCase.thresholds)) { + t.Errorf("Err not as expected, test: %v, Unexpected data: %s", testName, diff.ObjectDiff(testCase.expected, addAllocatableThresholds(testCase.thresholds))) + } + }) + } +} + func thresholdsEqual(expected []evictionapi.Threshold, actual []evictionapi.Threshold) bool { if len(expected) != len(actual) { return false