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

fix: add unique hash to cloned source to avoid conflict #1136

Merged
merged 2 commits into from
Nov 21, 2023

Conversation

yitsushi
Copy link
Collaborator

@yitsushi yitsushi commented Nov 17, 2023

Issue

When more than one Terraform resource points to the same git repository, when we clone the Source object, it does not create a new cloned Source object as the PullRequest ID is the same, the source name is the same, and we use only these two values to generate the name of the cloned resource.

Solution

Add a short and deterministic hash to the name of the cloned Source object.

Another thoughts

It's not necessary to get a deterministic unique string, it could be random as we save the result as SourceRef. I decided to use a short (first 10 bytes of a sha256 hash), because if in the future we have to regenerate the values somehow, then we don't have to care about migration, extra compatibility checks. I know it's not likely we need this, but generating a random number

  • doesn't yield shorter code.
  • still doesn't guarantee it will not generate a conflicting name, and knowing computers, I think it's even more likely (still unlikely) to generate the same random value if the two generation happens (nearly) at the same time.

Additional changes

  • Removed the hardcoded index-magic from poll_test. For some reasons it was not always in order. I don't know what changed tho.

Notes

Compatibility:
I was thinking about what happens with old resources, but after I spent some time on it, my conclusion is "they are not affected". We don't calculate names when we look up source objects for terraform objects. Their name is already saved as SourceRef. We generate this game only on newly created resources, therefore it does not break anything already in place.

Fixes #923

References:

Issue
-----

When more than one Terraform resource points to the same git repository,
when we clone the Source object, it does not create a new cloned Source
object as the PullRequest ID is the same, the source name is the same,
and we use only these two values to generate the name of the cloned
resource.

Solution
--------
Add a short and deterministic hash to the name of the cloned Source
object.

Another thoughts
----------------
It's not necessary to get a deterministic unique string, it could be
random as we save the result as SourceRef. I decided to use a short
(first 10 bytes of a sha256 hash), because if in the future we have to
regenerate the values somehow, then we don't have to care about
migration, extra compatibility checks. I know it's not likely we need
this, but generating a random number
- doesn't yield shorter code.
- still doesn't guarantee it will not generate a conflicting name, and
  knowing computers, I think it's even more likely (still unlikely) to
  generate the same random value if the two generation happens (nearly)
  at the same time.

Additional changes
------------------
- Removed the hardcoded index-magic from poll_test. For some reasons it was not
  always in order. I don't know what changed tho.

Notes
----
Compatibility:
I was thinking about what happens with old resources, but after I spent
some time on it, my conclusion is "they are not affected". We don't
calculate names when we look up source objects for terraform objects.
Their name is already saved as SourceRef. We generate this game only on
newly created resources, therefore it does not break anything already in
place.

Fixes #923

References:
- #923

Signed-off-by: Balazs Nadasdi <[email protected]>
@yitsushi yitsushi changed the title fix: add unique hash to cloned source to avoid conflict. fix: add unique hash to cloned source to avoid conflict Nov 17, 2023
@yiannistri yiannistri self-requested a review November 20, 2023 15:26
@yitsushi yitsushi requested a review from chanwit November 21, 2023 07:56
@yiannistri
Copy link
Contributor

yiannistri commented Nov 21, 2023

Currently testing this.

I have 1 source being referenced by 2 TF resources. Making a change to the TF code in a PR, results in 2 planned TF resources (one for each TF resource) and 2 new sources:

 k get terraforms.infra.contrib.fluxcd.io -A
NAMESPACE     NAME        READY     STATUS                                                         AGE
flux-system   dev         True      No drift: main@sha1:72f7cea60436fbeee0ba00b6f5e3dbd0428de8d7   9m56s
flux-system   dev-pr-9    Unknown   Plan generated: This object is in the plan only mode.          9m37s
flux-system   prod        True      No drift: main@sha1:72f7cea60436fbeee0ba00b6f5e3dbd0428de8d7   9m56s
flux-system   prod-pr-9   Unknown   Plan generated: This object is in the plan only mode.          9m35s
k get gitrepositories.source.toolkit.fluxcd.io -A
NAMESPACE     NAME                                    URL                                             AGE     READY   STATUS
flux-system   flux-system                             ssh://[email protected]/yiannistri/tf-controller   16m     True    stored artifact for revision 'main@sha1:72f7cea60436fbeee0ba00b6f5e3dbd0428de8d7'
flux-system   flux-system-466cb10d5c434f437c21-pr-9   ssh://[email protected]/yiannistri/tf-controller   7m37s   True    stored artifact for revision 'yiannistri-patch-1@sha1:f5799fa57bcd01d7a61ca8d182c6e90c44db35f1'
flux-system   flux-system-b1545adde51aec2de02b-pr-9   ssh://[email protected]/yiannistri/tf-controller   7m39s   True    stored artifact for revision 'yiannistri-patch-1@sha1:f5799fa57bcd01d7a61ca8d182c6e90c44db35f1'

for idx, item := range tfList.Items[1:] {
expectToEqual(t, g, item.Name, fmt.Sprintf("%s-pr-%d", original.Name, idx+1))
expectToEqual(t, g, item.Spec.SourceRef.Name, fmt.Sprintf("%s-source-pr-%d", original.Name, idx+1))
for _, item := range tfList.Items[1:] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this start from 0 or rather iterate through the whole slice? Since we skip the item with the same name below.

@yitsushi yitsushi merged commit 63b8453 into main Nov 21, 2023
15 checks passed
@bigkevmcd bigkevmcd deleted the 923-bp-add-unique-id-to-source branch December 12, 2023 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The branch-planner does not clean up after running
2 participants