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:
alexezio
2023-06-21 12:00:22 -07:00
committed by GitHub
co-authored by 连奔驰
parent 7cd39d14e3
commit 0743c19176
5 changed files with 232 additions and 3 deletions
+35 -2
View File
@@ -19,11 +19,14 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"time"
"github.com/GoogleContainerTools/kaniko/pkg/cache"
"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/logging"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/containerd/containerd/platforms"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors"
@@ -54,9 +57,14 @@ var RootCmd = &cobra.Command{
return err
}
if len(opts.Images) == 0 {
return errors.New("You must select at least one image to cache")
if len(opts.Images) == 0 && opts.DockerfilePath == "" {
return errors.New("You must select at least one image to cache or a dockerfilepath to parse")
}
if err := validateDockerfilePath(); err != nil {
return errors.Wrap(err, "error validating dockerfile path")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
@@ -89,6 +97,8 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().VarP(&opts.RegistriesClientCertificates, "registry-client-cert", "", "Use the provided client certificate for mutual TLS (mTLS) communication with the given registry. Expected format is 'my.registry.url=/path/to/client/cert,/path/to/client/key'.")
RootCmd.PersistentFlags().VarP(&opts.RegistryMirrors, "registry-mirror", "", "Registry mirror to use as pull-through cache instead of docker.io. Set it repeatedly for multiple mirrors.")
RootCmd.PersistentFlags().StringVarP(&opts.CustomPlatform, "customPlatform", "", "", "Specify the build platform if different from the current host")
RootCmd.PersistentFlags().StringVarP(&opts.DockerfilePath, "dockerfile", "d", "Dockerfile", "Path to the dockerfile to be cached. The kaniko warmer will parse and write out each stage's base image layers to the cache-dir. Using the same dockerfile path as what you plan to build in the kaniko executor is the expected usage.")
RootCmd.PersistentFlags().VarP(&opts.BuildArgs, "build-arg", "", "This flag should be used in conjunction with the dockerfile flag for scenarios where dynamic replacement of the base image is required.")
// Default the custom platform flag to our current platform, and validate it.
if opts.CustomPlatform == "" {
@@ -104,6 +114,29 @@ func addHiddenFlags() {
RootCmd.PersistentFlags().MarkHidden("azure-container-registry-config")
}
func validateDockerfilePath() error {
if isURL(opts.DockerfilePath) {
return nil
}
if util.FilepathExists(opts.DockerfilePath) {
abs, err := filepath.Abs(opts.DockerfilePath)
if err != nil {
return errors.Wrap(err, "getting absolute path for dockerfile")
}
opts.DockerfilePath = abs
return nil
}
return errors.New("please provide a valid path to a Dockerfile within the build context with --dockerfile")
}
func isURL(path string) bool {
if match, _ := regexp.MatchString("^https?://", path); match {
return true
}
return false
}
func exit(err error) {
fmt.Println(err)
os.Exit(1)