/
backupbatch.go
260 lines (223 loc) · 8.71 KB
/
backupbatch.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright AppsCode Inc. and Contributors
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 invoker
import (
"context"
"stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"
stash_scheme "stash.appscode.dev/apimachinery/client/clientset/versioned/scheme"
v1beta1_util "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1/util"
"gomodules.xyz/pointer"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/reference"
kmapi "kmodules.xyz/client-go/api/v1"
core_util "kmodules.xyz/client-go/core/v1"
"kmodules.xyz/client-go/meta"
ofst "kmodules.xyz/offshoot-api/api/v1"
)
type BackupBatchInvoker struct {
backupBatch *v1beta1.BackupBatch
stashClient cs.Interface
}
func NewBackupBatchInvoker(stashClient cs.Interface, backupBatch *v1beta1.BackupBatch) BackupInvoker {
return &BackupBatchInvoker{
stashClient: stashClient,
backupBatch: backupBatch,
}
}
func (inv *BackupBatchInvoker) GetObjectMeta() metav1.ObjectMeta {
return inv.backupBatch.ObjectMeta
}
func (inv *BackupBatchInvoker) GetTypeMeta() metav1.TypeMeta {
return metav1.TypeMeta{
Kind: v1beta1.ResourceKindBackupBatch,
APIVersion: v1beta1.SchemeGroupVersion.String(),
}
}
func (inv *BackupBatchInvoker) GetObjectRef() (*core.ObjectReference, error) {
return reference.GetReference(stash_scheme.Scheme, inv.backupBatch)
}
func (inv *BackupBatchInvoker) GetOwnerRef() *metav1.OwnerReference {
return metav1.NewControllerRef(inv.backupBatch, v1beta1.SchemeGroupVersion.WithKind(v1beta1.ResourceKindBackupBatch))
}
func (inv *BackupBatchInvoker) GetLabels() map[string]string {
return inv.backupBatch.OffshootLabels()
}
func (inv *BackupBatchInvoker) AddFinalizer() error {
updatedBackupBatch, _, err := v1beta1_util.PatchBackupBatch(context.TODO(), inv.stashClient.StashV1beta1(), inv.backupBatch, func(in *v1beta1.BackupBatch) *v1beta1.BackupBatch {
in.ObjectMeta = core_util.AddFinalizer(in.ObjectMeta, v1beta1.StashKey)
return in
}, metav1.PatchOptions{})
if err != nil {
return err
}
inv.backupBatch = updatedBackupBatch
return nil
}
func (inv *BackupBatchInvoker) RemoveFinalizer() error {
updatedBackupBatch, _, err := v1beta1_util.PatchBackupBatch(context.TODO(), inv.stashClient.StashV1beta1(), inv.backupBatch, func(in *v1beta1.BackupBatch) *v1beta1.BackupBatch {
in.ObjectMeta = core_util.RemoveFinalizer(in.ObjectMeta, v1beta1.StashKey)
return in
}, metav1.PatchOptions{})
if err != nil {
return err
}
inv.backupBatch = updatedBackupBatch
return nil
}
func (inv *BackupBatchInvoker) HasCondition(target *v1beta1.TargetRef, conditionType string) (bool, error) {
backupBatch, err := inv.stashClient.StashV1beta1().BackupBatches(inv.backupBatch.Namespace).Get(context.TODO(), inv.backupBatch.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
if target != nil {
return hasMemberCondition(backupBatch.Status.MemberConditions, *target, conditionType), nil
}
return kmapi.HasCondition(backupBatch.Status.Conditions, conditionType), nil
}
func (inv *BackupBatchInvoker) GetCondition(target *v1beta1.TargetRef, conditionType string) (int, *kmapi.Condition, error) {
backupBatch, err := inv.stashClient.StashV1beta1().BackupBatches(inv.backupBatch.Namespace).Get(context.TODO(), inv.backupBatch.Name, metav1.GetOptions{})
if err != nil {
return -1, nil, err
}
if target != nil {
idx, cond := getMemberCondition(backupBatch.Status.MemberConditions, *target, conditionType)
return idx, cond, nil
}
idx, cond := kmapi.GetCondition(backupBatch.Status.Conditions, conditionType)
return idx, cond, nil
}
func (inv *BackupBatchInvoker) SetCondition(target *v1beta1.TargetRef, newCondition kmapi.Condition) error {
updatedBackupBatch, err := v1beta1_util.UpdateBackupBatchStatus(context.TODO(), inv.stashClient.StashV1beta1(), inv.backupBatch.ObjectMeta, func(in *v1beta1.BackupBatchStatus) (types.UID, *v1beta1.BackupBatchStatus) {
if target != nil {
in.MemberConditions = setMemberCondition(in.MemberConditions, *target, newCondition)
} else {
in.Conditions = kmapi.SetCondition(in.Conditions, newCondition)
}
in.Phase = CalculateBackupInvokerPhase(inv.GetDriver(), in.Conditions)
return inv.backupBatch.UID, in
}, metav1.UpdateOptions{})
if err != nil {
return err
}
inv.backupBatch = updatedBackupBatch
return nil
}
func (inv *BackupBatchInvoker) IsConditionTrue(target *v1beta1.TargetRef, conditionType string) (bool, error) {
backupBatch, err := inv.stashClient.StashV1beta1().BackupBatches(inv.backupBatch.Namespace).Get(context.TODO(), inv.backupBatch.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
if target != nil {
return isMemberConditionTrue(backupBatch.Status.MemberConditions, *target, conditionType), nil
}
return kmapi.IsConditionTrue(backupBatch.Status.Conditions, conditionType), nil
}
func (inv *BackupBatchInvoker) GetTargetInfo() []BackupTargetInfo {
var targetInfo []BackupTargetInfo
for _, member := range inv.backupBatch.Spec.Members {
targetInfo = append(targetInfo, BackupTargetInfo{
Task: member.Task,
Target: member.Target,
RuntimeSettings: member.RuntimeSettings,
TempDir: member.TempDir,
InterimVolumeTemplate: member.InterimVolumeTemplate,
Hooks: member.Hooks,
})
}
return targetInfo
}
func (inv *BackupBatchInvoker) GetDriver() v1beta1.Snapshotter {
driver := inv.backupBatch.Spec.Driver
if driver == "" {
driver = v1beta1.ResticSnapshotter
}
return driver
}
func (inv *BackupBatchInvoker) GetRepoRef() kmapi.ObjectReference {
var repo kmapi.ObjectReference
repo.Name = inv.backupBatch.Spec.Repository.Name
repo.Namespace = inv.backupBatch.Spec.Repository.Namespace
if repo.Namespace == "" {
repo.Namespace = inv.backupBatch.Namespace
}
return repo
}
func (inv *BackupBatchInvoker) GetRepository() (*v1alpha1.Repository, error) {
repo := inv.GetRepoRef()
return inv.stashClient.StashV1alpha1().Repositories(repo.Namespace).Get(context.TODO(), repo.Name, metav1.GetOptions{})
}
func (inv *BackupBatchInvoker) GetRuntimeSettings() ofst.RuntimeSettings {
return inv.backupBatch.Spec.RuntimeSettings
}
func (inv *BackupBatchInvoker) GetSchedule() string {
return inv.backupBatch.Spec.Schedule
}
func (inv *BackupBatchInvoker) IsPaused() bool {
return inv.backupBatch.Spec.Paused
}
func (inv *BackupBatchInvoker) GetBackupHistoryLimit() *int32 {
return inv.backupBatch.Spec.BackupHistoryLimit
}
func (inv *BackupBatchInvoker) GetGlobalHooks() *v1beta1.BackupHooks {
return inv.backupBatch.Spec.Hooks
}
func (inv *BackupBatchInvoker) GetExecutionOrder() v1beta1.ExecutionOrder {
return inv.backupBatch.Spec.ExecutionOrder
}
func (inv *BackupBatchInvoker) NextInOrder(curTarget v1beta1.TargetRef, targetStatus []v1beta1.BackupTargetStatus) bool {
for _, t := range inv.GetTargetInfo() {
if t.Target != nil {
if TargetMatched(t.Target.Ref, curTarget) {
return true
}
if !TargetBackupCompleted(t.Target.Ref, targetStatus) {
return false
}
}
}
// By default, return true so that nil target(i.e. cluster backup) does not get stuck here.
return true
}
func (inv *BackupBatchInvoker) GetHash() string {
return inv.backupBatch.GetSpecHash()
}
func (inv *BackupBatchInvoker) GetObjectJSON() (string, error) {
jsonObj, err := meta.MarshalToJson(inv.backupBatch, v1beta1.SchemeGroupVersion)
if err != nil {
return "", err
}
return string(jsonObj), nil
}
func (inv *BackupBatchInvoker) GetRuntimeObject() runtime.Object {
return inv.backupBatch
}
func (inv *BackupBatchInvoker) GetRetentionPolicy() v1alpha1.RetentionPolicy {
return inv.backupBatch.Spec.RetentionPolicy
}
func (inv *BackupBatchInvoker) GetPhase() v1beta1.BackupInvokerPhase {
return inv.backupBatch.Status.Phase
}
func (inv *BackupBatchInvoker) GetSummary(target v1beta1.TargetRef, session kmapi.ObjectReference) *v1beta1.Summary {
summary := getTargetBackupSummary(inv.stashClient, target, session)
summary.Invoker = core.TypedLocalObjectReference{
APIGroup: pointer.StringP(v1beta1.SchemeGroupVersion.Group),
Kind: v1beta1.ResourceKindBackupBatch,
Name: inv.backupBatch.Name,
}
return summary
}