Skip to content

Commit

Permalink
added image caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw7 committed Jul 26, 2024
1 parent 22ad378 commit 3fc9de1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Artifact sources origin and metadata are now extracted from a configuration file (`config/artifacts.yaml`)
* Dropped `-chart` suffix from installed Helm chart names
* Added ability to build aarch64 images on an aarch64 host machine
* Added caching for container images

## API

Expand Down
7 changes: 1 addition & 6 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ type Cache struct {
cacheDir string
}

func New(rootDir string) (*Cache, error) {
cacheDir := filepath.Join(rootDir, "cache")
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("creating a cache directory: %w", err)
}

func New(cacheDir string) (*Cache, error) {
return &Cache{cacheDir: cacheDir}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/combustion/combustion.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Combustion struct {
RPMResolver rpmResolver
RPMRepoCreator rpmRepoCreator
Registry embeddedRegistry
CacheDir string
}

// Configure iterates over all separate Combustion components and configures them independently.
Expand Down
46 changes: 40 additions & 6 deletions pkg/combustion/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package combustion

import (
_ "embed"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -207,6 +208,11 @@ func registryArtefactsPath(ctx *image.Context) string {
}

func populateRegistry(ctx *image.Context, images []string) error {
imageCacheDir := filepath.Join(ctx.CacheDir, "images")
if err := os.MkdirAll(imageCacheDir, os.ModePerm); err != nil {
return fmt.Errorf("creating container image cache dir: %w", err)
}

bar := progressbar.Default(int64(len(images)), "Populating Embedded Artifact Registry...")
zap.S().Infof("Adding the following images to the embedded artifact registry:\n%s", images)

Expand All @@ -227,22 +233,50 @@ func populateRegistry(ctx *image.Context, images []string) error {
arch := ctx.ImageDefinition.Image.Arch.Short()

for _, img := range images {
if err = storeImage(img, arch, logFile); err != nil {
return fmt.Errorf("adding image to registry store: %w", err)
}

convertedImage := strings.ReplaceAll(img, "/", "_")
convertedImageName := fmt.Sprintf("%s-%s", convertedImage, registryTarSuffix)

imageCacheLocation := filepath.Join(imageCacheDir, convertedImageName)
imageTarDest := filepath.Join(registryArtefactsPath(ctx), convertedImageName)
if err = generateRegistryTar(imageTarDest, logFile); err != nil {
return fmt.Errorf("generating registry store tarball: %w", err)

exists, err := imageExists(imageCacheLocation)
if err != nil {
return fmt.Errorf("checking if image already cached: %w", err)
}

if exists {
if err = fileio.CopyFile(imageCacheLocation, imageTarDest, fileio.ExecutablePerms); err != nil {
return fmt.Errorf("copying cached container image: %w", err)
}
} else {
if err = storeImage(img, arch, logFile); err != nil {
return fmt.Errorf("adding image to registry store: %w", err)
}

if err = generateRegistryTar(imageTarDest, logFile); err != nil {
return fmt.Errorf("generating registry store tarball: %w", err)
}

if err = fileio.CopyFile(imageTarDest, imageCacheLocation, fileio.ExecutablePerms); err != nil {
return fmt.Errorf("copying container image to cache: %w", err)
}
}
if err = bar.Add(1); err != nil {
zap.S().Debugf("Error incrementing the progress bar: %s", err)
}
}

return nil
}

func imageExists(imagePath string) (bool, error) {
if _, err := os.Stat(imagePath); err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}

return false, fmt.Errorf("looking for cached image: %w", err)
}

return true, nil
}
8 changes: 7 additions & 1 deletion pkg/eib/eib.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func appendHelm(ctx *image.Context) {
}

func buildCombustion(ctx *image.Context, rootDir string) (*combustion.Combustion, error) {
cacheDir := filepath.Join(rootDir, "cache")
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("creating a cache directory: %w", err)
}
ctx.CacheDir = cacheDir

combustionHandler := &combustion.Combustion{
NetworkConfigGenerator: network.ConfigGenerator{},
NetworkConfiguratorInstaller: network.ConfiguratorInstaller{},
Expand Down Expand Up @@ -157,7 +163,7 @@ func buildCombustion(ctx *image.Context, rootDir string) (*combustion.Combustion
}

if ctx.ImageDefinition.Kubernetes.Version != "" {
c, err := cache.New(rootDir)
c, err := cache.New(cacheDir)
if err != nil {
return nil, fmt.Errorf("initialising cache instance: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/image/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Context struct {
ImageDefinition *Definition
// ArtifactSources contains the information necessary for the deployment of external artifacts.
ArtifactSources *ArtifactSources
// CacheDir contains all of the artifacts that are cached for the build process.
CacheDir string
}

type ArtifactSources struct {
Expand Down

0 comments on commit 3fc9de1

Please sign in to comment.