Skip to content

Commit

Permalink
Add a configuration for additional_columns at the Resource level (#378
Browse files Browse the repository at this point in the history
)

Issue #, if available:  [1281](aws-controllers-k8s/community#1281)

Description of changes:

We want to have the option to include additional printer columns in the output of `kubectl`.  This change allow the author to specify an arbitrary number of those per Resource, for each specifying the name, type, and JSON path of the field to produce.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
jljaco authored Dec 7, 2022
1 parent 20e82a5 commit 638a172
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ type PrefixConfig struct {
StatusField string `json:"status_field,omitempty"`
}

// GetAdditionalColumns extracts AdditionalColumns defined for a given Resource
func (c *Config) GetAdditionalColumns(resourceName string) []*AdditionalColumnConfig {
if c == nil {
return nil
}

resourceConfig, ok := c.Resources[resourceName]
if !ok || resourceConfig.Print == nil || len(resourceConfig.Print.AdditionalColumns) == 0 {
return nil
}
return resourceConfig.Print.AdditionalColumns
}

// GetCustomListFieldMembers finds all of the custom list fields that need to
// be generated as defined in the generator config.
func (c *Config) GetCustomListFieldMembers() []string {
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ type UpdateOperationConfig struct {
CustomMethodName string `json:"custom_method_name"`
}

// AdditionalConfig can be used to specify additional printer columns to be included
// in a Resource's output from kubectl.
type AdditionalColumnConfig struct {
// Name is the thing to display in the column's output.
Name string `json:"name"`
// JSONPath defines the source of the output.
JSONPath string `json:"json_path"`
// Type is the OpenAPI type of the output.
// c.f., https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
Type string `json:"type"`
// Priority of the column in the resource's output.
Priority int `json:"priority,omitempty"`
// Index is the zero-based index of the position at which to display the column in output.
Index int `json:"index,omitempty"`
}

// PrintConfig informs instruct the code generator on how to sort kubebuilder
// printcolumn marker coments.
type PrintConfig struct {
Expand All @@ -336,6 +352,10 @@ type PrintConfig struct {
AddSyncedColumn *bool `json:"add_synced_column"`
// OrderBy is the field used to sort the list of PrinterColumn options.
OrderBy string `json:"order_by"`

// AdditionalColumns can be used to add arbitrary extra columns to a Resource's output
// if present, should be a list of objects, each containing: name, json_path, and type
AdditionalColumns []*AdditionalColumnConfig `json:"additional_columns,omitempty"`
}

// ReconcileConfig describes options for controlling the reconciliation
Expand Down
12 changes: 12 additions & 0 deletions pkg/model/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,18 @@ func (r *CRD) PrintSyncedColumn() bool {
return r.cfg.ResourceDisplaysSyncedColumn(r.Names.Camel)
}

func (r *CRD) addAdditionalPrinterColumns(additionalColumns []*ackgenconfig.AdditionalColumnConfig) {
for _, additionalColumn := range additionalColumns {
printerColumn := &PrinterColumn{}
printerColumn.Name = additionalColumn.Name
printerColumn.JSONPath = additionalColumn.JSONPath
printerColumn.Type = additionalColumn.Type
printerColumn.Priority = additionalColumn.Priority
printerColumn.Index = additionalColumn.Index
r.additionalPrinterColumns = append(r.additionalPrinterColumns, printerColumn)
}
}

// ReconcileRequeuOnSuccessSeconds returns the duration after which to requeue
// the custom resource as int
func (r *CRD) ReconcileRequeuOnSuccessSeconds() int {
Expand Down
4 changes: 4 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ func (m *Model) GetCRDs() ([]*CRD, error) {
crd.AddStatusField(memberNames, memberShapeRef)
}

// Now add the additional printer columns that have been defined explicitly
// in additional_columns
crd.addAdditionalPrinterColumns(m.cfg.GetAdditionalColumns(crdName))

crds = append(crds, crd)
}
sort.Slice(crds, func(i, j int) bool {
Expand Down

0 comments on commit 638a172

Please sign in to comment.