/
builder.go
197 lines (166 loc) · 6.88 KB
/
builder.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
/*
Copyright 2021 The VolSync authors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package restic
import (
"flag"
"fmt"
"github.com/go-logr/logr"
"github.com/spf13/viper"
"k8s.io/client-go/tools/events"
"sigs.k8s.io/controller-runtime/pkg/client"
volsyncv1alpha1 "github.com/backube/volsync/api/v1alpha1"
"github.com/backube/volsync/controllers/mover"
"github.com/backube/volsync/controllers/utils"
"github.com/backube/volsync/controllers/volumehandler"
)
const (
// defaultResticContainerImage is the default container image for the restic
// data mover
defaultResticContainerImage = "quay.io/backube/volsync:latest"
// Command line flag will be checked first
// If command line flag not set, the RELATED_IMAGE_ env var will be used
resticContainerImageFlag = "restic-container-image"
resticContainerImageEnvVar = "RELATED_IMAGE_RESTIC_CONTAINER"
)
type Builder struct {
viper *viper.Viper // For unit tests to be able to override - global viper will be used by default in Register()
flags *flag.FlagSet // For unit tests to be able to override - global flags will be used by default in Register()
}
var _ mover.Builder = &Builder{}
func Register() error {
// Use global viper & command line flags
b, err := newBuilder(viper.GetViper(), flag.CommandLine)
if err != nil {
return err
}
mover.Register(b)
return nil
}
func newBuilder(viper *viper.Viper, flags *flag.FlagSet) (*Builder, error) {
b := &Builder{
viper: viper,
flags: flags,
}
// Set default restic container image - will be used if both command line flag and env var are not set
b.viper.SetDefault(resticContainerImageFlag, defaultResticContainerImage)
// Setup command line flag for the restic container image
b.flags.String(resticContainerImageFlag, defaultResticContainerImage,
"The container image for the restic data mover")
// Viper will check for command line flag first, then fallback to the env var
err := b.viper.BindEnv(resticContainerImageFlag, resticContainerImageEnvVar)
return b, err
}
func (rb *Builder) VersionInfo() string {
return fmt.Sprintf("Restic container: %s", rb.getResticContainerImage())
}
// resticContainerImage is the container image name of the restic data mover
func (rb *Builder) getResticContainerImage() string {
return rb.viper.GetString(resticContainerImageFlag)
}
func (rb *Builder) FromSource(client client.Client, logger logr.Logger,
eventRecorder events.EventRecorder,
source *volsyncv1alpha1.ReplicationSource, privileged bool) (mover.Mover, error) {
// Only build if the CR belongs to us
if source.Spec.Restic == nil {
return nil, nil
}
// Create ReplicationSourceResticStatus to write restic status
if source.Status.Restic == nil {
source.Status.Restic = &volsyncv1alpha1.ReplicationSourceResticStatus{}
}
if source.Status.LatestMoverStatus == nil {
source.Status.LatestMoverStatus = &volsyncv1alpha1.MoverStatus{}
}
vh, err := volumehandler.NewVolumeHandler(
volumehandler.WithClient(client),
volumehandler.WithRecorder(eventRecorder),
volumehandler.WithOwner(source),
volumehandler.FromSource(&source.Spec.Restic.ReplicationSourceVolumeOptions),
)
if err != nil {
return nil, err
}
isSource := true
saHandler := utils.NewSAHandler(client, source, isSource, privileged,
source.Spec.Restic.MoverServiceAccount)
return &Mover{
client: client,
logger: logger.WithValues("method", "Restic"),
eventRecorder: eventRecorder,
owner: source,
vh: vh,
saHandler: saHandler,
containerImage: rb.getResticContainerImage(),
cacheAccessModes: source.Spec.Restic.CacheAccessModes,
cacheCapacity: source.Spec.Restic.CacheCapacity,
cacheStorageClassName: source.Spec.Restic.CacheStorageClassName,
repositoryName: source.Spec.Restic.Repository,
isSource: isSource,
paused: source.Spec.Paused,
mainPVCName: &source.Spec.SourcePVC,
customCASpec: volsyncv1alpha1.CustomCASpec(source.Spec.Restic.CustomCA),
privileged: privileged,
pruneInterval: source.Spec.Restic.PruneIntervalDays,
retainPolicy: source.Spec.Restic.Retain,
unlock: source.Spec.Restic.Unlock,
sourceStatus: source.Status.Restic,
latestMoverStatus: source.Status.LatestMoverStatus,
moverConfig: source.Spec.Restic.MoverConfig,
}, nil
}
func (rb *Builder) FromDestination(client client.Client, logger logr.Logger,
eventRecorder events.EventRecorder,
destination *volsyncv1alpha1.ReplicationDestination, privileged bool) (mover.Mover, error) {
// Only build if the CR belongs to us
if destination.Spec.Restic == nil {
return nil, nil
}
if destination.Status.LatestMoverStatus == nil {
destination.Status.LatestMoverStatus = &volsyncv1alpha1.MoverStatus{}
}
vh, err := volumehandler.NewVolumeHandler(
volumehandler.WithClient(client),
volumehandler.WithRecorder(eventRecorder),
volumehandler.WithOwner(destination),
volumehandler.FromDestination(&destination.Spec.Restic.ReplicationDestinationVolumeOptions),
)
if err != nil {
return nil, err
}
isSource := false
saHandler := utils.NewSAHandler(client, destination, isSource, privileged,
destination.Spec.Restic.MoverServiceAccount)
return &Mover{
client: client,
logger: logger.WithValues("method", "Restic"),
eventRecorder: eventRecorder,
owner: destination,
vh: vh,
saHandler: saHandler,
containerImage: rb.getResticContainerImage(),
cacheAccessModes: destination.Spec.Restic.CacheAccessModes,
cacheCapacity: destination.Spec.Restic.CacheCapacity,
cacheStorageClassName: destination.Spec.Restic.CacheStorageClassName,
repositoryName: destination.Spec.Restic.Repository,
isSource: isSource,
paused: destination.Spec.Paused,
mainPVCName: destination.Spec.Restic.DestinationPVC,
customCASpec: volsyncv1alpha1.CustomCASpec(destination.Spec.Restic.CustomCA),
privileged: privileged,
restoreAsOf: destination.Spec.Restic.RestoreAsOf,
previous: destination.Spec.Restic.Previous,
latestMoverStatus: destination.Status.LatestMoverStatus,
moverConfig: destination.Spec.Restic.MoverConfig,
}, nil
}