github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/dataprotection/v1alpha1/backuppolicy_types.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v1alpha1 18 19 import ( 20 corev1 "k8s.io/api/core/v1" 21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 22 ) 23 24 // BackupPolicySpec defines the desired state of BackupPolicy 25 type BackupPolicySpec struct { 26 // backupRepoName is the name of BackupRepo and the backup data will be 27 // stored in this repository. If not set, will be stored in the default 28 // backup repository. 29 // +kubebuilder:validation:Pattern:=`^[a-z0-9]([a-z0-9\.\-]*[a-z0-9])?$` 30 // +optional 31 BackupRepoName *string `json:"backupRepoName,omitempty"` 32 33 // pathPrefix is the directory inside the backup repository to store the backup content. 34 // It is a relative to the path of the backup repository. 35 // +optional 36 PathPrefix string `json:"pathPrefix,omitempty"` 37 38 // Specifies the number of retries before marking the backup failed. 39 // +optional 40 // +kubebuilder:validation:Minimum=0 41 // +kubebuilder:validation:Maximum=10 42 BackoffLimit *int32 `json:"backoffLimit,omitempty"` 43 44 // target specifies the target information to back up. 45 // +kubebuilder:validation:Required 46 Target *BackupTarget `json:"target"` 47 48 // backupMethods defines the backup methods. 49 // +kubebuilder:validation:Required 50 BackupMethods []BackupMethod `json:"backupMethods"` 51 } 52 53 type BackupTarget struct { 54 // podSelector is used to find the target pod. The volumes of the target pod 55 // will be backed up. 56 // +kube:validation:Required 57 PodSelector *PodSelector `json:"podSelector,omitempty"` 58 59 // connectionCredential specifies the connection credential to connect to the 60 // target database cluster. 61 // +optional 62 ConnectionCredential *ConnectionCredential `json:"connectionCredential,omitempty"` 63 64 // resources specifies the kubernetes resources to back up. 65 // +optional 66 Resources *KubeResources `json:"resources,omitempty"` 67 68 // serviceAccountName specifies the service account to run the backup workload. 69 // +kubebuilder:validation:Required 70 ServiceAccountName string `json:"serviceAccountName,omitempty"` 71 } 72 73 type PodSelector struct { 74 // labelsSelector is the label selector to filter the target pods. 75 *metav1.LabelSelector `json:",inline"` 76 77 // strategy specifies the strategy to select the target pod when multiple pods 78 // are selected. 79 // Valid values are: 80 // - Any: select any one pod that match the labelsSelector. 81 // +kubebuilder:default=Any 82 Strategy PodSelectionStrategy `json:"strategy,omitempty"` 83 } 84 85 // PodSelectionStrategy specifies the strategy to select when multiple pods are 86 // selected for backup target 87 // +kubebuilder:validation:Enum=Any 88 type PodSelectionStrategy string 89 90 const ( 91 // PodSelectionStrategyAll selects all pods that match the labelsSelector. 92 // TODO: support PodSelectionStrategyAll 93 PodSelectionStrategyAll PodSelectionStrategy = "All" 94 95 // PodSelectionStrategyAny selects any one pod that match the labelsSelector. 96 PodSelectionStrategyAny PodSelectionStrategy = "Any" 97 ) 98 99 // ConnectionCredential specifies the connection credential to connect to the 100 // target database cluster. 101 type ConnectionCredential struct { 102 // secretName refers to the Secret object that contains the connection credential. 103 // +kubebuilder:validation:Required 104 // +kubebuilder:validation:Pattern:=`^[a-z0-9]([a-z0-9\.\-]*[a-z0-9])?$` 105 SecretName string `json:"secretName"` 106 107 // usernameKey specifies the map key of the user in the connection credential secret. 108 // +kubebuilder:default=username 109 UsernameKey string `json:"usernameKey,omitempty"` 110 111 // passwordKey specifies the map key of the password in the connection credential secret. 112 // This password will be saved in the backup annotation for full backup. 113 // You can use the environment variable DP_ENCRYPTION_KEY to specify encryption key. 114 // +kubebuilder:default=password 115 PasswordKey string `json:"passwordKey,omitempty"` 116 117 // hostKey specifies the map key of the host in the connection credential secret. 118 HostKey string `json:"hostKey,omitempty"` 119 120 // portKey specifies the map key of the port in the connection credential secret. 121 PortKey string `json:"portKey,omitempty"` 122 } 123 124 // KubeResources defines the kubernetes resources to back up. 125 type KubeResources struct { 126 // selector is a metav1.LabelSelector to filter the target kubernetes resources 127 // that need to be backed up. 128 // If not set, will do not back up any kubernetes resources. 129 // +kube:validation:Required 130 Selector *metav1.LabelSelector `json:"selector,omitempty"` 131 132 // included is a slice of namespaced-scoped resource type names to include in 133 // the kubernetes resources. 134 // The default value is "*", which means all resource types will be included. 135 // +optional 136 // +kubebuilder:default={"*"} 137 Included []string `json:"included,omitempty"` 138 139 // excluded is a slice of namespaced-scoped resource type names to exclude in 140 // the kubernetes resources. 141 // The default value is empty. 142 // +optional 143 Excluded []string `json:"excluded,omitempty"` 144 } 145 146 // BackupMethod defines the backup method. 147 type BackupMethod struct { 148 // the name of backup method. 149 // +kubebuilder:validation:Required 150 // +kubebuilder:validation:Pattern:=`^[a-z0-9]([a-z0-9\.\-]*[a-z0-9])?$` 151 Name string `json:"name"` 152 153 // snapshotVolumes specifies whether to take snapshots of persistent volumes. 154 // if true, the BackupScript is not required, the controller will use the CSI 155 // volume snapshotter to create the snapshot. 156 // +optional 157 // +kubebuilder:default=false 158 SnapshotVolumes *bool `json:"snapshotVolumes,omitempty"` 159 160 // actionSetName refers to the ActionSet object that defines the backup actions. 161 // For volume snapshot backup, the actionSet is not required, the controller 162 // will use the CSI volume snapshotter to create the snapshot. 163 // +optional 164 ActionSetName string `json:"actionSetName,omitempty"` 165 166 // targetVolumes specifies which volumes from the target should be mounted in 167 // the backup workload. 168 // +optional 169 TargetVolumes *TargetVolumeInfo `json:"targetVolumes,omitempty"` 170 171 // env specifies the environment variables for the backup workload. 172 // +optional 173 Env []corev1.EnvVar `json:"env,omitempty"` 174 175 // runtimeSettings specifies runtime settings for the backup workload container. 176 // +optional 177 RuntimeSettings *RuntimeSettings `json:"runtimeSettings,omitempty"` 178 } 179 180 // TargetVolumeInfo specifies the volumes and their mounts of the targeted application 181 // that should be mounted in backup workload. 182 type TargetVolumeInfo struct { 183 // Volumes indicates the list of volumes of targeted application that should 184 // be mounted on the backup job. 185 // +optional 186 Volumes []string `json:"volumes,omitempty"` 187 188 // volumeMounts specifies the mount for the volumes specified in `Volumes` section. 189 // +optional 190 VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"` 191 } 192 193 type RuntimeSettings struct { 194 // resources specifies the resource required by container. 195 // More info: https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/ 196 // +optional 197 Resources corev1.ResourceRequirements `json:"resources,omitempty"` 198 } 199 200 // BackupPolicyStatus defines the observed state of BackupPolicy 201 type BackupPolicyStatus struct { 202 // phase - in list of [Available,Unavailable] 203 // +optional 204 Phase Phase `json:"phase,omitempty"` 205 206 // A human-readable message indicating details about why the BackupPolicy is 207 // in this phase. 208 // +optional 209 Message string `json:"message,omitempty"` 210 211 // observedGeneration is the most recent generation observed for this 212 // BackupPolicy. It refers to the BackupPolicy's generation, which is 213 // updated on mutation by the API Server. 214 // +optional 215 ObservedGeneration int64 `json:"observedGeneration,omitempty"` 216 } 217 218 // BackupPolicyPhase defines phases for BackupPolicy. 219 // +enum 220 // +kubebuilder:validation:Enum={Available,Failed} 221 type BackupPolicyPhase string 222 223 const ( 224 BackupPolicyAvailable BackupPolicyPhase = "Available" 225 BackupPolicyFailed BackupPolicyPhase = "Failed" 226 ) 227 228 // +genclient 229 // +k8s:openapi-gen=true 230 // +kubebuilder:object:root=true 231 // +kubebuilder:subresource:status 232 // +kubebuilder:resource:categories={kubeblocks},scope=Namespaced,shortName=bp 233 // +kubebuilder:printcolumn:name="BACKUP-REPO", type=string, JSONPath=`.spec.backupRepoName` 234 // +kubebuilder:printcolumn:name="STATUS",type=string,JSONPath=`.status.phase` 235 // +kubebuilder:printcolumn:name="AGE",type=date,JSONPath=`.metadata.creationTimestamp` 236 237 // BackupPolicy is the Schema for the backuppolicies API. 238 type BackupPolicy struct { 239 metav1.TypeMeta `json:",inline"` 240 metav1.ObjectMeta `json:"metadata,omitempty"` 241 242 Spec BackupPolicySpec `json:"spec,omitempty"` 243 Status BackupPolicyStatus `json:"status,omitempty"` 244 } 245 246 // +kubebuilder:object:root=true 247 248 // BackupPolicyList contains a list of BackupPolicy 249 type BackupPolicyList struct { 250 metav1.TypeMeta `json:",inline"` 251 metav1.ListMeta `json:"metadata,omitempty"` 252 Items []BackupPolicy `json:"items"` 253 } 254 255 func init() { 256 SchemeBuilder.Register(&BackupPolicy{}, &BackupPolicyList{}) 257 }