Skip to content

Commit 367e7bf

Browse files
Add method to parse time
Signed-off-by: Anisur Rahman <[email protected]>
1 parent ed96ef6 commit 367e7bf

7 files changed

+31
-21
lines changed

apis/archiver/v1alpha1/openapi_generated.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/archiver/v1alpha1/types.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
"fmt"
21+
"regexp"
22+
"strconv"
23+
"time"
24+
2025
api "kubedb.dev/apimachinery/apis/kubedb/v1"
2126

2227
batch "k8s.io/api/batch/v1"
@@ -79,8 +84,8 @@ type LogBackupOptions struct {
7984
ConfigSecret *GenericSecretReference `json:"configSecret,omitempty"`
8085

8186
// RetentionPeriod is the retention policy to be used for Logs (i.e. '60d') means how long logs will be retained before being pruned.
82-
// The retention policy is expressed in the form of `XXu` where `XX` is a positive integer and `u` is in `[dwm]` - days, weeks, months.
83-
// +kubebuilder:validation:Pattern=^[1-9][0-9]*[dwm]$
87+
// The retention policy is expressed in the form of `XXu` where `XX` is a positive integer and `u` is in `[dwm]` - days, weeks, months, years.
88+
// +kubebuilder:validation:Pattern=^[1-9][0-9]*[dwmy]$
8489
// +optional
8590
RetentionPeriod string `json:"retentionPeriod,omitempty"`
8691
// time.RFC3339 We need to parse the time to RFC3339 format
@@ -104,19 +109,24 @@ type LogBackupOptions struct {
104109
LogRetentionHistoryLimit int32 `json:"logRetentionHistoryLimit,omitempty"`
105110
}
106111

107-
//func ParsePolicy(policy string) (string, error) {
108-
// unitName := map[string]string{
109-
// "d": "DAYS",
110-
// "w": "WEEKS",
111-
// "m": "MONTHS",
112-
// }
113-
// matches := regexPolicy.FindStringSubmatch(policy)
114-
// if len(matches) < 3 {
115-
// return "", fmt.Errorf("not a valid policy")
116-
// }
117-
//
118-
// return fmt.Sprintf("RECOVERY WINDOW OF %v %v", matches[1], unitName[matches[2]]), nil
119-
//}
112+
func ParseCutoffTimeFromPeriod(period string, now time.Time) (time.Time, error) {
113+
regexPolicy := regexp.MustCompile(`^([1-9][0-9]*)([dwmy])$`)
114+
unitFunc := map[string]func(int) time.Time{
115+
"d": func(v int) time.Time { return now.AddDate(0, 0, -v) },
116+
"w": func(v int) time.Time { return now.AddDate(0, 0, -v*7) },
117+
"m": func(v int) time.Time { return now.AddDate(0, -v, 0) },
118+
"y": func(v int) time.Time { return now.AddDate(-v, 0, 0) },
119+
}
120+
matches := regexPolicy.FindStringSubmatch(period)
121+
if len(matches) < 3 {
122+
return time.Time{}, fmt.Errorf("not a valid period")
123+
}
124+
value, err := strconv.Atoi(matches[1])
125+
if err != nil {
126+
return time.Time{}, fmt.Errorf("invalid numeric value: %w", err)
127+
}
128+
return unitFunc[matches[2]](value), nil
129+
}
120130

121131
type Task struct {
122132
Params *runtime.RawExtension `json:"params"`

crds/archiver.kubedb.com_mariadbarchivers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ spec:
62096209
format: int32
62106210
type: integer
62116211
retentionPeriod:
6212-
pattern: ^[1-9][0-9]*[dwm]$
6212+
pattern: ^[1-9][0-9]*[dwmy]$
62136213
type: string
62146214
runtimeSettings:
62156215
properties:

crds/archiver.kubedb.com_mongodbarchivers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ spec:
62096209
format: int32
62106210
type: integer
62116211
retentionPeriod:
6212-
pattern: ^[1-9][0-9]*[dwm]$
6212+
pattern: ^[1-9][0-9]*[dwmy]$
62136213
type: string
62146214
runtimeSettings:
62156215
properties:

crds/archiver.kubedb.com_mssqlserverarchivers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ spec:
62096209
format: int32
62106210
type: integer
62116211
retentionPeriod:
6212-
pattern: ^[1-9][0-9]*[dwm]$
6212+
pattern: ^[1-9][0-9]*[dwmy]$
62136213
type: string
62146214
runtimeSettings:
62156215
properties:

crds/archiver.kubedb.com_mysqlarchivers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ spec:
62096209
format: int32
62106210
type: integer
62116211
retentionPeriod:
6212-
pattern: ^[1-9][0-9]*[dwm]$
6212+
pattern: ^[1-9][0-9]*[dwmy]$
62136213
type: string
62146214
runtimeSettings:
62156215
properties:

crds/archiver.kubedb.com_postgresarchivers.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ spec:
62096209
format: int32
62106210
type: integer
62116211
retentionPeriod:
6212-
pattern: ^[1-9][0-9]*[dwm]$
6212+
pattern: ^[1-9][0-9]*[dwmy]$
62136213
type: string
62146214
runtimeSettings:
62156215
properties:

0 commit comments

Comments
 (0)