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

Conversation

jljaco
Copy link
Contributor

@jljaco jljaco commented Dec 5, 2022

Issue #, if available: 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.

@ack-bot
Copy link
Collaborator

ack-bot commented Dec 5, 2022

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ack-bot ack-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 5, 2022
@jljaco jljaco force-pushed the jljaco/1281_additional_printer_columns branch from 95fba7d to a474316 Compare December 5, 2022 02:22
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

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

Great stuff! Thank you @jljaco.
I left few comments and suggestions below


// 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

Comment on lines 290 to 293
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

}

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

Comment on lines 320 to 324
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

@@ -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

@jljaco jljaco requested a review from a-hilaly December 7, 2022 22:24
@jljaco jljaco marked this pull request as ready for review December 7, 2022 22:28
@ack-bot ack-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 7, 2022
pkg/config/config.go Outdated Show resolved Hide resolved
if !ok || resourceConfig.Print == nil || resourceConfig.Print.AdditionalColumns == nil {
return []AdditionalColumnConfig{}
if !ok || resourceConfig.Print == nil || len(resourceConfig.Print.AdditionalColumns) == 0 {
return []*AdditionalColumnConfig{}
Copy link
Member

Choose a reason for hiding this comment

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

Ditto

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

Comment on lines 323 to 333
// the name to display in the column's output
Name string `json:"name"`
// the JSONPath definining the source of the output
JSONPath string `json:"json_path"`
// 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"`
// the priority of the column in the resource's output
Priority int `json:"priority,omitempty"`
// the zero-based index of the position at which to display the column in output
Index int `json:"index,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Quotting from https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences:

Comments should begin with the name of the thing being described and end in a period

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

pkg/model/crd.go Outdated
Comment on lines 569 to 582
func (r *CRD) setAdditionalPrinterColumns(additionalColumns []*ackgenconfig.AdditionalColumnConfig) {
r.additionalPrinterColumns = []*PrinterColumn{}

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)
}
}

Copy link
Member

Choose a reason for hiding this comment

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

The allocation on L570 overrides the existing printerColumns previously added in model.go L284 and L236. Removing L570 should fix the code and the unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, good point. In that case, I'm going to rename this addAdditionalPrinterColumns to avoid ambiguity.

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

Co-authored-by: Amine <hilalyamine@gmail.com>
@jljaco jljaco force-pushed the jljaco/1281_additional_printer_columns branch from 247b895 to 0255fa5 Compare December 7, 2022 22:46
@jljaco jljaco requested a review from a-hilaly December 7, 2022 22:50
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

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

Top! Merci beaucoup :)
/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 7, 2022
@ack-bot
Copy link
Collaborator

ack-bot commented Dec 7, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: A-Hilaly, jljaco

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-bot ack-bot merged commit 638a172 into aws-controllers-k8s:main Dec 7, 2022
@jljaco jljaco deleted the jljaco/1281_additional_printer_columns branch December 7, 2022 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants