/
listconfigmaps.go
78 lines (69 loc) · 2.05 KB
/
listconfigmaps.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
package client
import (
"context"
"errors"
"fmt"
"github.com/wish/ctl/pkg/client/filter"
"github.com/wish/ctl/pkg/client/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"strings"
"sync"
)
// ListConfigMaps returns a list of all configmap that match the query
func (c *Client) ListConfigMaps(contextStr string, namespace string, options ListOptions) ([]types.ConfigMapDiscovery, error) {
cs, err := c.getContextInterface(contextStr)
if err != nil {
return nil, err
}
configmaps, err := cs.CoreV1().ConfigMaps(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
var items []types.ConfigMapDiscovery
for _, configmap := range configmaps.Items {
cm := types.ConfigMapDiscovery{contextStr, configmap}
c.Transform(&cm)
if filter.MatchLabel(cm, options.LabelMatch) && (options.Search == nil || options.Search.MatchString(cm.Name)) {
items = append(items, cm)
}
}
return items, nil
}
// ListConfigMapsOverContexts is like ListConfigMaps but operates over multiple clusters
func (c *Client) ListConfigMapsOverContexts(contexts []string, namespace string, options ListOptions) ([]types.ConfigMapDiscovery, error) {
if len(contexts) == 0 {
contexts = c.GetFilteredContexts(options.LabelMatch)
} else {
contexts = c.FilterContexts(contexts, options.LabelMatch)
}
var wait sync.WaitGroup
wait.Add(len(contexts))
var mutex sync.Mutex
var ret []types.ConfigMapDiscovery
var failed []string
for _, ctx := range contexts {
go func(ctx string) {
defer wait.Done()
configmaps, err := c.ListConfigMaps(ctx, namespace, options)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not connect to cluster \"%s\": %v\n", ctx, err)
mutex.Lock()
failed = append(failed, ctx)
mutex.Unlock()
return
}
mutex.Lock()
for _, configmap := range configmaps {
ret = append(ret, configmap)
}
mutex.Unlock()
}(ctx)
}
wait.Wait()
if failed != nil {
return ret, errors.New("failed connecting to the following contexts: " + strings.Join(failed, ", "))
}
sortObjs(ret)
return ret, nil
}