-
Notifications
You must be signed in to change notification settings - Fork 90
/
filesystem_lvp.go
333 lines (279 loc) · 11.4 KB
/
filesystem_lvp.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package snapshot
import (
"context"
"fmt"
"strconv"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/image"
"github.com/replicatedhq/kots/pkg/k8sutil"
kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types"
types "github.com/replicatedhq/kots/pkg/snapshot/types"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
veleroclientv1 "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
FileSystemLVPConfigMapName = "kotsadm-fs-lvp"
)
func DeployFileSystemLvp(ctx context.Context, clientset kubernetes.Interface, deployOptions FileSystemDeployOptions, registryConfig kotsadmtypes.RegistryConfig) error {
veleroNamespace, err := DetectVeleroNamespace(ctx, clientset, deployOptions.Namespace)
if err != nil {
return errors.Wrap(err, "failed to detect velero namespace")
}
// Ensure the config map exists for the plugin
if err = EnsureLocalVolumeProviderConfigMaps(deployOptions, veleroNamespace); err != nil {
return errors.Wrap(err, "failed to configure local volume provider plugin config map")
}
return nil
}
func ValidateFileSystemDeployment(ctx context.Context, clientset kubernetes.Interface, deployOptions FileSystemDeployOptions, registryConfig kotsadmtypes.RegistryConfig) (bool, bool, error) {
// configure fs directory/mount. This is a legacy check to see if this directory was migrated from Minio and has an intermediate directory
_, hasMinioConfig, writable, err := shouldResetFileSystemMinioMount(ctx, clientset, deployOptions, registryConfig)
if err != nil {
return false, false, errors.Wrap(err, "failed to check if should reset file system mount")
}
return hasMinioConfig, writable, nil
}
func GetCurrentLvpFileSystemConfig(ctx context.Context, namespace string) (*types.FileSystemConfig, error) {
cfg, err := k8sutil.GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
}
clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to create clientset")
}
veleroClient, err := veleroclientv1.NewForConfig(cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to create velero clientset")
}
bsl, err := FindBackupStoreLocation(ctx, clientset, veleroClient, namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to find velero backup storage location")
}
if bsl != nil {
// backup storage location exists, get file system config from there
switch bsl.Spec.Provider {
case SnapshotStoreHostPathProvider:
fileSystemConfig := &types.FileSystemConfig{}
hostPath := bsl.Spec.Config["path"]
fileSystemConfig.HostPath = &hostPath
return fileSystemConfig, nil
case SnapshotStoreNFSProvider:
fileSystemConfig := &types.FileSystemConfig{
NFS: &types.NFSConfig{
Path: bsl.Spec.Config["path"],
Server: bsl.Spec.Config["server"],
},
}
return fileSystemConfig, nil
}
return nil, nil
}
// backup storage location does not exist, get file system config from the config map
fileSystemConfigMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(ctx, FileSystemLVPConfigMapName, metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return nil, nil
}
return nil, errors.Wrap(err, "failed to get file system configmap")
}
if fileSystemConfigMap.Data == nil {
return nil, nil
}
fileSystemConfig := types.FileSystemConfig{}
if hostPath, ok := fileSystemConfigMap.Data["HOSTPATH"]; ok {
fileSystemConfig.HostPath = &hostPath
} else if _, ok := fileSystemConfigMap.Data["NFS_PATH"]; ok {
fileSystemConfig.NFS = &types.NFSConfig{
Path: fileSystemConfigMap.Data["NFS_PATH"],
Server: fileSystemConfigMap.Data["NFS_SERVER"],
}
}
return &fileSystemConfig, nil
}
// RevertToMinioFS will apply the spec of the previous BSL to the current one and then update.
// Used for recovery during a failed migration from Minio to LVP.
func RevertToMinioFS(ctx context.Context, clientset kubernetes.Interface, veleroClient veleroclientv1.VeleroV1Interface, kotsadmNamespace, veleroNamespace string, previousBsl *velerov1api.BackupStorageLocation) error {
bsl, err := FindBackupStoreLocation(context.TODO(), clientset, veleroClient, kotsadmNamespace)
if err != nil {
return errors.Wrap(err, "failed to find backupstoragelocations")
}
if bsl == nil {
return errors.New("backup storage location not found")
}
bsl.Spec = previousBsl.Spec
err = UpdateBackupStorageLocation(ctx, veleroNamespace, bsl)
if err != nil {
return errors.Wrap(err, "failed to revert to minio backup storage location")
}
return nil
}
// DeleteFileSystemMinio cleans up the minio resources for hostpath and nfs snapshot deployments.
// The secret is not deleted, just in case.
func DeleteFileSystemMinio(ctx context.Context, kotsadmNamespace string) error {
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get kubernetes clientset")
}
if err := clientset.CoreV1().ConfigMaps(kotsadmNamespace).Delete(ctx, FileSystemMinioConfigMapName, metav1.DeleteOptions{}); err != nil {
return errors.Wrap(err, "failed to delete fs minio config map")
}
if err := clientset.AppsV1().Deployments(kotsadmNamespace).Delete(ctx, FileSystemMinioDeploymentName, metav1.DeleteOptions{}); err != nil {
return errors.Wrap(err, "failed to delete fs minio deployment")
}
if err := clientset.CoreV1().Services(kotsadmNamespace).Delete(ctx, FileSystemMinioServiceName, metav1.DeleteOptions{}); err != nil {
return errors.Wrap(err, "failed to delete fs minio service")
}
return nil
}
// EnsureLocalVolumeProviderConfigMaps ensures two configmaps:
// one customizes the LVP plugin deployment with a config map based on the chosen file system backing and the detection of Openshift.
// This ensures that Velero and NodeAgent have permissions to write to the disk.
// the second config map is used to store the current file system configuration.
func EnsureLocalVolumeProviderConfigMaps(deployOptions FileSystemDeployOptions, veleroNamespace string) error {
clientset, err := k8sutil.GetClientset()
if err != nil {
return errors.Wrap(err, "failed to get kubernetes clientset")
}
if err := ensureFileSystemLVPConfigMap(context.TODO(), clientset, deployOptions); err != nil {
return errors.Wrap(err, "failed to ensure file system lvp config map")
}
if deployOptions.IsOpenShift || veleroNamespace == "" {
return nil
}
fsConfig := deployOptions.FileSystemConfig
bucket, err := GetLvpBucket(&fsConfig)
if err != nil {
return errors.Wrap(err, "failed to get lvp bucket")
}
var pluginConfigMapLabel string
if fsConfig.HostPath != nil {
pluginConfigMapLabel = SnapshotStoreHostPathProvider
} else {
pluginConfigMapLabel = SnapshotStoreNFSProvider
}
listOpts := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", pluginConfigMapLabel, "ObjectStore"),
}
configmaps, err := clientset.CoreV1().ConfigMaps(veleroNamespace).List(context.TODO(), listOpts)
if err != nil {
return errors.Wrap(err, "failed to list existing config maps")
}
if len(configmaps.Items) == 0 {
// Create the config map
configmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "local-volume-provider-config",
Namespace: veleroNamespace,
Labels: map[string]string{
"velero.io/plugin-config": "",
SnapshotStoreNFSProvider: "ObjectStore",
SnapshotStoreHostPathProvider: "ObjectStore",
},
},
// These values are the settings used for the minio filesystem deployment
Data: map[string]string{
"securityContextRunAsUser": "1001",
"securityContextFsGroup": "1001",
"preserveVolumes": bucket,
},
}
_, err = clientset.CoreV1().ConfigMaps(veleroNamespace).Create(context.TODO(), configmap, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create new local-volume-provider config map")
}
return nil
}
configmap := &configmaps.Items[0]
configmap.Data["securityContextRunAsUser"] = "1001"
configmap.Data["securityContextFsGroup"] = "1001"
configmap.Data["preserveVolumes"] = bucket
_, err = clientset.CoreV1().ConfigMaps(veleroNamespace).Update(context.TODO(), configmap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update local-volume-provider config map")
}
return nil
}
func ensureFileSystemLVPConfigMap(ctx context.Context, clientset kubernetes.Interface, deployOptions FileSystemDeployOptions) error {
configmap := fileSystemLVPConfigMapResource(deployOptions.FileSystemConfig)
existingConfigMap, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Get(ctx, configmap.Name, metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing configmap")
}
_, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Create(ctx, configmap, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "failed to create configmap")
}
return nil
}
existingConfigMap = updateFileSystemLVPConfigMap(existingConfigMap, configmap)
_, err = clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Update(ctx, existingConfigMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update deployment")
}
return nil
}
func fileSystemLVPConfigMapResource(fileSystemConfig types.FileSystemConfig) *corev1.ConfigMap {
data := map[string]string{}
if fileSystemConfig.HostPath != nil {
data["HOSTPATH"] = *fileSystemConfig.HostPath
} else if fileSystemConfig.NFS != nil {
data["NFS_PATH"] = fileSystemConfig.NFS.Path
data["NFS_SERVER"] = fileSystemConfig.NFS.Server
}
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: FileSystemLVPConfigMapName,
},
Data: data,
}
}
func updateFileSystemLVPConfigMap(existingConfigMap, desiredConfigMap *corev1.ConfigMap) *corev1.ConfigMap {
existingConfigMap.Data = desiredConfigMap.Data
return existingConfigMap
}
// IsFileSystemMinioDisable returns the value of an internal KOTS config map entry indicating
// if this installation has opted in or out of migrating from Minio to the LVP plugin.
func IsFileSystemMinioDisabled(kotsadmNamespace string) (bool, error) {
clientset, err := k8sutil.GetClientset()
if err != nil {
return false, errors.Wrap(err, "failed to get kubernetes clientset")
}
//Minio disabled is detected based on two cases
// 1. minio image is not present in the cluster
// 2. disableS3 flag is enabled
minioImage, err := image.GetMinioImage(clientset, kotsadmNamespace)
if err != nil {
return false, errors.Wrap(err, "failed to check minio image")
}
if minioImage == "" {
return true, nil
}
// Get minio snapshot migration status v1.48.0
kostadmConfig, err := clientset.CoreV1().ConfigMaps(kotsadmNamespace).Get(context.TODO(), "kotsadm-confg", metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
// TODO (dans) this behavior needs to change when this feature is opt-out.
return false, nil
}
return false, errors.Wrap(err, "failed to get kotsadm-config map")
}
var minioEnabled bool
if v, ok := kostadmConfig.Data["minio-enabled-snapshots"]; ok {
minioEnabled, err = strconv.ParseBool(v)
if err != nil {
return false, errors.Wrap(err, "failed to parse minio-enabled-snapshots from kotsadm-confg")
}
return !minioEnabled, nil
}
return false, nil
}