github.com/nmstate/kubernetes-nmstate@v0.82.0/pkg/enactmentstatus/conditions/counter.go (about) 1 /* 2 Copyright The Kubernetes NMState Authors. 3 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 */ 17 18 package conditions 19 20 import ( 21 "fmt" 22 23 corev1 "k8s.io/api/core/v1" 24 25 nmstate "github.com/nmstate/kubernetes-nmstate/api/shared" 26 nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1" 27 ) 28 29 type CountByConditionStatus map[corev1.ConditionStatus]int 30 31 type ConditionCount map[nmstate.ConditionType]CountByConditionStatus 32 33 func Count(enactments nmstatev1beta1.NodeNetworkConfigurationEnactmentList, policyGeneration int64) ConditionCount { 34 conditionCount := ConditionCount{} 35 for _, conditionType := range nmstate.NodeNetworkConfigurationEnactmentConditionTypes { 36 conditionCount[conditionType] = CountByConditionStatus{ 37 corev1.ConditionTrue: 0, 38 corev1.ConditionFalse: 0, 39 corev1.ConditionUnknown: 0, 40 } 41 for enactmentIndex := range enactments.Items { 42 enactment := enactments.Items[enactmentIndex] 43 condition := enactment.Status.Conditions.Find(conditionType) 44 // If there is a condition status and it's from the current policy update 45 if condition != nil && enactment.Status.PolicyGeneration == policyGeneration { 46 conditionCount[conditionType][condition.Status] += 1 47 } else { 48 conditionCount[conditionType][corev1.ConditionUnknown] += 1 49 } 50 } 51 } 52 return conditionCount 53 } 54 55 func (c ConditionCount) failed() CountByConditionStatus { 56 return c[nmstate.NodeNetworkConfigurationEnactmentConditionFailing] 57 } 58 func (c ConditionCount) progressing() CountByConditionStatus { 59 return c[nmstate.NodeNetworkConfigurationEnactmentConditionProgressing] 60 } 61 func (c ConditionCount) pending() CountByConditionStatus { 62 return c[nmstate.NodeNetworkConfigurationEnactmentConditionPending] 63 } 64 func (c ConditionCount) available() CountByConditionStatus { 65 return c[nmstate.NodeNetworkConfigurationEnactmentConditionAvailable] 66 } 67 func (c ConditionCount) aborted() CountByConditionStatus { 68 return c[nmstate.NodeNetworkConfigurationEnactmentConditionAborted] 69 } 70 71 func (c CountByConditionStatus) true() int { 72 return c[corev1.ConditionTrue] 73 } 74 75 func (c CountByConditionStatus) false() int { 76 return c[corev1.ConditionFalse] 77 } 78 79 func (c CountByConditionStatus) unknown() int { 80 return c[corev1.ConditionUnknown] 81 } 82 83 func (c ConditionCount) Failed() int { 84 return c.failed().true() 85 } 86 func (c ConditionCount) NotFailed() int { 87 return c.failed().false() 88 } 89 func (c ConditionCount) Progressing() int { 90 return c.progressing().true() 91 } 92 func (c ConditionCount) NotProgressing() int { 93 return c.progressing().false() 94 } 95 func (c ConditionCount) Pending() int { 96 return c.progressing().true() 97 } 98 func (c ConditionCount) NotPending() int { 99 return c.progressing().false() 100 } 101 func (c ConditionCount) Available() int { 102 return c.available().true() 103 } 104 func (c ConditionCount) NotAvailable() int { 105 return c.available().false() 106 } 107 func (c ConditionCount) Aborted() int { 108 return c.aborted().true() 109 } 110 func (c ConditionCount) NotAborted() int { 111 return c.aborted().false() 112 } 113 114 func (c ConditionCount) String() string { 115 return fmt.Sprintf( 116 "{failed: %s, progressing: %s, pending: %s, available: %s, aborted: %s}", 117 c.failed(), 118 c.progressing(), 119 c.pending(), 120 c.available(), 121 c.aborted(), 122 ) 123 } 124 125 func (c CountByConditionStatus) String() string { 126 return fmt.Sprintf("{true: %d, false: %d, unknown: %d}", c.true(), c.false(), c.unknown()) 127 }