github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/apps/v1alpha1/backuppolicytemplate_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 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 22 dpv1alpha1 "github.com/1aal/kubeblocks/apis/dataprotection/v1alpha1" 23 ) 24 25 // BackupPolicyTemplateSpec defines the desired state of BackupPolicyTemplate 26 type BackupPolicyTemplateSpec struct { 27 // clusterDefinitionRef references ClusterDefinition name, this is an immutable attribute. 28 // +kubebuilder:validation:Required 29 // +kubebuilder:validation:Pattern:=`^[a-z0-9]([a-z0-9\.\-]*[a-z0-9])?$` 30 // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="clusterDefinitionRef is immutable" 31 ClusterDefRef string `json:"clusterDefinitionRef"` 32 33 // backupPolicies is a list of backup policy template for the specified componentDefinition. 34 // +patchMergeKey=componentDefRef 35 // +patchStrategy=merge,retainKeys 36 // +listType=map 37 // +listMapKey=componentDefRef 38 // +kubebuilder:validation:Required 39 // +kubebuilder:validation:MinItems=1 40 BackupPolicies []BackupPolicy `json:"backupPolicies"` 41 42 // Identifier is a unique identifier for this BackupPolicyTemplate. 43 // this identifier will be the suffix of the automatically generated backupPolicy name. 44 // and must be added when multiple BackupPolicyTemplates exist, 45 // otherwise the generated backupPolicy override will occur. 46 // +optional 47 // +kubebuilder:validation:MaxLength=20 48 Identifier string `json:"identifier,omitempty"` 49 } 50 51 type BackupPolicy struct { 52 // componentDefRef references componentDef defined in ClusterDefinition spec. Need to 53 // comply with IANA Service Naming rule. 54 // +kubebuilder:validation:Required 55 // +kubebuilder:validation:MaxLength=22 56 // +kubebuilder:validation:Pattern:=`^[a-z]([a-z0-9\-]*[a-z0-9])?$` 57 ComponentDefRef string `json:"componentDefRef"` 58 59 // retentionPeriod determines a duration up to which the backup should be kept. 60 // controller will remove all backups that are older than the RetentionPeriod. 61 // For example, RetentionPeriod of `30d` will keep only the backups of last 30 days. 62 // Sample duration format: 63 // - years: 2y 64 // - months: 6mo 65 // - days: 30d 66 // - hours: 12h 67 // - minutes: 30m 68 // You can also combine the above durations. For example: 30d12h30m 69 // +optional 70 // +kubebuilder:default="7d" 71 RetentionPeriod dpv1alpha1.RetentionPeriod `json:"retentionPeriod,omitempty"` 72 73 // target instance for backup. 74 // +optional 75 Target TargetInstance `json:"target"` 76 77 // schedule policy for backup. 78 // +optional 79 Schedules []SchedulePolicy `json:"schedules,omitempty"` 80 81 // backupMethods defines the backup methods. 82 // +kubebuilder:validation:Required 83 BackupMethods []BackupMethod `json:"backupMethods"` 84 } 85 86 type BackupMethod struct { 87 dpv1alpha1.BackupMethod `json:",inline"` 88 89 // envMapping defines the variables of cluster mapped to env values' keys. 90 // +optional 91 EnvMapping []EnvMappingVar `json:"envMapping,omitempty"` 92 } 93 94 type EnvMappingVar struct { 95 // env key which needs to mapping. 96 // +kubebuilder:validation:Required 97 Key string `json:"key"` 98 99 // valueFrom defines source of the env value. 100 // +kubebuilder:validation:Required 101 ValueFrom ValueFrom `json:"valueFrom"` 102 } 103 104 type ValueFrom struct { 105 // mapped ClusterVersionRef to env value. 106 // +kubebuilder:validation:Required 107 ClusterVersionRef []ClusterVersionMapping `json:"clusterVersionRef"` 108 } 109 110 type ClusterVersionMapping struct { 111 // the array of ClusterVersion name which can be mapped to the env value. 112 // +kubebuilder:validation:Required 113 Names []string `json:"names"` 114 115 // mapping value for the specified ClusterVersion names. 116 // +kubebuilder:validation:Required 117 MappingValue string `json:"mappingValue"` 118 } 119 120 type SchedulePolicy struct { 121 // enabled specifies whether the backup schedule is enabled or not. 122 // +optional 123 Enabled *bool `json:"enabled,omitempty"` 124 125 // backupMethod specifies the backup method name that is defined in backupPolicy. 126 // +kubebuilder:validation:Required 127 BackupMethod string `json:"backupMethod"` 128 129 // the cron expression for schedule, the timezone is in UTC. 130 // see https://en.wikipedia.org/wiki/Cron. 131 // +kubebuilder:validation:Required 132 CronExpression string `json:"cronExpression"` 133 } 134 135 type TargetInstance struct { 136 // select instance of corresponding role for backup, role are: 137 // - the name of Leader/Follower/Leaner for Consensus component. 138 // - primary or secondary for Replication component. 139 // finally, invalid role of the component will be ignored. 140 // such as if workload type is Replication and component's replicas is 1, 141 // the secondary role is invalid. and it also will be ignored when component is Stateful/Stateless. 142 // the role will be transformed to a role LabelSelector for BackupPolicy's target attribute. 143 // +optional 144 Role string `json:"role"` 145 146 // refer to spec.componentDef.systemAccounts.accounts[*].name in ClusterDefinition. 147 // the secret created by this account will be used to connect the database. 148 // if not set, the secret created by spec.ConnectionCredential of the ClusterDefinition will be used. 149 // it will be transformed to a secret for BackupPolicy's target secret. 150 // +optional 151 Account string `json:"account,omitempty"` 152 153 // connectionCredentialKey defines connection credential key in secret 154 // which created by spec.ConnectionCredential of the ClusterDefinition. 155 // it will be ignored when "account" is set. 156 ConnectionCredentialKey ConnectionCredentialKey `json:"connectionCredentialKey,omitempty"` 157 } 158 159 type ConnectionCredentialKey struct { 160 // the key of password in the ConnectionCredential secret. 161 // if not set, the default key is "password". 162 // +optional 163 PasswordKey *string `json:"passwordKey,omitempty"` 164 165 // the key of username in the ConnectionCredential secret. 166 // if not set, the default key is "username". 167 // +optional 168 UsernameKey *string `json:"usernameKey,omitempty"` 169 170 // hostKey specifies the map key of the host in the connection credential secret. 171 HostKey *string `json:"hostKey,omitempty"` 172 173 // portKey specifies the map key of the port in the connection credential secret. 174 // +kubebuilder:default=port 175 PortKey *string `json:"portKey,omitempty"` 176 } 177 178 // BackupPolicyTemplateStatus defines the observed state of BackupPolicyTemplate 179 type BackupPolicyTemplateStatus struct { 180 } 181 182 // +genclient 183 // +genclient:nonNamespaced 184 // +k8s:openapi-gen=true 185 // +kubebuilder:object:root=true 186 // +kubebuilder:subresource:status 187 // +kubebuilder:resource:categories={kubeblocks},scope=Cluster,shortName=bpt 188 // +kubebuilder:printcolumn:name="CLUSTER-DEFINITION",type="string",JSONPath=".spec.clusterDefinitionRef",description="ClusterDefinition referenced by cluster." 189 // +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp" 190 191 // BackupPolicyTemplate is the Schema for the BackupPolicyTemplates API (defined by provider) 192 type BackupPolicyTemplate struct { 193 metav1.TypeMeta `json:",inline"` 194 metav1.ObjectMeta `json:"metadata,omitempty"` 195 196 Spec BackupPolicyTemplateSpec `json:"spec,omitempty"` 197 Status BackupPolicyTemplateStatus `json:"status,omitempty"` 198 } 199 200 // +kubebuilder:object:root=true 201 202 // BackupPolicyTemplateList contains a list of BackupPolicyTemplate 203 type BackupPolicyTemplateList struct { 204 metav1.TypeMeta `json:",inline"` 205 metav1.ListMeta `json:"metadata,omitempty"` 206 Items []BackupPolicyTemplate `json:"items"` 207 } 208 209 func init() { 210 SchemeBuilder.Register(&BackupPolicyTemplate{}, &BackupPolicyTemplateList{}) 211 }