-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathbackupvars.go
More file actions
122 lines (96 loc) · 3.17 KB
/
Copy pathbackupvars.go
File metadata and controls
122 lines (96 loc) · 3.17 KB
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
package backuphelpers
import (
"fmt"
"reflect"
"strings"
"sync"
backupv1alpha1 "github.com/openshift/api/config/v1alpha1"
prune "github.com/openshift/cluster-etcd-operator/pkg/cmd/prune-backups"
)
type Enqueueable interface {
Enqueue()
}
type BackupVar interface {
AddListener(listener Enqueueable)
SetBackupSpec(spec *backupv1alpha1.EtcdBackupSpec)
ArgString() string
ArgList() []string
}
type BackupConfig struct {
enabled bool
spec *backupv1alpha1.EtcdBackupSpec
listeners []Enqueueable
mux sync.Mutex
}
func NewDisabledBackupConfig() *BackupConfig {
return &BackupConfig{
enabled: false,
mux: sync.Mutex{},
}
}
func (b *BackupConfig) SetBackupSpec(spec *backupv1alpha1.EtcdBackupSpec) {
b.mux.Lock()
defer b.mux.Unlock()
if reflect.DeepEqual(b.spec, spec) {
return
}
b.enabled = spec != nil
if spec == nil {
b.spec = nil
} else {
b.spec = spec.DeepCopy()
}
for _, l := range b.listeners {
l.Enqueue()
}
}
func (b *BackupConfig) ArgList() []string {
b.mux.Lock()
defer b.mux.Unlock()
args := []string{fmt.Sprintf("--%s=%v", "enabled", b.enabled)}
if !b.enabled || b.spec == nil {
return args
}
if b.spec.TimeZone != "" {
args = append(args, fmt.Sprintf("--%s=%s", "timezone", b.spec.TimeZone))
}
if b.spec.Schedule != "" {
args = append(args, fmt.Sprintf("--%s=%s", "schedule", b.spec.Schedule))
}
if b.spec.RetentionPolicy.RetentionType == prune.RetentionTypeNumber {
args = append(args, fmt.Sprintf("--%s=%s", "type", b.spec.RetentionPolicy.RetentionType))
args = append(args, fmt.Sprintf("--%s=%d", "maxNumberOfBackups", b.spec.RetentionPolicy.RetentionNumber.MaxNumberOfBackups))
} else if b.spec.RetentionPolicy.RetentionType == prune.RetentionTypeSize {
args = append(args, fmt.Sprintf("--%s=%s", "type", b.spec.RetentionPolicy.RetentionType))
args = append(args, fmt.Sprintf("--%s=%d", "maxSizeOfBackupsGb", b.spec.RetentionPolicy.RetentionSize.MaxSizeOfBackupsGb))
}
return args
}
func (b *BackupConfig) ArgString() string {
b.mux.Lock()
defer b.mux.Unlock()
args := []string{" args:"}
args = append(args, fmt.Sprintf("- --%s=%v", "enabled", b.enabled))
if !b.enabled || b.spec == nil {
return strings.Join(args, "\n ")
}
if b.spec.TimeZone != "" {
args = append(args, fmt.Sprintf("- --%s=%s", "timezone", b.spec.TimeZone))
}
if b.spec.Schedule != "" {
args = append(args, fmt.Sprintf("- --%s=%s", "schedule", b.spec.Schedule))
}
if b.spec.RetentionPolicy.RetentionType == prune.RetentionTypeNumber {
args = append(args, fmt.Sprintf("- --%s=%s", "type", b.spec.RetentionPolicy.RetentionType))
args = append(args, fmt.Sprintf("- --%s=%d", "maxNumberOfBackups", b.spec.RetentionPolicy.RetentionNumber.MaxNumberOfBackups))
} else if b.spec.RetentionPolicy.RetentionType == prune.RetentionTypeSize {
args = append(args, fmt.Sprintf("- --%s=%s", "type", b.spec.RetentionPolicy.RetentionType))
args = append(args, fmt.Sprintf("- --%s=%d", "maxSizeOfBackupsGb", b.spec.RetentionPolicy.RetentionSize.MaxSizeOfBackupsGb))
}
return strings.Join(args, "\n ")
}
func (b *BackupConfig) AddListener(listener Enqueueable) {
b.mux.Lock()
defer b.mux.Unlock()
b.listeners = append(b.listeners, listener)
}