Skip to content

Create custom list and map fields #222

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

Merged
merged 4 commits into from
Oct 21, 2021

Conversation

RedbackThomson
Copy link
Contributor

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

Description of changes:
Supports creating custom list and map fields where the member shapes of those fields already exist in the SDK.
Rather than supplying a from in the field config, you can now supply a custom_field property which requires
either list_of or map_of with the name of an API shape as the value.

An example of generating a list (for an S3 Bucket):

resources:
  Bucket:
    fields:
      AnalyticsConfigurations:
        custom_field:
          list_of: AnalyticsConfiguration

Produces the following:

AnalyticsConfigurations []*AnalyticsConfiguration `json:"analyticsConfigurations,omitempty"`

An example of generating a map (for an S3 Bucket):

resources:
  Bucket:
    fields:
      AnalyticsConfigurations:
        custom_field:
          map_of: AnalyticsConfiguration

Produces the following:

AnalyticsConfigurations map[string]*AnalyticsConfiguration `json:"analyticsConfigurations,omitempty"` 

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 Oct 14, 2021

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@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 Oct 14, 2021
@ack-bot ack-bot requested review from jaypipes and vijtrip2 October 14, 2021 01:39
@RedbackThomson
Copy link
Contributor Author

Waiting for rebase on top of #221

Copy link
Contributor

@brycahta brycahta left a comment

Choose a reason for hiding this comment

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

The refactor from model to operations makes things a lot clearer 👍

@@ -22,6 +22,7 @@ import (
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
"github.com/aws-controllers-k8s/code-generator/pkg/generate/templateset"
ackmodel "github.com/aws-controllers-k8s/code-generator/pkg/model"
ackoperations "github.com/aws-controllers-k8s/code-generator/pkg/operations"
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use this import alias everywhere? Such as in check.go below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The import aliasing in ACK seems to be a bit random. Some places use ackmodel others import model without an alias. I have just stuck to whatever the file has as convention. I don't think ackmodel is necessary, because we are clearly in the ack project, so model should be sufficient.

typeRenames: nil,
}

h.InjectCustomShapes(sdkapi)
Copy link
Contributor

Choose a reason for hiding this comment

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

From the current implementation of Helper, I don't think it makes sense for one of its methods to be able to change/edit sdkapi.

Is there any reason this couldn't be a setter? ex: func (a *SDKAPI) SetCustomShapes(cfg ackgenconfig.Config)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My aim for some of the refactoring work was that SDKAPI is an artifact produced by the sdk.Helper struct. "Injecting" it here, is just one stage of creating that artifact - before it is eventually passed to the model struct initializer.

Because of tight code coupling, SDKAPI has to stay in the model package, but ideally it would be owned in the sdk package.

h.apiVersion = apiVersion
}

// API returns the aws-sdk-go API model for a supplied service model name.
func (h *SDKHelper) API(serviceModelName string) (*SDKAPI, error) {
func (h *Helper) API(serviceModelName string) (*SDKAPI, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I know you didn't change any logic here, but why have the helper do this instead of a constructor for SDKAPI requiring serviceModelName?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have any history for this method - they predate my introduction to the project. My best guess is that we don't care about the service name anywhere else in the Helper, but I think over time we have expanded it and now we have an awkward waterfall of passing it between methods.

@RedbackThomson RedbackThomson marked this pull request as ready for review October 21, 2021 19:44
@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 Oct 21, 2021
@RedbackThomson
Copy link
Contributor Author

/test s3-controller-test

Copy link
Contributor

@brycahta brycahta left a comment

Choose a reason for hiding this comment

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

Is the only way to use this custom field via Hooks in service-controller code?

"github.com/aws-controllers-k8s/code-generator/pkg/model"
"github.com/stretchr/testify/assert"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: 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.

Ugh damn linter, again.

)
panic(msg)
}
} else if fieldConfig.CustomField != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like the custom field is being added to both Spec and Status-- is that intended? Am I misreading?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The field can be added to either the Spec or the Status, depending on the value of is_read_only. Otherwise the implementations don't discriminate.

@RedbackThomson
Copy link
Contributor Author

Is the only way to use this custom field via Hooks in service-controller code?

At the moment, yes. Considering the type doesn't exist in the SDK, the generator won't really know how to use it - it's not linked to any of the operations and therefore not linked to any SDK methods. This could be smarter in the future, but not in this revision.

Copy link
Contributor

@brycahta brycahta left a comment

Choose a reason for hiding this comment

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

/lgtm

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

ack-bot commented Oct 21, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brycahta, RedbackThomson

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 af5f01e into aws-controllers-k8s:main Oct 21, 2021
@RedbackThomson RedbackThomson deleted the custom-fields branch October 21, 2021 20:30
ack-bot pushed a commit to aws-controllers-k8s/s3-controller that referenced this pull request Oct 27, 2021
Implements aws-controllers-k8s/community#914

Description of changes:
Uses the new custom fields code-generator PR (aws-controllers-k8s/code-generator#222) to create lists of configuration types. At runtime each of these configurations is put individually (mapped using their ID field) onto the bucket. For each reconciliation, we describe the configurations using a `List*` operation and determine the delta for each configuration using a generated `compare*` method in the `sdk.go`. The configuration elements are individually created, deleted, updated or no-op'd depending on the diff. 

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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants