github.com/nmstate/kubernetes-nmstate@v0.82.0/pkg/enactmentstatus/conditions/conditions.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 "github.com/go-logr/logr" 26 27 "k8s.io/apimachinery/pkg/types" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 logf "sigs.k8s.io/controller-runtime/pkg/log" 30 31 nmstate "github.com/nmstate/kubernetes-nmstate/api/shared" 32 "github.com/nmstate/kubernetes-nmstate/pkg/enactmentstatus" 33 ) 34 35 type EnactmentConditions struct { 36 client client.Client 37 enactmentKey types.NamespacedName 38 logger logr.Logger 39 } 40 41 func New(cli client.Client, enactmentKey types.NamespacedName) EnactmentConditions { 42 conditions := EnactmentConditions{ 43 client: cli, 44 enactmentKey: enactmentKey, 45 logger: logf.Log.WithName("enactmentconditions").WithValues("enactment", enactmentKey.Name), 46 } 47 return conditions 48 } 49 50 func (ec *EnactmentConditions) NotifyGenerateFailure(err error) { 51 ec.logger.Info("NotifyGenerateFailure") 52 message := fmt.Sprintf("failure generating desiredState and capturedStates: %v", err) 53 err = ec.updateEnactmentConditions(SetFailedToConfigure, message) 54 if err != nil { 55 ec.logger.Error(err, "Error notifying state generate captures with failure") 56 } 57 } 58 59 func (ec *EnactmentConditions) NotifyProgressing() { 60 ec.logger.Info("NotifyProgressing") 61 err := ec.updateEnactmentConditions(SetProgressing, "Applying desired state") 62 if err != nil { 63 ec.logger.Error(err, "Error notifying state Progressing") 64 } 65 } 66 67 func (ec *EnactmentConditions) NotifyFailedToConfigure(failedErr error) { 68 ec.logger.Info("NotifyFailedToConfigure") 69 err := ec.updateEnactmentConditions(SetFailedToConfigure, failedErr.Error()) 70 if err != nil { 71 ec.logger.Error(err, "Error notifying state FailingToConfigure") 72 } 73 } 74 75 func (ec *EnactmentConditions) NotifyAborted(failedErr error) { 76 ec.logger.Info("NotifyConfigurationAborted") 77 err := ec.updateEnactmentConditions(SetConfigurationAborted, failedErr.Error()) 78 if err != nil { 79 ec.logger.Error(err, "Error notifying state ConfigurationAborted") 80 } 81 } 82 83 func (ec *EnactmentConditions) NotifySuccess() { 84 ec.logger.Info("NotifySuccess") 85 err := ec.updateEnactmentConditions(SetSuccess, "successfully reconciled") 86 if err != nil { 87 ec.logger.Error(err, "Error notifying state Success") 88 } 89 } 90 91 func (ec *EnactmentConditions) NotifyPending() { 92 ec.logger.Info("NotifyPending") 93 err := ec.updateEnactmentConditions(SetPending, "Waiting for progressing nodes to finish") 94 if err != nil { 95 ec.logger.Error(err, "Error notifying state Pending") 96 } 97 } 98 99 func (ec *EnactmentConditions) Reset() { 100 ec.logger.Info("Reset") 101 err := ec.updateEnactmentConditions(func(conditionList *nmstate.ConditionList, message string) { 102 *conditionList = nil 103 }, "") 104 if err != nil { 105 ec.logger.Error(err, "Error resetting conditions") 106 } 107 } 108 109 func (ec *EnactmentConditions) updateEnactmentConditions( 110 conditionsSetter func(*nmstate.ConditionList, string), 111 message string, 112 ) error { 113 return enactmentstatus.Update(ec.client, ec.enactmentKey, 114 func(status *nmstate.NodeNetworkConfigurationEnactmentStatus) { 115 conditionsSetter(&status.Conditions, message) 116 }) 117 } 118 119 func SetFailedToConfigure(conditions *nmstate.ConditionList, message string) { 120 SetFailed(conditions, nmstate.NodeNetworkConfigurationEnactmentConditionFailedToConfigure, message) 121 } 122 123 func SetFailed(conditions *nmstate.ConditionList, reason nmstate.ConditionReason, message string) { 124 conditions.Set( 125 nmstate.NodeNetworkConfigurationEnactmentConditionFailing, 126 corev1.ConditionTrue, 127 reason, 128 message, 129 ) 130 conditions.Set( 131 nmstate.NodeNetworkConfigurationEnactmentConditionAvailable, 132 corev1.ConditionFalse, 133 reason, 134 "", 135 ) 136 conditions.Set( 137 nmstate.NodeNetworkConfigurationEnactmentConditionProgressing, 138 corev1.ConditionFalse, 139 reason, 140 "", 141 ) 142 conditions.Set( 143 nmstate.NodeNetworkConfigurationEnactmentConditionPending, 144 corev1.ConditionFalse, 145 reason, 146 "", 147 ) 148 conditions.Set( 149 nmstate.NodeNetworkConfigurationEnactmentConditionAborted, 150 corev1.ConditionFalse, 151 nmstate.NodeNetworkConfigurationEnactmentConditionSuccessfullyConfigured, 152 "", 153 ) 154 } 155 156 func SetConfigurationAborted(conditions *nmstate.ConditionList, message string) { 157 SetAborted(conditions, nmstate.NodeNetworkConfigurationEnactmentConditionConfigurationAborted, message) 158 } 159 160 func SetAborted(conditions *nmstate.ConditionList, reason nmstate.ConditionReason, message string) { 161 conditions.Set( 162 nmstate.NodeNetworkConfigurationEnactmentConditionFailing, 163 corev1.ConditionFalse, 164 reason, 165 "", 166 ) 167 conditions.Set( 168 nmstate.NodeNetworkConfigurationEnactmentConditionAvailable, 169 corev1.ConditionFalse, 170 reason, 171 "", 172 ) 173 conditions.Set( 174 nmstate.NodeNetworkConfigurationEnactmentConditionProgressing, 175 corev1.ConditionFalse, 176 reason, 177 "", 178 ) 179 conditions.Set( 180 nmstate.NodeNetworkConfigurationEnactmentConditionPending, 181 corev1.ConditionFalse, 182 reason, 183 "", 184 ) 185 conditions.Set( 186 nmstate.NodeNetworkConfigurationEnactmentConditionAborted, 187 corev1.ConditionTrue, 188 reason, 189 message, 190 ) 191 } 192 193 func SetSuccess(conditions *nmstate.ConditionList, message string) { 194 conditions.Set( 195 nmstate.NodeNetworkConfigurationEnactmentConditionAvailable, 196 corev1.ConditionTrue, 197 nmstate.NodeNetworkConfigurationEnactmentConditionSuccessfullyConfigured, 198 message, 199 ) 200 conditions.Set( 201 nmstate.NodeNetworkConfigurationEnactmentConditionFailing, 202 corev1.ConditionFalse, 203 nmstate.NodeNetworkConfigurationEnactmentConditionSuccessfullyConfigured, 204 "", 205 ) 206 conditions.Set( 207 nmstate.NodeNetworkConfigurationEnactmentConditionProgressing, 208 corev1.ConditionFalse, 209 nmstate.NodeNetworkConfigurationEnactmentConditionSuccessfullyConfigured, 210 "", 211 ) 212 conditions.Set( 213 nmstate.NodeNetworkConfigurationEnactmentConditionPending, 214 corev1.ConditionFalse, 215 nmstate.NodeNetworkConfigurationEnactmentConditionSuccessfullyConfigured, 216 "", 217 ) 218 conditions.Set( 219 nmstate.NodeNetworkConfigurationEnactmentConditionAborted, 220 corev1.ConditionFalse, 221 nmstate.NodeNetworkConfigurationEnactmentConditionSuccessfullyConfigured, 222 "", 223 ) 224 } 225 226 func SetProgressing(conditions *nmstate.ConditionList, message string) { 227 conditions.Set( 228 nmstate.NodeNetworkConfigurationEnactmentConditionProgressing, 229 corev1.ConditionTrue, 230 nmstate.NodeNetworkConfigurationEnactmentConditionConfigurationProgressing, 231 message, 232 ) 233 conditions.Set( 234 nmstate.NodeNetworkConfigurationEnactmentConditionFailing, 235 corev1.ConditionUnknown, 236 nmstate.NodeNetworkConfigurationEnactmentConditionConfigurationProgressing, 237 "", 238 ) 239 conditions.Set( 240 nmstate.NodeNetworkConfigurationEnactmentConditionAvailable, 241 corev1.ConditionUnknown, 242 nmstate.NodeNetworkConfigurationEnactmentConditionConfigurationProgressing, 243 "", 244 ) 245 conditions.Set( 246 nmstate.NodeNetworkConfigurationEnactmentConditionPending, 247 corev1.ConditionFalse, 248 nmstate.NodeNetworkConfigurationEnactmentConditionConfigurationProgressing, 249 "", 250 ) 251 conditions.Set( 252 nmstate.NodeNetworkConfigurationEnactmentConditionAborted, 253 corev1.ConditionFalse, 254 nmstate.NodeNetworkConfigurationEnactmentConditionConfigurationProgressing, 255 "", 256 ) 257 } 258 259 func SetPending(conditions *nmstate.ConditionList, message string) { 260 conditions.Set( 261 nmstate.NodeNetworkConfigurationEnactmentConditionPending, 262 corev1.ConditionTrue, 263 nmstate.NodeNetworkConfigurationEnactmentConditionMaxUnavailableLimitReached, 264 message, 265 ) 266 conditions.Set( 267 nmstate.NodeNetworkConfigurationEnactmentConditionAborted, 268 corev1.ConditionFalse, 269 nmstate.NodeNetworkConfigurationEnactmentConditionMaxUnavailableLimitReached, 270 "", 271 ) 272 conditions.Set( 273 nmstate.NodeNetworkConfigurationEnactmentConditionProgressing, 274 corev1.ConditionFalse, 275 nmstate.NodeNetworkConfigurationEnactmentConditionMaxUnavailableLimitReached, 276 message, 277 ) 278 conditions.Set( 279 nmstate.NodeNetworkConfigurationEnactmentConditionFailing, 280 corev1.ConditionFalse, 281 nmstate.NodeNetworkConfigurationEnactmentConditionMaxUnavailableLimitReached, 282 "", 283 ) 284 conditions.Set( 285 nmstate.NodeNetworkConfigurationEnactmentConditionAvailable, 286 corev1.ConditionFalse, 287 nmstate.NodeNetworkConfigurationEnactmentConditionMaxUnavailableLimitReached, 288 "", 289 ) 290 }