-
Notifications
You must be signed in to change notification settings - Fork 489
/
backup_control.go
165 lines (139 loc) · 6.02 KB
/
backup_control.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
// Copyright 2019 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package backup
import (
"context"
"fmt"
"github.com/pingcap/tidb-operator/pkg/apis/label"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/backup"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/third_party/k8s"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
)
// ControlInterface implements the control logic for updating Backup
// It is implemented as an interface to allow for extensions that provide different semantics.
// Currently, there is only one implementation.
type ControlInterface interface {
// UpdateBackup implements the control logic for backup job and backup clean job's creation, deletion
UpdateBackup(backup *v1alpha1.Backup) error
// UpdateStatus updates the status for a Backup, include condition and status info
UpdateStatus(backup *v1alpha1.Backup, condition *v1alpha1.BackupCondition, newStatus *controller.BackupUpdateStatus) error
}
// NewDefaultBackupControl returns a new instance of the default implementation BackupControlInterface that
// implements the documented semantics for Backup.
func NewDefaultBackupControl(
cli versioned.Interface,
backupManager backup.BackupManager) ControlInterface {
return &defaultBackupControl{
cli,
backupManager,
}
}
type defaultBackupControl struct {
cli versioned.Interface
backupManager backup.BackupManager
}
// UpdateBackup executes the core logic loop for a Backup.
func (c *defaultBackupControl) UpdateBackup(backup *v1alpha1.Backup) error {
backup.SetGroupVersionKind(controller.BackupControllerKind)
if err := c.addProtectionFinalizer(backup); err != nil {
return err
}
if err := c.removeProtectionFinalizer(backup); err != nil {
return err
}
return c.updateBackup(backup)
}
// UpdateStatus updates the status for a Backup, include condition and status info
func (c *defaultBackupControl) UpdateStatus(backup *v1alpha1.Backup, condition *v1alpha1.BackupCondition, newStatus *controller.BackupUpdateStatus) error {
return c.backupManager.UpdateStatus(backup, condition, newStatus)
}
func (c *defaultBackupControl) updateBackup(backup *v1alpha1.Backup) error {
return c.backupManager.Sync(backup)
}
// addProtectionFinalizer will be called when the Backup CR is created
func (c *defaultBackupControl) addProtectionFinalizer(backup *v1alpha1.Backup) error {
ns := backup.GetNamespace()
name := backup.GetName()
if needToAddFinalizer(backup) {
backup.Finalizers = append(backup.Finalizers, label.BackupProtectionFinalizer)
_, err := c.cli.PingcapV1alpha1().Backups(ns).Update(context.TODO(), backup, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("add backup %s/%s protection finalizers failed, err: %v", ns, name, err)
}
}
return nil
}
func (c *defaultBackupControl) removeProtectionFinalizer(backup *v1alpha1.Backup) error {
ns := backup.GetNamespace()
name := backup.GetName()
if needToRemoveFinalizer(backup) {
backup.Finalizers = k8s.RemoveString(backup.Finalizers, label.BackupProtectionFinalizer, nil)
_, err := c.cli.PingcapV1alpha1().Backups(ns).Update(context.TODO(), backup, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("remove backup %s/%s protection finalizers failed, err: %v", ns, name, err)
}
klog.Infof("remove backup %s/%s protection finalizers success", ns, name)
return controller.RequeueErrorf(fmt.Sprintf("backup %s/%s has been cleaned up", ns, name))
}
return nil
}
func needToAddFinalizer(backup *v1alpha1.Backup) bool {
return backup.DeletionTimestamp == nil && v1alpha1.IsCleanCandidate(backup) && !k8s.ContainsString(backup.Finalizers, label.BackupProtectionFinalizer, nil)
}
func needToRemoveFinalizer(backup *v1alpha1.Backup) bool {
return v1alpha1.IsCleanCandidate(backup) && isDeletionCandidate(backup) &&
(v1alpha1.IsBackupClean(backup) || v1alpha1.NeedNotClean(backup))
}
func isDeletionCandidate(backup *v1alpha1.Backup) bool {
return backup.DeletionTimestamp != nil && k8s.ContainsString(backup.Finalizers, label.BackupProtectionFinalizer, nil)
}
var _ ControlInterface = &defaultBackupControl{}
// FakeBackupControl is a fake BackupControlInterface
type FakeBackupControl struct {
backupIndexer cache.Indexer
updateBackupTracker controller.RequestTracker
condition *v1alpha1.BackupCondition
}
// NewFakeBackupControl returns a FakeBackupControl
func NewFakeBackupControl(backupInformer informers.BackupInformer) *FakeBackupControl {
return &FakeBackupControl{
backupInformer.Informer().GetIndexer(),
controller.RequestTracker{},
nil,
}
}
// SetUpdateBackupError sets the error attributes of updateBackupTracker
func (c *FakeBackupControl) SetUpdateBackupError(err error, after int) {
c.updateBackupTracker.SetError(err).SetAfter(after)
}
// UpdateBackup adds the backup to BackupIndexer
func (c *FakeBackupControl) UpdateBackup(backup *v1alpha1.Backup) error {
defer c.updateBackupTracker.Inc()
if c.updateBackupTracker.ErrorReady() {
defer c.updateBackupTracker.Reset()
return c.updateBackupTracker.GetError()
}
return c.backupIndexer.Add(backup)
}
// UpdateStatus updates the status for a Backup, include condition and status info
func (c *FakeBackupControl) UpdateStatus(_ *v1alpha1.Backup, condition *v1alpha1.BackupCondition, newStatus *controller.BackupUpdateStatus) error {
c.condition = condition
return nil
}
var _ ControlInterface = &FakeBackupControl{}