-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_types.go
161 lines (141 loc) · 4.6 KB
/
collection_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package v1beta1
import (
"fmt"
"strconv"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CycleCronByMinuteInSameHour cycles cron minute.
// Assumes cronStr is "1 2 3 4 5" format.
// "30 6 * * *" -> "31 6 * * *" -> ... "59 6 * * *" -> "0 6 * * *" -> "1 6 * * *" -> ...
func CycleCronByMinuteInSameHour(cronStr string) (string, error) {
ss := strings.Split(cronStr, " ")
if len(ss) != 5 {
return "", fmt.Errorf("cronStr must be \"1 2 3 4 5\" format but got %s", cronStr)
}
minute, err := strconv.Atoi(ss[0])
if err != nil || minute < 0 || minute >= 60 {
return "", fmt.Errorf("cronStr has invalid minute field cronStr=%s minute=%d err=%v", cronStr, minute, err)
}
minute = (minute + 1) % 60
ss[0] = strconv.Itoa(minute)
return strings.Join(ss, " "), nil
}
// GetOwnedConfigMapName returns "gitbackup-collection-{r.Name}-gitconfig"
func (r Collection) GetOwnedConfigMapName() string {
return strings.Join([]string{OperatorName, "collection", r.Name, "gitconfig"}, "-")
}
func ToRFC1123(s string, def string) string {
s = strings.ToLower(s)
s = strings.ReplaceAll(s, "_", "-")
var sb strings.Builder
for _, c := range s {
if c == '.' || c == '-' || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') {
sb.WriteByte(byte(c))
}
}
s = sb.String()
a := 0
b := len(s)
for i, c := range s {
if c != '.' && c != '-' {
a = i
break
}
// only '.' or '-'
if i == len(s)-1 {
return def
}
}
for i := len(s) - 1; i >= 0; i-- {
if s[i] != '.' && s[i] != '-' {
b = i + 1
break
}
}
s = s[a:b]
if len(s) == 0 {
return def
}
if len(s) > 253 {
return s[0:253]
}
return s
}
// GetOwnedRepositoryNames returns ["{r.Name}-{r.Repos[i].Name}", ...]
func (r Collection) GetOwnedRepositoryNames() []string {
prefix := strings.Join([]string{r.Name, ""}, "-")
names := make([]string, len(r.Spec.Repos))
for i, cr := range r.Spec.Repos {
var name string
if cr.Name != nil {
name = *cr.Name
} else {
// use the last element of cr.Src as name
crSrc := strings.Split(cr.Src, "/")
// convert repo name to RFC1123 DNS Subdomain Names
name = ToRFC1123(crSrc[len(crSrc)-1], "invalid-name")
}
names[i] = prefix + name
}
return names
}
// CollectionSpec defines the desired state of Collection
type CollectionSpec struct {
// Schedule in Cron format.
Schedule string `json:"schedule"`
// TimeZone in TZ database name.
// See also: https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/#time-zones
// +optional
TimeZone *string `json:"timeZone,omitempty"`
// GitImage specifies the container image to run.
// +optional
GitImage *string `json:"gitImage,omitempty"`
// ImagePullSecret specifies the name of the Secret in the same namespace used to pull the GitImage.
// +optional
ImagePullSecret *corev1.LocalObjectReference `json:"imagePullSecret,omitempty"`
// GitConfig specifies the name of the configmap resource in the same namespace used to mount .git-config
// Note that "[credential]\nhelper=store" is required to use GitCredentials.
// +optional
GitConfig *corev1.LocalObjectReference `json:"gitConfig,omitempty"`
// GitCredentials specifies the name of the Secret in the same namespace used to mount .git-credentials
// +optional
GitCredentials *corev1.LocalObjectReference `json:"gitCredentials,omitempty"`
// Repos specifies repositories to backup.
Repos []CollectionRepoURL `json:"repos"`
}
type CollectionRepoURL struct {
// Name specifies the name for the repository. (default: the last element of `Src`)
// +optional
Name *string `json:"name,omitempty"`
// Src specifies the source repository in URL format.
Src string `json:"src"`
// Dst specifies the destination repository in URL format.
Dst string `json:"dst"`
}
// CollectionStatus defines the observed state of Collection
type CollectionStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:shortName=coll;colls
// Collection is the Schema for the collections API
type Collection struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec CollectionSpec `json:"spec,omitempty"`
Status CollectionStatus `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// CollectionList contains a list of Collection
type CollectionList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Collection `json:"items"`
}
func init() {
SchemeBuilder.Register(&Collection{}, &CollectionList{})
}