Skip to content

Commit e050b9f

Browse files
authored
Add a new sort option for kubebuilder:printcolumns marker comments (#124)
Issue N/A This patch adds a new option `Index` for print column configuration. Setting an index for a field and `resource.print.sortBy` to `index` will intruct the code generator to sort the `kubebuilder:printcolumns` by the `Index` value. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 25e4809 commit e050b9f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pkg/generate/config/field.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ type PrintFieldConfig struct {
128128
// view (using the -o wide flag). Fields with priority 0 are shown in standard view.
129129
// Fields with priority greater than 0 are only shown in wide view. Default is 0
130130
Priority int `json:"priority"`
131+
// Index informs the code generator about the position/order of a specific field/column in
132+
// `kubectl get` response. To enable ordering by index, `$resource.print.orderBy` must be set
133+
// to `index`
134+
// The field with the smallest index will be right next to the first column (NAME).
135+
// The field with the biggest index will be positioned right before the last column (AGE).
136+
Index int `json:"index"`
131137
}
132138

133139
// FieldConfig contains instructions to the code generator about how

pkg/model/printer_column.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type PrinterColumn struct {
2626
Type string
2727
Priority int
2828
JSONPath string
29+
Index int
2930
}
3031

3132
// By can sort two PrinterColumns
@@ -64,7 +65,6 @@ func (pcs printerColumnSorter) Less(i, j int) bool {
6465
// sortFunction returns a Go function used the sort the printer columns.
6566
func sortFunction(sortByField string) func(a, b *PrinterColumn) bool {
6667
switch strings.ToLower(sortByField) {
67-
//TODO(a-hially): add Priority and Order sort functions
6868
case "name":
6969
return func(a, b *PrinterColumn) bool {
7070
return a.Name < b.Name
@@ -77,8 +77,13 @@ func sortFunction(sortByField string) func(a, b *PrinterColumn) bool {
7777
return func(a, b *PrinterColumn) bool {
7878
return a.JSONPath < b.JSONPath
7979
}
80+
case "index":
81+
return func(a, b *PrinterColumn) bool {
82+
return a.Index < b.Index
83+
}
8084
default:
81-
msg := fmt.Sprintf("unknown sort-by field: '%s'. must be one of 'Name', 'Type' and 'JSONPath'", sortByField)
85+
msg := fmt.Sprintf("unknown sort-by field: '%s'. must be one of 'Name',"+
86+
" 'Type', 'JSONPath' and 'Index'", sortByField)
8287
panic(msg)
8388
}
8489
}
@@ -143,6 +148,7 @@ func (r *CRD) addPrintableColumn(
143148
Type: printColumnType,
144149
Priority: field.FieldConfig.Print.Priority,
145150
JSONPath: jsonPath,
151+
Index: field.FieldConfig.Print.Index,
146152
}
147153
r.additionalPrinterColumns = append(r.additionalPrinterColumns, column)
148154
}

0 commit comments

Comments
 (0)