Skip to content

Commit 878da89

Browse files
authored
Add a new generator config to append an Age printcolumn (#84)
Issue: aws-controllers-k8s/community#822 Since All kubernetes resources have a `.metadata.creationTimestamp` field, we need a way to instruct the code generator to generate kubebuilder marker comments that will append the AGE field the `kubectl get` response. This patch extends `Resource.PrintConfig` to allow configuring the `ack-generate` to generate such marker comments. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent e278e1a commit 878da89

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

pkg/generate/config/resource.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ type UpdateOperationConfig struct {
255255
// PrintConfig informs instruct the code generator on how to sort kubebuilder
256256
// printcolumn marker coments.
257257
type PrintConfig struct {
258+
// AddAgeColumn a boolean informing the code generator whether to append a kubebuilder
259+
// marker comment to show a resource Age (created since date) in `kubectl get` response.
260+
// The Age value is parsed from '.metadata.creationTimestamp'.
261+
//
262+
// NOTE: this is the Kubernetes resource Age (creation time at the api-server/etcd)
263+
// and not the AWS resource Age.
264+
AddAgeColumn bool `json:"add_age_column"`
258265
// OrderBy is the field used to sort the list of PrinterColumn options.
259266
OrderBy string `json:"order_by"`
260267
}
@@ -428,3 +435,18 @@ func (c *Config) GetResourcePrintOrderByName(resourceName string) string {
428435
}
429436
return ""
430437
}
438+
439+
// GetResourcePrintAddAgeColumn returns the resource printer AddAgeColumn config
440+
func (c *Config) GetResourcePrintAddAgeColumn(resourceName string) bool {
441+
if c == nil {
442+
return false
443+
}
444+
rConfig, ok := c.Resources[resourceName]
445+
if !ok {
446+
return false
447+
}
448+
if rConfig.Print != nil {
449+
return rConfig.Print.AddAgeColumn
450+
}
451+
return false
452+
}

pkg/model/crd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ func (r *CRD) GetResourcePrintOrderByName() string {
461461
return orderBy
462462
}
463463

464+
// PrintAgeColumn returns whether the code generator should append 'Age'
465+
// kubebuilder:printcolumn comment marker
466+
func (r *CRD) PrintAgeColumn() bool {
467+
return r.cfg.GetResourcePrintAddAgeColumn(r.Names.Camel)
468+
}
469+
464470
// CustomUpdateMethodName returns the name of the custom resourceManager method
465471
// for updating the resource state, if any has been specified in the generator
466472
// config

templates/apis/crd.go.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type {{ .CRD.Kind }}Status struct {
5050
{{- range $column := .CRD.AdditionalPrinterColumns }}
5151
// +kubebuilder:printcolumn:name="{{$column.Name}}",type={{$column.Type}},priority={{$column.Priority}},JSONPath=`{{$column.JSONPath}}`
5252
{{- end }}
53+
{{- if .CRD.PrintAgeColumn }}
54+
// +kubebuilder:printcolumn:name="Age",type="date",priority=0,JSONPath=".metadata.creationTimestamp"
55+
{{- end }}
5356
{{- if .CRD.ShortNames }}
5457
// +kubebuilder:resource:shortName={{ Join .CRD.ShortNames ";" }}
5558
{{- end }}

0 commit comments

Comments
 (0)