This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
customresource.go
235 lines (179 loc) · 5.91 KB
/
customresource.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
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/pkg/errors"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/util/jsonpath"
"github.com/vmware/octant/internal/link"
octantStrings "github.com/vmware/octant/internal/util/strings"
"github.com/vmware/octant/pkg/view/component"
)
// CustomResourceListHandler prints a list of custom resources with
// optional custom columns.
func CustomResourceListHandler(
crdName string,
crd *apiextv1beta1.CustomResourceDefinition,
list *unstructured.UnstructuredList,
linkGenerator link.Interface,
isLoading bool) (component.Component, error) {
hasCustomColumns := len(crd.Spec.AdditionalPrinterColumns) > 0
if hasCustomColumns {
return printCustomCRDListTable(crdName, crd, list, linkGenerator, isLoading)
}
return printGenericCRDTable(crdName, list, linkGenerator, isLoading)
}
func printGenericCRDTable(crdName string, list *unstructured.UnstructuredList, linkGenerator link.Interface, isLoading bool) (component.Component, error) {
cols := component.NewTableCols("Name", "Labels", "Age")
table := component.NewTable(crdName, "We couldn't find any custom resources!", cols)
for i := range list.Items {
cr := list.Items[i]
row := component.TableRow{}
name, err := linkGenerator.ForObject(&cr, cr.GetName())
if err != nil {
return nil, err
}
row["Name"] = name
row["Labels"] = component.NewLabels(cr.GetLabels())
row["Age"] = component.NewTimestamp(cr.GetCreationTimestamp().Time)
table.Add(row)
}
table.SetIsLoading(isLoading)
table.Sort("Name", false)
return table, nil
}
func printCustomCRDListTable(
crdName string,
crd *apiextv1beta1.CustomResourceDefinition,
list *unstructured.UnstructuredList,
linkGenerator link.Interface,
isLoading bool) (component.Component, error) {
table := component.NewTable(crdName, "We couldn't find any custom resources!", component.NewTableCols("Name", "Labels"))
for _, column := range crd.Spec.AdditionalPrinterColumns {
name := column.Name
if octantStrings.Contains(column.Name, []string{"Name", "Labels", "Age"}) {
name = fmt.Sprintf("Resource %s", column.Name)
}
table.AddColumn(name)
}
table.AddColumn("Age")
for i := range list.Items {
cr := list.Items[i]
row := component.TableRow{}
name, err := linkGenerator.ForObject(&cr, cr.GetName())
if err != nil {
return nil, err
}
row["Name"] = name
row["Labels"] = component.NewLabels(cr.GetLabels())
row["Age"] = component.NewTimestamp(cr.GetCreationTimestamp().Time)
for _, column := range crd.Spec.AdditionalPrinterColumns {
s, err := printCustomColumn(cr.Object, column)
if err != nil {
return nil, errors.Wrapf(err, "print custom column %q in CRD %q",
column.Name, crd.Name)
}
name := column.Name
if _, ok := row[column.Name]; ok {
name = fmt.Sprintf("Resource %s", column.Name)
}
row[name] = component.NewText(s)
}
table.Add(row)
}
table.SetIsLoading(isLoading)
table.Sort("Name", false)
return table, nil
}
func printCustomColumn(m interface{}, column apiextv1beta1.CustomResourceColumnDefinition) (string, error) {
j := jsonpath.New(column.Name)
buf := bytes.Buffer{}
s := strings.Replace(column.JSONPath, "\\", "", -1)
if err := j.Parse(fmt.Sprintf("{%s}", s)); err != nil {
return "", errors.Wrapf(err, "jsonpath parse error: %s", s)
}
if err := j.Execute(&buf, m); err != nil {
// inspecting the error string because jsonpath doesn't do typed errors
if strings.Contains(err.Error(), "is not found") {
return "<not found>", nil
}
return "", errors.Wrapf(err, "jsonpath execute error")
}
return buf.String(), nil
}
// CustomResourceHandler prints custom resource objects. If the
// object has columns specified, it will print those columns as well.
func CustomResourceHandler(
ctx context.Context,
crd *apiextv1beta1.CustomResourceDefinition,
object *unstructured.Unstructured,
options Options) (component.Component, error) {
o := NewObject(object)
configSummary, err := printCustomResourceConfig(object, crd)
if err != nil {
return nil, err
}
statusSummary, err := printCustomResourceStatus(object, crd)
if err != nil {
return nil, err
}
o.RegisterConfig(configSummary)
o.RegisterSummary(statusSummary)
o.EnableEvents()
view, err := o.ToComponent(ctx, options)
if err != nil {
return nil, errors.Wrap(err, "print custom resource")
}
return view, nil
}
func printCustomResourceConfig(u *unstructured.Unstructured, crd *apiextv1beta1.CustomResourceDefinition) (*component.Summary, error) {
if crd == nil {
return nil, errors.New("CRD is nil")
}
summary := component.NewSummary("Configuration")
if len(crd.Spec.AdditionalPrinterColumns) < 1 {
return summary, nil
}
sections := component.SummarySections{}
for _, column := range crd.Spec.AdditionalPrinterColumns {
if strings.HasPrefix(column.JSONPath, ".spec") {
s, err := printCustomColumn(u.Object, column)
if err != nil {
return nil, errors.Wrap(err, "print custom column")
}
if s != "" {
sections.AddText(column.Name, s)
}
}
}
summary.Add(sections...)
return summary, nil
}
func printCustomResourceStatus(u *unstructured.Unstructured, crd *apiextv1beta1.CustomResourceDefinition) (*component.Summary, error) {
if crd == nil {
return nil, errors.New("CRD is nil")
}
summary := component.NewSummary("Status")
if len(crd.Spec.AdditionalPrinterColumns) < 1 {
return summary, nil
}
sections := component.SummarySections{}
for _, column := range crd.Spec.AdditionalPrinterColumns {
if strings.HasPrefix(column.JSONPath, ".status") {
s, err := printCustomColumn(u.Object, column)
if err != nil {
return nil, errors.Wrap(err, "print custom column")
}
sections.AddText(column.Name, s)
}
}
summary.Add(sections...)
return summary, nil
}