github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/configuration/parallel_upgrade_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  
    25  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    26  	cfgcore "github.com/1aal/kubeblocks/pkg/configuration/core"
    27  	podutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    28  )
    29  
    30  type parallelUpgradePolicy struct {
    31  }
    32  
    33  func init() {
    34  	RegisterPolicy(appsv1alpha1.RestartPolicy, &parallelUpgradePolicy{})
    35  }
    36  
    37  func (p *parallelUpgradePolicy) Upgrade(params reconfigureParams) (ReturnedStatus, error) {
    38  	var funcs RollingUpgradeFuncs
    39  
    40  	switch params.WorkloadType() {
    41  	default:
    42  		return makeReturnedStatus(ESNotSupport), cfgcore.MakeError("not supported component workload type[%s]", params.WorkloadType())
    43  	case appsv1alpha1.Consensus:
    44  		funcs = GetConsensusRollingUpgradeFuncs()
    45  	case appsv1alpha1.Stateful:
    46  		funcs = GetStatefulSetRollingUpgradeFuncs()
    47  	case appsv1alpha1.Replication:
    48  		funcs = GetReplicationRollingUpgradeFuncs()
    49  	}
    50  
    51  	pods, err := funcs.GetPodsFunc(params)
    52  	if err != nil {
    53  		return makeReturnedStatus(ESFailedAndRetry), err
    54  	}
    55  
    56  	return p.restartPods(params, pods, funcs)
    57  }
    58  
    59  func (p *parallelUpgradePolicy) GetPolicyName() string {
    60  	return string(appsv1alpha1.RestartPolicy)
    61  }
    62  
    63  func (p *parallelUpgradePolicy) restartPods(params reconfigureParams, pods []corev1.Pod, funcs RollingUpgradeFuncs) (ReturnedStatus, error) {
    64  	var configKey = params.getConfigKey()
    65  	var configVersion = params.getTargetVersionHash()
    66  
    67  	for _, pod := range pods {
    68  		if podutil.IsMatchConfigVersion(&pod, configKey, configVersion) {
    69  			continue
    70  		}
    71  		if err := funcs.RestartContainerFunc(&pod, params.Ctx.Ctx, params.ContainerNames, params.ReconfigureClientFactory); err != nil {
    72  			return makeReturnedStatus(ESFailedAndRetry), err
    73  		}
    74  		if err := updatePodLabelsWithConfigVersion(&pod, configKey, configVersion, params.Client, params.Ctx.Ctx); err != nil {
    75  			return makeReturnedStatus(ESFailedAndRetry), err
    76  		}
    77  	}
    78  	return makeReturnedStatus(ESNone), nil
    79  }