Skip to content

Conversation

a-hilaly
Copy link
Member

@a-hilaly a-hilaly commented Jan 4, 2023

Issue: aws-controllers-k8s/community#1609

Previously, the code-generator was unable to let us use GoCodeSetResourceForStruct for fields that are not part of a CRD top level fields. This caused issues when trying to generate custom code that transforms an eventbridge v1alpha1.Target to an svcsdk.Target.

This patch fixes the issue by allowing users to call GoCodeSetResourceForStruct for any field, not just CRD top level fields. In addition, unit tests have been added to ensure the correct behavior.

This patch is needed to allow us to properly execute the template written here https://github.com/embano1/eventbridge-controller/blob/aminemicha/templates/hooks/rule/sdk_file_end.go.tpl

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

@ack-bot ack-bot requested review from jljaco and vijtrip2 January 4, 2023 20:15
@a-hilaly a-hilaly changed the title Fix GoCodeSetResourceForStruct for non-CRD fields Allow calling GoCodeSetResourceForStruct for non-CRD fields Jan 4, 2023
@a-hilaly a-hilaly changed the title Allow calling GoCodeSetResourceForStruct for non-CRD fields Allow calling GoCodeSetResourceForStruct for non-CRD shapes Jan 4, 2023
@a-hilaly a-hilaly changed the title Allow calling GoCodeSetResourceForStruct for non-CRD shapes Allow calling GoCodeSetResourceForStruct for non-CRD SDK shapes Jan 4, 2023
@a-hilaly a-hilaly added the kind/enhancement Categorizes issue or PR as related to existing feature enhancements. label Jan 4, 2023
@RedbackThomson
Copy link
Contributor

I don't think I understand how this solves the problem? This seems to be throwing away error handling, but I don't see how it's able to access fields that aren't in the spec or status? It still uses f.Fields?

@a-hilaly
Copy link
Member Author

a-hilaly commented Jan 4, 2023

I don't think I understand how this solves the problem? This seems to be throwing away error handling, but I don't see how it's able to access fields that aren't in the spec or status? It still uses f.Fields?

Because the statement bellow will not return a positive ok unless if one targetFieldName is one of the spec/status fields.

        f, ok := r.Fields[targetFieldName]
	if !ok {
		return ""
	}

Reading the code again i just realized that i need to add the setCfg.ignore part.. adding that in a different commit

assert.Equal(
expected,
code.SetResourceForStruct(
crd.Config(), crd, "", "krTarget", krTargetShape, nil, "sdkTarget", sdkTargetShape, "", model.OpTypeList, 1,
Copy link
Collaborator

Choose a reason for hiding this comment

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

All of the other calls in this test file use "resp" and "ko" for the SDK and Kubernetes object variable names, respectively. Can we match that?

Copy link
Member Author

Choose a reason for hiding this comment

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

The thing is that Target is not a CRD nor a CRD Field - There is no need to use resp and ko as they really represent what the function is used for

krTarget.ARN = sdkTarget.Arn
}
if sdkTarget.BatchParameters != nil {
krTargetf1 := &svcapitypes.BatchParameters{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Normally, this would just be:

f1 := &svcapitypes.BatchParameters{}

not

krTargetf1 := &svcapitypes.BatchParameters{}

I think you are incorrectly specifying the target variable name...

Copy link
Member Author

Choose a reason for hiding this comment

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

The thing is if i change krTarget to an empty string, L3298 will become .ARN = resp.Arn which is not correct

@a-hilaly a-hilaly force-pushed the gocode-setresource branch from 63caf60 to f09171b Compare January 5, 2023 20:57
@a-hilaly
Copy link
Member Author

/retest

@embano1
Copy link
Member

embano1 commented Jan 17, 2023

Pinging @a-hilaly for who can review and LGTM since this is blocking the ACK EventBridge controller 🙏🏻

@jljaco jljaco requested review from RedbackThomson and jaypipes and removed request for jaypipes January 18, 2023 19:24
@RedbackThomson
Copy link
Contributor

Setting the test naming conventions aside, I am happy with this as a simple feature/fix.
/approve

@jljaco
Copy link
Contributor

jljaco commented Jan 20, 2023

/approve

ko.DeadLetterConfig = kof2
}
if resp.EcsParameters != nil {
kof3 := &svcapitypes.EcsParameters{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, this should be ECSParameters... we need to add ECS to the initialisms.

kof3.CapacityProviderStrategy = kof3f0
}
if resp.EcsParameters.EnableECSManagedTags != nil {
kof3.EnableECSManagedTags = resp.EcsParameters.EnableECSManagedTags
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting that they used "ECS" instead of "Ecs" in this field name...

assert.Equal(
expected,
code.SetResourceForStruct(
crd.Config(), crd, "", "ko", koShape, nil, "resp", targetShape, "", model.OpTypeList, 1,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Turns out that the targetField parameter isn't ever used by the SetResourceForStruct function :( Only the targetFieldPath parameter is ever used.

My request is that you:

  1. remove the unused targetField parameter (since it is just confusing) from the SetResourceForStruct function
  2. Pass fX for the targetVarName parameter (normally this would be something like "f12" if SetResourceForStruct is being called recursively from other functions in pkg/generate/code/set_resource.go or it would be the name of the field if SetResourceForStruct was called from a top-level template in sdk_file_end hooks...)

Previously, the code-generator was unable to use `GoCodeSetResourceForStruct` for fields that are not part of a CRD spec or status. This caused issues when trying to generate custom code that transforms an eventbridge `v1alpha1.Target` to an `svcsdk.Target`.

This patch fixes the issue by allowing users to call `GoCodeSetResourceForStruct` for any field, not just CRD top level fields. In addition, unit tests have been added to ensure the correct behavior.

Signed-off-by: Amine Hilaly <[email protected]>
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.

Much better. Thank you sir! :)

@a-hilaly
Copy link
Member Author

/retest

2 similar comments
@a-hilaly
Copy link
Member Author

/retest

@a-hilaly
Copy link
Member Author

/retest

@a-hilaly
Copy link
Member Author

/test all

@a-hilaly
Copy link
Member Author

/retest

@a-hilaly
Copy link
Member Author

/test all

1 similar comment
@a-hilaly
Copy link
Member Author

/test all

@jljaco
Copy link
Contributor

jljaco commented Jan 25, 2023

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 25, 2023
@ack-bot
Copy link
Collaborator

ack-bot commented Jan 25, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: A-Hilaly, jaypipes, jljaco, 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:
  • OWNERS [A-Hilaly,RedbackThomson,jljaco]

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 5204077 into aws-controllers-k8s:main Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved kind/enhancement Categorizes issue or PR as related to existing feature enhancements. lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants