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

Makefile, push: Prevent overwriting existing version tags #445

Conversation

RamLavi
Copy link
Member

@RamLavi RamLavi commented Dec 16, 2024

What this PR does / why we need it:
The IMAGE_GIT_TAG is generated using git describe to create a virtual
tag for the image, and used in order to tag every push to the repository
for later use.
However, when an actual git tag exists (e.g., v0.45.0), git describe
returns that tag. This behavior makes it possible to accidentally
overwrite push an existing version tag in the registry.

Flow Leading to the Issue:

  1. A new kmp release is created, pushing a new tag (e.g., v0.45.0).
  2. A stable branch is created from that commit, pushing a new stable
    branch tag (e.g., release-0.45_latest).
    2.1 . During this push, IMAGE_GIT_TAG resolves to this Git tag (e.g.,
    v0.45.0) due to git describe.
    2.2 Makefile attempts to push the image with this tag (e.g., v0.45.0) to
    the registry, overwriting the original tag sha256 digest.

To address this, introducing a check to ensure such tags are not
overwritten, preserving the integrity of published versions.

Special notes for your reviewer:

Release note:

NONE

Makefile Outdated
Comment on lines 93 to 107
check-tag-exists:
@echo "Checking if tag '${IMAGE_GIT_TAG}' exists in ${REGISTRY}/${IMG}..."
@if skopeo inspect docker://${REGISTRY}/${IMG}:${IMAGE_GIT_TAG} >/dev/null 2>&1; then \
echo "Tag '${IMAGE_GIT_TAG}' already exists. Skipping push."; \
exit 0; \
fi

# Push the docker image
docker-push:
$(OCI_BIN) push ${TLS_SETTING} ${REGISTRY}/${IMG}:${IMAGE_TAG}
$(OCI_BIN) tag ${REGISTRY}/${IMG}:${IMAGE_TAG} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}
$(OCI_BIN) push ${TLS_SETTING} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}
@$(MAKE) check-tag-exists || \
( \
$(OCI_BIN) tag ${REGISTRY}/${IMG}:${IMAGE_TAG} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}; \
$(OCI_BIN) push ${TLS_SETTING} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}; \
)
Copy link
Member

Choose a reason for hiding this comment

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

Can it be simplified please to one target ? (something like this, but didn't check it is correct, just as a direction)

docker-push:
	@echo "Checking if tag '${IMAGE_GIT_TAG}' exists in ${REGISTRY}/${IMG}..."
	@if ! skopeo inspect docker://${REGISTRY}/${IMG}:${IMAGE_GIT_TAG} >/dev/null 2>&1; then \
	    echo "Tag '${IMAGE_GIT_TAG}' does not exist. Pushing image..."; \
	    $(OCI_BIN) push ${TLS_SETTING} ${REGISTRY}/${IMG}:${IMAGE_TAG}; \
	    $(OCI_BIN) tag ${REGISTRY}/${IMG}:${IMAGE_TAG} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}; \
	    $(OCI_BIN) push ${TLS_SETTING} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}; \
	else \
	    echo "Tag '${IMAGE_GIT_TAG}' already exists. Skipping push."; \
	fi

Copy link
Member

Choose a reason for hiding this comment

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

btw it is raceful
lets say skopeo due to hiccup (but lets say tag exists), and then it entered the good path, and can override
need to assert the error of skopeo is not found, not any other error please

Copy link
Member Author

@RamLavi RamLavi Dec 16, 2024

Choose a reason for hiding this comment

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

sure, DONE

Copy link
Member Author

Choose a reason for hiding this comment

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

DONE.
My only worry is that if they change the error format then it might break, but I'm over analyzing this thing, so let's just keep it like this

Copy link
Member

Choose a reason for hiding this comment

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

sgtm
only thing that worth considering later if desired is if there are any side effects in case we match the last assertion
(when all the other branches didnt meet)

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

Please add in the description the flow of what you done that created it, it will help understanding when it happens and the solution / it's correctness

@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from ba18689 to 676c5d0 Compare December 16, 2024 07:12
@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from 676c5d0 to 0c5cb46 Compare December 16, 2024 07:18
@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

Please add in the description the flow of what you done that created it, it will help understanding when it happens and the solution / it's correctness

DONE, PTAL

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

Thanks please add the info you added to the commit to the PR desc

it means in this case we will only have release-0.45_latest for the branch right ?
i would say it is a bit confusing, and hopefully all the automation in case that it will need to read it will support it and wont be confused, best to always create patch after that on the branch maybe

Worth to consider please to think how we can create releases on main that wont affect future branching
but this is not for this scope, and no rush i guess

There is also the possible race above, please take a look
might worth to even inspect as the first operation, unless the side effects that are done are neglectable,
else the script should first inspect, see that it can have the answer and then do the rest

@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from 0c5cb46 to c826215 Compare December 16, 2024 07:42
@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

Thanks please add the info you added to the commit to the PR desc

it means in this case we will only have release-0.45_latest for the branch right ? i would say it is a bit confusing, and hopefully all the automation in case that it will need to read it will support it and wont be confused, best to always create patch after that on the branch maybe

