Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a configuration for additional_columns at the Resource level #378

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 []AdditionalColumnConfig{}
}

resourceConfig, ok := c.Resources[resourceName]
if !ok || resourceConfig.Print == nil || resourceConfig.Print.AdditionalColumns == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil checks on slices are not super reliable in Go. The len built-in function is the most accurate one for this use case (it checks the nility first then size of the slice)

Suggested change
if !ok || resourceConfig.Print == nil || resourceConfig.Print.AdditionalColumns == nil {
if !ok || resourceConfig.Print == nil || len(resourceConfig.Print.AdditionalColumns) == 0 {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return []AdditionalColumnConfig{}
}
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
10 changes: 10 additions & 0 deletions pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ type UpdateOperationConfig struct {
CustomMethodName string `json:"custom_method_name"`
}

type AdditionalColumnConfig struct {
Name string `json:"name"`
JSONPath string `json:"json_path"`
Type string `json:"type"`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Please include tiny comments for the struct and its fields. Those will be included in our generated GoDoc documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// PrintConfig informs instruct the code generator on how to sort kubebuilder
// printcolumn marker coments.
type PrintConfig struct {
Expand All @@ -334,6 +340,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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we make this a pointers array? if we do so GetAdditionalColumns will not have to allocate a new AdditionalColumnConfig every time there an error, or something missing.

Suggested change
AdditionalColumns []AdditionalColumnConfig `json:"additional_columns,omitempty"`
AdditionalColumns []*AdditionalColumnConfig `json:"additional_columns,omitempty"`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

// ReconcileConfig describes options for controlling the reconciliation
Expand Down
10 changes: 10 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ func (m *Model) GetCRDs() ([]*CRD, error) {
crd.AddStatusField(memberNames, memberShapeRef)
}

// Now add the additional printer columns that have been defined explicitly
// in additional_columns
for _, additionalColumn := range m.cfg.GetAdditionalColumns(crdName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny suggestion: Shall we make this a private method? thinking crd.setAdditionalPrinterColumns()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

printerColumn := &PrinterColumn{}
printerColumn.Name = additionalColumn.Name
printerColumn.JSONPath = additionalColumn.JSONPath
printerColumn.Type = additionalColumn.Type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should also set Priority and Index

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

crd.additionalPrinterColumns = append(crd.additionalPrinterColumns, printerColumn)
}

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