From 48cedde345fd68511a95467d542bc7f356bfdfe0 Mon Sep 17 00:00:00 2001 From: ajanikow <12255597+ajanikow@users.noreply.github.com> Date: Fri, 15 Dec 2023 09:29:27 +0000 Subject: [PATCH] [Improvement] Extract api.Condition Or function --- pkg/deployment/resources/pod_inspector.go | 28 ++++++++++------------- pkg/util/refs.go | 20 ++++++++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/pkg/deployment/resources/pod_inspector.go b/pkg/deployment/resources/pod_inspector.go index 5696f8d9a..a131cf846 100644 --- a/pkg/deployment/resources/pod_inspector.go +++ b/pkg/deployment/resources/pod_inspector.go @@ -339,9 +339,11 @@ func (r *Resources) InspectPods(ctx context.Context, cachedStatus inspectorInter if k8sutil.IsPodReady(pod) && k8sutil.AreContainersReady(pod, coreContainers) { // Pod is now ready - if anyOf(memberStatus.Conditions.Update(api.ConditionTypeReady, true, "Pod Ready", ""), + if util.Or( + memberStatus.Conditions.Update(api.ConditionTypeReady, true, "Pod Ready", ""), memberStatus.Conditions.Update(api.ConditionTypeStarted, true, "Pod Started", ""), - memberStatus.Conditions.Update(api.ConditionTypeServing, true, "Pod Serving", "")) { + memberStatus.Conditions.Update(api.ConditionTypeServing, true, "Pod Serving", ""), + ) { log.Str("pod-name", pod.GetName()).Debug("Updating member condition Ready, Started & Serving to true") if status.Topology.IsTopologyOwned(memberStatus.Topology) { @@ -363,16 +365,20 @@ func (r *Resources) InspectPods(ctx context.Context, cachedStatus inspectorInter } } else if k8sutil.AreContainersReady(pod, coreContainers) { // Pod is not ready, but core containers are fine - if anyOf(memberStatus.Conditions.Update(api.ConditionTypeReady, false, "Pod Not Ready", ""), - memberStatus.Conditions.Update(api.ConditionTypeServing, true, "Pod is still serving", "")) { + if util.Or( + memberStatus.Conditions.Update(api.ConditionTypeReady, false, "Pod Not Ready", ""), + memberStatus.Conditions.Update(api.ConditionTypeServing, true, "Pod is still serving", ""), + ) { log.Str("pod-name", pod.GetName()).Debug("Updating member condition Ready to false, while all core containers are ready") updateMemberStatusNeeded = true nextInterval = nextInterval.ReduceTo(recheckSoonPodInspectorInterval) } } else { // Pod is not ready - if anyOf(memberStatus.Conditions.Update(api.ConditionTypeReady, false, "Pod Not Ready", ""), - memberStatus.Conditions.Update(api.ConditionTypeServing, false, "Pod Core containers are not ready", strings.Join(coreContainers, ", "))) { + if util.Or( + memberStatus.Conditions.Update(api.ConditionTypeReady, false, "Pod Not Ready", ""), + memberStatus.Conditions.Update(api.ConditionTypeServing, false, "Pod Core containers are not ready", strings.Join(coreContainers, ", ")), + ) { log.Str("pod-name", pod.GetName()).Debug("Updating member condition Ready & Serving to false") updateMemberStatusNeeded = true nextInterval = nextInterval.ReduceTo(recheckSoonPodInspectorInterval) @@ -566,13 +572,3 @@ func removeLabel(labels map[string]string, key string) map[string]string { return labels } - -func anyOf(bools ...bool) bool { - for _, b := range bools { - if b { - return true - } - } - - return false -} diff --git a/pkg/util/refs.go b/pkg/util/refs.go index d529e7caf..e426f2be7 100644 --- a/pkg/util/refs.go +++ b/pkg/util/refs.go @@ -105,3 +105,23 @@ func CheckConditionalP1Nil[T, P1 interface{}](in ConditionalP1Function[T, P1], p return nil } + +func Or(in ...bool) bool { + for _, v := range in { + if v { + return true + } + } + + return false +} + +func And(in ...bool) bool { + for _, v := range in { + if !v { + return false + } + } + + return len(in) > 0 +}