Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/generate/config/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ type PrintFieldConfig struct {
// view (using the -o wide flag). Fields with priority 0 are shown in standard view.
// Fields with priority greater than 0 are only shown in wide view. Default is 0
Priority int `json:"priority"`
// Index informs the code generator about the position/order of a specific field/column in
// `kubectl get` response. To enable ordering by index, `$resource.print.orderBy` must be set
// to `index`
// The field with the smallest index will be right next to the first column (NAME).
// The field with the biggest index will be positioned right before the last column (AGE).
Index int `json:"index"`
}

// FieldConfig contains instructions to the code generator about how
Expand Down
10 changes: 8 additions & 2 deletions pkg/model/printer_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type PrinterColumn struct {
Type string
Priority int
JSONPath string
Index int
}

// By can sort two PrinterColumns
Expand Down Expand Up @@ -64,7 +65,6 @@ func (pcs printerColumnSorter) Less(i, j int) bool {
// sortFunction returns a Go function used the sort the printer columns.
func sortFunction(sortByField string) func(a, b *PrinterColumn) bool {
switch strings.ToLower(sortByField) {
//TODO(a-hially): add Priority and Order sort functions
case "name":
return func(a, b *PrinterColumn) bool {
return a.Name < b.Name
Expand All @@ -77,8 +77,13 @@ func sortFunction(sortByField string) func(a, b *PrinterColumn) bool {
return func(a, b *PrinterColumn) bool {
return a.JSONPath < b.JSONPath
}
case "index":
return func(a, b *PrinterColumn) bool {
return a.Index < b.Index
}
Comment on lines +80 to +83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the first case in the switch? Sorting by index should be preferred over any other?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't how moving this case could change the default/preferred value. sortByField will have one of the values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean moving it to the default case? or maybe somehow set a default for $resource.print.sortBy ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no I totally misread this method. Sorry

default:
msg := fmt.Sprintf("unknown sort-by field: '%s'. must be one of 'Name', 'Type' and 'JSONPath'", sortByField)
msg := fmt.Sprintf("unknown sort-by field: '%s'. must be one of 'Name',"+
" 'Type', 'JSONPath' and 'Index'", sortByField)
panic(msg)
}
}
Expand Down Expand Up @@ -143,6 +148,7 @@ func (r *CRD) addPrintableColumn(
Type: printColumnType,
Priority: field.FieldConfig.Print.Priority,
JSONPath: jsonPath,
Index: field.FieldConfig.Print.Index,
}
r.additionalPrinterColumns = append(r.additionalPrinterColumns, column)
}
Expand Down