github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/apis/dataprotection/v1alpha1/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  	"errors"
    21  	"strconv"
    22  	"strings"
    23  	"time"
    24  	"unicode"
    25  )
    26  
    27  // Phase defines the BackupPolicy and ActionSet CR .status.phase
    28  // +enum
    29  // +kubebuilder:validation:Enum={Available,Unavailable}
    30  type Phase string
    31  
    32  const (
    33  	AvailablePhase   Phase = "Available"
    34  	UnavailablePhase Phase = "Unavailable"
    35  )
    36  
    37  func (p Phase) IsAvailable() bool {
    38  	return p == AvailablePhase
    39  }
    40  
    41  // BackupRepoPhase defines phases for BackupRepo CR.
    42  // +enum
    43  // +kubebuilder:validation:Enum={PreChecking,Failed,Ready,Deleting}
    44  type BackupRepoPhase string
    45  
    46  const (
    47  	BackupRepoPreChecking BackupRepoPhase = "PreChecking"
    48  	BackupRepoFailed      BackupRepoPhase = "Failed"
    49  	BackupRepoReady       BackupRepoPhase = "Ready"
    50  	BackupRepoDeleting    BackupRepoPhase = "Deleting"
    51  )
    52  
    53  // RetentionPeriod represents a duration in the format "1y2mo3w4d5h6m", where
    54  // y=year, mo=month, w=week, d=day, h=hour, m=minute.
    55  type RetentionPeriod string
    56  
    57  // ToDuration converts the RetentionPeriod to time.Duration.
    58  func (r RetentionPeriod) ToDuration() (time.Duration, error) {
    59  	if len(r.String()) == 0 {
    60  		return time.Duration(0), nil
    61  	}
    62  
    63  	minutes, err := r.toMinutes()
    64  	if err != nil {
    65  		return time.Duration(0), err
    66  	}
    67  	return time.Minute * time.Duration(minutes), nil
    68  }
    69  
    70  func (r RetentionPeriod) String() string {
    71  	return string(r)
    72  }
    73  
    74  func (r RetentionPeriod) toMinutes() (int, error) {
    75  	d, err := r.parseDuration()
    76  	if err != nil {
    77  		return 0, err
    78  	}
    79  	minutes := d.Minutes
    80  	minutes += d.Hours * 60
    81  	minutes += d.Days * 24 * 60
    82  	minutes += d.Weeks * 7 * 24 * 60
    83  	minutes += d.Months * 30 * 24 * 60
    84  	minutes += d.Years * 365 * 24 * 60
    85  	return minutes, nil
    86  }
    87  
    88  type duration struct {
    89  	Minutes int
    90  	Hours   int
    91  	Days    int
    92  	Weeks   int
    93  	Months  int
    94  	Years   int
    95  }
    96  
    97  var errInvalidDuration = errors.New("invalid duration provided")
    98  
    99  // parseDuration parses a duration from a string. The format is `6y5m234d37h`
   100  func (r RetentionPeriod) parseDuration() (duration, error) {
   101  	var (
   102  		d   duration
   103  		num int
   104  		err error
   105  	)
   106  
   107  	s := strings.TrimSpace(r.String())
   108  	for s != "" {
   109  		num, s, err = r.nextNumber(s)
   110  		if err != nil {
   111  			return duration{}, err
   112  		}
   113  
   114  		if len(s) == 0 {
   115  			return duration{}, errInvalidDuration
   116  		}
   117  
   118  		if len(s) > 1 && s[0] == 'm' && s[1] == 'o' {
   119  			d.Months = num
   120  			s = s[2:]
   121  			continue
   122  		}
   123  
   124  		switch s[0] {
   125  		case 'y':
   126  			d.Years = num
   127  		case 'w':
   128  			d.Weeks = num
   129  		case 'd':
   130  			d.Days = num
   131  		case 'h':
   132  			d.Hours = num
   133  		case 'm':
   134  			d.Minutes = num
   135  		default:
   136  			return duration{}, errInvalidDuration
   137  		}
   138  		s = s[1:]
   139  	}
   140  	return d, nil
   141  }
   142  
   143  func (r RetentionPeriod) nextNumber(input string) (num int, rest string, err error) {
   144  	if len(input) == 0 {
   145  		return 0, "", nil
   146  	}
   147  
   148  	var (
   149  		n        string
   150  		negative bool
   151  	)
   152  
   153  	if input[0] == '-' {
   154  		negative = true
   155  		input = input[1:]
   156  	}
   157  
   158  	for i, s := range input {
   159  		if !unicode.IsNumber(s) {
   160  			rest = input[i:]
   161  			break
   162  		}
   163  
   164  		n += string(s)
   165  	}
   166  
   167  	if len(n) == 0 {
   168  		return 0, input, errInvalidDuration
   169  	}
   170  
   171  	num, err = strconv.Atoi(n)
   172  	if err != nil {
   173  		return 0, input, err
   174  	}
   175  
   176  	if negative {
   177  		num = -num
   178  	}
   179  	return num, rest, nil
   180  }
   181  
   182  // RestorePhase The current phase. Valid values are Running, Completed, Failed, AsDataSource.
   183  // +enum
   184  // +kubebuilder:validation:Enum={Running,Completed,Failed,AsDataSource}
   185  type RestorePhase string
   186  
   187  const (
   188  	RestorePhaseRunning      RestorePhase = "Running"
   189  	RestorePhaseCompleted    RestorePhase = "Completed"
   190  	RestorePhaseFailed       RestorePhase = "Failed"
   191  	RestorePhaseAsDataSource RestorePhase = "AsDataSource"
   192  )
   193  
   194  // RestoreActionStatus the status of restore action.
   195  // +enum
   196  // +kubebuilder:validation:Enum={Processing,Completed,Failed}
   197  type RestoreActionStatus string
   198  
   199  const (
   200  	RestoreActionProcessing RestoreActionStatus = "Processing"
   201  	RestoreActionCompleted  RestoreActionStatus = "Completed"
   202  	RestoreActionFailed     RestoreActionStatus = "Failed"
   203  )
   204  
   205  type RestoreStage string
   206  
   207  const (
   208  	PrepareData RestoreStage = "prepareData"
   209  	PostReady   RestoreStage = "postReady"
   210  )
   211  
   212  // VolumeClaimRestorePolicy defines restore policy for persistent volume claim.
   213  // Supported policies are as follows:
   214  // 1. Parallel: parallel recovery of persistent volume claim.
   215  // 2. Serial: restore the persistent volume claim in sequence, and wait until the
   216  // previous persistent volume claim is restored before restoring a new one.
   217  // +enum
   218  // +kubebuilder:validation:Enum={Parallel,Serial}
   219  type VolumeClaimRestorePolicy string
   220  
   221  const (
   222  	VolumeClaimRestorePolicyParallel VolumeClaimRestorePolicy = "Parallel"
   223  	VolumeClaimRestorePolicySerial   VolumeClaimRestorePolicy = "Serial"
   224  )