github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/apps/v1alpha1/opsrequest_conditions.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 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 v1alpha1 18 19 import ( 20 "fmt" 21 22 "k8s.io/apimachinery/pkg/api/meta" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 ) 25 26 const ( 27 // condition types 28 ConditionTypeCancelled = "Cancelled" 29 ConditionTypeProgressing = "Progressing" 30 ConditionTypeValidated = "Validated" 31 ConditionTypeSucceed = "Succeed" 32 ConditionTypeFailed = "Failed" 33 ConditionTypeRestarting = "Restarting" 34 ConditionTypeVerticalScaling = "VerticalScaling" 35 ConditionTypeHorizontalScaling = "HorizontalScaling" 36 ConditionTypeVolumeExpanding = "VolumeExpanding" 37 ConditionTypeReconfigure = "Reconfigure" 38 ConditionTypeSwitchover = "Switchover" 39 ConditionTypeStop = "Stopping" 40 ConditionTypeStart = "Starting" 41 ConditionTypeVersionUpgrading = "VersionUpgrading" 42 ConditionTypeExpose = "Exposing" 43 ConditionTypeDataScript = "ExecuteDataScript" 44 ConditionTypeBackup = "Backup" 45 46 // condition and event reasons 47 48 ReasonReconfigurePersisting = "ReconfigurePersisting" 49 ReasonReconfigurePersisted = "ReconfigurePersisted" 50 ReasonReconfigureFailed = "ReconfigureFailed" 51 ReasonReconfigureRestartFailed = "ReconfigureRestartFailed" 52 ReasonReconfigureRestart = "ReconfigureRestarted" 53 ReasonReconfigureNoChanged = "ReconfigureNoChanged" 54 ReasonReconfigureSucceed = "ReconfigureSucceed" 55 ReasonReconfigureRunning = "ReconfigureRunning" 56 ReasonClusterPhaseMismatch = "ClusterPhaseMismatch" 57 ReasonOpsTypeNotSupported = "OpsTypeNotSupported" 58 ReasonValidateFailed = "ValidateFailed" 59 ReasonClusterNotFound = "ClusterNotFound" 60 ReasonOpsRequestFailed = "OpsRequestFailed" 61 ReasonOpsCanceling = "Canceling" 62 ReasonOpsCancelFailed = "CancelFailed" 63 ReasonOpsCancelSucceed = "CancelSucceed" 64 ) 65 66 func (r *OpsRequest) SetStatusCondition(condition metav1.Condition) { 67 meta.SetStatusCondition(&r.Status.Conditions, condition) 68 } 69 70 // NewProgressingCondition the controller is progressing the OpsRequest 71 func NewProgressingCondition(ops *OpsRequest) *metav1.Condition { 72 return &metav1.Condition{ 73 Type: ConditionTypeProgressing, 74 Status: metav1.ConditionTrue, 75 Reason: "OpsRequestProgressingStarted", 76 LastTransitionTime: metav1.Now(), 77 Message: fmt.Sprintf("Start to process the OpsRequest: %s in Cluster: %s", 78 ops.Name, ops.Spec.ClusterRef), 79 } 80 } 81 82 // NewCancelingCondition the controller is canceling the OpsRequest 83 func NewCancelingCondition(ops *OpsRequest) *metav1.Condition { 84 return &metav1.Condition{ 85 Type: ConditionTypeCancelled, 86 Status: metav1.ConditionFalse, 87 Reason: ReasonOpsCanceling, 88 LastTransitionTime: metav1.Now(), 89 Message: fmt.Sprintf(`Start to cancel the OpsRequest "%s" in Cluster: "%s"`, 90 ops.Name, ops.Spec.ClusterRef), 91 } 92 } 93 94 // NewCancelFailedCondition creates a condition for canceling failed. 95 func NewCancelFailedCondition(ops *OpsRequest, err error) *metav1.Condition { 96 msg := fmt.Sprintf(`Failed to cancel OpsRequest "%s"`, ops.Name) 97 if err != nil { 98 msg = err.Error() 99 } 100 return &metav1.Condition{ 101 Type: ConditionTypeCancelled, 102 Status: metav1.ConditionTrue, 103 Reason: ReasonOpsCancelFailed, 104 LastTransitionTime: metav1.Now(), 105 Message: msg, 106 } 107 } 108 109 // NewCancelSucceedCondition creates a condition for canceling successfully. 110 func NewCancelSucceedCondition(opsName string) *metav1.Condition { 111 return &metav1.Condition{ 112 Type: ConditionTypeCancelled, 113 Status: metav1.ConditionTrue, 114 Reason: ReasonOpsCancelSucceed, 115 LastTransitionTime: metav1.Now(), 116 Message: fmt.Sprintf(`Cancel OpsRequest "%s" successfully`, opsName), 117 } 118 } 119 120 // NewValidatePassedCondition creates a condition for operation validation to pass. 121 func NewValidatePassedCondition(opsRequestName string) *metav1.Condition { 122 return &metav1.Condition{ 123 Type: ConditionTypeValidated, 124 Status: metav1.ConditionTrue, 125 Reason: "ValidateOpsRequestPassed", 126 LastTransitionTime: metav1.Now(), 127 Message: fmt.Sprintf("OpsRequest: %s is validated", opsRequestName), 128 } 129 } 130 131 // NewValidateFailedCondition creates a condition for operation validation failure. 132 func NewValidateFailedCondition(reason, message string) *metav1.Condition { 133 return &metav1.Condition{ 134 Type: ConditionTypeValidated, 135 Status: metav1.ConditionFalse, 136 Reason: reason, 137 LastTransitionTime: metav1.Now(), 138 Message: message, 139 } 140 } 141 142 // NewFailedCondition creates a condition that the OpsRequest processing failed 143 func NewFailedCondition(ops *OpsRequest, err error) *metav1.Condition { 144 msg := fmt.Sprintf("Failed to process OpsRequest: %s in cluster: %s", ops.Name, ops.Spec.ClusterRef) 145 if err != nil { 146 msg = err.Error() 147 } 148 return &metav1.Condition{ 149 Type: ConditionTypeFailed, 150 Status: metav1.ConditionFalse, 151 Reason: ReasonOpsRequestFailed, 152 LastTransitionTime: metav1.Now(), 153 Message: msg, 154 } 155 } 156 157 // NewSucceedCondition creates a condition that the controller has successfully processed the OpsRequest 158 func NewSucceedCondition(ops *OpsRequest) *metav1.Condition { 159 return &metav1.Condition{ 160 Type: ConditionTypeSucceed, 161 Status: metav1.ConditionTrue, 162 Reason: "OpsRequestProcessedSuccessfully", 163 LastTransitionTime: metav1.Now(), 164 Message: fmt.Sprintf("Successfully processed the OpsRequest: %s in Cluster: %s", 165 ops.Name, ops.Spec.ClusterRef), 166 } 167 } 168 169 // NewRestartingCondition creates a condition that the operation starts to restart components 170 func NewRestartingCondition(ops *OpsRequest) *metav1.Condition { 171 return &metav1.Condition{ 172 Type: ConditionTypeRestarting, 173 Status: metav1.ConditionTrue, 174 Reason: "RestartStarted", 175 LastTransitionTime: metav1.Now(), 176 Message: fmt.Sprintf("Start to restart database in Cluster: %s", ops.Spec.ClusterRef), 177 } 178 } 179 180 // NewSwitchoveringCondition creates a condition that the operation starts to switchover components 181 func NewSwitchoveringCondition(generation int64, message string) *metav1.Condition { 182 return &metav1.Condition{ 183 Type: ConditionTypeSwitchover, 184 Status: metav1.ConditionTrue, 185 Reason: "SwitchoverStarted", 186 LastTransitionTime: metav1.Now(), 187 Message: message, 188 ObservedGeneration: generation, 189 } 190 } 191 192 // NewVerticalScalingCondition creates a condition that the OpsRequest starts to vertical scale cluster 193 func NewVerticalScalingCondition(ops *OpsRequest) *metav1.Condition { 194 return &metav1.Condition{ 195 Type: ConditionTypeVerticalScaling, 196 Status: metav1.ConditionTrue, 197 Reason: "VerticalScalingStarted", 198 LastTransitionTime: metav1.Now(), 199 Message: fmt.Sprintf("Start to vertical scale resources in Cluster: %s", ops.Spec.ClusterRef), 200 } 201 } 202 203 // NewHorizontalScalingCondition creates a condition that the OpsRequest starts to horizontal scale cluster 204 func NewHorizontalScalingCondition(ops *OpsRequest) *metav1.Condition { 205 return &metav1.Condition{ 206 Type: ConditionTypeHorizontalScaling, 207 Status: metav1.ConditionTrue, 208 Reason: "HorizontalScalingStarted", 209 LastTransitionTime: metav1.Now(), 210 Message: fmt.Sprintf("Start to horizontal scale replicas in Cluster: %s", ops.Spec.ClusterRef), 211 } 212 } 213 214 // NewVolumeExpandingCondition creates a condition that the OpsRequest starts to expand volume 215 func NewVolumeExpandingCondition(ops *OpsRequest) *metav1.Condition { 216 return &metav1.Condition{ 217 Type: ConditionTypeVolumeExpanding, 218 Status: metav1.ConditionTrue, 219 Reason: "VolumeExpansionStarted", 220 LastTransitionTime: metav1.Now(), 221 Message: fmt.Sprintf("Start to expand the volumes in Cluster: %s", ops.Spec.ClusterRef), 222 } 223 } 224 225 func NewExposingCondition(ops *OpsRequest) *metav1.Condition { 226 return &metav1.Condition{ 227 Type: ConditionTypeExpose, 228 Status: metav1.ConditionTrue, 229 Reason: "ExposeStarted", 230 LastTransitionTime: metav1.Now(), 231 Message: fmt.Sprintf("Start to expose the services in Cluster: %s", ops.Spec.ClusterRef), 232 } 233 } 234 235 // NewUpgradingCondition creates a condition that the OpsRequest starts to upgrade the cluster version 236 func NewUpgradingCondition(ops *OpsRequest) *metav1.Condition { 237 return &metav1.Condition{ 238 Type: ConditionTypeVersionUpgrading, 239 Status: metav1.ConditionTrue, 240 Reason: "VersionUpgradeStarted", 241 LastTransitionTime: metav1.Now(), 242 Message: fmt.Sprintf("Start to upgrade the version in Cluster: %s", ops.Spec.ClusterRef), 243 } 244 } 245 246 // NewStopCondition creates a condition that the OpsRequest starts to stop the cluster. 247 func NewStopCondition(ops *OpsRequest) *metav1.Condition { 248 return &metav1.Condition{ 249 Type: ConditionTypeStop, 250 Status: metav1.ConditionTrue, 251 Reason: "StopStarted", 252 LastTransitionTime: metav1.Now(), 253 Message: fmt.Sprintf("Start to stop the Cluster: %s", ops.Spec.ClusterRef), 254 } 255 } 256 257 // NewStartCondition creates a condition that the OpsRequest starts the cluster. 258 func NewStartCondition(ops *OpsRequest) *metav1.Condition { 259 return &metav1.Condition{ 260 Type: ConditionTypeStart, 261 Status: metav1.ConditionTrue, 262 Reason: "StartCluster", 263 LastTransitionTime: metav1.Now(), 264 Message: fmt.Sprintf("Start the Cluster: %s", ops.Spec.ClusterRef), 265 } 266 } 267 268 // NewReconfigureCondition creates a condition that the OpsRequest updating component configuration 269 func NewReconfigureCondition(ops *OpsRequest) *metav1.Condition { 270 return &metav1.Condition{ 271 Type: ConditionTypeReconfigure, 272 Status: metav1.ConditionTrue, 273 Reason: "ReconfigureStarted", 274 LastTransitionTime: metav1.Now(), 275 Message: fmt.Sprintf("Start to reconfigure in Cluster: %s, Component: %s", 276 ops.Spec.ClusterRef, 277 ops.Spec.Reconfigure.ComponentName), 278 } 279 } 280 281 func NewDataScriptCondition(ops *OpsRequest) *metav1.Condition { 282 return newOpsCondition(ops, ConditionTypeDataScript, "DataScriptStarted", fmt.Sprintf("Start to execute data script in Cluster: %s", ops.Spec.ClusterRef)) 283 } 284 285 func newOpsCondition(ops *OpsRequest, condType, reason, message string) *metav1.Condition { 286 return &metav1.Condition{ 287 Type: condType, 288 Status: metav1.ConditionTrue, 289 Reason: reason, 290 LastTransitionTime: metav1.Now(), 291 Message: message, 292 } 293 } 294 295 // NewReconfigureRunningCondition creates a condition that the OpsRequest reconfigure workflow 296 func NewReconfigureRunningCondition(ops *OpsRequest, conditionType string, configSpecName string, info ...string) *metav1.Condition { 297 status := metav1.ConditionTrue 298 if conditionType == ReasonReconfigureFailed { 299 status = metav1.ConditionFalse 300 } 301 message := fmt.Sprintf("Reconfiguring in Cluster: %s, Component: %s, ConfigSpec: %s", 302 ops.Spec.ClusterRef, 303 ops.Spec.Reconfigure.ComponentName, 304 configSpecName) 305 if len(info) > 0 { 306 message = message + ", info: " + info[0] 307 } 308 return &metav1.Condition{ 309 Type: conditionType, 310 Status: status, 311 Reason: conditionType, 312 LastTransitionTime: metav1.Now(), 313 Message: message, 314 } 315 } 316 317 // NewReconfigureFailedCondition creates a condition for the failed reconfigure. 318 func NewReconfigureFailedCondition(ops *OpsRequest, err error) *metav1.Condition { 319 var msg string 320 if err != nil { 321 msg = err.Error() 322 } else { 323 msg = fmt.Sprintf("Failed to reconfigure: %s in cluster: %s", ops.Name, ops.Spec.ClusterRef) 324 } 325 return &metav1.Condition{ 326 Type: ReasonReconfigureFailed, 327 Status: metav1.ConditionFalse, 328 Reason: "ReconfigureFailed", 329 LastTransitionTime: metav1.Now(), 330 Message: msg, 331 } 332 } 333 334 // NewBackupCondition creates a condition that the OpsRequest backup the cluster. 335 func NewBackupCondition(ops *OpsRequest) *metav1.Condition { 336 return &metav1.Condition{ 337 Type: ConditionTypeBackup, 338 Status: metav1.ConditionTrue, 339 Reason: "BackupStarted", 340 LastTransitionTime: metav1.Now(), 341 Message: fmt.Sprintf("Start to backup the Cluster: %s", ops.Spec.ClusterRef), 342 } 343 }