-
Notifications
You must be signed in to change notification settings - Fork 104
/
list_labels.go
170 lines (141 loc) · 4.13 KB
/
list_labels.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package tools
import (
"fmt"
"github.com/cppforlife/go-cli-ui/ui"
uitable "github.com/cppforlife/go-cli-ui/ui/table"
cmdcore "github.com/k14s/kapp/pkg/kapp/cmd/core"
"github.com/k14s/kapp/pkg/kapp/logger"
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/labels"
)
type ListLabelsOptions struct {
ui ui.UI
depsFactory cmdcore.DepsFactory
logger logger.Logger
FileFlags FileFlags
Accessor string
Values bool
}
func NewListLabelsOptions(ui ui.UI, depsFactory cmdcore.DepsFactory, logger logger.Logger) *ListLabelsOptions {
return &ListLabelsOptions{ui: ui, depsFactory: depsFactory, logger: logger}
}
func NewListLabelsCmd(o *ListLabelsOptions, flagsFactory cmdcore.FlagsFactory) *cobra.Command {
cmd := &cobra.Command{
Use: "list-labels",
Aliases: []string{"ls-labels"},
Short: "List labels",
RunE: func(_ *cobra.Command, _ []string) error { return o.Run() },
}
o.FileFlags.Set(cmd)
cmd.Flags().StringVar(&o.Accessor, "accessor", "labels", "Extracted field (labels, annotations, ownerrefs)")
cmd.Flags().BoolVar(&o.Values, "values", false, "Show values")
return cmd
}
func (o *ListLabelsOptions) Run() error {
rs, err := o.listResources()
if err != nil {
return err
}
data := map[string]map[string]int{}
resAccessor, ok := resAccessors[o.Accessor]
if !ok {
return fmt.Errorf("Unknown resource accessor")
}
for _, res := range rs {
kvs := resAccessor.KVs(res)
for k, v := range kvs {
if _, found := data[k]; !found {
data[k] = map[string]int{}
}
data[k][v]++
}
if len(kvs) == 0 {
data[""] = map[string]int{}
}
}
valueHeader := uitable.NewHeader("Value")
valueHeader.Hidden = !o.Values
table := uitable.Table{
Title: "Labels",
Content: "labels",
Header: []uitable.Header{
uitable.NewHeader("Name"),
valueHeader,
uitable.NewHeader("Resources"),
},
SortBy: []uitable.ColumnSort{
{Column: 0, Asc: true},
{Column: 1, Asc: true},
},
}
for name, counts := range data {
totalCount := 0
for val, count := range counts {
totalCount += count
if o.Values {
table.Rows = append(table.Rows, []uitable.Value{
uitable.NewValueString(name),
uitable.NewValueString(val),
uitable.NewValueInt(count),
})
}
}
if !o.Values {
table.Rows = append(table.Rows, []uitable.Value{
uitable.NewValueString(name),
uitable.NewValueString(""),
uitable.NewValueInt(totalCount),
})
}
}
o.ui.PrintTable(table)
return nil
}
func (o *ListLabelsOptions) listResources() ([]ctlres.Resource, error) {
coreClient, err := o.depsFactory.CoreClient()
if err != nil {
return nil, err
}
dynamicClient, err := o.depsFactory.DynamicClient(cmdcore.DynamicClientOpts{Warnings: true})
if err != nil {
return nil, err
}
mutedDynamicClient, err := o.depsFactory.DynamicClient(cmdcore.DynamicClientOpts{Warnings: false})
if err != nil {
return nil, err
}
resTypes := ctlres.NewResourceTypesImpl(coreClient, ctlres.ResourceTypesImplOpts{})
resources := ctlres.NewResourcesImpl(
resTypes, coreClient, dynamicClient, mutedDynamicClient, ctlres.ResourcesImplOpts{}, o.logger)
identifiedResources := ctlres.NewIdentifiedResources(coreClient, resTypes, resources, nil, o.logger)
labelSelector, err := labels.Parse("!kapp")
if err != nil {
return nil, err
}
return ctlres.NewLabeledResources(labelSelector, identifiedResources, o.logger).All(ctlres.IdentifiedResourcesListOpts{})
}
type resourceAccessor struct {
KVs func(ctlres.Resource) map[string]string
}
var (
resAccessors = map[string]resourceAccessor{
"labels": resourceAccessor{
KVs: func(res ctlres.Resource) map[string]string { return res.Labels() },
},
"annotations": resourceAccessor{
KVs: func(res ctlres.Resource) map[string]string { return res.Annotations() },
},
"ownerrefs": resourceAccessor{
KVs: func(res ctlres.Resource) map[string]string {
result := map[string]string{}
for _, ref := range res.OwnerRefs() {
result[res.Namespace()+"/"+ref.APIVersion+"/"+ref.Kind+"/"+ref.Name] = ""
}
return result
},
},
}
)