This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
forked from NetApp/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd_data_migrator.go
72 lines (65 loc) · 1.75 KB
/
etcd_data_migrator.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
// Copyright 2018 NetApp, Inc. All Rights Reserved.
package persistentstore
import (
"fmt"
log "github.com/sirupsen/logrus"
)
type EtcdDataMigrator struct {
SourceClient EtcdClient
DestClient EtcdClient
}
func NewEtcdDataMigrator(SourceClient, DestClient EtcdClient) *EtcdDataMigrator {
return &EtcdDataMigrator{
SourceClient: SourceClient,
DestClient: DestClient,
}
}
func (m *EtcdDataMigrator) Start(keyPrefix string, deleteSrc bool) error {
keys, err := m.SourceClient.ReadKeys(keyPrefix)
if err != nil {
if MatchKeyNotFoundErr(err) {
log.Infof("No key with prefix %v to migrate.", keyPrefix)
return nil
}
return fmt.Errorf("reading keys from the source client failed: %v", err)
}
for _, key := range keys {
val, err := m.SourceClient.Read(key)
if err != nil {
return fmt.Errorf("reading key %v by the source client failed: %v",
key, err)
}
log.WithFields(log.Fields{
"key": key,
}).Debug("Read key from the source.")
err = m.DestClient.Set(key, val)
if err != nil {
return fmt.Errorf("setting key %v by the destination client failed: %v",
key, err)
}
log.WithFields(log.Fields{
"key": key,
}).Debug("Wrote key to the destination.")
if deleteSrc {
err = m.SourceClient.Delete(key)
if err != nil {
return fmt.Errorf("deleting key %v by the source client failed: %v",
key, err)
}
log.WithFields(log.Fields{
"key": key,
}).Debug("Deleted key from the source.")
}
}
return nil
}
func (m *EtcdDataMigrator) Stop() error {
if err := m.SourceClient.Stop(); err != nil {
return fmt.Errorf("closing the source etcd client failed: %v", err)
}
if err := m.DestClient.Stop(); err != nil {
return fmt.Errorf("closing the destination etcd client failed: %v",
err)
}
return nil
}