Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(warmer): Warmer now supports all registry-related flags #1499

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/warmer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "c", "/cache", "Directory of the cache.")
RootCmd.PersistentFlags().BoolVarP(&opts.Force, "force", "f", false, "Force cache overwriting.")
RootCmd.PersistentFlags().DurationVarP(&opts.CacheTTL, "cache-ttl", "", time.Hour*336, "Cache timeout in hours. Defaults to two weeks.")
RootCmd.PersistentFlags().BoolVarP(&opts.InsecurePull, "insecure-pull", "", false, "Pull from insecure registry using plain HTTP")
RootCmd.PersistentFlags().BoolVarP(&opts.SkipTLSVerifyPull, "skip-tls-verify-pull", "", false, "Pull from insecure registry ignoring TLS verify")
RootCmd.PersistentFlags().VarP(&opts.InsecureRegistries, "insecure-registry", "", "Insecure registry using plain HTTP to pull. Set it repeatedly for multiple registries.")
RootCmd.PersistentFlags().VarP(&opts.SkipTLSVerifyRegistries, "skip-tls-verify-registry", "", "Insecure registry ignoring TLS verify to pull. Set it repeatedly for multiple registries.")
opts.RegistriesCertificates = make(map[string]string)
RootCmd.PersistentFlags().VarP(&opts.RegistriesCertificates, "registry-certificate", "", "Use the provided certificate for TLS communication with the given registry. Expected format is 'my.registry.url=/path/to/the/server/certificate'.")
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")
}

// addHiddenFlags marks certain flags as hidden from the executor help text
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (rc *RegistryCache) RetrieveLayer(ck string) (v1.Image, error) {
cacheRef.Repository.Registry = newReg
}

tr := util.MakeTransport(rc.Opts, registryName)
tr := util.MakeTransport(rc.Opts.RegistryOptions, registryName)

img, err := remote.Image(cacheRef, remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain()))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cache/doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"log"

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
)

