-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (178 loc) · 4.71 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
"github.com/deinstapel/rook-obc-backup/env"
"github.com/deinstapel/rook-obc-backup/kubernetes"
"github.com/deinstapel/rook-obc-backup/sync"
"github.com/ericchiang/k8s"
"github.com/ghodss/yaml"
"github.com/larrabee/s3sync/storage"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.TraceLevel)
}
func makeKubeconfigClient(path string) (*k8s.Client, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
config := new(k8s.Config)
if err := yaml.Unmarshal(data, config); err != nil {
return nil, err
}
client, err := k8s.NewClient(config)
if err != nil {
return nil, err
}
return client, nil
}
func makeClient(path string) (*k8s.Client, error) {
if path != "" {
return makeKubeconfigClient(path)
}
return k8s.NewInClusterClient()
}
func handleMainError(err error) {
if err != nil {
log.Error(err)
panic(err)
}
}
func main() {
readEnv := env.ReadEnv()
sourceClient, err := makeClient(readEnv.SOURCE_KUBECONFIG)
handleMainError(err)
targetClient, err := makeClient(readEnv.TARGET_KUBECONFIG)
handleMainError(err)
ctx, cancel := context.WithCancel(context.Background())
go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
<-signalChannel
cancel()
}()
if err := backupOBCs(ctx, sourceClient, targetClient, readEnv); err != nil {
log.WithField("err", err).Fatal("Backup failed")
os.Exit(1)
}
os.Exit(0)
}
func backupOBCs(ctx context.Context, sourceClient, targetClient *k8s.Client, env env.Environment) error {
log.Trace("start crd watch")
crdList := &kubernetes.ObjectBucketClaimList{}
if err := sourceClient.List(ctx, k8s.AllNamespaces, crdList); err != nil {
log.WithField("err", err).Warning("Failed to list OBCs")
return err
}
var failedBuckets []string
for index := range crdList.Items {
item := crdList.Items[index]
err := backupOBC(ctx, item, sourceClient, targetClient, env)
if err != nil {
log.WithFields(log.Fields{"name": *item.Metadata.Name, "namespace": *item.Metadata.Namespace}).Error(err)
failedBuckets = append(failedBuckets, *item.Metadata.Name)
continue
}
}
if len(failedBuckets) > 0 {
return fmt.Errorf("OBCs %v failed", failedBuckets)
}
return nil
}
func backupOBC(ctx context.Context, objectBucketClaim kubernetes.ObjectBucketClaim, sourceClient, targetClient *k8s.Client, env env.Environment) error {
item := objectBucketClaim
targetBucketName := env.TARGET_BUCKET_PREFIX + "-" + *item.Metadata.Namespace + "-" + *item.Metadata.Name
scopedLog := log.WithFields(log.Fields{
"sourceName": *item.Metadata.Name,
"sourceNamespace": *item.Metadata.Namespace,
"targetName": targetBucketName,
"targetNamespace": env.TARGET_BUCKET_NAMESPACE,
})
scopedLog.Info("Working on Element")
childCtx, cancelChildCtx := context.WithCancel(ctx)
defer cancelChildCtx()
sourceDetails, err := kubernetes.GetDetails(
sourceClient,
childCtx,
*item.Metadata.Namespace,
*item.Metadata.Name,
)
if err != nil {
log.Errorf("sourceDetails could not be obtained, err: %v", err)
return errors.New("sourceDetails could not be obtained")
}
doesTargetNamespaceExist, err := kubernetes.DoesExist(
targetClient,
childCtx,
env.TARGET_BUCKET_NAMESPACE,
targetBucketName,
)
if err != nil {
return errors.New("target namespace could not be checked")
}
if !doesTargetNamespaceExist {
err := kubernetes.CreateOBC(
targetClient,
childCtx,
env.TARGET_BUCKET_NAMESPACE,
targetBucketName,
env.TARGET_STORAGE_CLASS_NAME,
)
if err != nil {
log.Errorf("Target object bucket claim could not be created, err: %v", err)
return errors.New("target object bucket claim could not be created")
}
time.Sleep(10 * time.Second)
}
targetDetails, err := kubernetes.GetDetails(
targetClient,
childCtx,
env.TARGET_BUCKET_NAMESPACE,
targetBucketName,
)
if err != nil {
return errors.New("targetDetails could not be obtained")
}
var retryFiles []*storage.Object = nil
// Retry three times
for i := 0; i < 3; i++ {
if len(retryFiles) > 0 {
log.Printf("Retrying %d files", len(retryFiles))
}
syncGroup, err := sync.PrepareSyncGroup(
childCtx,
sourceDetails,
env.SOURCE_S3_URL,
targetDetails,
env.TARGET_S3_URL,
env.NUM_WORKERS,
retryFiles,
)
if err != nil {
return errors.New("sync Group could not be prepared")
}
retryFiles, err = sync.RunSyncGroup(
childCtx,
targetBucketName,
syncGroup,
)
if err != nil && len(retryFiles) > 0 {
continue
} else if err != nil {
return err
} else {
break
}
}
return nil
}