/
builder.go
206 lines (173 loc) · 6.92 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
198
199
200
201
202
203
204
205
206
/*
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 rsynctls
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 (
// defaultRsyncContainerImage is the default container image for the
// rsync-tls data mover
defaultRsyncTLSContainerImage = "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
rsyncTLSContainerImageFlag = "rsync-tls-container-image"
rsyncTLSContainerImageEnvVar = "RELATED_IMAGE_RSYNC_TLS_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 rsync-tls container image - will be used if both command line flag and env var are not set
b.viper.SetDefault(rsyncTLSContainerImageFlag, defaultRsyncTLSContainerImage)
// Setup command line flag for the rsync-tls container image
b.flags.String(rsyncTLSContainerImageFlag, defaultRsyncTLSContainerImage,
"The container image for the rsync-tls data mover")
// Viper will check for command line flag first, then fallback to the env var
err := b.viper.BindEnv(rsyncTLSContainerImageFlag, rsyncTLSContainerImageEnvVar)
return b, err
}
func (rb *Builder) VersionInfo() string {
return fmt.Sprintf("Rsync TLS container: %s", rb.getRsyncTLSContainerImage())
}
// rsyncTLSContainerImage is the container image name of the rsync-tls data mover
func (rb *Builder) getRsyncTLSContainerImage() string {
return rb.viper.GetString(rsyncTLSContainerImageFlag)
}
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.RsyncTLS == nil {
return nil, nil
}
// Make sure there's a place to write status info
if source.Status.RsyncTLS == nil {
source.Status.RsyncTLS = &volsyncv1alpha1.ReplicationSourceRsyncTLSStatus{}
}
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.RsyncTLS.ReplicationSourceVolumeOptions),
)
if err != nil {
return nil, err
}
isSource := true
saHandler := utils.NewSAHandler(client, source, isSource, privileged,
source.Spec.RsyncTLS.MoverServiceAccount)
return &Mover{
client: client,
logger: logger.WithValues("method", "RsyncTLS"),
eventRecorder: eventRecorder,
owner: source,
vh: vh,
saHandler: saHandler,
containerImage: rb.getRsyncTLSContainerImage(),
key: source.Spec.RsyncTLS.KeySecret,
serviceType: nil,
serviceAnnotations: nil,
address: source.Spec.RsyncTLS.Address,
port: source.Spec.RsyncTLS.Port,
isSource: isSource,
paused: source.Spec.Paused,
mainPVCName: &source.Spec.SourcePVC,
privileged: privileged,
sourceStatus: source.Status.RsyncTLS,
latestMoverStatus: source.Status.LatestMoverStatus,
moverConfig: source.Spec.RsyncTLS.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.RsyncTLS == nil {
return nil, nil
}
// Make sure there's a place to write status info
if destination.Status.RsyncTLS == nil {
destination.Status.RsyncTLS = &volsyncv1alpha1.ReplicationDestinationRsyncTLSStatus{}
}
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.RsyncTLS.ReplicationDestinationVolumeOptions),
)
if err != nil {
return nil, err
}
isSource := false
saHandler := utils.NewSAHandler(client, destination, isSource, privileged,
destination.Spec.RsyncTLS.MoverServiceAccount)
var svcAnnotations map[string]string
if destination.Spec.RsyncTLS.ServiceAnnotations != nil {
// If nil we will assume VolSync can set defaults
// if not nil, we will assume we will use the users settings (and empty map will mean
// we do not set any annotations at all on the service)
svcAnnotations = *destination.Spec.RsyncTLS.ServiceAnnotations
}
return &Mover{
client: client,
logger: logger.WithValues("method", "RsyncTLS"),
eventRecorder: eventRecorder,
owner: destination,
vh: vh,
saHandler: saHandler,
containerImage: rb.getRsyncTLSContainerImage(),
key: destination.Spec.RsyncTLS.KeySecret,
serviceType: destination.Spec.RsyncTLS.ServiceType,
serviceAnnotations: svcAnnotations,
address: nil,
port: nil,
isSource: isSource,
paused: destination.Spec.Paused,
mainPVCName: destination.Spec.RsyncTLS.DestinationPVC,
privileged: privileged,
destStatus: destination.Status.RsyncTLS,
latestMoverStatus: destination.Status.LatestMoverStatus,
moverConfig: destination.Spec.RsyncTLS.MoverConfig,
}, nil
}