This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
/
scheduledbackup_types.go
169 lines (138 loc) · 5.15 KB
/
scheduledbackup_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
162
163
164
165
166
167
168
169
/*
Copyright 2022 Riotkit.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"crypto/sha256"
"encoding/hex"
json "encoding/json"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)
// TemplateSpec represents .spec.templateRef section
type TemplateSpec struct {
Name string `json:"name"`
Kind string `json:"kind"`
}
// GPGKeySecretSpec represents .spec.gpgKeySecretRef section
type GPGKeySecretSpec struct {
SecretName string `json:"secretName"`
PublicKey string `json:"publicKey"`
PrivateKey string `json:"privateKey"`
PassphraseKey string `json:"passphraseKey"`
Email string `json:"email"`
CreateIfNotExists bool `json:"createIfNotExists"`
}
func (in *GPGKeySecretSpec) GetPrivateKeyIndex() string {
if in.PrivateKey == "" {
return "key"
}
return in.PrivateKey
}
func (in *GPGKeySecretSpec) GetPublicKeyIndex() string {
if in.PublicKey == "" {
return "key.pub"
}
return in.PublicKey
}
func (in *GPGKeySecretSpec) GetPassphraseIndex() string {
if in.PassphraseKey == "" {
return "passphrase"
}
return in.PassphraseKey
}
func (in *GPGKeySecretSpec) GetEmailIndex() string {
return "e-mail.txt"
}
// TokenSecretSpec represents .spec.tokenSecretRef
type TokenSecretSpec struct {
SecretName string `json:"secretName"`
TokenKey string `json:"tokenKey"`
}
// VarsSecretSpec represents .spec.varsSecretRef
type VarsSecretSpec struct {
SecretName string `json:"secretName,omitempty"`
ImportOnlyKeys []string `json:"importOnlyKeys,omitempty"`
}
// VarsSpec represents .spec.vars - a hashmap of values applied to template's backup & restore scripts
type VarsSpec string
// ScheduledBackupSpec defines the desired state of ScheduledBackup
type ScheduledBackupSpec struct {
CollectionId string `json:"collectionId"`
TemplateRef TemplateSpec `json:"templateRef"`
GPGKeySecretRef GPGKeySecretSpec `json:"gpgKeySecretRef"`
TokenSecretRef TokenSecretSpec `json:"tokenSecretRef"`
VarsSecretRef VarsSecretSpec `json:"varsSecretRef"`
Vars VarsSpec `json:"vars"`
CronJob CronJobSpec `json:"cronJob"`
// +kubebuilder:validation:Enum=backup;restore
Operation string `json:"operation"`
}
func (in *ScheduledBackupSpec) CalculateHash() string {
doc, _ := json.Marshal(in)
sum := sha256.Sum256(doc)
return hex.EncodeToString(sum[0:])
}
type CronJobSpec struct {
Enabled bool `json:"enabled"`
// +kubebuilder:default:="00 02 * * *"
ScheduleEvery string `json:"scheduleEvery,omitempty"`
}
// ScheduledBackupStatus defines the observed state of ScheduledBackup
type ScheduledBackupStatus struct {
LastAppliedSpecHash string `json:"lastAppliedSpecHash,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
OwnedReferences ChildrenReferences `json:"ownedReferences,omitempty"`
ChildrenResourcesHealth []JobHealthStatus `json:"childrenResourcesHealth,omitempty"`
Healthy bool `json:"healthy,omitempty"`
}
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Schedule",type="string",JSONPath=".spec.cronjob.scheduleEvery",description="Cron expression"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// ScheduledBackup is the Schema for the scheduledbackups API
type ScheduledBackup struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ScheduledBackupSpec `json:"spec,omitempty"`
Status ScheduledBackupStatus `json:"status,omitempty"`
}
// HasSpecChanged is telling if the current object's spec differs from the already applied object, basing on the status field
func (in *ScheduledBackup) HasSpecChanged() bool {
return in.Spec.CalculateHash() != in.Status.LastAppliedSpecHash
}
func (in *ScheduledBackup) IsBeingReconciledAlready() bool {
for _, condition := range in.Status.Conditions {
timeDiff := time.Now().Sub(condition.LastTransitionTime.Time).Seconds()
// if there were less than 60 seconds from previous reconcilation
if timeDiff < 60 && condition.Status == "Unknown" {
return true
}
}
return false
}
func (in *ScheduledBackup) GetTypeMeta() *metav1.TypeMeta {
return &in.TypeMeta
}
func (in *ScheduledBackup) GetObjectMeta() *metav1.ObjectMeta {
return &in.ObjectMeta
}
// +kubebuilder:object:root=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// ScheduledBackupList contains a list of ScheduledBackup
type ScheduledBackupList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ScheduledBackup `json:"items"`
}