github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/dataprotection/v1alpha1/actionset_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  // ActionSetSpec defines the desired state of ActionSet
    25  type ActionSetSpec struct {
    26  	// backupType specifies the backup type, supported values: Full, Continuous.
    27  	// Full means full backup.
    28  	// Incremental means back up data that have changed since the last backup (full or incremental).
    29  	// Differential means back up data that have changed since the last full backup.
    30  	// Continuous will back up the transaction log continuously, the PITR (Point in Time Recovery).
    31  	// can be performed based on the continuous backup and full backup.
    32  	// +kubebuilder:validation:Enum={Full,Incremental,Differential,Continuous}
    33  	// +kubebuilder:default=Full
    34  	// +kubebuilder:validation:Required
    35  	BackupType BackupType `json:"backupType"`
    36  
    37  	// List of environment variables to set in the container.
    38  	// +kubebuilder:pruning:PreserveUnknownFields
    39  	// +optional
    40  	Env []corev1.EnvVar `json:"env,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
    41  
    42  	// List of sources to populate environment variables in the container.
    43  	// The keys defined within a source must be a C_IDENTIFIER. All invalid keys
    44  	// will be reported as an event when the container is starting. When a key exists in multiple
    45  	// sources, the value associated with the last source will take precedence.
    46  	// Values defined by an Env with a duplicate key will take precedence.
    47  	// Cannot be updated.
    48  	// +kubebuilder:pruning:PreserveUnknownFields
    49  	// +optional
    50  	EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
    51  
    52  	// backup specifies the backup action.
    53  	// +optional
    54  	Backup *BackupActionSpec `json:"backup,omitempty"`
    55  
    56  	// restore specifies the restore action.
    57  	// +optional
    58  	Restore *RestoreActionSpec `json:"restore,omitempty"`
    59  }
    60  
    61  // ActionSetStatus defines the observed state of ActionSet
    62  type ActionSetStatus struct {
    63  	// phase - in list of [Available,Unavailable]
    64  	// +optional
    65  	Phase Phase `json:"phase,omitempty"`
    66  
    67  	// A human-readable message indicating details about why the ActionSet is in this phase.
    68  	// +optional
    69  	Message string `json:"message,omitempty"`
    70  
    71  	// generation number
    72  	// +optional
    73  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
    74  }
    75  
    76  // BackupType the backup type.
    77  // +enum
    78  // +kubebuilder:validation:Enum={Full,Incremental,Differential,Continuous}
    79  type BackupType string
    80  
    81  const (
    82  	BackupTypeFull         BackupType = "Full"
    83  	BackupTypeIncremental  BackupType = "Incremental"
    84  	BackupTypeDifferential BackupType = "Differential"
    85  	BackupTypeContinuous   BackupType = "Continuous"
    86  )
    87  
    88  type BackupActionSpec struct {
    89  	// backupData specifies the backup data action.
    90  	// +kubebuilder:validation:Required
    91  	BackupData *BackupDataActionSpec `json:"backupData,omitempty"`
    92  
    93  	// preBackup specifies a hook that should be executed before the backup.
    94  	// +optional
    95  	PreBackup []ActionSpec `json:"preBackup,omitempty"`
    96  
    97  	// postBackup specifies a hook that should be executed after the backup.
    98  	// +optional
    99  	PostBackup []ActionSpec `json:"postBackup,omitempty"`
   100  }
   101  
   102  // BackupDataActionSpec defines how to back up data.
   103  type BackupDataActionSpec struct {
   104  	JobActionSpec `json:",inline"`
   105  
   106  	// syncProgress specifies whether to sync the backup progress and its interval seconds.
   107  	// +optional
   108  	SyncProgress *SyncProgress `json:"syncProgress,omitempty"`
   109  }
   110  
   111  type SyncProgress struct {
   112  	// enabled specifies whether to sync the backup progress. If enabled,
   113  	// a sidecar container will be created to sync the backup progress to the
   114  	// Backup CR status.
   115  	// +optional
   116  	Enabled *bool `json:"enabled,omitempty"`
   117  
   118  	// intervalSeconds specifies the interval seconds to sync the backup progress.
   119  	// +optional
   120  	// +kubebuilder:default=60
   121  	IntervalSeconds *int32 `json:"intervalSeconds,omitempty"`
   122  }
   123  
   124  // RestoreActionSpec defines how to restore data.
   125  type RestoreActionSpec struct {
   126  	// prepareData specifies the action to prepare data.
   127  	// +optional
   128  	PrepareData *JobActionSpec `json:"prepareData,omitempty"`
   129  
   130  	// postReady specifies the action to execute after the data is ready.
   131  	// +optional
   132  	PostReady []ActionSpec `json:"postReady,omitempty"`
   133  }
   134  
   135  // ActionSpec defines an action that should be executed. Only one of the fields may be set.
   136  type ActionSpec struct {
   137  	// exec specifies the action should be executed by the pod exec API in a container.
   138  	// +optional
   139  	Exec *ExecActionSpec `json:"exec,omitempty"`
   140  
   141  	// job specifies the action should be executed by a Kubernetes Job.
   142  	// +optional
   143  	Job *JobActionSpec `json:"job,omitempty"`
   144  }
   145  
   146  // ExecActionSpec is an action that uses the pod exec API to execute a command in a container
   147  // in a pod.
   148  type ExecActionSpec struct {
   149  	// container is the container in the pod where the command should be executed.
   150  	// If not specified, the pod's first container is used.
   151  	// +optional
   152  	Container string `json:"container,omitempty"`
   153  
   154  	// Command is the command and arguments to execute.
   155  	// +kubebuilder:validation:MinItems=1
   156  	Command []string `json:"command"`
   157  
   158  	// OnError specifies how should behave if it encounters an error executing this action.
   159  	// +optional
   160  	// +kubebuilder:default=Fail
   161  	OnError ActionErrorMode `json:"onError,omitempty"`
   162  
   163  	// Timeout defines the maximum amount of time should wait for the hook to complete before
   164  	// considering the execution a failure.
   165  	// +optional
   166  	Timeout metav1.Duration `json:"timeout,omitempty"`
   167  }
   168  
   169  // JobActionSpec is an action that creates a Kubernetes Job to execute a command.
   170  type JobActionSpec struct {
   171  	// image specifies the image of backup container.
   172  	// +kubebuilder:validation:Required
   173  	Image string `json:"image"`
   174  
   175  	// runOnTargetPodNode specifies whether to run the job workload on the
   176  	// target pod node. If backup container should mount the target pod's
   177  	// volumes, this field should be set to true. otherwise the target pod's
   178  	// volumes will be ignored.
   179  	// +optional
   180  	// +kubebuilder:default=false
   181  	RunOnTargetPodNode *bool `json:"runOnTargetPodNode,omitempty"`
   182  
   183  	// command specifies the commands to back up the volume data.
   184  	// +kubebuilder:validation:Required
   185  	Command []string `json:"command"`
   186  
   187  	// OnError specifies how should behave if it encounters an error executing
   188  	// this action.
   189  	// +optional
   190  	// +kubebuilder:default=Fail
   191  	OnError ActionErrorMode `json:"onError,omitempty"`
   192  }
   193  
   194  // ActionErrorMode defines how should treat an error from an action.
   195  // +kubebuilder:validation:Enum=Continue;Fail
   196  type ActionErrorMode string
   197  
   198  const (
   199  	// ActionErrorModeContinue means that an error from an action is acceptable.
   200  	ActionErrorModeContinue ActionErrorMode = "Continue"
   201  
   202  	// ActionErrorModeFail means that an error from an action is problematic.
   203  	ActionErrorModeFail ActionErrorMode = "Fail"
   204  )
   205  
   206  // +genclient
   207  // +genclient:nonNamespaced
   208  // +k8s:openapi-gen=true
   209  // +kubebuilder:object:root=true
   210  // +kubebuilder:subresource:status
   211  // +kubebuilder:resource:categories={kubeblocks},scope=Cluster,shortName=as
   212  // +kubebuilder:printcolumn:name="BACKUP-TYPE",type="string",JSONPath=".spec.backupType"
   213  // +kubebuilder:printcolumn:name="STATUS",type="string",JSONPath=".status.phase"
   214  // +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
   215  
   216  // ActionSet is the Schema for the actionsets API
   217  type ActionSet struct {
   218  	metav1.TypeMeta   `json:",inline"`
   219  	metav1.ObjectMeta `json:"metadata,omitempty"`
   220  
   221  	Spec   ActionSetSpec   `json:"spec,omitempty"`
   222  	Status ActionSetStatus `json:"status,omitempty"`
   223  }
   224  
   225  // +kubebuilder:object:root=true
   226  
   227  // ActionSetList contains a list of ActionSet
   228  type ActionSetList struct {
   229  	metav1.TypeMeta `json:",inline"`
   230  	metav1.ListMeta `json:"metadata,omitempty"`
   231  	Items           []ActionSet `json:"items"`
   232  }
   233  
   234  func init() {
   235  	SchemeBuilder.Register(&ActionSet{}, &ActionSetList{})
   236  }
   237  
   238  func (r *ActionSet) HasPrepareDataStage() bool {
   239  	if r == nil || r.Spec.Restore == nil {
   240  		return false
   241  	}
   242  	return r.Spec.Restore.PrepareData != nil
   243  }
   244  
   245  func (r *ActionSet) HasPostReadyStage() bool {
   246  	if r == nil || r.Spec.Restore == nil {
   247  		return false
   248  	}
   249  	return len(r.Spec.Restore.PostReady) > 0
   250  }