github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/testutil/dataprotection/restore_factory.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 dataprotection
    21  
    22  import (
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/resource"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1"
    28  	"github.com/1aal/kubeblocks/pkg/constant"
    29  	testapps "github.com/1aal/kubeblocks/pkg/testutil/apps"
    30  )
    31  
    32  type MockRestoreFactory struct {
    33  	testapps.BaseFactory[dpv1alpha1.Restore, *dpv1alpha1.Restore, MockRestoreFactory]
    34  }
    35  
    36  func NewRestoreactory(namespace, name string) *MockRestoreFactory {
    37  	f := &MockRestoreFactory{}
    38  	f.Init(namespace, name,
    39  		&dpv1alpha1.Restore{
    40  			Spec: dpv1alpha1.RestoreSpec{},
    41  		}, f)
    42  	return f
    43  }
    44  
    45  func (f *MockRestoreFactory) SetBackup(name, namespace string) *MockRestoreFactory {
    46  	f.Get().Spec.Backup = dpv1alpha1.BackupRef{
    47  		Name:      name,
    48  		Namespace: namespace,
    49  	}
    50  	return f
    51  }
    52  
    53  func (f *MockRestoreFactory) SetRestoreTime(restoreTime string) *MockRestoreFactory {
    54  	f.Get().Spec.RestoreTime = restoreTime
    55  	return f
    56  }
    57  
    58  func (f *MockRestoreFactory) SetLabels(labels map[string]string) *MockRestoreFactory {
    59  	f.Get().SetLabels(labels)
    60  	return f
    61  }
    62  
    63  func (f *MockRestoreFactory) AddEnv(env corev1.EnvVar) *MockRestoreFactory {
    64  	f.Get().Spec.Env = append(f.Get().Spec.Env, env)
    65  	return f
    66  }
    67  
    68  func (f *MockRestoreFactory) initPrepareDataConfig() {
    69  	prepareDataConfig := f.Get().Spec.PrepareDataConfig
    70  	if prepareDataConfig == nil {
    71  		f.Get().Spec.PrepareDataConfig = &dpv1alpha1.PrepareDataConfig{
    72  			VolumeClaimRestorePolicy: dpv1alpha1.VolumeClaimRestorePolicyParallel,
    73  		}
    74  	}
    75  }
    76  
    77  func (f *MockRestoreFactory) initReadyConfig() {
    78  	ReadyConfig := f.Get().Spec.ReadyConfig
    79  	if ReadyConfig == nil {
    80  		f.Get().Spec.ReadyConfig = &dpv1alpha1.ReadyConfig{}
    81  	}
    82  }
    83  
    84  func (f *MockRestoreFactory) buildRestoreVolumeClaim(name, volumeSource, mountPath, storageClass string) dpv1alpha1.RestoreVolumeClaim {
    85  	return dpv1alpha1.RestoreVolumeClaim{
    86  		ObjectMeta: metav1.ObjectMeta{
    87  			Name: name,
    88  			Labels: map[string]string{
    89  				constant.AppManagedByLabelKey: "restore",
    90  			},
    91  		},
    92  		VolumeConfig: dpv1alpha1.VolumeConfig{
    93  			VolumeSource: volumeSource,
    94  			MountPath:    mountPath,
    95  		},
    96  		VolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
    97  			StorageClassName: &storageClass,
    98  			Resources: corev1.ResourceRequirements{
    99  				Requests: corev1.ResourceList{
   100  					corev1.ResourceStorage: resource.MustParse("20Gi"),
   101  				},
   102  			},
   103  			AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
   104  		},
   105  	}
   106  }
   107  
   108  func (f *MockRestoreFactory) SetVolumeClaimRestorePolicy(policy dpv1alpha1.VolumeClaimRestorePolicy) *MockRestoreFactory {
   109  	f.initPrepareDataConfig()
   110  	f.Get().Spec.PrepareDataConfig.VolumeClaimRestorePolicy = policy
   111  	return f
   112  }
   113  
   114  func (f *MockRestoreFactory) SetSchedulingSpec(schedulingSpec dpv1alpha1.SchedulingSpec) *MockRestoreFactory {
   115  	f.initPrepareDataConfig()
   116  	f.Get().Spec.PrepareDataConfig.SchedulingSpec = schedulingSpec
   117  	return f
   118  }
   119  
   120  func (f *MockRestoreFactory) SetDataSourceRef(volumeSource, mountPath string) *MockRestoreFactory {
   121  	f.initPrepareDataConfig()
   122  	f.Get().Spec.PrepareDataConfig.DataSourceRef = &dpv1alpha1.VolumeConfig{
   123  		VolumeSource: volumeSource,
   124  		MountPath:    mountPath,
   125  	}
   126  	return f
   127  }
   128  
   129  func (f *MockRestoreFactory) SetVolumeClaimsTemplate(templateName, volumeSource, mountPath, storageClass string, replicas, startingIndex int32) *MockRestoreFactory {
   130  	f.initPrepareDataConfig()
   131  	f.Get().Spec.PrepareDataConfig.RestoreVolumeClaimsTemplate = &dpv1alpha1.RestoreVolumeClaimsTemplate{
   132  		Replicas:      replicas,
   133  		StartingIndex: startingIndex,
   134  		Templates: []dpv1alpha1.RestoreVolumeClaim{
   135  			f.buildRestoreVolumeClaim(templateName, volumeSource, mountPath, storageClass),
   136  		},
   137  	}
   138  	return f
   139  }
   140  
   141  func (f *MockRestoreFactory) AddVolumeClaim(claimName, volumeSource, mountPath, storageClass string) *MockRestoreFactory {
   142  	f.initPrepareDataConfig()
   143  	f.Get().Spec.PrepareDataConfig.RestoreVolumeClaims = append(f.Get().Spec.PrepareDataConfig.RestoreVolumeClaims,
   144  		f.buildRestoreVolumeClaim(claimName, volumeSource, mountPath, storageClass))
   145  	return f
   146  }
   147  
   148  func (f *MockRestoreFactory) SetConnectCredential(secretName string) *MockRestoreFactory {
   149  	f.initReadyConfig()
   150  	f.Get().Spec.ReadyConfig.ConnectionCredential = &dpv1alpha1.ConnectionCredential{
   151  		SecretName:  secretName,
   152  		UsernameKey: "username",
   153  		PasswordKey: "password",
   154  	}
   155  	return f
   156  }
   157  
   158  func (f *MockRestoreFactory) SetJobActionConfig(matchLabels map[string]string) *MockRestoreFactory {
   159  	f.initReadyConfig()
   160  	f.Get().Spec.ReadyConfig.JobAction = &dpv1alpha1.JobAction{
   161  		Target: dpv1alpha1.JobActionTarget{
   162  			PodSelector: metav1.LabelSelector{
   163  				MatchLabels: matchLabels,
   164  			},
   165  			VolumeMounts: []corev1.VolumeMount{
   166  				{
   167  					Name:      DataVolumeName,
   168  					MountPath: DataVolumeMountPath,
   169  				},
   170  			},
   171  		},
   172  	}
   173  	return f
   174  }
   175  
   176  func (f *MockRestoreFactory) SetExecActionConfig(matchLabels map[string]string) *MockRestoreFactory {
   177  	f.initReadyConfig()
   178  	f.Get().Spec.ReadyConfig.ExecAction = &dpv1alpha1.ExecAction{
   179  		Target: dpv1alpha1.ExecActionTarget{
   180  			PodSelector: metav1.LabelSelector{
   181  				MatchLabels: matchLabels,
   182  			},
   183  		},
   184  	}
   185  	return f
   186  }