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 510f43ba..93a0a880 100644 --- a/pkg/config/resource.go +++ b/pkg/config/resource.go @@ -317,6 +317,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 { @@ -334,6 +350,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 {