Skip to content

Commit 75e06d1

Browse files
oceanc80Per Goncalves da Silva
authored andcommitted
Clear cache on startup, use tempDir for unpacking
Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com>
1 parent 037b9e2 commit 75e06d1

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

catalogd/cmd/catalogd/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ func main() {
257257
systemNamespace = podNamespace()
258258
}
259259

260+
if err := clearCacheDir(cacheDir); err != nil {
261+
setupLog.Error(err, "unable to clear cache directory")
262+
os.Exit(1)
263+
}
260264
if err := os.MkdirAll(cacheDir, 0700); err != nil {
261265
setupLog.Error(err, "unable to create cache directory")
262266
os.Exit(1)
@@ -392,3 +396,16 @@ func podNamespace() string {
392396
}
393397
return string(namespace)
394398
}
399+
400+
func clearCacheDir(cacheDirPath string) error {
401+
entries, err := os.ReadDir(cacheDirPath)
402+
if err != nil && !os.IsNotExist(err) {
403+
return err
404+
}
405+
for _, entry := range entries {
406+
if err := os.RemoveAll(filepath.Join(cacheDirPath, entry.Name())); err != nil {
407+
return err
408+
}
409+
}
410+
return nil
411+
}

catalogd/internal/source/containers_image.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,9 @@ func (i *ContainersImageRegistry) Unpack(ctx context.Context, catalog *catalogdv
7676
//
7777
//////////////////////////////////////////////////////
7878
unpackPath := i.unpackPath(catalog.Name, canonicalRef.Digest())
79-
if unpackStat, err := os.Stat(unpackPath); err == nil {
80-
if !unpackStat.IsDir() {
81-
panic(fmt.Sprintf("unexpected file at unpack path %q: expected a directory", unpackPath))
82-
}
79+
if isUnpacked, unpackTime := isImageUnpacked(unpackPath); isUnpacked {
8380
l.Info("image already unpacked", "ref", imgRef.String(), "digest", canonicalRef.Digest().String())
84-
return successResult(unpackPath, canonicalRef, unpackStat.ModTime()), nil
81+
return successResult(unpackPath, canonicalRef, unpackTime), nil
8582
}
8683

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

296+
// ensure unpack directory is empty
297+
if err := os.RemoveAll(unpackPath); err != nil {
298+
return fmt.Errorf("error removing unpacked path: %w", err)
299+
}
299300
if err := os.MkdirAll(unpackPath, 0700); err != nil {
300301
return fmt.Errorf("error creating unpack directory: %w", err)
301302
}
@@ -431,3 +432,13 @@ func wrapTerminal(err error, isTerminal bool) error {
431432
}
432433
return reconcile.TerminalError(err)
433434
}
435+
436+
func isImageUnpacked(unpackPath string) (bool, time.Time) {
437+
if unpackStat, err := os.Stat(unpackPath); err == nil {
438+
if !unpackStat.IsDir() {
439+
panic(fmt.Sprintf("unexpected file at unpack path %q: expected a directory", unpackPath))
440+
}
441+
return true, unpackStat.ModTime()
442+
}
443+
return false, time.Time{}
444+
}

0 commit comments

Comments
 (0)