Skip to content

feat: Annotate CRDs with Controller version #602

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions cmd/ack-generate/command/apis.go
Copy link
Member

Choose a reason for hiding this comment

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

just leaving thoughts here: another way would've been to modify the release script instead, and leverage yq to inject the annotation. But i pref what you're doing here

Copy link
Member Author

Choose a reason for hiding this comment

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

this might be the better approach..since we don't run make build-controller when we do releases, the latest release version may not be annotated..Maybe i should add a controller-gen command here as well

Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ func saveGeneratedMetadata(cmd *cobra.Command, args []string) error {
// generateAPIs generates the Go files for each resource in the AWS service
// API.
func generateAPIs(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
if len(args) < 1 {
return fmt.Errorf("please specify the service alias for the AWS service API to generate")
}
svcAlias := strings.ToLower(args[0])
releaseVersion := strings.ToLower(args[1])
releaseVersion = strings.TrimPrefix(releaseVersion, "v")
if optOutputPath == "" {
optOutputPath = filepath.Join(optServicesDir, svcAlias)
}
Expand All @@ -107,7 +109,7 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
ts, err := ackgenerate.APIs(m, optTemplateDirs)
ts, err := ackgenerate.APIs(m, optTemplateDirs, releaseVersion)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/ack-generate/command/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func init() {

// generateController generates the Go files for a service controller
func generateController(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("please specify the service alias for the AWS service API to generate")
if len(args) < 1 {
return fmt.Errorf("please specify the service alias for the AWS service API to generate: controller")
Copy link
Member

Choose a reason for hiding this comment

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

Why add the post fix : controller

Copy link
Member Author

Choose a reason for hiding this comment

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

just needed to see where the error is coming from..we have this exact error message in 3 different places.
Maybe it can be handled in a different PR

}
svcAlias := strings.ToLower(args[0])
releaseVersion := strings.ToLower(args[1])
releaseVersion = strings.TrimPrefix(releaseVersion, "v")
if optOutputPath == "" {
optOutputPath = filepath.Join(optServicesDir, svcAlias)
}
Expand All @@ -72,7 +74,7 @@ func generateController(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
ts, err := ackgenerate.Controller(m, optTemplateDirs, serviceAccountName)
ts, err := ackgenerate.Controller(m, optTemplateDirs, serviceAccountName, releaseVersion)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/generate/ack/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
func APIs(
m *ackmodel.Model,
templateBasePaths []string,
releaseVersion string,
) (*templateset.TemplateSet, error) {
enumDefs, err := m.GetEnumDefs()
if err != nil {
Expand Down Expand Up @@ -86,6 +87,7 @@ func APIs(
metaVars,
m.SDKAPI,
crd,
releaseVersion,
}
if err = ts.Add(crdFileName, "apis/crd.go.tpl", crdVars); err != nil {
return nil, err
Expand All @@ -106,6 +108,7 @@ type templateAPIVars struct {
// code for a single top-level resource's API definition
type templateCRDVars struct {
templateset.MetaVars
SDKAPI *ackmodel.SDKAPI
CRD *ackmodel.CRD
SDKAPI *ackmodel.SDKAPI
CRD *ackmodel.CRD
ReleaseVersion string
}
3 changes: 3 additions & 0 deletions pkg/generate/ack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func Controller(
templateBasePaths []string,
// serviceAccountName is the name of the ServiceAccount used in the Helm chart
serviceAccountName string,
releaseVersion string,
) (*templateset.TemplateSet, error) {
crds, err := m.GetCRDs()
if err != nil {
Expand All @@ -234,6 +235,7 @@ func Controller(
metaVars,
m.SDKAPI,
r,
releaseVersion,
}
code, err := ResourceHookCode(templateBasePaths, r, hookID, crdVars, controllerFuncMap)
if err != nil {
Expand Down Expand Up @@ -274,6 +276,7 @@ func Controller(
metaVars,
m.SDKAPI,
crd,
releaseVersion,
}
if err = ts.Add(outPath, tplPath, crdVars); err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion scripts/build-controller.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ if [[ ! -d $SERVICE_CONTROLLER_SOURCE_PATH ]]; then
exit 1
fi

pushd "$SERVICE_CONTROLLER_SOURCE_PATH" 1>/dev/null
RELEASE_VERSION=${RELEASE_VERSION:-$(git describe --tags --abbrev=0 2>/dev/null || echo $NON_RELEASE_VERSION)}
popd 1>/dev/null

BOILERPLATE_TXT_PATH="$ROOT_DIR/templates/boilerplate.txt"
DEFAULT_TEMPLATE_DIRS="$ROOT_DIR/templates"
# If the service controller source repository has a templates/ directory, add
Expand Down Expand Up @@ -165,7 +169,7 @@ if [ -z "$ACK_DOCUMENTATION_CONFIG_PATH" ]; then
fi
fi

ag_args=("$SERVICE" -o "$SERVICE_CONTROLLER_SOURCE_PATH" --template-dirs "$TEMPLATE_DIRS")
ag_args=("$SERVICE" "$RELEASE_VERSION" -o "$SERVICE_CONTROLLER_SOURCE_PATH" --template-dirs "$TEMPLATE_DIRS")
if [ -n "$ACK_GENERATE_CACHE_DIR" ]; then
ag_args=("${ag_args[@]}" --cache-dir "$ACK_GENERATE_CACHE_DIR")
fi
Expand Down
1 change: 1 addition & 0 deletions templates/apis/crd.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type {{ .CRD.Kind }}Status struct {
// {{ .CRD.Kind }} is the Schema for the {{ .CRD.Plural }} API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:metadata:annotations="controller-version={{ .ReleaseVersion }}"
Copy link
Member

Choose a reason for hiding this comment

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

nice, TIL

{{- range $column := .CRD.AdditionalPrinterColumns }}
// +kubebuilder:printcolumn:name="{{$column.Name}}",type={{$column.Type}},priority={{$column.Priority}},JSONPath=`{{$column.JSONPath}}`
{{- end }}
Expand Down