Skip to content

Commit 8edbb0c

Browse files
committed
Move lowestNonZeroRetryAfterSeconds to util
Signed-off-by: Bingtan Lu <[email protected]>
1 parent 5ff76fb commit 8edbb0c

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

internal/controllers/topology/cluster/scope/hookresponsetracker.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
2525
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
26+
"sigs.k8s.io/cluster-api/util"
2627
)
2728

2829
// HookResponseTracker is a helper to capture the responses of the various lifecycle hooks.
@@ -48,7 +49,7 @@ func (h *HookResponseTracker) AggregateRetryAfter() time.Duration {
4849
res := int32(0)
4950
for _, resp := range h.responses {
5051
if retryResponse, ok := resp.(runtimehooksv1.RetryResponseObject); ok {
51-
res = lowestNonZeroRetryAfterSeconds(res, retryResponse.GetRetryAfterSeconds())
52+
res = util.LowestNonZeroInt32(res, retryResponse.GetRetryAfterSeconds())
5253
}
5354
}
5455
return time.Duration(res) * time.Second
@@ -74,16 +75,3 @@ func (h *HookResponseTracker) AggregateMessage() string {
7475
}
7576
return strings.Join(hookAndMessages, "; ")
7677
}
77-
78-
func lowestNonZeroRetryAfterSeconds(i, j int32) int32 {
79-
if i == 0 {
80-
return j
81-
}
82-
if j == 0 {
83-
return i
84-
}
85-
if i < j {
86-
return i
87-
}
88-
return j
89-
}

internal/runtime/client/client.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func aggregateSuccessfulResponses(aggregatedResponse runtimehooksv1.ResponseObje
263263
for _, resp := range responses {
264264
aggregatedRetryResponse, ok := aggregatedResponse.(runtimehooksv1.RetryResponseObject)
265265
if ok {
266-
aggregatedRetryResponse.SetRetryAfterSeconds(lowestNonZeroRetryAfterSeconds(
266+
aggregatedRetryResponse.SetRetryAfterSeconds(util.LowestNonZeroInt32(
267267
aggregatedRetryResponse.GetRetryAfterSeconds(),
268268
resp.(runtimehooksv1.RetryResponseObject).GetRetryAfterSeconds(),
269269
))
@@ -275,20 +275,6 @@ func aggregateSuccessfulResponses(aggregatedResponse runtimehooksv1.ResponseObje
275275
aggregatedResponse.SetMessage(strings.Join(messages, ", "))
276276
}
277277

278-
// lowestNonZeroRetryAfterSeconds returns the lowest non-zero value of the two provided values.
279-
func lowestNonZeroRetryAfterSeconds(i, j int32) int32 {
280-
if i == 0 {
281-
return j
282-
}
283-
if j == 0 {
284-
return i
285-
}
286-
if i < j {
287-
return i
288-
}
289-
return j
290-
}
291-
292278
// CallExtension makes the call to the extension with the given name.
293279
// The response object passed will be updated with the response of the call.
294280
// An error is returned if the extension is not compatible with the hook.

util/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,20 @@ func LowestNonZeroResult(i, j ctrl.Result) ctrl.Result {
593593
}
594594
}
595595

596+
// LowestNonZeroInt32 returns the lowest non-zero value of the two provided values.
597+
func LowestNonZeroInt32(i, j int32) int32 {
598+
if i == 0 {
599+
return j
600+
}
601+
if j == 0 {
602+
return i
603+
}
604+
if i < j {
605+
return i
606+
}
607+
return j
608+
}
609+
596610
// IsNil returns an error if the passed interface is equal to nil or if it has an interface value of nil.
597611
func IsNil(i interface{}) bool {
598612
if i == nil {

0 commit comments

Comments
 (0)