From 638a1727818de4ff82dc1d95f8ea3a3338e97cdd Mon Sep 17 00:00:00 2001 From: John Jacobs Date: Wed, 7 Dec 2022 14:58:14 -0800 Subject: [PATCH] Add a configuration for `additional_columns` at the Resource level (#378) Issue #, if available: [1281](https://github.com/aws-controllers-k8s/community/issues/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. --- pkg/config/config.go | 13 +++++++++++++ pkg/config/resource.go | 20 ++++++++++++++++++++ pkg/model/crd.go | 12 ++++++++++++ pkg/model/model.go | 4 ++++ 4 files changed, 49 insertions(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index 52a0a13e..b8cce84a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 { diff --git a/pkg/config/resource.go b/pkg/config/resource.go index 936eb292..ddd53da1 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -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 { @@ -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 diff --git a/pkg/model/crd.go b/pkg/model/crd.go index 1ef2a7fb..b85ccfab 100644 --- a/pkg/model/crd.go +++ b/pkg/model/crd.go @@ -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 { diff --git a/pkg/model/model.go b/pkg/model/model.go index c9242718..f7cc075a 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -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 {