Skip to content

Conversation

mbaijal
Copy link
Contributor

@mbaijal mbaijal commented Aug 24, 2021

Allow OperatorType to be a list such that one API can map to multiple operations.
This is required in certain cases where the same API call maps to both Create and Update operations such as RegisterScalableTarget and PutScalingPolicy

Link to ApplicationAutoscaling PR with the related change
Note: This change will require all other services to update their generator configs as well.

Issue #, if available:
aws-controllers-k8s/community#867

Description of changes:

The getOpTypeAndResourceName method handles each of the following cases same as before in addition to the new case 5-

  1. the cfg itself is nil
  2. cfg is not nil but opId is not in the cfg
  3. cfg is not nil, the opID is in the cfg but the opType is not specified
operations:
  DescribeScalableTargets:
  primary_identifier_field_name: ResourceID
  1. One API -> one operations
operations:
  PutScalingPolicy:
    operation_type: 
    - Create
  1. One API -> multiple operations
operations:
  PutScalingPolicy:
    operation_type: 
    - Create
    - Update

Testing

  • I tested by generating the controller code for both applicationAutoscaling and the SageMaker repos.
  • The unit tests pass as is.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ack-bot
Copy link
Collaborator

ack-bot commented Aug 24, 2021

Hi @mbaijal. Thanks for your PR.

I'm waiting for a aws-controllers-k8s member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ack-bot ack-bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 24, 2021
@mbaijal mbaijal changed the title [Review] Allow OperatorType to be a list such that one API can map to multiple operations Allow OperatorType to be a list such that one API can map to multiple operations Aug 24, 2021
}

return opType, resName
if len(operationConfig.OperationType) > 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clean up

@mbaijal mbaijal requested a review from a-hilaly August 26, 2021 06:05
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

@mbaijal see inline for a helpful hint at solving the "need to update all the existing generator.yaml files" problem.

// Override for operation type in case of heuristic failure
// An example of this is `Put...` or `Register...` API operations not being correctly classified as `Create` op type
OperationType string `json:"operation_type"`
OperationType []string `json:"operation_type"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll give you a little Go code I wrote for a different project that will allow you to change the type of this field to an array of strings without requiring all the existing generator.yaml files to be updated.

// NOTE(jaypipes): A type that can be represented in JSON as *either* a string
// *or* an array of strings, which is what JSONSchema's type field needs.
// see: https://github.com/go-yaml/yaml/issues/100
type StringArray []string

func (a *StringArray) UnmarshalJSON(b []byte) error {
	var multi []string
	err := json.Unmarshal(b, &multi)
	if err != nil {
		var single string
		err := json.Unmarshal(b, &single)
		if err != nil {
			return err
		}
		*a = []string{single}
	} else {
		*a = multi
	}
	return nil
}

You'd then change the above field definition to this instead:

	OperationType StringArray `json:"operation_type"`

The existing generator.yaml files that specify operation_type as a string will work as-is without modification and the generator.yaml files that need to specify multiple operation types can specify a list of strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks good, I'll update the PR later today, thanks !

@mbaijal mbaijal changed the title Allow OperatorType to be a list such that one API can map to multiple operations [wip] Allow OperatorType to be a list such that one API can map to multiple operations Aug 27, 2021
@ack-bot ack-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 27, 2021
Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

Couple tiny nits inline, @mbaijal, otherwise this looks great.

import (
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"

"encoding/json"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a standard library import, so please place above the awssdkmodel import line and separate with an empty newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

"path/filepath"
"sort"
"strings"
"encoding/json"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please run go fmt on your code or add an auto-formatter to your IDE. It will automatically correct the above into an alphabetical import order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add an auto-formatter to your IDE

I should really, always forget to run the formatter. Ran it manually on the two files for now.

@mbaijal mbaijal changed the title [wip] Allow OperatorType to be a list such that one API can map to multiple operations Allow OperatorType to be a list such that one API can map to multiple operations Aug 29, 2021
@ack-bot ack-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 29, 2021

"encoding/json"
"github.com/aws-controllers-k8s/code-generator/pkg/util"
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
Copy link
Member

Choose a reason for hiding this comment

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

please change the import section to:

import (
	"encoding/json"

        awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"

	"github.com/aws-controllers-k8s/code-generator/pkg/util"
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. gofmt does not fix this. Would you recommend using goimports or such to avoid such issues in the future ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes exactly :). Sometimes goimports doesn't work as expected so you might need to fix the imports order manually. Here are the guidelines for imports https://github.com/golang/go/wiki/CodeReviewComments#imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!

if operationConfig.ResourceName != "" {
resName = operationConfig.ResourceName
if b, err := json.Marshal(operationConfig.OperationType); err == nil {
unmarshaledOpTypes.UnmarshalJSON(b)
Copy link
Member

Choose a reason for hiding this comment

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

You don't have to call the method UnmarshalJSON here, bur rather call json.Unmarshal(b, &unmarshaledOpTypes)

@ack-bot ack-bot removed the approved label Aug 30, 2021
if operationConfig.ResourceName != "" {
resName = operationConfig.ResourceName
if b, err := json.Marshal(operationConfig.OperationType); err == nil {
json.Unmarshal(b, &unmarshaledOpTypes)
Copy link
Member

Choose a reason for hiding this comment

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

the Unmarshal error should be handled here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not applicable now.


if operationConfig.ResourceName != "" {
resName = operationConfig.ResourceName
if b, err := json.Marshal(operationConfig.OperationType); err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Thinking a second time about this, i think that you don't have to call json.Marshal/json.Unmarshall here - because StringArray.UnmarshalJSON will be called when model.Model is getting initialized https://github.com/aws-controllers-k8s/code-generator/blob/main/pkg/model/model.go#L700

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep you are right :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, is operation.go the right place for the UnmarshalJSON method ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes it makes sense to have the type and it methods on the same file so it's good!

@mbaijal
Copy link
Contributor Author

mbaijal commented Aug 31, 2021

/test unit-tests

@ack-bot
Copy link
Collaborator

ack-bot commented Aug 31, 2021

@mbaijal: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/test unit-tests

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@a-hilaly
Copy link
Member

/ok-to-test

@ack-bot ack-bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 31, 2021
"github.com/aws-controllers-k8s/code-generator/pkg/names"
"github.com/aws-controllers-k8s/code-generator/pkg/util"

awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
Copy link
Member

Choose a reason for hiding this comment

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

Tiny nit: this import should be in the second block of imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let me set up the goimports and create a tiny PR for this one.

Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

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

Looks good! thanks @mbaijal!

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Aug 31, 2021
@ack-bot
Copy link
Collaborator

ack-bot commented Aug 31, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: A-Hilaly, mbaijal

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-bot ack-bot merged commit 9f6ca92 into aws-controllers-k8s:main Aug 31, 2021
ack-bot pushed a commit to aws-controllers-k8s/applicationautoscaling-controller that referenced this pull request Sep 1, 2021
)

Issue #, if available:
aws-controllers-k8s/community#867

Description of changes:
1. Enable updates based on changes to code generator made in this PR - aws-controllers-k8s/code-generator#166
2. Add an integration test to check for updates

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants