Skip to content

Commit

Permalink
Add Remove into filesystemCache
Browse files Browse the repository at this point in the history
Enables us to deletes cache directory
for a given catalog from the filesystem.

Signed-off-by: Mikalai Radchuk <[email protected]>
  • Loading branch information
Mikalai Radchuk committed Sep 16, 2024
1 parent 188858c commit 0608b8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ func main() {
setupLog.Error(err, "unable to create catalogs cache directory")
os.Exit(1)
}
catalogClient := catalogclient.New(cache.NewFilesystemCache(catalogsCachePath, func() (*http.Client, error) {
cacheFetcher := cache.NewFilesystemCache(catalogsCachePath, func() (*http.Client, error) {
return httputil.BuildHTTPClient(certPoolWatcher)
}))
})
catalogClient := catalogclient.New(cacheFetcher)

resolver := &resolve.CatalogResolver{
WalkCatalogsFunc: resolve.CatalogWalker(
Expand Down
26 changes: 24 additions & 2 deletions internal/catalogmetadata/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var _ client.Fetcher = &filesystemCache{}
// - IF cached it will verify the cache is up to date. If it is up to date it will return
// the cached contents, if not it will fetch the new contents from the catalogd HTTP
// server and update the cached contents.
func NewFilesystemCache(cachePath string, clientFunc func() (*http.Client, error)) client.Fetcher {
func NewFilesystemCache(cachePath string, clientFunc func() (*http.Client, error)) *filesystemCache {
return &filesystemCache{
cachePath: cachePath,
mutex: sync.RWMutex{},
Expand Down Expand Up @@ -80,7 +80,7 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c
return nil, fmt.Errorf("error: catalog %q has a nil status.resolvedSource.image value", catalog.Name)
}

cacheDir := filepath.Join(fsc.cachePath, catalog.Name)
cacheDir := fsc.cacheDir(catalog.Name)
fsc.mutex.RLock()
if data, ok := fsc.cacheDataByCatalogName[catalog.Name]; ok {
if catalog.Status.ResolvedSource.Image.ResolvedRef == data.ResolvedRef {
Expand Down Expand Up @@ -166,3 +166,25 @@ func (fsc *filesystemCache) FetchCatalogContents(ctx context.Context, catalog *c

return os.DirFS(cacheDir), nil
}

// Remove deletes cache directory for a given catalog from the filesystem
func (fsc *filesystemCache) Remove(catalogName string) error {
cacheDir := fsc.cacheDir(catalogName)

fsc.mutex.Lock()
defer fsc.mutex.Unlock()

if _, exists := fsc.cacheDataByCatalogName[catalogName]; !exists {
return nil
}
delete(fsc.cacheDataByCatalogName, catalogName)

if err := os.RemoveAll(cacheDir); err != nil {
return fmt.Errorf("error removing cache directory: %v", err)
}
return nil
}

func (fsc *filesystemCache) cacheDir(catalogName string) string {
return filepath.Join(fsc.cachePath, catalogName)
}

0 comments on commit 0608b8e

Please sign in to comment.