Worth to consider please to think how we can create releases on main that wont affect future branching but this is not for this scope, and no rush i guess

yes, for follow up for sure.

There is also the possible race above, please take a look might worth to even inspect as the first operation, unless the side effects that are done are neglectable, else the script should first inspect, see that it can have the answer and then do the rest

fixed. PTAL

Makefile Outdated
$(OCI_BIN) tag ${REGISTRY}/${IMG}:${IMAGE_TAG} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}; \
$(OCI_BIN) push ${TLS_SETTING} ${REGISTRY}/${IMG}:${IMAGE_GIT_TAG}; \
else \
echo "Error checking for tag '${IMAGE_GIT_TAG}'. Aborting to avoid potential overwrite."; \
Copy link
Member

Choose a reason for hiding this comment

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

Do we need exit 1 so we will know it happens ?
needed if the tag can be mandatory on some cases (which i believe it does)

Copy link
Member Author

@RamLavi RamLavi Dec 16, 2024

Choose a reason for hiding this comment

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

It's not mandatory but it doesn't hurt to know it failed on the gitActions.. Otherwise we'll never see it.
DONE

@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from c826215 to ff5cf5a Compare December 16, 2024 08:10
@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

Thanks
/lgtm

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

/bin/sh: -c: line 9: syntax error: unexpected end of file
make[1]: *** [Makefile:96: docker-push] Error 2
make[1]: Leaving directory '/tmp/kubemacpool/kubemacpool'
make: *** [Makefile:113: cluster-sync] Error 2

@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from ff5cf5a to 11cb4ac Compare December 16, 2024 08:27
@kubevirt-bot kubevirt-bot removed the lgtm label Dec 16, 2024
@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

/bin/sh: -c: line 9: syntax error: unexpected end of file
make[1]: *** [Makefile:96: docker-push] Error 2
make[1]: Leaving directory '/tmp/kubemacpool/kubemacpool'
make: *** [Makefile:113: cluster-sync] Error 2

my bad, forgot the \ after the exit 1

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

Thanks
/lgtm

btw ; might not be needed when it is on multi line, but it is nit and also i didnt make sure about it

@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

/approve

@kubevirt-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RamLavi

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

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

Error checking for tag 'v0.45.0-2-gbd72b46c'. Aborting to avoid potential overwrite.
make[1]: *** [Makefile:96: docker-push] Error 1
make[1]: Leaving directory '/tmp/kubemacpool/kubemacpool'
make: *** [Makefile:113: cluster-sync] Error 2
+ teardown

@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from 11cb4ac to 8394c23 Compare December 16, 2024 08:48
@kubevirt-bot kubevirt-bot removed the lgtm label Dec 16, 2024
@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

For some reason it doesn't catch it, while locally it works:

$ IMAGE_GIT_TAG=v0.45.0-2-g089d9c26 make docker-push
Tagging 'v0.45.0-2-g089d9c26'!
$ IMAGE_GIT_TAG=latest make docker-push
Tag 'latest' already exists. Skipping tagging and push.

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

maybe old skopeo ?
worth to ask Brian maybe please
or to try to dirty install new skopeo first to see if it helps

@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

maybe old skopeo ?

could be. I'm thinking we drop the check, and if we see a race in the future we tackle it.
The IMAGE_GIT_TAG is not important for prod

@oshoval
Copy link
Member

oshoval commented Dec 16, 2024

if you drop the whole check, doesnt it mean it will always give wrong answer?
we do need a basic condition there right ?
and that one need to be correct
(worth to check what it prints etc on CI)

@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch from 8deb8a5 to 4635633 Compare December 16, 2024 10:22
The IMAGE_GIT_TAG is generated using `git describe` to create a virtual
tag for the image, and used in order to tag every push to the repository
for later use.
However, when an actual git tag exists (e.g., v0.45.0), git describe
returns that tag. This behavior makes it possible to accidentally
overwrite push an existing version tag in the registry.

Flow Leading to the Issue:
1. A new kmp release is created, pushing a new tag (e.g., v0.45.0).
2. A stable branch is created from that commit, pushing a new stable
branch tag (e.g., release-0.45_latest).
2.1 . During this push, IMAGE_GIT_TAG resolves to this Git tag (e.g.,
v0.45.0) due to git describe.
2.2 Makefile attempts to push the image with this tag (e.g., v0.45.0) to
the registry, overwriting the original tag sha256 digest.

To address this, introducing a check to ensure such tags are not
overwritten when pushed to remote repositories, preserving the integrity
of published versions. In case of local repositories the push to
IMAGE_GIT_TAG is removed entirely.

Signed-off-by: Ram Lavi <[email protected]>
@RamLavi RamLavi force-pushed the prevent_docker_git_tag_overwrite branch 2 times, most recently from 18e5fd9 to 4f33dbf Compare December 16, 2024 12:40
@RamLavi
Copy link
Member Author

RamLavi commented Dec 16, 2024

@oshoval fixed the issue, PTAL

@kubevirt-bot kubevirt-bot merged commit 38a92a2 into k8snetworkplumbingwg:main Dec 16, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants