-
Notifications
You must be signed in to change notification settings - Fork 88
/
backup.go
310 lines (267 loc) · 8.26 KB
/
backup.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
package supportbundle
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/redact"
"github.com/replicatedhq/kots/pkg/store"
troubleshootanalyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
troubleshootcollect "github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/convert"
troubleshootversion "github.com/replicatedhq/troubleshoot/pkg/version"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func CreateBundleForBackup(appID string, backupName string, backupNamespace string) (string, error) {
logger.Debug("executing support bundle for backup",
zap.String("backupName", backupName),
zap.String("backupNamespace", backupNamespace))
progressChan := make(chan interface{}, 0)
defer close(progressChan)
go func() {
for {
msg, ok := <-progressChan
if ok {
logger.Debugf("%v", msg)
} else {
return
}
}
}()
var RBACErrors []error
var collectors []troubleshootcollect.Collector
restConfig, err := k8sutil.GetClusterConfig()
if err != nil {
return "", errors.Wrap(err, "failed to get cluster config")
}
k8sClientSet, err := k8sutil.GetClientset()
if err != nil {
return "", errors.Wrap(err, "failed to get kubernetes client")
}
selectors := []string{
"component=velero",
"app.kubernetes.io/name=velero",
}
ctx := context.TODO()
for _, selector := range selectors {
logsCollector := &troubleshootv1beta2.Logs{
CollectorMeta: troubleshootv1beta2.CollectorMeta{
CollectorName: "velero",
},
Name: "velero",
Namespace: backupNamespace,
Selector: []string{selector},
}
collectors = append(collectors, &troubleshootcollect.CollectLogs{
Collector: logsCollector,
Namespace: backupNamespace,
ClientConfig: restConfig,
Client: k8sClientSet,
Context: ctx,
RBACErrors: RBACErrors,
})
}
// make a temp file to store the bundle in
bundlePath, err := ioutil.TempDir("", "troubleshoot")
if err != nil {
return "", errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(bundlePath)
if err = writeVersionFile(bundlePath); err != nil {
return "", errors.Wrap(err, "failed to write version file")
}
redacts := []*troubleshootv1beta2.Redact{}
globalRedact, err := redact.GetRedact()
if err == nil && globalRedact != nil {
redacts = globalRedact.Spec.Redactors
} else if err != nil {
return "", errors.Wrap(err, "failed to get global redactors")
}
result := make(map[string][]byte)
// Run preflights collectors synchronously
for _, collector := range collectors {
if collector.HasRBACErrors() {
// don't skip clusterResources collector due to RBAC issues
if _, ok := collector.(*troubleshootcollect.CollectClusterResources); !ok {
progressChan <- fmt.Sprintf("skipping collector %s with insufficient RBAC permissions", collector.Title())
continue
}
}
progressChan <- collector.Title()
result, err = collector.Collect(progressChan)
if err != nil {
progressChan <- errors.Wrapf(err, "failed to run collector %q", collector.Title())
continue
}
if result != nil {
err = saveCollectorOutput(result, bundlePath)
if err != nil {
progressChan <- errors.Wrapf(err, "failed to parse collector spec %q", collector.Title())
continue
}
}
}
// Redact result before creating archive
err = troubleshootcollect.RedactResult(bundlePath, result, redacts)
if err != nil {
return "", errors.Wrap(err, "failed to redact")
}
// create an archive of this bundle
supportBundleArchivePath, err := ioutil.TempDir("", "kotsadm")
if err != nil {
return "", errors.Wrap(err, "failed to create archive dir")
}
defer os.RemoveAll(supportBundleArchivePath)
if err = tarSupportBundleDir(bundlePath, filepath.Join(supportBundleArchivePath, "support-bundle.tar.gz")); err != nil {
return "", errors.Wrap(err, "failed to create support bundle archive")
}
// we have a support bundle...
// store it
supportBundle, err := CreateBundle(
fmt.Sprintf("backup-%s", backupName),
appID,
filepath.Join(supportBundleArchivePath, "support-bundle.tar.gz"))
if err != nil {
return "", errors.Wrap(err, "failed to create support bundle")
}
// analyze it
analyzers := []*troubleshootv1beta2.Analyze{}
analyzers = append(analyzers, &troubleshootv1beta2.Analyze{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "Velero Errors",
},
CollectorName: "velero",
FileName: "velero/velero*/velero.log",
RegexPattern: "level=error",
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
Message: "Velero has errors",
},
},
{
Pass: &troubleshootv1beta2.SingleOutcome{
Message: "Velero does not have errors",
},
},
},
},
})
analyzers = append(analyzers, &troubleshootv1beta2.Analyze{
TextAnalyze: &troubleshootv1beta2.TextAnalyze{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "Restic Volumes",
},
CollectorName: "restic",
FileName: "restic/*.log",
RegexPattern: "expected one matching path, got 0",
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
Message: "Restic volume error",
},
},
{
Pass: &troubleshootv1beta2.SingleOutcome{
Message: "No restic volume error",
},
},
},
},
})
analyzer := troubleshootv1beta2.Analyzer{
TypeMeta: metav1.TypeMeta{
APIVersion: "troubleshoot.sh/v1beta2",
Kind: "Analyzer",
},
ObjectMeta: metav1.ObjectMeta{
Name: backupName,
},
Spec: troubleshootv1beta2.AnalyzerSpec{
Analyzers: analyzers,
},
}
b, err := json.Marshal(analyzer)
if err != nil {
return "", errors.Wrap(err, "failed to marshal analyzers")
}
analyzeResult, err := troubleshootanalyze.DownloadAndAnalyze(filepath.Join(supportBundleArchivePath, "support-bundle.tar.gz"), string(b))
if err != nil {
return "", errors.Wrap(err, "failed to analyze")
}
data := convert.FromAnalyzerResult(analyzeResult)
insights, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", errors.Wrap(err, "failed to marshal analysis")
}
if err := store.GetStore().SetSupportBundleAnalysis(supportBundle.ID, insights); err != nil {
return "", errors.Wrap(err, "failed to update bundle status")
}
return supportBundle.ID, nil
}
func tarSupportBundleDir(inputDir string, outputFilename string) error {
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: false,
},
}
paths := []string{
filepath.Join(inputDir, "version.yaml"), // version file should be first in tar archive for quick extraction
}
topLevelFiles, err := ioutil.ReadDir(inputDir)
if err != nil {
return errors.Wrap(err, "failed to list bundle directory contents")
}
for _, f := range topLevelFiles {
if f.Name() == "version.yaml" {
continue
}
paths = append(paths, filepath.Join(inputDir, f.Name()))
}
if err := tarGz.Archive(paths, outputFilename); err != nil {
return errors.Wrap(err, "failed to create archive")
}
return nil
}
func saveCollectorOutput(output map[string][]byte, bundlePath string) error {
for filename, maybeContents := range output {
fileDir, fileName := filepath.Split(filename)
outPath := filepath.Join(bundlePath, fileDir)
if err := os.MkdirAll(outPath, 0777); err != nil {
return errors.Wrap(err, "failed to create output file")
}
if err := ioutil.WriteFile(filepath.Join(outPath, fileName), maybeContents, 0644); err != nil {
return errors.Wrap(err, "failed to write file")
}
}
return nil
}
func writeVersionFile(path string) error {
version := troubleshootv1beta2.SupportBundleVersion{
ApiVersion: "troubleshoot.sh/v1beta2",
Kind: "SupportBundle",
Spec: troubleshootv1beta2.SupportBundleVersionSpec{
VersionNumber: troubleshootversion.Version(),
},
}
b, err := yaml.Marshal(version)
if err != nil {
return err
}
filename := filepath.Join(path, "version.yaml")
err = ioutil.WriteFile(filename, b, 0644)
if err != nil {
return err
}
return nil
}