Skip to content

Commit a38edf3

Browse files
authored
Merge pull request kubernetes#129444 from adrianmoisey/podautoscaler_deprecations
Remove use of deprecated functions
2 parents 0b789d7 + 42a5e5f commit a38edf3

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

pkg/controller/podautoscaler/horizontal_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import (
5454
metricsfake "k8s.io/metrics/pkg/client/clientset/versioned/fake"
5555
cmfake "k8s.io/metrics/pkg/client/custom_metrics/fake"
5656
emfake "k8s.io/metrics/pkg/client/external_metrics/fake"
57-
"k8s.io/utils/pointer"
57+
"k8s.io/utils/ptr"
5858

5959
"github.com/stretchr/testify/assert"
6060

@@ -675,7 +675,7 @@ func findCpuUtilization(metricStatus []autoscalingv2.MetricStatus) (utilization
675675
return nil
676676
}
677677

678-
func (tc *testCase) verifyResults(t *testing.T, m *mockMonitor) {
678+
func (tc *testCase) verifyResults(ctx context.Context, t *testing.T, m *mockMonitor) {
679679
tc.Lock()
680680
defer tc.Unlock()
681681

@@ -685,12 +685,12 @@ func (tc *testCase) verifyResults(t *testing.T, m *mockMonitor) {
685685
assert.Equal(t, tc.specReplicas != tc.expectedDesiredReplicas, tc.eventCreated, "an event should have been created only if we expected a change in replicas")
686686
}
687687

688-
tc.verifyRecordedMetric(t, m)
688+
tc.verifyRecordedMetric(ctx, t, m)
689689
}
690690

691-
func (tc *testCase) verifyRecordedMetric(t *testing.T, m *mockMonitor) {
691+
func (tc *testCase) verifyRecordedMetric(ctx context.Context, t *testing.T, m *mockMonitor) {
692692
// First, wait for the reconciliation completed at least once.
693-
m.waitUntilRecorded(t)
693+
m.waitUntilRecorded(ctx, t)
694694

695695
assert.Equal(t, tc.expectedReportedReconciliationActionLabel, m.reconciliationActionLabels[0], "the reconciliation action should be recorded in monitor expectedly")
696696
assert.Equal(t, tc.expectedReportedReconciliationErrorLabel, m.reconciliationErrorLabels[0], "the reconciliation error should be recorded in monitor expectedly")
@@ -833,7 +833,7 @@ func (tc *testCase) runTestWithController(t *testing.T, hpaController *Horizonta
833833
if !ok {
834834
t.Fatalf("test HPA controller should have mockMonitor, but actually not")
835835
}
836-
tc.verifyResults(t, m)
836+
tc.verifyResults(ctx, t, m)
837837
}
838838

839839
func (tc *testCase) runTest(t *testing.T) {
@@ -875,8 +875,8 @@ func (m *mockMonitor) ObserveMetricComputationResult(action monitor.ActionLabel,
875875
}
876876

877877
// waitUntilRecorded waits for the HPA controller to reconcile at least once.
878-
func (m *mockMonitor) waitUntilRecorded(t *testing.T) {
879-
if err := wait.Poll(20*time.Millisecond, 100*time.Millisecond, func() (done bool, err error) {
878+
func (m *mockMonitor) waitUntilRecorded(ctx context.Context, t *testing.T) {
879+
if err := wait.PollUntilContextTimeout(ctx, 20*time.Millisecond, 100*time.Millisecond, true, func(ctx context.Context) (done bool, err error) {
880880
m.RWMutex.RLock()
881881
defer m.RWMutex.RUnlock()
882882
if len(m.reconciliationActionLabels) == 0 || len(m.reconciliationErrorLabels) == 0 {
@@ -925,7 +925,7 @@ func TestScaleUpContainer(t *testing.T) {
925925
Name: v1.ResourceCPU,
926926
Target: autoscalingv2.MetricTarget{
927927
Type: autoscalingv2.UtilizationMetricType,
928-
AverageUtilization: pointer.Int32(30),
928+
AverageUtilization: ptr.To(int32(30)),
929929
},
930930
Container: "container1",
931931
},
@@ -1619,7 +1619,7 @@ func TestScaleDownContainerResource(t *testing.T) {
16191619
Name: v1.ResourceCPU,
16201620
Target: autoscalingv2.MetricTarget{
16211621
Type: autoscalingv2.UtilizationMetricType,
1622-
AverageUtilization: pointer.Int32(50),
1622+
AverageUtilization: ptr.To(int32(50)),
16231623
},
16241624
},
16251625
}},
@@ -3848,7 +3848,7 @@ func TestCalculateScaleUpLimitWithScalingRules(t *testing.T) {
38483848
policy := autoscalingv2.MinChangePolicySelect
38493849

38503850
calculated := calculateScaleUpLimitWithScalingRules(1, []timestampedScaleEvent{}, []timestampedScaleEvent{}, &autoscalingv2.HPAScalingRules{
3851-
StabilizationWindowSeconds: pointer.Int32(300),
3851+
StabilizationWindowSeconds: ptr.To(int32(300)),
38523852
SelectPolicy: &policy,
38533853
Policies: []autoscalingv2.HPAScalingPolicy{
38543854
{
@@ -3870,7 +3870,7 @@ func TestCalculateScaleDownLimitWithBehaviors(t *testing.T) {
38703870
policy := autoscalingv2.MinChangePolicySelect
38713871

38723872
calculated := calculateScaleDownLimitWithBehaviors(5, []timestampedScaleEvent{}, []timestampedScaleEvent{}, &autoscalingv2.HPAScalingRules{
3873-
StabilizationWindowSeconds: pointer.Int32(300),
3873+
StabilizationWindowSeconds: ptr.To(int32(300)),
38743874
SelectPolicy: &policy,
38753875
Policies: []autoscalingv2.HPAScalingPolicy{
38763876
{
@@ -3891,7 +3891,7 @@ func TestCalculateScaleDownLimitWithBehaviors(t *testing.T) {
38913891
func generateScalingRules(pods, podsPeriod, percent, percentPeriod, stabilizationWindow int32) *autoscalingv2.HPAScalingRules {
38923892
policy := autoscalingv2.MaxChangePolicySelect
38933893
directionBehavior := autoscalingv2.HPAScalingRules{
3894-
StabilizationWindowSeconds: pointer.Int32(stabilizationWindow),
3894+
StabilizationWindowSeconds: ptr.To(int32(stabilizationWindow)),
38953895
SelectPolicy: &policy,
38963896
}
38973897
if pods != 0 {

pkg/controller/podautoscaler/replica_calculator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ func (c *ReplicaCalculator) GetExternalPerPodMetricReplicas(statusReplicas int32
375375
return replicaCount, usage, timestamp, nil
376376
}
377377

378-
func groupPods(pods []*v1.Pod, metrics metricsclient.PodMetricsInfo, resource v1.ResourceName, cpuInitializationPeriod, delayOfInitialReadinessStatus time.Duration) (readyPodCount int, unreadyPods, missingPods, ignoredPods sets.String) {
379-
missingPods = sets.NewString()
380-
unreadyPods = sets.NewString()
381-
ignoredPods = sets.NewString()
378+
func groupPods(pods []*v1.Pod, metrics metricsclient.PodMetricsInfo, resource v1.ResourceName, cpuInitializationPeriod, delayOfInitialReadinessStatus time.Duration) (readyPodCount int, unreadyPods, missingPods, ignoredPods sets.Set[string]) {
379+
missingPods = sets.New[string]()
380+
unreadyPods = sets.New[string]()
381+
ignoredPods = sets.New[string]()
382382
for _, pod := range pods {
383383
if pod.DeletionTimestamp != nil || pod.Status.Phase == v1.PodFailed {
384384
ignoredPods.Insert(pod.Name)
@@ -446,7 +446,7 @@ func calculatePodRequests(pods []*v1.Pod, container string, resource v1.Resource
446446
return requests, nil
447447
}
448448

449-
func removeMetricsForPods(metrics metricsclient.PodMetricsInfo, pods sets.String) {
449+
func removeMetricsForPods(metrics metricsclient.PodMetricsInfo, pods sets.Set[string]) {
450450
for _, pod := range pods.UnsortedList() {
451451
delete(metrics, pod)
452452
}

pkg/controller/podautoscaler/replica_calculator_test.go

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,19 +1614,19 @@ func TestGroupPods(t *testing.T) {
16141614
metrics metricsclient.PodMetricsInfo
16151615
resource v1.ResourceName
16161616
expectReadyPodCount int
1617-
expectUnreadyPods sets.String
1618-
expectMissingPods sets.String
1619-
expectIgnoredPods sets.String
1617+
expectUnreadyPods sets.Set[string]
1618+
expectMissingPods sets.Set[string]
1619+
expectIgnoredPods sets.Set[string]
16201620
}{
16211621
{
16221622
name: "void",
16231623
pods: []*v1.Pod{},
16241624
metrics: metricsclient.PodMetricsInfo{},
16251625
resource: v1.ResourceCPU,
16261626
expectReadyPodCount: 0,
1627-
expectUnreadyPods: sets.NewString(),
1628-
expectMissingPods: sets.NewString(),
1629-
expectIgnoredPods: sets.NewString(),
1627+
expectUnreadyPods: sets.New[string](),
1628+
expectMissingPods: sets.New[string](),
1629+
expectIgnoredPods: sets.New[string](),
16301630
}, {
16311631
name: "count in a ready pod - memory",
16321632
pods: []*v1.Pod{
@@ -1644,9 +1644,9 @@ func TestGroupPods(t *testing.T) {
16441644
},
16451645
resource: v1.ResourceMemory,
16461646
expectReadyPodCount: 1,
1647-
expectUnreadyPods: sets.NewString(),
1648-
expectMissingPods: sets.NewString(),
1649-
expectIgnoredPods: sets.NewString(),
1647+
expectUnreadyPods: sets.New[string](),
1648+
expectMissingPods: sets.New[string](),
1649+
expectIgnoredPods: sets.New[string](),
16501650
}, {
16511651
name: "unready a pod without ready condition - CPU",
16521652
pods: []*v1.Pod{
@@ -1667,9 +1667,9 @@ func TestGroupPods(t *testing.T) {
16671667
},
16681668
resource: v1.ResourceCPU,
16691669
expectReadyPodCount: 0,
1670-
expectUnreadyPods: sets.NewString("lucretius"),
1671-
expectMissingPods: sets.NewString(),
1672-
expectIgnoredPods: sets.NewString(),
1670+
expectUnreadyPods: sets.New[string]("lucretius"),
1671+
expectMissingPods: sets.New[string](),
1672+
expectIgnoredPods: sets.New[string](),
16731673
}, {
16741674
name: "count in a ready pod with fresh metrics during initialization period - CPU",
16751675
pods: []*v1.Pod{
@@ -1697,9 +1697,9 @@ func TestGroupPods(t *testing.T) {
16971697
},
16981698
resource: v1.ResourceCPU,
16991699
expectReadyPodCount: 1,
1700-
expectUnreadyPods: sets.NewString(),
1701-
expectMissingPods: sets.NewString(),
1702-
expectIgnoredPods: sets.NewString(),
1700+
expectUnreadyPods: sets.New[string](),
1701+
expectMissingPods: sets.New[string](),
1702+
expectIgnoredPods: sets.New[string](),
17031703
}, {
17041704
name: "unready a ready pod without fresh metrics during initialization period - CPU",
17051705
pods: []*v1.Pod{
@@ -1727,9 +1727,9 @@ func TestGroupPods(t *testing.T) {
17271727
},
17281728
resource: v1.ResourceCPU,
17291729
expectReadyPodCount: 0,
1730-
expectUnreadyPods: sets.NewString("bentham"),
1731-
expectMissingPods: sets.NewString(),
1732-
expectIgnoredPods: sets.NewString(),
1730+
expectUnreadyPods: sets.New[string]("bentham"),
1731+
expectMissingPods: sets.New[string](),
1732+
expectIgnoredPods: sets.New[string](),
17331733
}, {
17341734
name: "unready an unready pod during initialization period - CPU",
17351735
pods: []*v1.Pod{
@@ -1757,9 +1757,9 @@ func TestGroupPods(t *testing.T) {
17571757
},
17581758
resource: v1.ResourceCPU,
17591759
expectReadyPodCount: 0,
1760-
expectUnreadyPods: sets.NewString("lucretius"),
1761-
expectMissingPods: sets.NewString(),
1762-
expectIgnoredPods: sets.NewString(),
1760+
expectUnreadyPods: sets.New[string]("lucretius"),
1761+
expectMissingPods: sets.New[string](),
1762+
expectIgnoredPods: sets.New[string](),
17631763
}, {
17641764
name: "count in a ready pod without fresh metrics after initialization period - CPU",
17651765
pods: []*v1.Pod{
@@ -1787,9 +1787,9 @@ func TestGroupPods(t *testing.T) {
17871787
},
17881788
resource: v1.ResourceCPU,
17891789
expectReadyPodCount: 1,
1790-
expectUnreadyPods: sets.NewString(),
1791-
expectMissingPods: sets.NewString(),
1792-
expectIgnoredPods: sets.NewString(),
1790+
expectUnreadyPods: sets.New[string](),
1791+
expectMissingPods: sets.New[string](),
1792+
expectIgnoredPods: sets.New[string](),
17931793
}, {
17941794
name: "count in an unready pod that was ready after initialization period - CPU",
17951795
pods: []*v1.Pod{
@@ -1817,9 +1817,9 @@ func TestGroupPods(t *testing.T) {
18171817
},
18181818
resource: v1.ResourceCPU,
18191819
expectReadyPodCount: 1,
1820-
expectUnreadyPods: sets.NewString(),
1821-
expectMissingPods: sets.NewString(),
1822-
expectIgnoredPods: sets.NewString(),
1820+
expectUnreadyPods: sets.New[string](),
1821+
expectMissingPods: sets.New[string](),
1822+
expectIgnoredPods: sets.New[string](),
18231823
}, {
18241824
name: "unready pod that has never been ready after initialization period - CPU",
18251825
pods: []*v1.Pod{
@@ -1847,9 +1847,9 @@ func TestGroupPods(t *testing.T) {
18471847
},
18481848
resource: v1.ResourceCPU,
18491849
expectReadyPodCount: 1,
1850-
expectUnreadyPods: sets.NewString(),
1851-
expectMissingPods: sets.NewString(),
1852-
expectIgnoredPods: sets.NewString(),
1850+
expectUnreadyPods: sets.New[string](),
1851+
expectMissingPods: sets.New[string](),
1852+
expectIgnoredPods: sets.New[string](),
18531853
}, {
18541854
name: "a missing pod",
18551855
pods: []*v1.Pod{
@@ -1868,9 +1868,9 @@ func TestGroupPods(t *testing.T) {
18681868
metrics: metricsclient.PodMetricsInfo{},
18691869
resource: v1.ResourceCPU,
18701870
expectReadyPodCount: 0,
1871-
expectUnreadyPods: sets.NewString(),
1872-
expectMissingPods: sets.NewString("epicurus"),
1873-
expectIgnoredPods: sets.NewString(),
1871+
expectUnreadyPods: sets.New[string](),
1872+
expectMissingPods: sets.New[string]("epicurus"),
1873+
expectIgnoredPods: sets.New[string](),
18741874
}, {
18751875
name: "several pods",
18761876
pods: []*v1.Pod{
@@ -1921,9 +1921,9 @@ func TestGroupPods(t *testing.T) {
19211921
},
19221922
resource: v1.ResourceCPU,
19231923
expectReadyPodCount: 1,
1924-
expectUnreadyPods: sets.NewString("lucretius"),
1925-
expectMissingPods: sets.NewString("epicurus"),
1926-
expectIgnoredPods: sets.NewString(),
1924+
expectUnreadyPods: sets.New[string]("lucretius"),
1925+
expectMissingPods: sets.New[string]("epicurus"),
1926+
expectIgnoredPods: sets.New[string](),
19271927
}, {
19281928
name: "pending pods are unreadied",
19291929
pods: []*v1.Pod{
@@ -1939,9 +1939,9 @@ func TestGroupPods(t *testing.T) {
19391939
metrics: metricsclient.PodMetricsInfo{},
19401940
resource: v1.ResourceCPU,
19411941
expectReadyPodCount: 0,
1942-
expectUnreadyPods: sets.NewString("unscheduled"),
1943-
expectMissingPods: sets.NewString(),
1944-
expectIgnoredPods: sets.NewString(),
1942+
expectUnreadyPods: sets.New[string]("unscheduled"),
1943+
expectMissingPods: sets.New[string](),
1944+
expectIgnoredPods: sets.New[string](),
19451945
}, {
19461946
name: "ignore pods with deletion timestamps",
19471947
pods: []*v1.Pod{
@@ -1960,9 +1960,9 @@ func TestGroupPods(t *testing.T) {
19601960
},
19611961
resource: v1.ResourceCPU,
19621962
expectReadyPodCount: 0,
1963-
expectUnreadyPods: sets.NewString(),
1964-
expectMissingPods: sets.NewString(),
1965-
expectIgnoredPods: sets.NewString("deleted"),
1963+
expectUnreadyPods: sets.New[string](),
1964+
expectMissingPods: sets.New[string](),
1965+
expectIgnoredPods: sets.New[string]("deleted"),
19661966
}, {
19671967
name: "ignore pods in a failed state",
19681968
pods: []*v1.Pod{
@@ -1980,9 +1980,9 @@ func TestGroupPods(t *testing.T) {
19801980
},
19811981
resource: v1.ResourceCPU,
19821982
expectReadyPodCount: 0,
1983-
expectUnreadyPods: sets.NewString(),
1984-
expectMissingPods: sets.NewString(),
1985-
expectIgnoredPods: sets.NewString("failed"),
1983+
expectUnreadyPods: sets.New[string](),
1984+
expectMissingPods: sets.New[string](),
1985+
expectIgnoredPods: sets.New[string]("failed"),
19861986
},
19871987
}
19881988
for _, tc := range tests {

0 commit comments

Comments
 (0)