Skip to content

Commit 818c022

Browse files
authored
Add a new generator config to change PrinterColumn sort function (#83)
Issue aws-controllers-k8s/community#823 This patch introduce a new generator configuration under resource configuration, mainly to tune the `kubebuilder:printcolumn` marker comments generation. More precisely we add `PrintConfig` struct that contains one field called `OrderBy` used to intruct the code generator how to sort the `kubebuilder:printcolumn` marker comments. e.g configuration: ```yaml resources: Deployment: print: order_by: JSONPath ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 5d5ae05 commit 818c022

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

pkg/generate/config/resource.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ type ResourceConfig struct {
7676
// If set to false, the user will be given an error if they attempt to adopt a resource
7777
// with this type.
7878
IsAdoptable *bool `json:"is_adoptable,omitempty"`
79+
// Print contains instructions for the code generator to generate kubebuilder printcolumns
80+
// marker comments.
81+
Print *PrintConfig `json:"print,omitempty"`
7982
}
8083

8184
// HooksConfig instructs the code generator how to inject custom callback hooks
@@ -249,6 +252,13 @@ type UpdateOperationConfig struct {
249252
CustomMethodName string `json:"custom_method_name"`
250253
}
251254

255+
// PrintConfig informs instruct the code generator on how to sort kubebuilder
256+
// printcolumn marker coments.
257+
type PrintConfig struct {
258+
// OrderBy is the field used to sort the list of PrinterColumn options.
259+
OrderBy string `json:"order_by"`
260+
}
261+
252262
// ResourceConfig returns the ResourceConfig for a given named resource
253263
func (c *Config) ResourceConfig(name string) (*ResourceConfig, bool) {
254264
rc, ok := c.Resources[name]
@@ -403,3 +413,18 @@ func (c *Config) ResourceIsAdoptable(resourceName string) bool {
403413
}
404414
return *rConfig.IsAdoptable
405415
}
416+
417+
// GetResourcePrintOrderByName returns the Printer Column order-by field name
418+
func (c *Config) GetResourcePrintOrderByName(resourceName string) string {
419+
if c == nil {
420+
return ""
421+
}
422+
rConfig, ok := c.Resources[resourceName]
423+
if !ok {
424+
return ""
425+
}
426+
if rConfig.Print != nil {
427+
return rConfig.Print.OrderBy
428+
}
429+
return ""
430+
}

pkg/generate/testdata/models/apis/codedeploy/0000-00-00/generator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
resources:
22
Deployment:
3+
print:
4+
order_by: jsonpath
35
exceptions:
46
errors:
57
404:

pkg/model/crd.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,15 @@ func (r *CRD) IsAdoptable() bool {
417417
return r.cfg.ResourceIsAdoptable(r.Names.Original)
418418
}
419419

420+
// GetResourcePrintOrderByName returns the Printer Column order-by field name
421+
func (r *CRD) GetResourcePrintOrderByName() string {
422+
orderBy := r.cfg.GetResourcePrintOrderByName(r.Names.Camel)
423+
if orderBy == "" {
424+
return "name"
425+
}
426+
return orderBy
427+
}
428+
420429
// CustomUpdateMethodName returns the name of the custom resourceManager method
421430
// for updating the resource state, if any has been specified in the generator
422431
// config

pkg/model/printer_column.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package model
1616
import (
1717
"fmt"
1818
"sort"
19+
"strings"
1920
)
2021

2122
// PrinterColumn represents a single field in the CRD's Spec or Status objects
@@ -59,13 +60,34 @@ func (pcs printerColumnSorter) Less(i, j int) bool {
5960
return pcs.by(pcs.cols[i], pcs.cols[j])
6061
}
6162

63+
// sortFunction returns a Go function used the sort the printer columns.
64+
func sortFunction(sortByField string) func(a, b *PrinterColumn) bool {
65+
switch strings.ToLower(sortByField) {
66+
//TODO(a-hially): add Priority and Order sort functions
67+
case "name":
68+
return func(a, b *PrinterColumn) bool {
69+
return a.Name < b.Name
70+
}
71+
case "type":
72+
return func(a, b *PrinterColumn) bool {
73+
return a.Type < b.Type
74+
}
75+
case "jsonpath":
76+
return func(a, b *PrinterColumn) bool {
77+
return a.JSONPath < b.JSONPath
78+
}
79+
default:
80+
msg := fmt.Sprintf("unknown sort-by field: '%s'. must be one of 'Name', 'Type' and 'JSONPath'", sortByField)
81+
panic(msg)
82+
}
83+
}
84+
6285
// AdditionalPrinterColumns returns a sorted list of PrinterColumn structs for
6386
// the resource
64-
func (r CRD) AdditionalPrinterColumns() []*PrinterColumn {
65-
byName := func(a, b *PrinterColumn) bool {
66-
return a.Name < b.Name
67-
}
68-
By(byName).Sort(r.additionalPrinterColumns)
87+
func (r *CRD) AdditionalPrinterColumns() []*PrinterColumn {
88+
orderByFieldName := r.GetResourcePrintOrderByName()
89+
sortFn := sortFunction(orderByFieldName)
90+
By(sortFn).Sort(r.additionalPrinterColumns)
6991
return r.additionalPrinterColumns
7092
}
7193

0 commit comments

Comments
 (0)