...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package collector
15
16 import (
17 "context"
18
19 "k8s.io/client-go/rest"
20 "sigs.k8s.io/controller-runtime/pkg/client"
21 )
22
23
24 type Collector interface {
25 CollectContext(ctx context.Context) (env map[string]interface{}, err error)
26 }
27
28 type ComposeCollector struct {
29 collectors []Collector
30 }
31
32 func (it *ComposeCollector) CollectContext(ctx context.Context) (env map[string]interface{}, err error) {
33 if len(it.collectors) == 0 {
34 return nil, nil
35 }
36 if len(it.collectors) == 1 {
37 return it.collectors[0].CollectContext(ctx)
38 }
39
40 result := make(map[string]interface{})
41 for _, collector := range it.collectors {
42 temp, err := collector.CollectContext(ctx)
43 if err != nil {
44 return nil, err
45 }
46 mapExtend(result, temp)
47 }
48 return result, nil
49 }
50
51
52
53 func mapExtend(origin map[string]interface{}, another map[string]interface{}) {
54 if origin == nil || another == nil {
55 return
56 }
57 for k, v := range another {
58 origin[k] = v
59 }
60 }
61
62 func DefaultCollector(kubeClient client.Client, restConfig *rest.Config, namespace, podName, containerName string) Collector {
63 return &ComposeCollector{collectors: []Collector{
64 NewExitCodeCollector(kubeClient, namespace, podName, containerName),
65 NewStdoutCollector(restConfig, namespace, podName, containerName),
66 }}
67 }
68