From 063f2bfee9398c1d5aa353634f98377fcd981b44 Mon Sep 17 00:00:00 2001 From: Ziming Zhang Date: Wed, 28 Aug 2019 17:48:42 +0800 Subject: [PATCH] prevent retained tag with same digest deleted by other tag Signed-off-by: Ziming Zhang Change-Id: I6a69b405b454ca0350677204e06bfa2b24616b33 --- src/pkg/retention/dep/client.go | 1 + src/pkg/retention/policy/alg/or/processor_test.go | 2 ++ src/pkg/retention/res/candidate.go | 7 ++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pkg/retention/dep/client.go b/src/pkg/retention/dep/client.go index 9ccb951f7d..c51d427e80 100644 --- a/src/pkg/retention/dep/client.go +++ b/src/pkg/retention/dep/client.go @@ -109,6 +109,7 @@ func (bc *basicClient) GetCandidates(repository *res.Repository) ([]*res.Candida Namespace: repository.Namespace, Repository: repository.Name, Tag: image.Name, + Digest: image.Digest, Labels: labels, CreationTime: image.Created.Unix(), PulledTime: image.PullTime.Unix(), diff --git a/src/pkg/retention/policy/alg/or/processor_test.go b/src/pkg/retention/policy/alg/or/processor_test.go index d37a602c9a..8d09966e57 100644 --- a/src/pkg/retention/policy/alg/or/processor_test.go +++ b/src/pkg/retention/policy/alg/or/processor_test.go @@ -56,6 +56,7 @@ func (suite *ProcessorTestSuite) SetupSuite() { Repository: "harbor", Kind: "image", Tag: "latest", + Digest: "latest", PushedTime: time.Now().Unix(), Labels: []string{"L1", "L2"}, }, @@ -64,6 +65,7 @@ func (suite *ProcessorTestSuite) SetupSuite() { Repository: "harbor", Kind: "image", Tag: "dev", + Digest: "dev", PushedTime: time.Now().Unix(), Labels: []string{"L3"}, }, diff --git a/src/pkg/retention/res/candidate.go b/src/pkg/retention/res/candidate.go index ff8c8d145d..45c704c4f1 100644 --- a/src/pkg/retention/res/candidate.go +++ b/src/pkg/retention/res/candidate.go @@ -72,6 +72,8 @@ type Candidate struct { Kind string // Tag info Tag string + // Digest + Digest string // Pushed time in seconds PushedTime int64 // Pulled time in seconds @@ -84,7 +86,10 @@ type Candidate struct { // Hash code based on the candidate info for differentiation func (c *Candidate) Hash() string { - raw := fmt.Sprintf("%s:%s/%s:%s", c.Kind, c.Namespace, c.Repository, c.Tag) + if c.Digest == "" { + panic("Lack Digest of Candidate") + } + raw := fmt.Sprintf("%s:%s/%s:%s", c.Kind, c.Namespace, c.Repository, c.Digest) return base64.StdEncoding.EncodeToString([]byte(raw)) }