Skip to content

Commit

Permalink
Clear cache on startup, use tempDir for unpacking
Browse files Browse the repository at this point in the history
Signed-off-by: Per Goncalves da Silva <[email protected]>
  • Loading branch information
oceanc80 authored and Per Goncalves da Silva committed Jan 30, 2025
1 parent 037b9e2 commit 75e06d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
17 changes: 17 additions & 0 deletions catalogd/cmd/catalogd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ func main() {
systemNamespace = podNamespace()
}

if err := clearCacheDir(cacheDir); err != nil {
setupLog.Error(err, "unable to clear cache directory")
os.Exit(1)
}
if err := os.MkdirAll(cacheDir, 0700); err != nil {
setupLog.Error(err, "unable to create cache directory")
os.Exit(1)
Expand Down Expand Up @@ -392,3 +396,16 @@ func podNamespace() string {
}
return string(namespace)
}

func clearCacheDir(cacheDirPath string) error {
entries, err := os.ReadDir(cacheDirPath)
if err != nil && !os.IsNotExist(err) {
return err
}
for _, entry := range entries {
if err := os.RemoveAll(filepath.Join(cacheDirPath, entry.Name())); err != nil {
return err
}
}
return nil
}
21 changes: 16 additions & 5 deletions catalogd/internal/source/containers_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,9 @@ func (i *ContainersImageRegistry) Unpack(ctx context.Context, catalog *catalogdv
//
//////////////////////////////////////////////////////
unpackPath := i.unpackPath(catalog.Name, canonicalRef.Digest())
if unpackStat, err := os.Stat(unpackPath); err == nil {
if !unpackStat.IsDir() {
panic(fmt.Sprintf("unexpected file at unpack path %q: expected a directory", unpackPath))
}
if isUnpacked, unpackTime := isImageUnpacked(unpackPath); isUnpacked {
l.Info("image already unpacked", "ref", imgRef.String(), "digest", canonicalRef.Digest().String())
return successResult(unpackPath, canonicalRef, unpackStat.ModTime()), nil
return successResult(unpackPath, canonicalRef, unpackTime), nil
}

//////////////////////////////////////////////////////
Expand Down Expand Up @@ -296,6 +293,10 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
return wrapTerminal(fmt.Errorf("catalog image is missing the required label %q", ConfigDirLabel), specIsCanonical)
}

// ensure unpack directory is empty
if err := os.RemoveAll(unpackPath); err != nil {
return fmt.Errorf("error removing unpacked path: %w", err)
}
if err := os.MkdirAll(unpackPath, 0700); err != nil {
return fmt.Errorf("error creating unpack directory: %w", err)
}
Expand Down Expand Up @@ -431,3 +432,13 @@ func wrapTerminal(err error, isTerminal bool) error {
}
return reconcile.TerminalError(err)
}

func isImageUnpacked(unpackPath string) (bool, time.Time) {
if unpackStat, err := os.Stat(unpackPath); err == nil {
if !unpackStat.IsDir() {
panic(fmt.Sprintf("unexpected file at unpack path %q: expected a directory", unpackPath))
}
return true, unpackStat.ModTime()
}
return false, time.Time{}
}

0 comments on commit 75e06d1

Please sign in to comment.