-
Notifications
You must be signed in to change notification settings - Fork 190
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
Add a configuration for additional_columns
at the Resource level
#378
Conversation
Skipping CI for Draft Pull Request. |
95fba7d
to
a474316
Compare
There was a problem hiding this 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
pkg/config/resource.go
Outdated
|
||
// 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"` |
There was a problem hiding this comment.
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.
AdditionalColumns []AdditionalColumnConfig `json:"additional_columns,omitempty"` | |
AdditionalColumns []*AdditionalColumnConfig `json:"additional_columns,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/model/model.go
Outdated
printerColumn := &PrinterColumn{} | ||
printerColumn.Name = additionalColumn.Name | ||
printerColumn.JSONPath = additionalColumn.JSONPath | ||
printerColumn.Type = additionalColumn.Type |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/config/config.go
Outdated
} | ||
|
||
resourceConfig, ok := c.Resources[resourceName] | ||
if !ok || resourceConfig.Print == nil || resourceConfig.Print.AdditionalColumns == nil { |
There was a problem hiding this comment.
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)
if !ok || resourceConfig.Print == nil || resourceConfig.Print.AdditionalColumns == nil { | |
if !ok || resourceConfig.Print == nil || len(resourceConfig.Print.AdditionalColumns) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/config/resource.go
Outdated
type AdditionalColumnConfig struct { | ||
Name string `json:"name"` | ||
JSONPath string `json:"json_path"` | ||
Type string `json:"type"` | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/model/model.go
Outdated
@@ -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) { |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/config/config.go
Outdated
if !ok || resourceConfig.Print == nil || resourceConfig.Print.AdditionalColumns == nil { | ||
return []AdditionalColumnConfig{} | ||
if !ok || resourceConfig.Print == nil || len(resourceConfig.Print.AdditionalColumns) == 0 { | ||
return []*AdditionalColumnConfig{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
pkg/config/resource.go
Outdated
// 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"` |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
247b895
to
0255fa5
Compare
There was a problem hiding this 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
[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 |
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.