-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtableconvertor.go
More file actions
98 lines (85 loc) · 2.69 KB
/
tableconvertor.go
File metadata and controls
98 lines (85 loc) · 2.69 KB
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
package apiserver
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metatable "k8s.io/apimachinery/pkg/api/meta/table"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
"github.com/grafana/grafana-app-sdk/resource"
)
var swaggerMetadataDescriptions = metav1.ObjectMeta{}.SwaggerDoc()
type columnDefinition struct {
valueFunc func(resource.Object) (any, error)
header metav1.TableColumnDefinition
}
type additionalColumnsTableConvertor struct {
headers []metav1.TableColumnDefinition
columns []columnDefinition
}
// newTableConvertor creates a rest.TableConvertor from Schema-provided TableColumns.
func newTableConvertor(columns []resource.TableColumn) rest.TableConvertor {
c := &additionalColumnsTableConvertor{
headers: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: swaggerMetadataDescriptions["name"]},
},
}
for _, col := range columns {
desc := fmt.Sprintf("Custom resource definition column (in JSONPath format): %s", col.JSONPath)
if col.Description != "" {
desc = col.Description
}
c.columns = append(c.columns, columnDefinition{
valueFunc: col.ValueFunc,
header: metav1.TableColumnDefinition{
Name: col.Name,
Type: col.Type,
Format: col.Format,
Description: desc,
Priority: col.Priority,
},
})
c.headers = append(c.headers, c.columns[len(c.columns)-1].header)
}
return c
}
func (c *additionalColumnsTableConvertor) ConvertToTable(_ context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
table := &metav1.Table{}
opt, ok := tableOptions.(*metav1.TableOptions)
noHeaders := ok && opt != nil && opt.NoHeaders
if !noHeaders {
table.ColumnDefinitions = c.headers
}
if m, err := meta.ListAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion()
table.Continue = m.GetContinue()
table.RemainingItemCount = m.GetRemainingItemCount()
} else {
if m, err := meta.CommonAccessor(obj); err == nil {
table.ResourceVersion = m.GetResourceVersion()
}
}
var tableErr error
table.Rows, tableErr = metatable.MetaToTableRow(obj, func(obj runtime.Object, _ metav1.Object, name, _ string) ([]any, error) {
cells := make([]any, 1, 1+len(c.columns))
cells[0] = name
resourceObj, ok := obj.(resource.Object)
if !ok {
for range c.columns {
cells = append(cells, nil)
}
return cells, nil
}
for _, col := range c.columns {
value, err := col.valueFunc(resourceObj)
if err != nil || value == nil {
cells = append(cells, nil)
continue
}
cells = append(cells, value)
}
return cells, nil
})
return table, tableErr
}