-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
264 lines (216 loc) · 7.77 KB
/
handler.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
261
262
263
264
//
// DISCLAIMER
//
// Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package policy
import (
"context"
"fmt"
"reflect"
"time"
"github.com/robfig/cron"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/dalet-oss/arangodb-operator/pkg/apis/backup"
backupApi "github.com/dalet-oss/arangodb-operator/pkg/apis/backup/v1"
deployment "github.com/dalet-oss/arangodb-operator/pkg/apis/deployment/v1"
arangoClientSet "github.com/dalet-oss/arangodb-operator/pkg/generated/clientset/versioned"
operator "github.com/dalet-oss/arangodb-operator/pkg/operatorV2"
"github.com/dalet-oss/arangodb-operator/pkg/operatorV2/event"
"github.com/dalet-oss/arangodb-operator/pkg/operatorV2/operation"
"github.com/dalet-oss/arangodb-operator/pkg/util/errors"
"github.com/dalet-oss/arangodb-operator/pkg/util/globals"
"github.com/dalet-oss/arangodb-operator/pkg/util/k8sutil"
)
const (
backupCreated = "ArangoBackupCreated"
policyError = "Error"
rescheduled = "Rescheduled"
scheduleSkipped = "ScheduleSkipped"
)
type handler struct {
client arangoClientSet.Interface
kubeClient kubernetes.Interface
eventRecorder event.RecorderInstance
operator operator.Operator
}
func (*handler) Name() string {
return backup.ArangoBackupPolicyResourceKind
}
func (h *handler) Handle(item operation.Item) error {
// Do not act on delete event, finalizers are used
if item.Operation == operation.Delete {
return nil
}
// Get Backup object. It also cover NotFound case
policy, err := h.client.BackupV1().ArangoBackupPolicies(item.Namespace).Get(context.Background(), item.Name, meta.GetOptions{})
if err != nil {
return err
}
status := h.processBackupPolicy(policy.DeepCopy())
// Nothing to update, objects are equal
if reflect.DeepEqual(policy.Status, status) {
return nil
}
policy.Status = status
// Update status on object
if _, err = h.client.BackupV1().ArangoBackupPolicies(item.Namespace).UpdateStatus(context.Background(), policy, meta.UpdateOptions{}); err != nil {
return err
}
return nil
}
func (h *handler) processBackupPolicy(policy *backupApi.ArangoBackupPolicy) backupApi.ArangoBackupPolicyStatus {
if err := policy.Validate(); err != nil {
h.eventRecorder.Warning(policy, policyError, "Policy Error: %s", err.Error())
return backupApi.ArangoBackupPolicyStatus{
Message: fmt.Sprintf("Validation error: %s", err.Error()),
}
}
now := time.Now()
expr, err := cron.ParseStandard(policy.Spec.Schedule)
if err != nil {
h.eventRecorder.Warning(policy, policyError, "Policy Error: %s", err.Error())
return backupApi.ArangoBackupPolicyStatus{
Message: fmt.Sprintf("error while parsing expr: %s", err.Error()),
}
}
if policy.Status.Scheduled.IsZero() {
next := expr.Next(now)
return backupApi.ArangoBackupPolicyStatus{
Scheduled: meta.Time{
Time: next,
},
}
}
// Check if schedule is required
if policy.Status.Scheduled.Unix() > now.Unix() {
// check if we need to update schedule in case that string changed
// in other case schedule string will be updated after scheduling objects
next := expr.Next(now)
if next != policy.Status.Scheduled.Time {
return backupApi.ArangoBackupPolicyStatus{
Scheduled: meta.Time{
Time: next,
},
}
}
return policy.Status
}
// Schedule new deployments
listOptions := meta.ListOptions{}
if policy.Spec.DeploymentSelector != nil &&
(policy.Spec.DeploymentSelector.MatchLabels != nil &&
len(policy.Spec.DeploymentSelector.MatchLabels) > 0 ||
policy.Spec.DeploymentSelector.MatchExpressions != nil) {
listOptions.LabelSelector = meta.FormatLabelSelector(policy.Spec.DeploymentSelector)
}
deployments, err := h.client.DatabaseV1().ArangoDeployments(policy.Namespace).List(context.Background(), listOptions)
if err != nil {
h.eventRecorder.Warning(policy, policyError, "Policy Error: %s", err.Error())
return backupApi.ArangoBackupPolicyStatus{
Scheduled: policy.Status.Scheduled,
Message: fmt.Sprintf("deployments listing failed: %s", err.Error()),
}
}
for _, deployment := range deployments.Items {
depl := deployment.DeepCopy()
if !policy.Spec.GetAllowConcurrent() {
previousBackupInProgress, err := h.isPreviousBackupInProgress(context.Background(), depl, policy.Name)
if err != nil {
h.eventRecorder.Warning(policy, policyError, "Policy Error: %s", err.Error())
return backupApi.ArangoBackupPolicyStatus{
Scheduled: policy.Status.Scheduled,
Message: fmt.Sprintf("backup creation failed: %s", err.Error()),
}
}
if previousBackupInProgress {
eventMsg := fmt.Sprintf("Skipping ArangoBackup creation because earlier backup still running %s/%s", deployment.Namespace, deployment.Name)
h.eventRecorder.Normal(policy, scheduleSkipped, eventMsg)
continue
}
}
b := policy.NewBackup(depl)
if _, err := h.client.BackupV1().ArangoBackups(b.Namespace).Create(context.Background(), b, meta.CreateOptions{}); err != nil {
h.eventRecorder.Warning(policy, policyError, "Policy Error: %s", err.Error())
return backupApi.ArangoBackupPolicyStatus{
Scheduled: policy.Status.Scheduled,
Message: fmt.Sprintf("backup creation failed: %s", err.Error()),
}
}
h.eventRecorder.Normal(policy, backupCreated, "Created ArangoBackup: %s/%s", b.Namespace, b.Name)
}
next := expr.Next(time.Now())
h.eventRecorder.Normal(policy, rescheduled, "Rescheduled for: %s", next.String())
return backupApi.ArangoBackupPolicyStatus{
Scheduled: meta.Time{
Time: next,
},
}
}
func (*handler) CanBeHandled(item operation.Item) bool {
return item.Group == backupApi.SchemeGroupVersion.Group &&
item.Version == backupApi.SchemeGroupVersion.Version &&
item.Kind == backup.ArangoBackupPolicyResourceKind
}
func (h *handler) listAllBackupsForPolicy(ctx context.Context, d *deployment.ArangoDeployment, policyName string) ([]*backupApi.ArangoBackup, error) {
var r []*backupApi.ArangoBackup
if err := k8sutil.APIList[*backupApi.ArangoBackupList](ctx, h.client.BackupV1().ArangoBackups(d.Namespace), meta.ListOptions{
Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(),
}, func(result *backupApi.ArangoBackupList, err error) error {
if err != nil {
return err
}
for _, b := range result.Items {
if b.Spec.PolicyName == nil || *b.Spec.PolicyName != policyName {
continue
}
if b.Spec.Deployment.Name != d.Name {
continue
}
r = append(r, b.DeepCopy())
}
return nil
}); err != nil {
return nil, err
}
return r, nil
}
func (h *handler) isPreviousBackupInProgress(ctx context.Context, d *deployment.ArangoDeployment, policyName string) (bool, error) {
// It would be nice to List CRs with fieldSelector set, but this is not supported:
// https://github.com/kubernetes/kubernetes/issues/53459
// Instead we fetch all ArangoBackups:
backups, err := h.listAllBackupsForPolicy(ctx, d, policyName)
if err != nil {
return false, errors.Wrap(err, "Failed to list ArangoBackups")
}
for _, b := range backups {
// Check if we are in the failed state
switch b.Status.State {
case backupApi.ArangoBackupStateFailed:
continue
}
if b.Spec.Download != nil {
continue
}
// Backup is not yet done
if b.Status.Backup == nil {
return true, nil
}
}
return false, nil
}