github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/dataprotection/utils/backup.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 utils
    21  
    22  import (
    23  	"fmt"
    24  
    25  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    26  	dptypes "github.com/1aal/kubeblocks/pkg/dataprotection/types"
    27  	"github.com/1aal/kubeblocks/pkg/dataprotection/utils/boolptr"
    28  )
    29  
    30  // GetBackupMethodsFromBackupPolicy get backup methods from backup policy
    31  // if backup policy is specified, search the backup policy with the name
    32  // if backup policy is not specified, search the default backup policy
    33  // if method's snapshotVolumes is true, use the method as the default backup method
    34  func GetBackupMethodsFromBackupPolicy(backupPolicyList *dpv1alpha1.BackupPolicyList, backupPolicyName string) (string, map[string]struct{}, error) {
    35  	var defaultBackupMethod string
    36  	var backupMethodsMap = make(map[string]struct{})
    37  	for _, policy := range backupPolicyList.Items {
    38  		// if backupPolicyName is not empty, only use the backup policy with the name
    39  		if backupPolicyName != "" && policy.Name != backupPolicyName {
    40  			continue
    41  		}
    42  		// if backupPolicyName is empty, only use the default backup policy
    43  		if backupPolicyName == "" && policy.Annotations[dptypes.DefaultBackupPolicyAnnotationKey] != "true" {
    44  			continue
    45  		}
    46  		if policy.Status.Phase != dpv1alpha1.AvailablePhase {
    47  			continue
    48  		}
    49  		for _, method := range policy.Spec.BackupMethods {
    50  			if boolptr.IsSetToTrue(method.SnapshotVolumes) {
    51  				defaultBackupMethod = method.Name
    52  			}
    53  			backupMethodsMap[method.Name] = struct{}{}
    54  		}
    55  	}
    56  	if defaultBackupMethod == "" {
    57  		return "", nil, fmt.Errorf("failed to find default backup method which snapshotVolumes is true, please check cluster's backup policy")
    58  	}
    59  	return defaultBackupMethod, backupMethodsMap, nil
    60  }