Change:
tc.runTestWithController(t, controller, informerFactory) } <ins> func TestConvertDesiredReplicasWithRules(t *testing.T) { conversionTestCases := []struct { currentReplicas                  int32 desiredReplicas                  int32 hpaMinReplicas                   int32 hpaMaxReplicas                   int32 expectedConvertedDesiredReplicas int32 expectedCondition                string annotation                       string }{ { currentReplicas:                  5, desiredReplicas:                  7, hpaMinReplicas:                   3, hpaMaxReplicas:                   8, expectedConvertedDesiredReplicas: 7, expectedCondition:                "DesiredWithinRange", annotation:                       "prenormalized desired replicas within range", }, { currentReplicas:                  3, desiredReplicas:                  1, hpaMinReplicas:                   2, hpaMaxReplicas:                   8, expectedConvertedDesiredReplicas: 2, expectedCondition:                "TooFewReplicas", annotation:                       "prenormalized desired replicas < minReplicas", }, { currentReplicas:                  1, desiredReplicas:                  0, hpaMinReplicas:                   0, hpaMaxReplicas:                   10, expectedConvertedDesiredReplicas: 1, expectedCondition:                "TooFewReplicas", annotation:                       "1 is minLimit because hpaMinReplicas < 1", }, { currentReplicas:                  20, desiredReplicas:                  1000, hpaMinReplicas:                   1, hpaMaxReplicas:                   10, expectedConvertedDesiredReplicas: 10, expectedCondition:                "TooManyReplicas", annotation:                       "maxReplicas is the limit because maxReplicas < scaleUpLimit", }, { currentReplicas:                  3, desiredReplicas:                  1000, hpaMinReplicas:                   1, hpaMaxReplicas:                   2000, expectedConvertedDesiredReplicas: calculateScaleUpLimit(3), expectedCondition:                "ScaleUpLimit", annotation:                       "scaleUpLimit is the limit because scaleUpLimit < maxReplicas", }, } for _, ctc := range conversionTestCases { actualConvertedDesiredReplicas, actualCondition, _ := convertDesiredReplicasWithRules( ctc.currentReplicas, ctc.desiredReplicas, ctc.hpaMinReplicas, ctc.hpaMaxReplicas, ) assert.Equal(t, ctc.expectedConvertedDesiredReplicas, actualConvertedDesiredReplicas, ctc.annotation) assert.Equal(t, ctc.expectedCondition, actualCondition, ctc.annotation) } } </ins> // TODO: add more tests

Discussion:
We recently had 2 serious bugs caused by non-trivial logic calculating desiredReplicas in hpa reconcileAutoscaler method (, ). I think we should extract this part of logic (starting around ) to a separate function and make sure it's covered with unittests for different possible edge condition. The logic itself takes a few ints as input (calculated desiredReplicas, min/max replicas, currentReplicas, ...) to produce a single integer value and set some status conditions, making it very easily unittestable if we extract it to a separate function. WDYT
+100. It would be super unfortunate if we had yet another issue in this area of the code.
agreed. you've been going over these pieces of code recently, you want to tackle this?
Yes, I'd love to! Thanks for the ping. I'll tackle this this weekend :)