Skip to content

Commit 9e3709f

Browse files
authored
feat(cache): add DeleteBlobs to ArtifactCache (fanal#426)
1 parent ee54733 commit 9e3709f

File tree

13 files changed

+321
-2
lines changed

13 files changed

+321
-2
lines changed

artifact/artifact.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ func (o *Option) Sort() {
3030

3131
type Artifact interface {
3232
Inspect(ctx context.Context) (reference types.ArtifactReference, err error)
33+
Clean(reference types.ArtifactReference) error
3334
}

artifact/image/image.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
120120
}, nil
121121
}
122122

123+
func (Artifact) Clean(_ types.ArtifactReference) error {
124+
return nil
125+
}
126+
123127
func (a Artifact) calcCacheKeys(imageID string, diffIDs []string) (string, []string, map[string]string, error) {
124128
// Pass an empty config scanner option so that the cache key can be the same, even when policies are updated.
125129
imageKey, err := cache.CalcKey(imageID, a.analyzer.ImageConfigAnalyzerVersions(), nil, artifact.Option{}, config.ScannerOption{})

artifact/local/fs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,7 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
165165
BlobIDs: []string{cacheKey},
166166
}, nil
167167
}
168+
169+
func (a Artifact) Clean(reference types.ArtifactReference) error {
170+
return a.cache.DeleteBlobs(reference.BlobIDs)
171+
}

artifact/mock_artifact.go

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

artifact/remote/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
8181
return ref, nil
8282
}
8383

84+
func (Artifact) Clean(_ types.ArtifactReference) error {
85+
return nil
86+
}
87+
8488
func newURL(rawurl string) (*url.URL, error) {
8589
u, err := url.Parse(rawurl)
8690
if err != nil {

cache/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type ArtifactCache interface {
2828

2929
// PutBlob stores blob information such as layer information in local cache
3030
PutBlob(blobID string, blobInfo types.BlobInfo) (err error)
31+
32+
// DeleteBlobs removes blobs by IDs
33+
DeleteBlobs(blobIDs []string) error
3134
}
3235

3336
// LocalArtifactCache always uses local cache

cache/fs.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"os"
66
"path/filepath"
77

8-
"github.com/aquasecurity/fanal/types"
8+
"github.com/hashicorp/go-multierror"
99
bolt "go.etcd.io/bbolt"
1010
"golang.org/x/xerrors"
11+
12+
"github.com/aquasecurity/fanal/types"
1113
)
1214

1315
var _ Cache = &FSCache{}
@@ -113,6 +115,24 @@ func (fs FSCache) GetArtifact(artifactID string) (types.ArtifactInfo, error) {
113115
return info, nil
114116
}
115117

118+
// DeleteBlobs removes blobs by IDs
119+
func (fs FSCache) DeleteBlobs(blobIDs []string) error {
120+
var errs error
121+
err := fs.db.Update(func(tx *bolt.Tx) error {
122+
bucket := tx.Bucket([]byte(blobBucket))
123+
for _, blobID := range blobIDs {
124+
if err := bucket.Delete([]byte(blobID)); err != nil {
125+
errs = multierror.Append(errs, err)
126+
}
127+
}
128+
return nil
129+
})
130+
if err != nil {
131+
return xerrors.Errorf("DB delete error: %w", err)
132+
}
133+
return errs
134+
}
135+
116136
// PutArtifact stores artifact information such as image metadata in local cache
117137
func (fs FSCache) PutArtifact(artifactID string, artifactInfo types.ArtifactInfo) (err error) {
118138
b, err := json.Marshal(artifactInfo)

cache/mock_artifact_cache.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cache/mock_cache.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cache/redis.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/go-redis/redis/v8"
9+
"github.com/hashicorp/go-multierror"
910
"golang.org/x/xerrors"
1011

1112
"github.com/aquasecurity/fanal/types"
@@ -50,6 +51,16 @@ func (c RedisCache) PutBlob(blobID string, blobInfo types.BlobInfo) error {
5051
}
5152
return nil
5253
}
54+
func (c RedisCache) DeleteBlobs(blobIDs []string) error {
55+
var errs error
56+
for _, blobID := range blobIDs {
57+
key := fmt.Sprintf("%s::%s::%s", redisPrefix, artifactBucket, blobID)
58+
if err := c.client.Del(context.TODO(), key).Err(); err != nil {
59+
errs = multierror.Append(errs, err)
60+
}
61+
}
62+
return errs
63+
}
5364

5465
func (c RedisCache) GetArtifact(artifactID string) (types.ArtifactInfo, error) {
5566
key := fmt.Sprintf("%s::%s::%s", redisPrefix, artifactBucket, artifactID)

0 commit comments

Comments
 (0)