/
worker.go
354 lines (312 loc) · 8.23 KB
/
worker.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package worker
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/bakito/kubexporter/pkg/client"
"github.com/bakito/kubexporter/pkg/log"
"github.com/bakito/kubexporter/pkg/types"
"github.com/bakito/kubexporter/pkg/utils"
"github.com/vbauerster/mpb/v5"
"github.com/vbauerster/mpb/v5/decor"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
)
// Worker interface
type Worker interface {
GenerateWork(s *sync.WaitGroup, out chan *types.GroupResource) func(resource *types.GroupResource)
Stop() Stats
}
type worker struct {
id int
config *types.Config
mainBar *mpb.Bar
recBar *mpb.Bar
currentKind string
currentPage int
elapsedDecorator decor.Decorator
ac *client.ApiClient
queryFinished bool
stats Stats
}
// Stats worker stats
type Stats struct {
Errors int
namespaces map[string]bool
Kinds int
Pages int
Resources int
}
// Add stats
func (s *Stats) Add(o *Stats) {
if o != nil {
s.Kinds += o.Kinds
s.Pages += o.Pages
s.Resources += o.Resources
s.Errors += o.Errors
for ns := range o.namespaces {
s.addNamespace(ns)
}
}
}
func (s *Stats) addNamespace(ns string) {
if s.namespaces == nil {
s.namespaces = make(map[string]bool)
}
s.namespaces[ns] = true
}
// Namespaces get the number of namespaces
func (s *Stats) Namespaces() int {
return len(s.namespaces)
}
// HasErrors true if errors >0
func (s *Stats) HasErrors() bool {
return s.Errors > 0
}
// New create a new worker
func New(id int, config *types.Config, ac *client.ApiClient, prog *mpb.Progress, mainBar *mpb.Bar) Worker {
w := &worker{
id: id + 1,
mainBar: mainBar,
config: config,
ac: ac,
elapsedDecorator: decor.NewElapsed(decor.ET_STYLE_GO, time.Now()),
}
if prog != nil {
w.recBar = prog.AddBar(1,
mpb.PrependDecorators(
w.preDecorator(),
),
mpb.AppendDecorators(
w.postDecorator(),
),
)
}
return w
}
// Stop end worker
func (w *worker) Stop() Stats {
if w.recBar != nil {
w.recBar.SetTotal(100, true)
}
return w.stats
}
// list resources
func (w *worker) list(ctx context.Context, group, version, kind string, continueValue string) (*unstructured.UnstructuredList, error) {
mapping, err := w.ac.Mapper.RESTMapping(schema.GroupKind{Group: group, Kind: kind}, version)
if err != nil {
return nil, err
}
var dr dynamic.ResourceInterface
if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
// namespaced resources should specify the namespace
dr = w.ac.Client.Resource(mapping.Resource).Namespace(w.config.Namespace)
} else {
// for cluster-wide resources
dr = w.ac.Client.Resource(mapping.Resource)
}
opts := metav1.ListOptions{Continue: continueValue}
if !w.config.AsLists {
// for lists we do no pagination
opts.Limit = int64(w.config.QueryPageSize)
}
return dr.List(ctx, opts)
}
func (w *worker) preDecorator() decor.Decorator {
return decor.Any(func(s decor.Statistics) string {
if s.Completed {
return fmt.Sprintf("👷 %2d:", w.id)
}
if w.queryFinished && s.Total == 0 {
return fmt.Sprintf("\U0001F971 %2d: idle", w.id)
}
page := ""
if w.config.QueryPageSize > 0 {
page = fmt.Sprintf(" (page %d)", w.currentPage)
}
if !w.queryFinished {
return fmt.Sprintf("🔍 %2d: %s%s", w.id, w.currentKind, page)
}
return fmt.Sprintf("👷 %2d: %s%s %s", w.id, w.currentKind, page, w.elapsedDecorator.Decor(s))
})
}
func (w *worker) postDecorator() decor.Decorator {
return decor.Any(func(s decor.Statistics) string {
if s.Completed {
return log.Check
}
return fmt.Sprintf("%s / %s %s",
decor.CurrentNoUnit("").Decor(s),
decor.TotalNoUnit("").Decor(s),
decor.Percentage().Decor(s))
})
}
// GenerateWork generate the work function
func (w *worker) GenerateWork(wg *sync.WaitGroup, out chan *types.GroupResource) func(resource *types.GroupResource) {
return func(res *types.GroupResource) {
defer wg.Done()
w.stats.Kinds++
w.queryFinished = false
ctx := context.TODO()
w.currentKind = res.GroupKind()
w.elapsedDecorator = decor.NewElapsed(decor.ET_STYLE_GO, time.Now())
hasMorePages := ""
for {
hasMorePages = w.listResources(ctx, res, hasMorePages)
if hasMorePages == "" {
break
}
}
w.stats.Resources += res.ExportedInstances
w.stats.Pages += res.Pages
if w.config.Progress == types.ProgressSimple {
w.config.Logger().Checkf("%s\n", res.GroupKind())
}
if w.mainBar != nil {
w.mainBar.Increment()
}
out <- res
}
}
func (w *worker) listResources(ctx context.Context, res *types.GroupResource, hasMorePages string) string {
w.currentPage = res.Pages + 1
if w.recBar != nil {
w.recBar.SetCurrent(0)
w.recBar.SetTotal(0, false)
}
start := time.Now()
ul, err := w.list(ctx, res.APIGroup, res.APIVersion, res.APIResource.Kind, hasMorePages)
res.QueryDuration += time.Since(start)
w.queryFinished = true
start = time.Now()
if err != nil {
w.stats.Errors++
if errors.IsNotFound(err) {
res.Error = "Not Found"
} else if errors.IsMethodNotSupported(err) {
res.Error = "Not Allowed"
} else {
res.Error = "Error:" + err.Error()
}
} else {
if w.recBar != nil {
w.recBar.SetTotal(int64(len(ul.Items)), false)
}
if w.config.AsLists {
res.ExportedInstances += w.exportLists(res, ul)
} else {
res.ExportedInstances += w.exportSingleResources(res, ul)
}
}
res.ExportDuration += time.Since(start)
res.Instances += len(ul.Items)
res.Pages++
return ul.GetContinue()
}
func (w *worker) exportLists(res *types.GroupResource, ul *unstructured.UnstructuredList) int {
if res == nil || ul == nil {
return 0
}
clone := ul.DeepCopy()
clone.Items = nil
unstructured.RemoveNestedField(clone.Object, "metadata")
perNs := make(map[string]*unstructured.UnstructuredList)
for _, u := range ul.Items {
if !w.config.IsInstanceExcluded(res, u) {
w.config.FilterFields(res, u)
w.config.MaskFields(res, u)
w.config.EncryptFields(res, u)
w.config.SortSliceFields(res, u)
if _, ok := perNs[u.GetNamespace()]; !ok {
ul := &unstructured.UnstructuredList{}
clone.DeepCopyInto(ul)
perNs[u.GetNamespace()] = ul
}
perNs[u.GetNamespace()].Items = append(perNs[u.GetNamespace()].Items, u)
}
}
cnt := 0
for ns, usl := range perNs {
w.stats.addNamespace(ns)
filename, err := w.config.ListFileName(res, ns)
if err != nil {
res.Error = err.Error()
continue
}
filename = filepath.Join(w.config.Target, filename)
_ = os.MkdirAll(filepath.Dir(filename), os.ModePerm)
f, err := os.Create(filename)
if err != nil {
res.Error = err.Error()
continue
}
err = utils.PrintObj(w.config.PrintFlags, usl, f)
if err != nil {
res.Error = err.Error()
continue
}
closeIgnoreError(f)()
if w.recBar != nil {
w.recBar.IncrBy(len(usl.Items))
}
cnt += len(usl.Items)
}
return cnt
}
func (w *worker) exportSingleResources(res *types.GroupResource, ul *unstructured.UnstructuredList) int {
if res == nil || ul == nil {
return 0
}
names := make(map[string]int)
cnt := 0
for _, u := range ul.Items {
if !w.config.IsInstanceExcluded(res, u) {
cnt++
w.stats.addNamespace(u.GetNamespace())
w.config.FilterFields(res, u)
w.config.MaskFields(res, u)
w.config.EncryptFields(res, u)
w.config.SortSliceFields(res, u)
us := &u
namespaceName := strings.ToLower(fmt.Sprintf("%s.%s", us.GetNamespace(), us.GetName()))
nameCnt := names[namespaceName]
filename, err := w.config.FileName(res, us, nameCnt)
if err != nil {
res.Error = err.Error()
continue
}
names[namespaceName] = nameCnt + 1
filename = filepath.Join(w.config.Target, filename)
_ = os.MkdirAll(filepath.Dir(filename), os.ModePerm)
f, err := os.Create(filename)
if err != nil {
res.Error = err.Error()
continue
}
err = utils.PrintObj(w.config.PrintFlags, us, f)
if err != nil {
res.Error = err.Error()
continue
}
closeIgnoreError(f)
if w.recBar != nil {
w.recBar.Increment()
}
}
}
return cnt
}
func closeIgnoreError(f io.Closer) func() {
return func() {
_ = f.Close()
}
}