sigs.k8s.io/cluster-api@v1.7.1/exp/topology/scope/hookresponsetracker.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package scope 18 19 import ( 20 "fmt" 21 "strings" 22 "time" 23 24 runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog" 25 runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" 26 "sigs.k8s.io/cluster-api/util" 27 ) 28 29 // HookResponseTracker is a helper to capture the responses of the various lifecycle hooks. 30 type HookResponseTracker struct { 31 responses map[string]runtimehooksv1.ResponseObject 32 } 33 34 // NewHookResponseTracker returns a new HookResponseTracker. 35 func NewHookResponseTracker() *HookResponseTracker { 36 return &HookResponseTracker{ 37 responses: map[string]runtimehooksv1.ResponseObject{}, 38 } 39 } 40 41 // Add add the response of a hook to the tracker. 42 func (h *HookResponseTracker) Add(hook runtimecatalog.Hook, response runtimehooksv1.ResponseObject) { 43 hookName := runtimecatalog.HookName(hook) 44 h.responses[hookName] = response 45 } 46 47 // IsBlocking returns true if the hook returned a blocking response. 48 // If the hook is not called or did not return a blocking response it returns false. 49 func (h *HookResponseTracker) IsBlocking(hook runtimecatalog.Hook) bool { 50 hookName := runtimecatalog.HookName(hook) 51 response, ok := h.responses[hookName] 52 if !ok { 53 return false 54 } 55 retryableResponse, ok := response.(runtimehooksv1.RetryResponseObject) 56 if !ok { 57 // Not a retryable response. Cannot be blocking. 58 return false 59 } 60 if retryableResponse.GetRetryAfterSeconds() == 0 { 61 // Not a blocking response. 62 return false 63 } 64 return true 65 } 66 67 // AggregateRetryAfter calculates the lowest non-zero retryAfterSeconds time from all the tracked responses. 68 func (h *HookResponseTracker) AggregateRetryAfter() time.Duration { 69 res := int32(0) 70 for _, resp := range h.responses { 71 if retryResponse, ok := resp.(runtimehooksv1.RetryResponseObject); ok { 72 res = util.LowestNonZeroInt32(res, retryResponse.GetRetryAfterSeconds()) 73 } 74 } 75 return time.Duration(res) * time.Second 76 } 77 78 // AggregateMessage returns a human friendly message about the blocking status of hooks. 79 func (h *HookResponseTracker) AggregateMessage() string { 80 blockingHooks := map[string]string{} 81 for hook, resp := range h.responses { 82 if retryResponse, ok := resp.(runtimehooksv1.RetryResponseObject); ok { 83 if retryResponse.GetRetryAfterSeconds() != 0 { 84 blockingHooks[hook] = resp.GetMessage() 85 } 86 } 87 } 88 if len(blockingHooks) == 0 { 89 return "" 90 } 91 92 hookAndMessages := []string{} 93 for hook, message := range blockingHooks { 94 hookAndMessages = append(hookAndMessages, fmt.Sprintf("hook %q is blocking: %s", hook, message)) 95 } 96 return strings.Join(hookAndMessages, "; ") 97 }