-
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
Load service model name from generator #216
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,18 @@ import ( | |
"os" | ||
"os/signal" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"golang.org/x/mod/modfile" | ||
|
||
ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack" | ||
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config" | ||
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model" | ||
"github.com/aws-controllers-k8s/code-generator/pkg/util" | ||
k8sversion "k8s.io/apimachinery/pkg/version" | ||
) | ||
|
||
const ( | ||
|
@@ -207,3 +212,67 @@ func getSDKVersionFromGoMod(goModPath string) (string, error) { | |
} | ||
return "", fmt.Errorf("couldn't find %s in the go.mod require block", sdkModule) | ||
} | ||
|
||
// loadModelWithLatestAPIVersion finds the AWS SDK for a given service alias and | ||
// creates a new model with the latest API version. | ||
func loadModelWithLatestAPIVersion(svcAlias string) (*ackmodel.Model, error) { | ||
latestAPIVersion, err := getLatestAPIVersion() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return loadModel(svcAlias, latestAPIVersion) | ||
} | ||
|
||
// loadModel finds the AWS SDK for a given service alias and creates a new model | ||
// with the given API version. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if |
||
func loadModel(svcAlias string, apiVersion string) (*ackmodel.Model, error) { | ||
cfg, err := ackgenconfig.New(optGeneratorConfigPath, ackgenerate.DefaultConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
modelName := strings.ToLower(cfg.ModelName) | ||
if modelName == "" { | ||
modelName = svcAlias | ||
} | ||
|
||
sdkHelper := ackmodel.NewSDKHelper(sdkDir) | ||
sdkAPI, err := sdkHelper.API(modelName) | ||
if err != nil { | ||
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias) | ||
RedbackThomson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
sdkAPI, err = sdkHelper.API(newSvcAlias) // retry with serviceID | ||
if err != nil { | ||
return nil, fmt.Errorf("service %s not found", svcAlias) | ||
} | ||
} | ||
m, err := ackmodel.New( | ||
sdkAPI, svcAlias, apiVersion, cfg, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
|
||
// getLatestAPIVersion looks in a target output directory to determine what the | ||
// latest Kubernetes API version for CRDs exposed by the generated service | ||
// controller. | ||
func getLatestAPIVersion() (string, error) { | ||
apisPath := filepath.Join(optOutputPath, "apis") | ||
versions := []string{} | ||
subdirs, err := ioutil.ReadDir(apisPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, subdir := range subdirs { | ||
versions = append(versions, subdir.Name()) | ||
} | ||
sort.Slice(versions, func(i, j int) bool { | ||
return k8sversion.CompareKubeAwareVersionStrings(versions[i], versions[j]) < 0 | ||
}) | ||
return versions[len(versions)-1], nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,6 @@ import ( | |
|
||
ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack" | ||
ackmetadata "github.com/aws-controllers-k8s/code-generator/pkg/metadata" | ||
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model" | ||
) | ||
|
||
var ( | ||
|
@@ -74,21 +73,7 @@ func generateRelease(cmd *cobra.Command, args []string) error { | |
if err := ensureSDKRepo(ctx, optCacheDir, optRefreshCache); err != nil { | ||
return err | ||
} | ||
sdkHelper := ackmodel.NewSDKHelper(sdkDir) | ||
sdkAPI, err := sdkHelper.API(svcAlias) | ||
if err != nil { | ||
newSvcAlias, err := FallBackFindServiceID(sdkDir, svcAlias) | ||
if err != nil { | ||
return err | ||
} | ||
sdkAPI, err = sdkHelper.API(newSvcAlias) // retry with serviceID | ||
if err != nil { | ||
return fmt.Errorf("service %s not found", svcAlias) | ||
} | ||
} | ||
m, err := ackmodel.New( | ||
sdkAPI, "", optGeneratorConfigPath, ackgenerate.DefaultConfig, | ||
) | ||
m, err := loadModel(svcAlias, "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could you add a comment as to why the apiVersion is empty here? If it doesn't matter, then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually couldn't tell you why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @a-hilaly I would love your input on this. |
||
if err != nil { | ||
return err | ||
} | ||
|
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.
this and the code below, considering (in a future PR if you agree with me) perhaps putting this into an importable package under
pkg/