This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
configmap.go
225 lines (175 loc) · 5.47 KB
/
configmap.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
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"fmt"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/vmware-tanzu/octant/pkg/view/component"
corev1 "k8s.io/api/core/v1"
)
// ConfigMapListHandler is a printFunc that prints ConfigMaps
func ConfigMapListHandler(ctx context.Context, list *corev1.ConfigMapList, opts Options) (component.Component, error) {
if list == nil {
return nil, errors.New("list is nil")
}
// Data column
cols := component.NewTableCols("Name", "Labels", "Data", "Age")
ot := NewObjectTable("ConfigMaps", "We couldn't find any config maps!", cols, opts.DashConfig.ObjectStore())
ot.EnablePluginStatus(opts.DashConfig.PluginManager())
for _, c := range list.Items {
row := component.TableRow{}
nameLink, err := opts.Link.ForObject(&c, c.Name)
if err != nil {
return nil, err
}
row["Name"] = nameLink
row["Labels"] = component.NewLabels(c.Labels)
data := fmt.Sprintf("%d", len(c.Data))
row["Data"] = component.NewText(data)
ts := c.CreationTimestamp.Time
row["Age"] = component.NewTimestamp(ts)
if err := ot.AddRowForObject(ctx, &c, row); err != nil {
return nil, fmt.Errorf("add row for object: %w", err)
}
}
return ot.ToComponent()
}
// ConfigMapHandler is a printFunc that prints a ConfigMap
func ConfigMapHandler(ctx context.Context, cm *corev1.ConfigMap, options Options) (component.Component, error) {
o := NewObject(cm)
ch, err := newConfigMapHandler(cm, o)
if err != nil {
return nil, err
}
if err := ch.Config(options); err != nil {
return nil, errors.Wrap(err, "print configmap configuration")
}
if err := ch.Data(options); err != nil {
return nil, errors.Wrap(err, "print configmap data")
}
return o.ToComponent(ctx, options)
}
// ConfigMapConfiguration generates config map configuration
type ConfigMapConfiguration struct {
configmap *corev1.ConfigMap
}
// NewConfigMapConfiguration creates an instance of ConfigMapConfiguration
func NewConfigMapConfiguration(cm *corev1.ConfigMap) *ConfigMapConfiguration {
return &ConfigMapConfiguration{
configmap: cm,
}
}
// Create a configmap configuration summary
func (c *ConfigMapConfiguration) Create(options Options) (*component.Summary, error) {
if c.configmap == nil {
return nil, errors.New("config map is nil")
}
configMap := c.configmap
sections := component.SummarySections{}
creationTimestamp := configMap.CreationTimestamp.Time
sections = append(sections, component.SummarySection{
Header: "Age",
Content: component.NewTimestamp(creationTimestamp),
})
summary := component.NewSummary("Configuration", sections...)
return summary, nil
}
// describeDataTable returns a table containing config map data
func describeConfigMapData(cm *corev1.ConfigMap) (*component.Table, error) {
if cm == nil {
return nil, errors.New("config map is nil")
}
cols := component.NewTableCols("Key", "Value")
table := component.NewTable("Data", "No data has been configured for this config map!", cols)
rows, err := describeConfigMapDataRows(cm)
if err != nil {
return nil, err
}
for _, row := range rows {
table.Add(row)
}
table.Sort("Key")
return table, nil
}
// describeDataRows prints key value pairs from data
func describeConfigMapDataRows(cm *corev1.ConfigMap) ([]component.TableRow, error) {
if cm == nil {
return nil, errors.New("config map is nil")
}
rows := make([]component.TableRow, 0)
data := cm.Data
// Alpha sort keys so that output is consistent
var keys []string
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
row := component.TableRow{}
rows = append(rows, row)
row["Key"] = component.NewText(k)
if strings.Contains(data[k], "\n") {
row["Value"] = component.NewCodeBlock(data[k])
} else {
row["Value"] = component.NewText(data[k])
}
}
return rows, nil
}
type configMapObject interface {
Config(options Options) error
Data(option Options) error
}
type configMapHandler struct {
configMap *corev1.ConfigMap
configFunc func(*corev1.ConfigMap, Options) (*component.Summary, error)
dataFunc func(*corev1.ConfigMap, Options) (*component.Table, error)
object *Object
}
var _ configMapObject = (*configMapHandler)(nil)
func newConfigMapHandler(configMap *corev1.ConfigMap, object *Object) (*configMapHandler, error) {
if configMap == nil {
return nil, errors.New("can't print a nil configmap")
}
if object == nil {
return nil, errors.New("can't print configmap using a nil object printer")
}
ch := &configMapHandler{
configMap: configMap,
configFunc: defaultConfigMapConfig,
dataFunc: defaultConfigMapData,
object: object,
}
return ch, nil
}
func (c *configMapHandler) Config(options Options) error {
out, err := c.configFunc(c.configMap, options)
if err != nil {
return err
}
c.object.RegisterConfig(out)
return nil
}
func defaultConfigMapConfig(configMap *corev1.ConfigMap, options Options) (*component.Summary, error) {
return NewConfigMapConfiguration(configMap).Create(options)
}
func (c *configMapHandler) Data(options Options) error {
if c.configMap == nil {
return errors.New("can't display data for nil configmap")
}
c.object.RegisterItems(ItemDescriptor{
Width: component.WidthFull,
Func: func() (component.Component, error) {
return c.dataFunc(c.configMap, options)
},
})
return nil
}
func defaultConfigMapData(configMap *corev1.ConfigMap, options Options) (*component.Table, error) {
return describeConfigMapData(configMap)
}