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 3, 2024
1 parent ccaf67d commit 60f915e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,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
23 changes: 22 additions & 1 deletion 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 @@ -166,3 +166,24 @@ 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(catalog *catalogd.ClusterCatalog) error {
if catalog == nil {
return fmt.Errorf("error: provided catalog must be non-nil")
}

cacheDir := filepath.Join(fsc.cachePath, catalog.Name)

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

if _, exists := fsc.cacheDataByCatalogName[catalog.Name]; !exists {
return nil
}

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

0 comments on commit 60f915e

Please sign in to comment.