Skip to content

Commit

Permalink
Clear cache on startup, use tempDir for unpacking
Browse files Browse the repository at this point in the history
  • Loading branch information
oceanc80 committed Jan 30, 2025
1 parent 037b9e2 commit 99cb71a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions catalogd/cmd/catalogd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func main() {
}

unpackCacheBasePath := filepath.Join(cacheDir, source.UnpackCacheDir)
if err := os.RemoveAll(unpackCacheBasePath); err != nil {
setupLog.Error(err, "unable to clear cache directory for unpacking on startup")
os.Exit(1)
}
if err := os.MkdirAll(unpackCacheBasePath, 0770); err != nil {
setupLog.Error(err, "unable to create cache directory for unpacking")
os.Exit(1)
Expand Down
24 changes: 18 additions & 6 deletions catalogd/internal/source/containers_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,17 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
return wrapTerminal(fmt.Errorf("catalog image is missing the required label %q", ConfigDirLabel), specIsCanonical)
}

if err := os.MkdirAll(unpackPath, 0700); err != nil {
return fmt.Errorf("error creating unpack directory: %w", err)
}
l := log.FromContext(ctx)
l.Info("unpacking image", "path", unpackPath)
l.Info("unpacking image", "path", unpackPath, "temp path", tempUnpackPath)

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / extension-developer-e2e

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / extension-developer-e2e

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / unit-test-basic

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / e2e-kind

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / e2e-kind

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / verify

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / verify

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / upgrade-e2e

undefined: tempUnpackPath

Check failure on line 300 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / upgrade-e2e

undefined: tempUnpackPath
tempUnpackPath, err := os.MkdirTemp("", fmt.Sprintf("unpack-%s", catalog.Name))

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / extension-developer-e2e

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / extension-developer-e2e

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: catalog) (typecheck)

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: catalog) (typecheck)

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / lint

undefined: catalog (typecheck)

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / unit-test-basic

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / e2e-kind

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / e2e-kind

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / verify

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / verify

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / upgrade-e2e

undefined: catalog

Check failure on line 301 in catalogd/internal/source/containers_image.go

View workflow job for this annotation

GitHub Actions / upgrade-e2e

undefined: catalog
if err != nil {
return fmt.Errorf("error creating temporary unpack directory: %w", err)
}
defer func() {
if err := os.RemoveAll(tempUnpackPath); err != nil {
l.Error(err, "error removing temporary unpack directory")
}
}()
for i, layerInfo := range img.LayerInfos() {
if err := func() error {
layerReader, _, err := layoutSrc.GetBlob(ctx, layerInfo, none.NoCache)
Expand All @@ -309,15 +315,21 @@ func (i *ContainersImageRegistry) unpackImage(ctx context.Context, unpackPath st
}
defer layerReader.Close()

if err := applyLayer(ctx, unpackPath, dirToUnpack, layerReader); err != nil {
if err := applyLayer(ctx, tempUnpackPath, dirToUnpack, layerReader); err != nil {
return fmt.Errorf("error applying layer[%d]: %w", i, err)
}
l.Info("applied layer", "layer", i)
return nil
}(); err != nil {
return errors.Join(err, deleteRecursive(unpackPath))
return errors.Join(err, deleteRecursive(tempUnpackPath))
}
}
if err := os.MkdirAll(unpackPath, 0700); err != nil {
return fmt.Errorf("error creating unpack directory: %w", err)
}
if err := os.Rename(tempUnpackPath, unpackPath); err != nil {
return fmt.Errorf("error moving temporary unpack directory to final unpack directory: %w", err)
}
if err := setReadOnlyRecursive(unpackPath); err != nil {
return fmt.Errorf("error making unpack directory read-only: %w", err)
}
Expand Down

0 comments on commit 99cb71a

Please sign in to comment.