Skip to content

Commit 638a172

Browse files
authored
Add a configuration for additional_columns at the Resource level (#378)
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.
1 parent 20e82a5 commit 638a172

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

pkg/config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ type PrefixConfig struct {
7474
StatusField string `json:"status_field,omitempty"`
7575
}
7676

77+
// GetAdditionalColumns extracts AdditionalColumns defined for a given Resource
78+
func (c *Config) GetAdditionalColumns(resourceName string) []*AdditionalColumnConfig {
79+
if c == nil {
80+
return nil
81+
}
82+
83+
resourceConfig, ok := c.Resources[resourceName]
84+
if !ok || resourceConfig.Print == nil || len(resourceConfig.Print.AdditionalColumns) == 0 {
85+
return nil
86+
}
87+
return resourceConfig.Print.AdditionalColumns
88+
}
89+
7790
// GetCustomListFieldMembers finds all of the custom list fields that need to
7891
// be generated as defined in the generator config.
7992
func (c *Config) GetCustomListFieldMembers() []string {

pkg/config/resource.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ type UpdateOperationConfig struct {
319319
CustomMethodName string `json:"custom_method_name"`
320320
}
321321

322+
// AdditionalConfig can be used to specify additional printer columns to be included
323+
// in a Resource's output from kubectl.
324+
type AdditionalColumnConfig struct {
325+
// Name is the thing to display in the column's output.
326+
Name string `json:"name"`
327+
// JSONPath defines the source of the output.
328+
JSONPath string `json:"json_path"`
329+
// Type is the OpenAPI type of the output.
330+
// c.f., https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types
331+
Type string `json:"type"`
332+
// Priority of the column in the resource's output.
333+
Priority int `json:"priority,omitempty"`
334+
// Index is the zero-based index of the position at which to display the column in output.
335+
Index int `json:"index,omitempty"`
336+
}
337+
322338
// PrintConfig informs instruct the code generator on how to sort kubebuilder
323339
// printcolumn marker coments.
324340
type PrintConfig struct {
@@ -336,6 +352,10 @@ type PrintConfig struct {
336352
AddSyncedColumn *bool `json:"add_synced_column"`
337353
// OrderBy is the field used to sort the list of PrinterColumn options.
338354
OrderBy string `json:"order_by"`
355+
356+
// AdditionalColumns can be used to add arbitrary extra columns to a Resource's output
357+
// if present, should be a list of objects, each containing: name, json_path, and type
358+
AdditionalColumns []*AdditionalColumnConfig `json:"additional_columns,omitempty"`
339359
}
340360

341361
// ReconcileConfig describes options for controlling the reconciliation

pkg/model/crd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,18 @@ func (r *CRD) PrintSyncedColumn() bool {
566566
return r.cfg.ResourceDisplaysSyncedColumn(r.Names.Camel)
567567
}
568568

569+
func (r *CRD) addAdditionalPrinterColumns(additionalColumns []*ackgenconfig.AdditionalColumnConfig) {
570+
for _, additionalColumn := range additionalColumns {
571+
printerColumn := &PrinterColumn{}
572+
printerColumn.Name = additionalColumn.Name
573+
printerColumn.JSONPath = additionalColumn.JSONPath
574+
printerColumn.Type = additionalColumn.Type
575+
printerColumn.Priority = additionalColumn.Priority
576+
printerColumn.Index = additionalColumn.Index
577+
r.additionalPrinterColumns = append(r.additionalPrinterColumns, printerColumn)
578+
}
579+
}
580+
569581
// ReconcileRequeuOnSuccessSeconds returns the duration after which to requeue
570582
// the custom resource as int
571583
func (r *CRD) ReconcileRequeuOnSuccessSeconds() int {

pkg/model/model.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ func (m *Model) GetCRDs() ([]*CRD, error) {
284284
crd.AddStatusField(memberNames, memberShapeRef)
285285
}
286286

287+
// Now add the additional printer columns that have been defined explicitly
288+
// in additional_columns
289+
crd.addAdditionalPrinterColumns(m.cfg.GetAdditionalColumns(crdName))
290+
287291
crds = append(crds, crd)
288292
}
289293
sort.Slice(crds, func(i, j int) bool {

0 commit comments

Comments
 (0)