Skip to content

Commit 208b1e1

Browse files
Use metadata as source of truth for latest version (#348)
Fixes aws-controllers-k8s/community#1361 Description of changes: Code-generator currently compares the subdirectories in the API output directory (`apis/`) to determine the latest K8s version of the CRDs. The information about which API versions should be generated, and their status (`available`, `deprecated`, etc.), already exist in the `metadata.yaml` file. This pull request updates the source of truth for the code-generator to be the `metadata.yaml` file. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 4b54669 commit 208b1e1

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

cmd/ack-generate/command/apis.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ func generateAPIs(cmd *cobra.Command, args []string) error {
9696
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
9797
return err
9898
}
99-
m, err := loadModelWithLatestAPIVersion(svcAlias)
99+
metadata, err := ackmetadata.NewServiceMetadata(optMetadataConfigPath)
100+
if err != nil {
101+
return err
102+
}
103+
m, err := loadModelWithLatestAPIVersion(svcAlias, metadata)
100104
if err != nil {
101105
return err
102106
}

cmd/ack-generate/command/common.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ import (
2626
"time"
2727

2828
"golang.org/x/mod/modfile"
29+
k8sversion "k8s.io/apimachinery/pkg/version"
2930

3031
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/config"
3132
ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack"
33+
ackmetadata "github.com/aws-controllers-k8s/code-generator/pkg/metadata"
3234
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
3335
acksdk "github.com/aws-controllers-k8s/code-generator/pkg/sdk"
3436
"github.com/aws-controllers-k8s/code-generator/pkg/util"
35-
k8sversion "k8s.io/apimachinery/pkg/version"
3637
)
3738

3839
const (
@@ -216,8 +217,8 @@ func getSDKVersionFromGoMod(goModPath string) (string, error) {
216217

217218
// loadModelWithLatestAPIVersion finds the AWS SDK for a given service alias and
218219
// creates a new model with the latest API version.
219-
func loadModelWithLatestAPIVersion(svcAlias string) (*ackmodel.Model, error) {
220-
latestAPIVersion, err := getLatestAPIVersion()
220+
func loadModelWithLatestAPIVersion(svcAlias string, metadata *ackmetadata.ServiceMetadata) (*ackmodel.Model, error) {
221+
latestAPIVersion, err := getLatestAPIVersion(metadata.APIVersions)
221222
if err != nil {
222223
return nil, err
223224
}
@@ -227,7 +228,6 @@ func loadModelWithLatestAPIVersion(svcAlias string) (*ackmodel.Model, error) {
227228
// loadModel finds the AWS SDK for a given service alias and creates a new model
228229
// with the given API version.
229230
func loadModel(svcAlias string, apiVersion string, apiGroup string, defaultCfg ackgenconfig.Config) (*ackmodel.Model, error) {
230-
231231
cfg, err := ackgenconfig.New(optGeneratorConfigPath, defaultCfg)
232232
if err != nil {
233233
return nil, err
@@ -265,19 +265,14 @@ func loadModel(svcAlias string, apiVersion string, apiGroup string, defaultCfg a
265265
return m, nil
266266
}
267267

268-
// getLatestAPIVersion looks in a target output directory to determine what the
269-
// latest Kubernetes API version for CRDs exposed by the generated service
268+
// getLatestAPIVersion looks in the controller metadata file to determine what
269+
// the latest Kubernetes API version for CRDs exposed by the generated service
270270
// controller.
271-
func getLatestAPIVersion() (string, error) {
272-
apisPath := filepath.Join(optOutputPath, "apis")
271+
func getLatestAPIVersion(apiVersions []ackmetadata.ServiceVersion) (string, error) {
273272
versions := []string{}
274-
subdirs, err := ioutil.ReadDir(apisPath)
275-
if err != nil {
276-
return "", err
277-
}
278273

279-
for _, subdir := range subdirs {
280-
versions = append(versions, subdir.Name())
274+
for _, version := range apiVersions {
275+
versions = append(versions, version.APIVersion)
281276
}
282277
sort.Slice(versions, func(i, j int) bool {
283278
return k8sversion.CompareKubeAwareVersionStrings(versions[i], versions[j]) < 0

cmd/ack-generate/command/controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/spf13/cobra"
2727

2828
ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack"
29+
ackmetadata "github.com/aws-controllers-k8s/code-generator/pkg/metadata"
2930
)
3031

3132
var (
@@ -59,7 +60,11 @@ func generateController(cmd *cobra.Command, args []string) error {
5960
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
6061
return err
6162
}
62-
m, err := loadModelWithLatestAPIVersion(svcAlias)
63+
metadata, err := ackmetadata.NewServiceMetadata(optMetadataConfigPath)
64+
if err != nil {
65+
return err
66+
}
67+
m, err := loadModelWithLatestAPIVersion(svcAlias, metadata)
6368
if err != nil {
6469
return err
6570
}

cmd/ack-generate/command/olm.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/spf13/cobra"
2525

2626
olmgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/olm"
27+
ackmetadata "github.com/aws-controllers-k8s/code-generator/pkg/metadata"
2728
)
2829

2930
const (
@@ -87,7 +88,11 @@ func generateOLMAssets(cmd *cobra.Command, args []string) error {
8788
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil {
8889
return err
8990
}
90-
m, err := loadModelWithLatestAPIVersion(svcAlias)
91+
metadata, err := ackmetadata.NewServiceMetadata(optMetadataConfigPath)
92+
if err != nil {
93+
return err
94+
}
95+
m, err := loadModelWithLatestAPIVersion(svcAlias, metadata)
9196
if err != nil {
9297
return err
9398
}

scripts/build-controller-release.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ if [[ $ACK_GENERATE_OLM == "true" ]]; then
252252
if [ -n "$ACK_GENERATE_IMAGE_REPOSITORY" ]; then
253253
ag_olm_args="$ag_olm_args --image-repository $ACK_GENERATE_IMAGE_REPOSITORY"
254254
fi
255+
if [ -n "$ACK_METADATA_CONFIG_PATH" ]; then
256+
ag_olm_args="$ag_olm_args --metadata-config-path $ACK_METADATA_CONFIG_PATH"
257+
fi
255258

256259
$ACK_GENERATE_BIN_PATH olm $ag_olm_args
257260
$SCRIPTS_DIR/olm-create-bundle.sh "$SERVICE" "$RELEASE_VERSION"

0 commit comments

Comments
 (0)