mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 09:37:02 +02:00
feat: cache dockerfile images through warmer (#2499)
* feat: cache dockerfile images through warmer * Fix logical error in conditional statement * Addressed review feedback 1. Updated help text for the --build-arg flag to indicate it should be used with the dockerfile flag. 2. Updated the documentation to include the optional --build-arg flag. 3. Added unit tests for `ParseDockerfile`, covering scenarios for missing Dockerfile, invalid Dockerfile, single stage Dockerfile, multi-stage Dockerfile and Args Dockerfile --------- Co-authored-by: 连奔驰 <benchi.lian@thoughtworks.com>
This commit is contained in:
Vendored
+56
@@ -18,13 +18,18 @@ package cache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/tarball"
|
||||
@@ -34,8 +39,21 @@ import (
|
||||
|
||||
// WarmCache populates the cache
|
||||
func WarmCache(opts *config.WarmerOptions) error {
|
||||
var dockerfileImages []string
|
||||
cacheDir := opts.CacheDir
|
||||
images := opts.Images
|
||||
|
||||
// if opts.image is empty,we need to parse dockerfilepath to get images list
|
||||
if opts.DockerfilePath != "" {
|
||||
var err error
|
||||
if dockerfileImages, err = ParseDockerfile(opts); err != nil {
|
||||
return errors.Wrap(err, "failed to parse Dockerfile")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Implement deduplication logic later.
|
||||
images = append(images, dockerfileImages...)
|
||||
|
||||
logrus.Debugf("%s\n", cacheDir)
|
||||
logrus.Debugf("%s\n", images)
|
||||
|
||||
@@ -157,3 +175,41 @@ func (w *Warmer) Warm(image string, opts *config.WarmerOptions) (v1.Hash, error)
|
||||
|
||||
return digest, nil
|
||||
}
|
||||
|
||||
func ParseDockerfile(opts *config.WarmerOptions) ([]string, error) {
|
||||
var err error
|
||||
var d []uint8
|
||||
var baseNames []string
|
||||
match, _ := regexp.MatchString("^https?://", opts.DockerfilePath)
|
||||
if match {
|
||||
response, e := http.Get(opts.DockerfilePath) //nolint:noctx
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
d, err = ioutil.ReadAll(response.Body)
|
||||
} else {
|
||||
d, err = ioutil.ReadFile(opts.DockerfilePath)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("reading dockerfile at path %s", opts.DockerfilePath))
|
||||
}
|
||||
|
||||
stages, _, err := dockerfile.Parse(d)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parsing dockerfile")
|
||||
}
|
||||
|
||||
for i, s := range stages {
|
||||
resolvedBaseName, err := util.ResolveEnvironmentReplacement(s.BaseName, opts.BuildArgs, false)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("resolving base name %s", s.BaseName))
|
||||
}
|
||||
if s.BaseName != resolvedBaseName {
|
||||
stages[i].BaseName = resolvedBaseName
|
||||
}
|
||||
baseNames = append(baseNames, resolvedBaseName)
|
||||
}
|
||||
return baseNames, nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user