Skip to content
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

Errorneous generated code for ResolveReferences for nested non array types #1514

Closed
Vandita2020 opened this issue Oct 18, 2022 · 3 comments · Fixed by aws-controllers-k8s/code-generator#369
Assignees
Labels
area/code-generation Issues or PRs as related to controllers or docs code generation kind/bug Categorizes issue or PR as related to a bug. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release.

Comments

@Vandita2020
Copy link
Member

Vandita2020 commented Oct 18, 2022

Describe the bug
While generating the code for references for the Lambda function, specifically more for the S3 bucket field, we observe that there is like use of array keywords with a non-array field which is like len(S3.Bucket) which is incorrect. While doing some debugging we found that there was some missing if-else conditions, a specific case where this should be handled.

Trying to run this code in lambda-controller/generator.yaml

resources:
  Function:
    fields:
      Code.S3Bucket:
        references:
          resource: Bucket
          path: Spec.Name
          service_name: s3

This configuration instruct the code generator generate this line of code:

    // This doesn't work because s3BucketRef is not an array
    len(ko.Spec.Code.S3BucketRef) > 0

This most likely happening because of a missing/non discussed condition {{ else if not $isList -}} in https://github.com/aws-controllers-k8s/code-generator/blob/main/templates/pkg/resource/references.go.tpl#L146

Steps to reproduce
Use the configuration above to generate code for code.s3Bucket references

Expected outcome
Generate code that properly resolves references for nested non array types

Environment

  • Kubernetes version
  • Using EKS (yes/no), if so version?
  • AWS service targeted (S3, RDS, etc.)
@Vandita2020 Vandita2020 added the kind/bug Categorizes issue or PR as related to a bug. label Oct 18, 2022
@Vandita2020
Copy link
Member Author

/assign

@a-hilaly a-hilaly changed the title Missing if-else condition in generator Errorneous generated code for ResolveReferences for nested non array types Oct 18, 2022
@a-hilaly a-hilaly added the area/code-generation Issues or PRs as related to controllers or docs code generation label Oct 18, 2022
@a-hilaly a-hilaly changed the title Errorneous generated code for ResolveReferences for nested non array types Errorneous generated code for ResolveReferences for nested non array types Oct 18, 2022
@a-hilaly a-hilaly added the priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. label Oct 18, 2022
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Oct 21, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Oct 21, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Oct 24, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Oct 24, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Oct 24, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
ack-bot pushed a commit to aws-controllers-k8s/lambda-controller that referenced this issue Oct 25, 2022
Description of changes:

Adding functionality to the lambda controller to be able to reference Code.S3Bucket resource using resource references.

This was generated using the patch aws-controllers-k8s/community#1514 is implemented. 

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Nov 1, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
a-hilaly pushed a commit to aws-controllers-k8s/code-generator that referenced this issue Nov 4, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
Vandita2020 added a commit to Vandita2020/ack-code-generator that referenced this issue Dec 13, 2022
While generating the code for references for the Lambda function, more specifically for the S3 bucket field, we observe that there is use of array keywords with a non-array field e.g len(S3.Bucket) which is incorrect.

After debugging code-generator, we observe that there was an incorrect piece of code (most likely becasue of copy paste) that considered nested parent field as an array.

This patch fixes this issue by adding the right code for handling nested parent fields.

Unfortunately we cannot add unit tests for this change because this part of the code generation resides inside the template and it's not a part of the pkg/generate/code package.

Fixes aws-controllers-k8s/community#1514
@jljaco
Copy link
Contributor

jljaco commented Feb 1, 2023

Re-opening this issue because aws-controllers-k8s/code-generator#369 introduced a regression as detailed in

aws-controllers-k8s/code-generator#398 and
#1653

@jljaco jljaco reopened this Feb 1, 2023
@RedbackThomson
Copy link
Contributor

As part of the refactoring references PR - aws-controllers-k8s/code-generator#417 - this issue seems to have gone away. When regenerating the lambda-controller, I now see the following code which compiles and runs:

// resolveReferenceForCode_S3Bucket reads the resource referenced
// from Code.S3BucketRef field and sets the Code.S3Bucket
// from referenced resource
func resolveReferenceForCode_S3Bucket(
	ctx context.Context,
	apiReader client.Reader,
	namespace string,
	ko *svcapitypes.Function,
) error {
	if ko.Spec.Code == nil {
		return nil
	}
	if ko.Spec.Code != nil {
		if ko.Spec.Code.S3BucketRef != nil && ko.Spec.Code.S3BucketRef.From != nil {
			arr := ko.Spec.Code.S3BucketRef.From
			if arr == nil || arr.Name == nil || *arr.Name == "" {
				return fmt.Errorf("provided resource reference is nil or empty: Code.S3BucketRef")
			}
			obj := &s3apitypes.Bucket{}
			if err := getReferencedResourceState_Bucket(ctx, apiReader, obj, *arr.Name, namespace); err != nil {
				return err
			}
			ko.Spec.Code.S3Bucket = (*string)(obj.Spec.Name)
		}
	}

	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/code-generation Issues or PRs as related to controllers or docs code generation kind/bug Categorizes issue or PR as related to a bug. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants