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

✨ Add test for provider-managed infrastructure, fix writing latest status back into ClusterScope #4637

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

AndiDog
Copy link
Contributor

@AndiDog AndiDog commented Nov 15, 2023

What type of PR is this?

/kind feature

What this PR does / why we need it:

In #4474, @vincepri re-introduced the managed-subnet functionality. This PR adds a test to ensure that we don't lose the functionality again by mistake.

This is a forward-port of the test which I had originally added in our CAPA fork (giantswarm@407bd98). Back then, I found an issue where updated information wasn't written back to the scope because func (s Subnets) Find[...] *SubnetSpec returned a pointer to a copy. I left this change in here – let me know if it should be split out.

Which issue(s) this PR fixes:

Adds a test for #4026

Checklist:

  • squashed commits
  • includes documentation
  • adds unit tests
  • adds or updates e2e tests

Release note:

Add tests for the re-introduced managed subnet functionality in #4474 

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-priority labels Nov 15, 2023
@k8s-ci-robot k8s-ci-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Nov 15, 2023
if x.GetResourceID() == id {
return &x
return &(*s)[i] // pointer to original structure
Copy link
Member

Choose a reason for hiding this comment

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

For readability, can we allocate x after the range above and then use that?

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 – it's now consistent with the other pointer uses of x 👍

Comment on lines +69 to +70
// Sort so that unit tests can expect a stable order
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
Copy link
Member

Choose a reason for hiding this comment

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

Could we make Tags an alias sliced and satisfy the sort interface instead of copying this function over?

Copy link
Member

Choose a reason for hiding this comment

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

type Tags map[string]string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean something like this?

type EC2Tags []*ec2.Tag

func (a EC2Tags) Len() int {
	return len(a)
}

func (a EC2Tags) Less(i, j int) bool {
	if a[i].Key == nil {
		return a[j].Key != nil
	} else if a[j].Key == nil {
		return false
	}
	return *a[i].Key < *a[j].Key
}

func (a EC2Tags) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

// MapToTags converts a infrav1.Tags to a []*ec2.Tag.
func MapToTags(src infrav1.Tags) EC2Tags {
	tags := make(EC2Tags, 0, len(src))

	for k, v := range src {
		tag := &ec2.Tag{
			Key:   aws.String(k),
			Value: aws.String(v),
		}

		tags = append(tags, tag)
	}

	// Sort so that unit tests can expect a stable order
	sort.Sort(tags)

	return tags
}

We'd have to do this for every type i.e. IAM, EC2, etc. That also probably means changing interfaces from []*ec2.Tag to our alias.

Alternative suggestion: avoid repetition with slices.SortFunc(tags, SortByKey) and implement the SortByKey function once.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the SortByKey function idea; it saves on a bigger refactoring.

However, I do think that revisiting these functions and types for the Tags makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

slices.SortFunc won't work because Go generics do not yet support accessing common fields.

Can we leave this for another issue since it blows up the scope of this PR and the problem is also in existing code?

if (spec.GetResourceID() != "" && x.GetResourceID() == spec.GetResourceID()) ||
(spec.CidrBlock == x.CidrBlock) ||
(spec.IPv6CidrBlock != "" && spec.IPv6CidrBlock == x.IPv6CidrBlock) {
return &x
return &(*s)[i] // pointer to original structure
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as @vincepri mentioned above

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

Comment on lines +69 to +70
// Sort so that unit tests can expect a stable order
sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key })
Copy link
Member

Choose a reason for hiding this comment

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

type Tags map[string]string

@AndiDog AndiDog force-pushed the managed-subnet-test branch from b9381c1 to 6b52231 Compare November 29, 2023 13:51
@AndiDog AndiDog changed the title Add test for provider-managed infrastructure, fix writing latest status back into ClusterScope ✨ Add test for provider-managed infrastructure, fix writing latest status back into ClusterScope Nov 29, 2023
@AndiDog AndiDog force-pushed the managed-subnet-test branch from 6b52231 to 987df24 Compare November 30, 2023 08:44
@richardcase
Copy link
Member

/milestone v2.4.0

@k8s-ci-robot k8s-ci-robot added this to the v2.4.0 milestone Jan 23, 2024
@richardcase
Copy link
Member

/test pull-cluster-api-provider-aws-apidiff-main

@AndiDog AndiDog force-pushed the managed-subnet-test branch 2 times, most recently from 93206d3 to e12117f Compare February 6, 2024 04:58
@vincepri
Copy link
Member

vincepri commented Feb 6, 2024 via email

@AndiDog
Copy link
Contributor Author

AndiDog commented Feb 6, 2024

We could make Key a getter function instead?

On Mon, Feb 5, 2024 at 8:58 PM Andreas Sommer @.> wrote: @.* commented on this pull request. ------------------------------ In pkg/cloud/converters/tags.go <#4637 (comment)> : > + // Sort so that unit tests can expect a stable order + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) slices.SortFunc won't work because Go generics do not yet support accessing common fields. Can we leave this for another issue since it blows up the scope of this PR and the problem is also in existing code? — Reply to this email directly, view it on GitHub <#4637 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAXZJ73J3YER4JA55VB7ZFTYSGZ7ZAVCNFSM6AAAAAA7M3UAXKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQNRUGI3TSMZQGU . You are receiving this because you were mentioned.Message ID: </pull/4637/review/1864279305@ github.com>

Yes, I had the same thought but then the types must be changed, I think (as in my sorting interface idea). Still, this increases the scope of this PR which is targeted for this week's release milestone. I'd rather follow up in a separate PR to make that cleaner.

Copy link
Member

@richardcase richardcase left a comment

Choose a reason for hiding this comment

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

This looks good to me. I'll wait for a lgtm.

@AndiDog AndiDog requested a review from nrb February 7, 2024 11:39
@nrb
Copy link
Contributor

nrb commented Feb 7, 2024

/lgtm

I'm ok with deferring the sorting to a different PR.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 7, 2024
Comment on lines 461 to 462
// Deprecated: This function makes a copy, so the returned pointer cannot be used to write back into the original
// slice. Please use FindByIDPtr.
Copy link
Member

Choose a reason for hiding this comment

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

Hm, s Subnets is a slice, which is generally already a pointer internally. Could we modify this function to return the pointer to the struct within the slice?

Copy link
Member

@vincepri vincepri Feb 7, 2024

Choose a reason for hiding this comment

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

func (s Subnets) FindByID(id string) *SubnetSpec {
	for i := 0; i < len(s); i++ {
		x := &(s[i]) // pointer to original structure
		if x.GetResourceID() == id {
			return x
		}
	}
	return nil
}

This should do?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TIL – that works. I pushed this change.

@AndiDog AndiDog force-pushed the managed-subnet-test branch from e12117f to 99fb707 Compare February 8, 2024 11:51
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 8, 2024
@AndiDog AndiDog requested a review from vincepri February 13, 2024 19:41
@nrb
Copy link
Contributor

nrb commented Feb 14, 2024

/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Feb 14, 2024
@richardcase
Copy link
Member

I updated the release note. Thanks @AndiDog for this.

/approve

@richardcase
Copy link
Member

richardcase commented Feb 15, 2024

Until the e2e have finished running

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 15, 2024
@richardcase
Copy link
Member

/test ?

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: richardcase

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 15, 2024
@k8s-ci-robot
Copy link
Contributor

@richardcase: The following commands are available to trigger required jobs:

  • /test pull-cluster-api-provider-aws-build
  • /test pull-cluster-api-provider-aws-test
  • /test pull-cluster-api-provider-aws-verify

The following commands are available to trigger optional jobs:

  • /test pull-cluster-api-provider-aws-apidiff-main
  • /test pull-cluster-api-provider-aws-e2e
  • /test pull-cluster-api-provider-aws-e2e-blocking
  • /test pull-cluster-api-provider-aws-e2e-clusterclass
  • /test pull-cluster-api-provider-aws-e2e-conformance
  • /test pull-cluster-api-provider-aws-e2e-conformance-with-ci-artifacts
  • /test pull-cluster-api-provider-aws-e2e-eks
  • /test pull-cluster-api-provider-aws-e2e-eks-gc
  • /test pull-cluster-api-provider-aws-e2e-eks-testing

Use /test all to run the following jobs that were automatically triggered:

  • pull-cluster-api-provider-aws-apidiff-main
  • pull-cluster-api-provider-aws-build
  • pull-cluster-api-provider-aws-test
  • pull-cluster-api-provider-aws-verify

In response to this:

/test ?

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.

@richardcase
Copy link
Member

/test pull-cluster-api-provider-aws-e2e
/test pull-cluster-api-provider-aws-e2e-eks

@richardcase
Copy link
Member

The e2e passed so:

/unhold

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 15, 2024
@AndiDog
Copy link
Contributor Author

AndiDog commented Feb 15, 2024

Meh, that failure was for an old commit 🤷. Running it again on latest commit.

/test pull-cluster-api-provider-aws-test

@AndiDog
Copy link
Contributor Author

AndiDog commented Feb 15, 2024

Ah no, it's based on a merge and actually failing. I need to fix this and rebase once more. Give me a few minutes.

@AndiDog AndiDog force-pushed the managed-subnet-test branch from 99fb707 to 3e62fbf Compare February 15, 2024 13:31
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 15, 2024
@AndiDog
Copy link
Contributor Author

AndiDog commented Feb 15, 2024

/test pull-cluster-api-provider-aws-test

@AndiDog
Copy link
Contributor Author

AndiDog commented Feb 16, 2024

/test pull-cluster-api-provider-aws-verify

@richardcase
Copy link
Member

Belt and braces again:

/test pull-cluster-api-provider-aws-e2e
/test pull-cluster-api-provider-aws-e2e-eks

@richardcase
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 20, 2024
@k8s-ci-robot k8s-ci-robot merged commit bf0cabd into kubernetes-sigs:main Feb 20, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants