github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/configuration/simple_policy.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 configuration 21 22 import ( 23 corev1 "k8s.io/api/core/v1" 24 "sigs.k8s.io/controller-runtime/pkg/client" 25 26 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 27 "github.com/1aal/kubeblocks/pkg/configuration/core" 28 ) 29 30 type simplePolicy struct { 31 } 32 33 func init() { 34 RegisterPolicy(appsv1alpha1.NormalPolicy, &simplePolicy{}) 35 } 36 37 func (s *simplePolicy) Upgrade(params reconfigureParams) (ReturnedStatus, error) { 38 params.Ctx.Log.V(1).Info("simple policy begin....") 39 40 var funcs RollingUpgradeFuncs 41 switch params.WorkloadType() { 42 default: 43 return makeReturnedStatus(ESNotSupport), core.MakeError("not supported component workload type:[%s]", params.WorkloadType()) 44 case appsv1alpha1.Consensus: 45 funcs = GetConsensusRollingUpgradeFuncs() 46 case appsv1alpha1.Stateful: 47 funcs = GetStatefulSetRollingUpgradeFuncs() 48 case appsv1alpha1.Replication: 49 funcs = GetReplicationRollingUpgradeFuncs() 50 case appsv1alpha1.Stateless: 51 funcs = GetDeploymentRollingUpgradeFuncs() 52 } 53 return restartAndCheckComponent(params, funcs, fromWorkloadObjects(params)) 54 } 55 56 func (s *simplePolicy) GetPolicyName() string { 57 return string(appsv1alpha1.NormalPolicy) 58 } 59 60 func restartAndCheckComponent(param reconfigureParams, funcs RollingUpgradeFuncs, objs []client.Object) (ReturnedStatus, error) { 61 var ( 62 newVersion = param.getTargetVersionHash() 63 configKey = param.getConfigKey() 64 65 retStatus = ESRetry 66 progress = core.NotStarted 67 ) 68 69 recordEvent := func(obj client.Object) { 70 param.Ctx.Recorder.Eventf(obj, 71 corev1.EventTypeNormal, appsv1alpha1.ReasonReconfigureRestart, 72 "restarting component[%s] in cluster[%s], version: %s", param.ClusterComponent.Name, param.Cluster.Name, newVersion) 73 } 74 if obj, err := funcs.RestartComponent(param.Client, param.Ctx, configKey, newVersion, objs, recordEvent); err != nil { 75 param.Ctx.Recorder.Eventf(obj, 76 corev1.EventTypeWarning, appsv1alpha1.ReasonReconfigureRestartFailed, 77 "failed to restart component[%s] in cluster[%s], version: %s", client.ObjectKeyFromObject(obj), param.Cluster.Name, newVersion) 78 return makeReturnedStatus(ESFailedAndRetry), err 79 } 80 81 pods, err := funcs.GetPodsFunc(param) 82 if err != nil { 83 return makeReturnedStatus(ESFailedAndRetry), err 84 } 85 if len(pods) != 0 { 86 progress = CheckReconfigureUpdateProgress(pods, configKey, newVersion) 87 } 88 if len(pods) == int(progress) { 89 retStatus = ESNone 90 } 91 return makeReturnedStatus(retStatus, withExpected(int32(len(pods))), withSucceed(progress)), nil 92 }