func ExampleWarmer_Warm() {
tarBuf := new(bytes.Buffer)
manifestBuf := new(bytes.Buffer)
w := &Warmer{
Remote: remote.Image,
Remote: remote.RetrieveRemoteImage,
Local: LocalSource,
TarWriter: tarBuf,
ManifestWriter: manifestBuf,
Expand Down
31 changes: 8 additions & 23 deletions pkg/cache/warm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ import (
"bytes"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"runtime"

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/creds"
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -42,18 +39,18 @@ func WarmCache(opts *config.WarmerOptions) error {
logrus.Debugf("%s\n", cacheDir)
logrus.Debugf("%s\n", images)

for _, image := range images {
for _, img := range images {
tarBuf := new(bytes.Buffer)
manifestBuf := new(bytes.Buffer)

cw := &Warmer{
Remote: remote.Image,
Remote: remote.RetrieveRemoteImage,
Local: LocalSource,
TarWriter: tarBuf,
ManifestWriter: manifestBuf,
}

digest, err := cw.Warm(image, opts)
digest, err := cw.Warm(img, opts)
if err != nil {
if !IsAlreadyCached(err) {
return err
Expand All @@ -68,7 +65,7 @@ func WarmCache(opts *config.WarmerOptions) error {
return err
}

logrus.Debugf("Wrote %s to cache", image)
logrus.Debugf("Wrote %s to cache", img)
}
return nil
}
Expand All @@ -93,9 +90,9 @@ func writeBufsToFile(cachePath string, tarBuf, manifestBuf *bytes.Buffer) error
}

// FetchRemoteImage retrieves a Docker image manifest from a remote source.
// github.com/google/go-containerregistry/pkg/v1/remote.Image can be used as
// github.com/GoogleContainerTools/kaniko/image/remote.RetrieveRemoteImage can be used as
// this type.
type FetchRemoteImage func(name.Reference, ...remote.Option) (v1.Image, error)
type FetchRemoteImage func(image string, opts config.RegistryOptions, customPlatform string) (v1.Image, error)

// FetchLocalSource retrieves a Docker image manifest from a local source.
// github.com/GoogleContainerTools/kaniko/cache.LocalSource can be used as
Expand All @@ -118,11 +115,7 @@ func (w *Warmer) Warm(image string, opts *config.WarmerOptions) (v1.Hash, error)
return v1.Hash{}, errors.Wrapf(err, "Failed to verify image name: %s", image)
}

transport := http.DefaultTransport.(*http.Transport)
platform := currentPlatform()

rOpts := []remote.Option{remote.WithTransport(transport), remote.WithAuthFromKeychain(creds.GetKeychain()), remote.WithPlatform(platform)}
img, err := w.Remote(cacheRef, rOpts...)
img, err := w.Remote(image, opts.RegistryOptions, opts.CustomPlatform)
if err != nil || img == nil {
return v1.Hash{}, errors.Wrapf(err, "Failed to retrieve image: %s", image)
}
Expand Down Expand Up @@ -155,11 +148,3 @@ func (w *Warmer) Warm(image string, opts *config.WarmerOptions) (v1.Hash, error)

return digest, nil
}

// CurrentPlatform returns the v1.Platform on which the code runs.
func currentPlatform() v1.Platform {
return v1.Platform{
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
}
}
8 changes: 3 additions & 5 deletions pkg/cache/warm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (

"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/fakes"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
)

const (
Expand All @@ -36,7 +34,7 @@ func Test_Warmer_Warm_not_in_cache(t *testing.T) {
manifestBuf := new(bytes.Buffer)

cw := &Warmer{
Remote: func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
Remote: func(_ string, _ config.RegistryOptions, _ string) (v1.Image, error) {
return fakes.FakeImage{}, nil
},
Local: func(_ *config.CacheOptions, _ string) (v1.Image, error) {
Expand Down Expand Up @@ -64,7 +62,7 @@ func Test_Warmer_Warm_in_cache_not_expired(t *testing.T) {
manifestBuf := new(bytes.Buffer)

cw := &Warmer{
Remote: func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
Remote: func(_ string, _ config.RegistryOptions, _ string) (v1.Image, error) {
return fakes.FakeImage{}, nil
},
Local: func(_ *config.CacheOptions, _ string) (v1.Image, error) {
Expand Down Expand Up @@ -92,7 +90,7 @@ func Test_Warmer_Warm_in_cache_expired(t *testing.T) {
manifestBuf := new(bytes.Buffer)

cw := &Warmer{
Remote: func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
Remote: func(_ string, _ config.RegistryOptions, _ string) (v1.Image, error) {
return fakes.FakeImage{}, nil
},
Local: func(_ *config.CacheOptions, _ string) (v1.Image, error) {
Expand Down
63 changes: 35 additions & 28 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,45 @@ type CacheOptions struct {
CacheTTL time.Duration
}

// KanikoOptions are options that are set by command line arguments
type KanikoOptions struct {
CacheOptions
DockerfilePath string
SrcContext string
SnapshotMode string
CustomPlatform string
Bucket string
TarPath string
Target string
CacheRepo string
DigestFile string
ImageNameDigestFile string
OCILayoutPath string
// RegistryOptions are all the options related to the registries, set by command line arguments.
type RegistryOptions struct {
RegistryMirrors multiArg
Destinations multiArg
BuildArgs multiArg
InsecureRegistries multiArg
Labels multiArg
SkipTLSVerifyRegistries multiArg
RegistriesCertificates keyValueArg
Insecure bool
SkipTLSVerify bool
InsecurePull bool
SkipTLSVerifyPull bool
SingleSnapshot bool
Reproducible bool
NoPush bool
Cache bool
Cleanup bool
IgnoreVarRun bool
SkipUnusedStages bool
RunV2 bool
Git KanikoGitOptions
}

// KanikoOptions are options that are set by command line arguments
type KanikoOptions struct {
CacheOptions
RegistryOptions
DockerfilePath string
SrcContext string
SnapshotMode string
CustomPlatform string
Bucket string
TarPath string
Target string
CacheRepo string
DigestFile string
ImageNameDigestFile string
OCILayoutPath string
Destinations multiArg
BuildArgs multiArg
Labels multiArg
SingleSnapshot bool
Reproducible bool
NoPush bool
Cache bool
Cleanup bool
IgnoreVarRun bool
SkipUnusedStages bool
RunV2 bool
Git KanikoGitOptions
}

type KanikoGitOptions struct {
Expand Down Expand Up @@ -109,6 +114,8 @@ func (k *KanikoGitOptions) Set(s string) error {
// WarmerOptions are options that are set by command line arguments to the cache warmer.
type WarmerOptions struct {
CacheOptions
Images multiArg
Force bool
RegistryOptions
CustomPlatform string
Images multiArg
Force bool
}
12 changes: 5 additions & 7 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,27 @@ import (
"strings"
"time"

"github.com/google/go-containerregistry/pkg/v1/partial"

"github.com/moby/buildkit/frontend/dockerfile/instructions"

"golang.org/x/sync/errgroup"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

"github.com/GoogleContainerTools/kaniko/pkg/cache"
"github.com/GoogleContainerTools/kaniko/pkg/commands"
"github.com/GoogleContainerTools/kaniko/pkg/config"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
image_util "github.com/GoogleContainerTools/kaniko/pkg/image"
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
"github.com/GoogleContainerTools/kaniko/pkg/snapshot"
"github.com/GoogleContainerTools/kaniko/pkg/timing"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1/partial"
)

// This is the size of an empty tar in Go
Expand Down Expand Up @@ -745,7 +743,7 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e

// This must be an image name, fetch it.
logrus.Debugf("Found extra base image stage %s", c.From)
sourceImage, err := image_util.RetrieveRemoteImage(c.From, opts)
sourceImage, err := remote.RetrieveRemoteImage(c.From, opts.RegistryOptions, opts.CustomPlatform)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func CheckPushPermissions(opts *config.KanikoOptions) error {
}
destRef.Repository.Registry = newReg
}
tr := newRetry(util.MakeTransport(opts, registryName))
tr := newRetry(util.MakeTransport(opts.RegistryOptions, registryName))
if err := checkRemotePushPermission(destRef, creds.GetKeychain(), tr); err != nil {
return errors.Wrapf(err, "checking push permission for %q", destRef)
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
return errors.Wrap(err, "resolving pushAuth")
}

tr := newRetry(util.MakeTransport(opts, registryName))
tr := newRetry(util.MakeTransport(opts.RegistryOptions, registryName))
rt := &withUserAgent{t: tr}

if err := remote.Write(destRef, image, remote.WithAuth(pushAuth), remote.WithTransport(rt)); err != nil {
Expand Down
Loading