-
Notifications
You must be signed in to change notification settings - Fork 87
/
execute.go
290 lines (244 loc) · 9.68 KB
/
execute.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
package supportbundle
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/k8sutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/supportbundle/types"
"github.com/replicatedhq/troubleshoot/pkg/redact"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/supportbundle"
)
type supportBundleProgressUpdate struct {
Message string
Status types.SupportBundleStatus
Type supportBundleProgressUpdateType
Size *float64
}
type supportBundleProgressUpdateType string
const (
BUNDLE_PROGRESS_ERROR supportBundleProgressUpdateType = "error"
BUNDLE_PROGRESS_COLLECTOR supportBundleProgressUpdateType = "collector"
BUNDLE_PROGRESS_FILETREE supportBundleProgressUpdateType = "filetree"
BUNDLE_PROGRESS_UPLOADED supportBundleProgressUpdateType = "uploaded"
)
// supportBundleIsComplete checks the current progress against the declared totals in
// the support bundle.
func supportBundleIsComplete(bundle *types.SupportBundle, collectorCount int) bool {
return collectorCount == bundle.Progress.CollectorCount &&
bundle.Status == types.BUNDLE_UPLOADED
}
// executeUpdateRoutine creates a goroutine to manage updates to the support bundle secret while processing.
// The function returns a progress channel to be used by the executor. The goroutine completes
// the executor closes the channel or the internal timeout expires.
func executeUpdateRoutine(bundle *types.SupportBundle) chan interface{} {
progressChan := make(chan interface{})
timeout := time.After(60 * time.Minute)
logger.Infof("Executing Update go routine for support bundle ID: %s", bundle.ID)
go func() {
var collectorsComplete int
logger.Debugf("Waiting for %d collectors to complete", bundle.Progress.CollectorCount)
updateTicker := time.NewTicker(5 * time.Second) // without updates, bundle will be considlered failed after 10 seconds.
defer updateTicker.Stop()
for {
select {
case msg, ok := <-progressChan:
// Closed by sender
if !ok {
logger.Debugf("Progress channel update closed for support bundle ID: %s", bundle.ID)
if !supportBundleIsComplete(bundle, collectorsComplete) {
bundleErrorUpdate(bundle)
} else {
logger.Debugf("Bundle Complete: %s", bundle.ID)
}
return
}
switch val := msg.(type) {
case error:
// Errors could be expected with RBAC, just log and continue
logger.Infof("Progress channel received an error, %s, for support bundle ID: %s", val, bundle.ID)
case supportBundleProgressUpdate:
// Collect events are saved separately since there are many
if val.Type == BUNDLE_PROGRESS_COLLECTOR {
logger.Debugf("Received collector progress update %d, %s, for support bundle ID: %s", collectorsComplete, val.Message, bundle.ID)
collectorsComplete++
bundle.Progress.CollectorsCompleted = collectorsComplete
bundle.Progress.Message = val.Message
if err := store.GetStore().UpdateSupportBundle(bundle); err != nil {
logger.Error(errors.Wrap(err, "could not update collector counter for bundle"))
return
}
// Something went wrong and the loop finished
} else if val.Type == BUNDLE_PROGRESS_ERROR {
logger.Debugf("Received error in progress update, %s, for support bundle ID: %s", val.Message, bundle.ID)
bundle.Progress.Message = val.Message
bundleErrorUpdate(bundle)
// Finished
} else if val.Type == BUNDLE_PROGRESS_UPLOADED && val.Status == types.BUNDLE_UPLOADED {
logger.Debugf("Received %s progress update, %s, for support bundle ID: %s", val.Type, val.Message, bundle.ID)
now := time.Now()
bundle.UploadedAt = &now
bundle.Size = *val.Size
bundle.Status = types.BUNDLE_UPLOADED
bundle.Progress.Message = val.Message
if err := store.GetStore().UpdateSupportBundle(bundle); err != nil {
logger.Error(errors.Wrap(err, "could not update uploaded status for bundle"))
return
}
// Generic updates
} else {
logger.Debugf("Received %s progress update, %s, for support bundle ID: %s", val.Type, val.Message, bundle.ID)
bundle.Progress.Message = val.Message
if err := store.GetStore().UpdateSupportBundle(bundle); err != nil {
logger.Error(errors.Wrap(err, "could not update progress for bundle"))
return
}
}
default:
logger.Errorf("Received unknown progress update, %v, of type %T, for support bundle ID: %s", val, val, bundle.ID)
}
case <-updateTicker.C:
if err := store.GetStore().UpdateSupportBundle(bundle); err != nil {
logger.Error(errors.Wrap(err, "could not update bundle"))
}
case <-timeout:
logger.Errorf("Timeout exceeded for support bundle ID: %s", bundle.ID)
if err := store.GetStore().UpdateSupportBundle(bundle); err != nil {
logger.Error(errors.Wrap(err, "could not write failure to bundle"))
return
}
return
}
}
}()
return progressChan
}
// executeSupportBundleCollectRoutine creates a goroutine to collect the support bundle, upload, analyze and
// send redactors. The function takes a channel for progress updates and closes it when collectors are complete.
func executeSupportBundleCollectRoutine(bundle *types.SupportBundle, progressChan chan interface{}) {
collectorCB := func(c chan interface{}, msg string) {
c <- supportBundleProgressUpdate{
Message: msg,
Status: types.BUNDLE_RUNNING,
Type: BUNDLE_PROGRESS_COLLECTOR,
}
}
k8sconfig, err := k8sutil.GetClusterConfig()
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Could not get k8s rest config for support bundle ID: %s", bundle.ID))
logger.Error(err)
return
}
opts := troubleshootv1beta2.SupportBundleCreateOpts{
CollectorProgressCallback: collectorCB,
CollectWithoutPermissions: true,
HttpClient: http.DefaultClient,
KubernetesRestConfig: k8sconfig,
Namespace: "",
ProgressChan: progressChan,
Redact: true,
}
logger.Infof("Executing Collection go routine for support bundle ID: %s", bundle.ID)
go func() {
defer close(progressChan)
// redactions are global in troubleshoot....
redact.ResetRedactionList()
var response *troubleshootv1beta2.SupportBundleResponse
if bundle.URI != "" {
response, err = troubleshootv1beta2.CollectSupportBundleFromURI(bundle.URI, bundle.RedactURIs, opts)
if err != nil {
logger.Error(errors.Wrap(err, fmt.Sprintf("error collecting support bundle ID from URI: %s", bundle.ID)))
return
}
} else if bundle.BundleSpec != nil {
response, err = troubleshootv1beta2.CollectSupportBundleFromSpec(&bundle.BundleSpec.Spec, bundle.AdditionalRedactors, opts)
if err != nil {
logger.Error(errors.Wrap(err, fmt.Sprintf("error collecting support bundle ID: %s from spec", bundle.ID)))
return
}
} else {
logger.Errorf("cannot collect support bundle; no bundle URI or spec provided")
return
}
defer os.RemoveAll(response.ArchivePath)
progressChan <- supportBundleProgressUpdate{
Message: "creating file tree",
Status: types.BUNDLE_RUNNING,
Type: BUNDLE_PROGRESS_FILETREE,
}
fileTree, err := archiveToFileTree(response.ArchivePath)
if err != nil {
progressChan <- supportBundleProgressUpdate{
Message: "could not create file tree",
Status: types.BUNDLE_FAILED,
Type: BUNDLE_PROGRESS_ERROR,
}
logger.Error(errors.Wrap(err, "error parsing archive for tree"))
return
}
marshalledTree, err := json.Marshal(fileTree.Nodes)
if err != nil {
progressChan <- supportBundleProgressUpdate{
Message: "could not marshal tree",
Status: types.BUNDLE_FAILED,
Type: BUNDLE_PROGRESS_ERROR,
}
logger.Error(errors.Wrap(err, "error marshallling archive tree"))
return
}
progressChan <- supportBundleProgressUpdate{
Message: "uploading bundle to store",
Status: types.BUNDLE_RUNNING,
Type: BUNDLE_PROGRESS_UPLOADED,
}
if err = store.GetStore().UploadSupportBundle(bundle.ID, response.ArchivePath, marshalledTree); err != nil {
progressChan <- supportBundleProgressUpdate{
Message: "could not upload bundle",
Status: types.BUNDLE_FAILED,
Type: BUNDLE_PROGRESS_ERROR,
}
logger.Error(errors.Wrap(err, "error uploading the support bundle"))
return
}
fi, err := os.Stat(response.ArchivePath)
if err != nil {
progressChan <- supportBundleProgressUpdate{
Message: "could not get archive info",
Status: types.BUNDLE_FAILED,
Type: BUNDLE_PROGRESS_ERROR,
}
logger.Error(errors.Wrap(err, "error getting archive info"))
return
}
size := float64(fi.Size())
// last update is uploaded for parity with existing support bundles
progressChan <- supportBundleProgressUpdate{
Message: "support bundle uploaded",
Status: types.BUNDLE_UPLOADED,
Type: BUNDLE_PROGRESS_UPLOADED,
Size: &size,
}
// we need the app archive to get the analyzers for old support bundles that don't include the analysis in the bundle
if err := CreateSupportBundleAnalysis(bundle.AppID, response.ArchivePath, bundle); err != nil {
logger.Error(errors.Wrap(err, "failed to create analysis"))
return
}
redactions := redact.GetRedactionList()
if err = store.GetStore().SetRedactions(bundle.ID, redactions); err != nil {
logger.Error(errors.Wrap(err, "failed to set redactions"))
return
}
}()
}
func bundleErrorUpdate(bundle *types.SupportBundle) {
logger.Errorf("Support bundle collection exited before completion for support bundle ID: %s", bundle.ID)
bundle.Status = types.BUNDLE_FAILED
if err := store.GetStore().UpdateSupportBundle(bundle); err != nil {
logger.Error(errors.Wrap(err, "could not write failure to bundle"))
return
}
}