github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/operations/type.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package operations 21 22 import ( 23 "time" 24 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/client-go/tools/record" 27 "sigs.k8s.io/controller-runtime/pkg/client" 28 29 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 30 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 31 ) 32 33 type OpsHandler interface { 34 // Action The action duration should be short. if it fails, it will be reconciled by the OpsRequest controller. 35 // Do not patch OpsRequest status in this function with k8s client, just modify the status of ops. 36 // The opsRequest controller will patch it to the k8s apiServer. 37 Action(reqCtx intctrlutil.RequestCtx, cli client.Client, opsResource *OpsResource) error 38 39 // ReconcileAction loops till the operation is completed. 40 // return OpsRequest.status.phase and requeueAfter time. 41 ReconcileAction(reqCtx intctrlutil.RequestCtx, cli client.Client, opsResource *OpsResource) (appsv1alpha1.OpsPhase, time.Duration, error) 42 43 // ActionStartedCondition appends to OpsRequest.status.conditions when start performing Action function 44 ActionStartedCondition(reqCtx intctrlutil.RequestCtx, cli client.Client, opsRes *OpsResource) (*metav1.Condition, error) 45 46 // SaveLastConfiguration saves last configuration to the OpsRequest.status.lastConfiguration, 47 // and this method will be executed together when opsRequest in running. 48 SaveLastConfiguration(reqCtx intctrlutil.RequestCtx, cli client.Client, opsResource *OpsResource) error 49 } 50 51 type OpsBehaviour struct { 52 FromClusterPhases []appsv1alpha1.ClusterPhase 53 54 // ToClusterPhase indicates that the cluster will enter this phase during the operation. 55 ToClusterPhase appsv1alpha1.ClusterPhase 56 57 // ProcessingReasonInClusterCondition indicates the reason of the condition that type is "OpsRequestProcessed" in Cluster.Status.Conditions and 58 // is only valid when ToClusterPhase is not empty. it will indicate what operation the cluster is doing and 59 // will be displayed of "kbcli cluster list". 60 ProcessingReasonInClusterCondition string 61 62 // CancelFunc this function defines the cancel action and does not patch/update the opsRequest by client-go in here. 63 // only update the opsRequest object, then opsRequest controller will update uniformly. 64 CancelFunc func(reqCtx intctrlutil.RequestCtx, cli client.Client, opsResource *OpsResource) error 65 66 OpsHandler OpsHandler 67 } 68 69 type OpsResource struct { 70 OpsRequest *appsv1alpha1.OpsRequest 71 Cluster *appsv1alpha1.Cluster 72 Recorder record.EventRecorder 73 ToClusterPhase appsv1alpha1.ClusterPhase 74 } 75 76 type OpsManager struct { 77 OpsMap map[appsv1alpha1.OpsType]OpsBehaviour 78 } 79 80 type progressResource struct { 81 // opsMessageKey progress message key of specified OpsType, it is a verb and will form the message of progressDetail 82 // such as "vertical scale" of verticalScaling OpsRequest. 83 opsMessageKey string 84 clusterComponent *appsv1alpha1.ClusterComponentSpec 85 clusterComponentDef *appsv1alpha1.ClusterComponentDefinition 86 opsIsCompleted bool 87 } 88 89 const ( 90 // ProcessingReasonHorizontalScaling is the reason of the "OpsRequestProcessed" condition for the horizontal scaling opsRequest processing in cluster. 91 ProcessingReasonHorizontalScaling = "HorizontalScaling" 92 // ProcessingReasonVerticalScaling is the reason of the "OpsRequestProcessed" condition for the vertical scaling opsRequest processing in cluster. 93 ProcessingReasonVerticalScaling = "VerticalScaling" 94 // ProcessingReasonStarting is the reason of the "OpsRequestProcessed" condition for the start opsRequest processing in cluster. 95 ProcessingReasonStarting = "Starting" 96 // ProcessingReasonStopping is the reason of the "OpsRequestProcessed" condition for the stop opsRequest processing in cluster. 97 ProcessingReasonStopping = "Stopping" 98 // ProcessingReasonRestarting is the reason of the "OpsRequestProcessed" condition for the restart opsRequest processing in cluster. 99 ProcessingReasonRestarting = "Restarting" 100 // ProcessingReasonReconfiguring is the reason of the "OpsRequestProcessed" condition for the reconfiguration opsRequest processing in cluster. 101 ProcessingReasonReconfiguring = "Reconfiguring" 102 // ProcessingReasonVersionUpgrading is the reason of the "OpsRequestProcessed" condition for the version upgrade opsRequest processing in cluster. 103 ProcessingReasonVersionUpgrading = "VersionUpgrading" 104 // ProcessingReasonSwitchovering is the reason of the "OpsRequestProcessed" condition for the switchover opsRequest processing in cluster. 105 ProcessingReasonSwitchovering = "Switchovering" 106 // ProcessingReasonBackup is the reason of the "OpsRequestProcessed" condition for the backup opsRequest processing in cluster. 107 ProcessingReasonBackup = "Backup" 108 )