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: Pick up correct commit SHA when using annotated git tags (#4231) #4538

Merged
merged 18 commits into from
Oct 22, 2020
Merged

fix: Pick up correct commit SHA when using annotated git tags (#4231) #4538

merged 18 commits into from
Oct 22, 2020

Conversation

jaideepr97
Copy link
Contributor

@jaideepr97 jaideepr97 commented Oct 12, 2020

Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • Optional. My organization is added to USERS.md.
  • I've signed the CLA and my build is green (troubleshooting builds).

fixes: #4231
Argocd seems to be picking up the hash of the tag from the output of ls-remote rather than the commit SHA in case of annotated tags, this fix checks to see if the picked up revision matches the commit SHA or not, and replaces the tag hash with the commit SHA if not

@CLAassistant
Copy link

CLAassistant commented Oct 12, 2020

CLA assistant check
All committers have signed the CLA.

@@ -163,6 +163,11 @@ func (s *Service) runRepoOperation(
if err != nil {
return nil, err
}
sha, err := gitClient.CommitSHA()
Copy link
Contributor

@wtam2018 wtam2018 Oct 14, 2020

Choose a reason for hiding this comment

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

I am skeptical about this fix. gitClient.CommitSHA() gives us the SHA of the HEAD which may not be the resolving tag is pointing to. I think we want to figure out what caused SHA resolution hehavior change since v1.4.2.

Copy link
Contributor Author

@jaideepr97 jaideepr97 Oct 14, 2020

Choose a reason for hiding this comment

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

I just tested it again to be sure, it is picking up the correct commit SHA for the specified tag even when HEAD points to a different SHA
I think it maybe using the state of the commit history from a particular revision so HEAD resolves to the same as the required commit

As for the behaviour change, from what I understand, the lsRemote function just runs git ls-remote's output, which is the list of all refs and tags with their SHAs. In this list, it seems the annotated tag's hash always appears before the SHA of the commit it points to (which is in the next line below it) and the current code just traverses the list and picks up the first one it sees, and ends up taking the hash of the tag instead of the commit SHA

if I had to guess, maybe the commit SHA showed up before the tag hash at some point and the ordering of the hashes present in the output of ls-remote may have changed recently (since the argocd code for this doesn't seem to have changed between 1.4.2 and the current version)

Copy link
Member

Choose a reason for hiding this comment

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

IMHO, the only way to get the actual revision of the commit an annotated tag is pointing to is to checkout the tag, bringing the repository into detached HEAD state, and then resolve HEAD revision.

The checkout to the target revision is done a little later down the code path (

_, err = checkoutRevision(gitClient, revision, log.WithField("repo", repo.Repo))
). So probably the current incarnation of this PR will only work if the annotated tag is actually pointing to the real HEAD of the repository's default branch.

Copy link
Contributor Author

@jaideepr97 jaideepr97 Oct 15, 2020

Choose a reason for hiding this comment

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

@jaanfis
The fix is working even when HEAD points to a different commit SHA
Seeing some strange behavior I haven't been able to explain yet though, CommitSHA() seems to be running git rev-parse HEAD but returning the correct commit SHA almost as if the correct revision of the repo is somehow already being checked out even before the actual call to checkoutRevision happens, which happens later like you pointed out
Even when immediately before/after HEAD does get resolved to the actual HEAD in a different function call
There seems to be some kind of inconsistency between the api-server and repo-server here as to what HEAD actually points to

22:09:32 repo-server | DEBU[0039] revision 'v0.0.8' resolved to '95249be61b028d566c29d47b19e65c5603388a41' 
22:09:32 repo-server | INFO[0039] git fetch origin --tags --force               dir="/tmp/https:__github.com_jaideepr97_argocd-experiment" execID=OAWIY
22:09:32 repo-server | DEBU[0039] revision 'v0.0.8' resolved to '95249be61b028d566c29d47b19e65c5603388a41' 
22:09:32 repo-server | INFO[0039] git rev-parse HEAD                            dir="/tmp/https:__github.com_jaideepr97_argocd-experiment" execID=W212M
22:09:32 repo-server | DEBU[0039] 632039659e542ed7de0c170a4fcc1c571b288fc0      duration=3.83898ms execID=W212M
22:09:32 repo-server | INFO[0039] app details cache miss: 632039659e542ed7de0c170a4fcc1c571b288fc0/&ApplicationSource{RepoURL:https://github.com/jaideepr97/ARGOCD-EXPERIMENT,Path:helm-guestbook,TargetRevision:v0.0.8,Helm:nil,Kustomize:nil,Ksonnet:nil,Directory:nil,Plugin:nil,Chart:,} 
22:09:32  controller | INFO[0040] Successfully saved info of 1 clusters        
22:09:32  api-server | 2020/10/15 22:09:32 proto: tag has too few fields: "-"
22:09:32  api-server | INFO[0039] received unary call /application.ApplicationService/Create  grpc.method=Create grpc.request.claims=null grpc.request.content="%!v(PANIC=String method: reflect.Value.Bytes of non-byte slice)" grpc.service=application.ApplicationService grpc.start_time="2020-10-15T22:09:32Z" span.kind=server system=grpc
22:09:33  api-server | DEBU[0040] symbolic reference 'HEAD' (refs/heads/master) resolved to '401ea4c66bbd95c18eb3ed4d05912514cae83f30'

Here 401ea4c66bbd95c18eb3ed4d05912514cae83f30 is the actual HEAD, 95249be61b028d566c29d47b19e65c5603388a41 is the hash of the annotated tag and 632039659e542ed7de0c170a4fcc1c571b288fc0 is the commit I want

Copy link
Contributor

@wtam2018 wtam2018 Oct 16, 2020

Choose a reason for hiding this comment

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

@jaideepr97
It is a regression introduced in v1.6.0.
The revision returned from checkoutRevision() was ignored. This should fix it.
wtam2018@3b61354

Copy link
Contributor

Choose a reason for hiding this comment

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

@alexmt Do you recall the reason to ignore the revision returned from checkoutRevision()?
4f49168

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for finding the root cause @wtam2018 , @jaideepr97 . Finally found why changes 4f49168 was implemented.
That was an attempt to fix caching for git tags. The problem is that we were using tag hash, returned by ls-remote to load cached manifests:

result, obj, err := getCached(revision, true)

and then commit sha to store generated manifests in cache:

return operation(appPath, gitClient.Root(), revision, signature)

(operation eventually uses revision to store cache).

So repo-server was unable to property cache manifests for annotated tags and had to regenerate them again and again.

I think this PR re-introduced old bug again. @jaideepr97 I think we can fix it by adding one more parameter to operation that holds ls-remote result, so that operation has both commitSHA and revision used to retrieve cache.

@codecov-io
Copy link

codecov-io commented Oct 19, 2020

Codecov Report

Merging #4538 into master will increase coverage by 0.02%.
The diff coverage is 84.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4538      +/-   ##
==========================================
+ Coverage   41.31%   41.34%   +0.02%     
==========================================
  Files         124      124              
  Lines       16976    16976              
==========================================
+ Hits         7014     7018       +4     
+ Misses       8950     8948       -2     
+ Partials     1012     1010       -2     
Impacted Files Coverage Δ
reposerver/repository/repository.go 60.61% <84.61%> (+0.61%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf03c1d...155eecf. Read the comment docs.

@@ -145,7 +145,7 @@ func (s *Service) runRepoOperation(
source *v1alpha1.ApplicationSource,
verifyCommit bool,
getCached func(revision string, firstInvocation bool) (bool, interface{}, error),
operation func(appPath, repoRoot, revision, verifyResult string) (interface{}, error),
operation func(appPath, repoRoot, commitSHA, revision, verifyResult string) (interface{}, error),
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to add some commment to describe the difference between these parameters. commitSHA is a resolved SHA from a revision. It goes to the generated manifest. revision is used as a key to manifest cache. I would probably rename revision to cacheKey.

@wtam2018
Copy link
Contributor

LGTM
@jaideepr97 thanks for the fix.

@jaideepr97
Copy link
Contributor Author

LGTM
@jaideepr97 thanks for the fix.

Thanks for the help and feedback!

Copy link
Collaborator

@alexmt alexmt left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you @jaideepr97 !

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.

Git tags pointing to wrong hash
6 participants