Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 6a37fe5

Browse files
committed
feat(warmer): Warmer now supports all registry-related flags
which means we can now: - set up one or more mirrors - set up registries certificates - skip TLS verify - use plain HTTP using the same set of flags that are defined for the executor
1 parent 6220dbd commit 6a37fe5

14 files changed

Lines changed: 239 additions & 192 deletions

File tree

cmd/warmer/cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ func addKanikoOptionsFlags() {
7575
RootCmd.PersistentFlags().StringVarP(&opts.CacheDir, "cache-dir", "c", "/cache", "Directory of the cache.")
7676
RootCmd.PersistentFlags().BoolVarP(&opts.Force, "force", "f", false, "Force cache overwriting.")
7777
RootCmd.PersistentFlags().DurationVarP(&opts.CacheTTL, "cache-ttl", "", time.Hour*336, "Cache timeout in hours. Defaults to two weeks.")
78+
RootCmd.PersistentFlags().BoolVarP(&opts.InsecurePull, "insecure-pull", "", false, "Pull from insecure registry using plain HTTP")
79+
RootCmd.PersistentFlags().BoolVarP(&opts.SkipTLSVerifyPull, "skip-tls-verify-pull", "", false, "Pull from insecure registry ignoring TLS verify")
80+
RootCmd.PersistentFlags().VarP(&opts.InsecureRegistries, "insecure-registry", "", "Insecure registry using plain HTTP to pull. Set it repeatedly for multiple registries.")
81+
RootCmd.PersistentFlags().VarP(&opts.SkipTLSVerifyRegistries, "skip-tls-verify-registry", "", "Insecure registry ignoring TLS verify to pull. Set it repeatedly for multiple registries.")
82+
opts.RegistriesCertificates = make(map[string]string)
83+
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'.")
84+
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.")
7885
}
7986

8087
// addHiddenFlags marks certain flags as hidden from the executor help text

pkg/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (rc *RegistryCache) RetrieveLayer(ck string) (v1.Image, error) {
6666
cacheRef.Repository.Registry = newReg
6767
}
6868

69-
tr := util.MakeTransport(rc.Opts, registryName)
69+
tr := util.MakeTransport(rc.Opts.RegistryOptions, registryName)
7070

7171
img, err := remote.Image(cacheRef, remote.WithTransport(tr), remote.WithAuthFromKeychain(creds.GetKeychain()))
7272
if err != nil {

pkg/cache/doc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"log"
2222

2323
"github.com/GoogleContainerTools/kaniko/pkg/config"
24-
"github.com/google/go-containerregistry/pkg/v1/remote"
24+
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
2525
)
2626

2727
func ExampleWarmer_Warm() {
2828
tarBuf := new(bytes.Buffer)
2929
manifestBuf := new(bytes.Buffer)
3030
w := &Warmer{
31-
Remote: remote.Image,
31+
Remote: remote.RetrieveRemoteImage,
3232
Local: LocalSource,
3333
TarWriter: tarBuf,
3434
ManifestWriter: manifestBuf,

pkg/cache/warm.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ import (
2020
"bytes"
2121
"io"
2222
"io/ioutil"
23-
"net/http"
2423
"os"
2524
"path"
26-
"runtime"
2725

2826
"github.com/GoogleContainerTools/kaniko/pkg/config"
29-
"github.com/GoogleContainerTools/kaniko/pkg/creds"
27+
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
3028
"github.com/google/go-containerregistry/pkg/name"
3129
v1 "github.com/google/go-containerregistry/pkg/v1"
32-
"github.com/google/go-containerregistry/pkg/v1/remote"
3330
"github.com/google/go-containerregistry/pkg/v1/tarball"
3431
"github.com/pkg/errors"
3532
"github.com/sirupsen/logrus"
@@ -42,18 +39,18 @@ func WarmCache(opts *config.WarmerOptions) error {
4239
logrus.Debugf("%s\n", cacheDir)
4340
logrus.Debugf("%s\n", images)
4441

45-
for _, image := range images {
42+
for _, img := range images {
4643
tarBuf := new(bytes.Buffer)
4744
manifestBuf := new(bytes.Buffer)
4845

4946
cw := &Warmer{
50-
Remote: remote.Image,
47+
Remote: remote.RetrieveRemoteImage,
5148
Local: LocalSource,
5249
TarWriter: tarBuf,
5350
ManifestWriter: manifestBuf,
5451
}
5552

56-
digest, err := cw.Warm(image, opts)
53+
digest, err := cw.Warm(img, opts)
5754
if err != nil {
5855
if !IsAlreadyCached(err) {
5956
return err
@@ -68,7 +65,7 @@ func WarmCache(opts *config.WarmerOptions) error {
6865
return err
6966
}
7067

71-
logrus.Debugf("Wrote %s to cache", image)
68+
logrus.Debugf("Wrote %s to cache", img)
7269
}
7370
return nil
7471
}
@@ -93,9 +90,9 @@ func writeBufsToFile(cachePath string, tarBuf, manifestBuf *bytes.Buffer) error
9390
}
9491

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

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

121-
transport := http.DefaultTransport.(*http.Transport)
122-
platform := currentPlatform()
123-
124-
rOpts := []remote.Option{remote.WithTransport(transport), remote.WithAuthFromKeychain(creds.GetKeychain()), remote.WithPlatform(platform)}
125-
img, err := w.Remote(cacheRef, rOpts...)
118+
img, err := w.Remote(image, opts.RegistryOptions)
126119
if err != nil || img == nil {
127120
return v1.Hash{}, errors.Wrapf(err, "Failed to retrieve image: %s", image)
128121
}
@@ -155,11 +148,3 @@ func (w *Warmer) Warm(image string, opts *config.WarmerOptions) (v1.Hash, error)
155148

156149
return digest, nil
157150
}
158-
159-
// CurrentPlatform returns the v1.Platform on which the code runs.
160-
func currentPlatform() v1.Platform {
161-
return v1.Platform{
162-
OS: runtime.GOOS,
163-
Architecture: runtime.GOARCH,
164-
}
165-
}

pkg/cache/warm_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import (
2222

2323
"github.com/GoogleContainerTools/kaniko/pkg/config"
2424
"github.com/GoogleContainerTools/kaniko/pkg/fakes"
25-
"github.com/google/go-containerregistry/pkg/name"
2625
v1 "github.com/google/go-containerregistry/pkg/v1"
27-
"github.com/google/go-containerregistry/pkg/v1/remote"
2826
)
2927

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

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

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

9492
cw := &Warmer{
95-
Remote: func(_ name.Reference, _ ...remote.Option) (v1.Image, error) {
93+
Remote: func(_ string, _ config.RegistryOptions) (v1.Image, error) {
9694
return fakes.FakeImage{}, nil
9795
},
9896
Local: func(_ *config.CacheOptions, _ string) (v1.Image, error) {

pkg/config/options.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,44 @@ type CacheOptions struct {
3030
CacheTTL time.Duration
3131
}
3232

33-
// KanikoOptions are options that are set by command line arguments
34-
type KanikoOptions struct {
35-
CacheOptions
36-
DockerfilePath string
37-
SrcContext string
38-
SnapshotMode string
39-
Bucket string
40-
TarPath string
41-
Target string
42-
CacheRepo string
43-
DigestFile string
44-
ImageNameDigestFile string
45-
OCILayoutPath string
33+
// RegistryOptions are all the options related to the registries, set by command line arguments.
34+
type RegistryOptions struct {
4635
RegistryMirrors multiArg
47-
Destinations multiArg
48-
BuildArgs multiArg
4936
InsecureRegistries multiArg
50-
Labels multiArg
5137
SkipTLSVerifyRegistries multiArg
5238
RegistriesCertificates keyValueArg
5339
Insecure bool
5440
SkipTLSVerify bool
5541
InsecurePull bool
5642
SkipTLSVerifyPull bool
57-
SingleSnapshot bool
58-
Reproducible bool
59-
NoPush bool
60-
Cache bool
61-
Cleanup bool
62-
IgnoreVarRun bool
63-
SkipUnusedStages bool
64-
RunV2 bool
65-
Git KanikoGitOptions
43+
}
44+
45+
// KanikoOptions are options that are set by command line arguments
46+
type KanikoOptions struct {
47+
CacheOptions
48+
RegistryOptions
49+
DockerfilePath string
50+
SrcContext string
51+
SnapshotMode string
52+
Bucket string
53+
TarPath string
54+
Target string
55+
CacheRepo string
56+
DigestFile string
57+
ImageNameDigestFile string
58+
OCILayoutPath string
59+
Destinations multiArg
60+
BuildArgs multiArg
61+
Labels multiArg
62+
SingleSnapshot bool
63+
Reproducible bool
64+
NoPush bool
65+
Cache bool
66+
Cleanup bool
67+
IgnoreVarRun bool
68+
SkipUnusedStages bool
69+
RunV2 bool
70+
Git KanikoGitOptions
6671
}
6772

6873
type KanikoGitOptions struct {
@@ -108,6 +113,7 @@ func (k *KanikoGitOptions) Set(s string) error {
108113
// WarmerOptions are options that are set by command line arguments to the cache warmer.
109114
type WarmerOptions struct {
110115
CacheOptions
116+
RegistryOptions
111117
Images multiArg
112118
Force bool
113119
}

pkg/executor/build.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,27 @@ import (
2525
"strings"
2626
"time"
2727

28-
"github.com/google/go-containerregistry/pkg/v1/partial"
29-
30-
"github.com/moby/buildkit/frontend/dockerfile/instructions"
31-
32-
"golang.org/x/sync/errgroup"
33-
3428
"github.com/google/go-containerregistry/pkg/name"
3529
v1 "github.com/google/go-containerregistry/pkg/v1"
3630
"github.com/google/go-containerregistry/pkg/v1/empty"
3731
"github.com/google/go-containerregistry/pkg/v1/mutate"
3832
"github.com/google/go-containerregistry/pkg/v1/tarball"
33+
"github.com/moby/buildkit/frontend/dockerfile/instructions"
3934
"github.com/pkg/errors"
4035
"github.com/sirupsen/logrus"
36+
"golang.org/x/sync/errgroup"
4137

4238
"github.com/GoogleContainerTools/kaniko/pkg/cache"
4339
"github.com/GoogleContainerTools/kaniko/pkg/commands"
4440
"github.com/GoogleContainerTools/kaniko/pkg/config"
4541
"github.com/GoogleContainerTools/kaniko/pkg/constants"
4642
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
4743
image_util "github.com/GoogleContainerTools/kaniko/pkg/image"
44+
"github.com/GoogleContainerTools/kaniko/pkg/image/remote"
4845
"github.com/GoogleContainerTools/kaniko/pkg/snapshot"
4946
"github.com/GoogleContainerTools/kaniko/pkg/timing"
5047
"github.com/GoogleContainerTools/kaniko/pkg/util"
48+
"github.com/google/go-containerregistry/pkg/v1/partial"
5149
)
5250

5351
// This is the size of an empty tar in Go
@@ -740,7 +738,7 @@ func fetchExtraStages(stages []config.KanikoStage, opts *config.KanikoOptions) e
740738

741739
// This must be an image name, fetch it.
742740
logrus.Debugf("Found extra base image stage %s", c.From)
743-
sourceImage, err := image_util.RetrieveRemoteImage(c.From, opts)
741+
sourceImage, err := remote.RetrieveRemoteImage(c.From, opts.RegistryOptions)
744742
if err != nil {
745743
return err
746744
}

pkg/executor/push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func CheckPushPermissions(opts *config.KanikoOptions) error {
147147
}
148148
destRef.Repository.Registry = newReg
149149
}
150-
tr := newRetry(util.MakeTransport(opts, registryName))
150+
tr := newRetry(util.MakeTransport(opts.RegistryOptions, registryName))
151151
if err := checkRemotePushPermission(destRef, creds.GetKeychain(), tr); err != nil {
152152
return errors.Wrapf(err, "checking push permission for %q", destRef)
153153
}
@@ -244,7 +244,7 @@ func DoPush(image v1.Image, opts *config.KanikoOptions) error {
244244
return errors.Wrap(err, "resolving pushAuth")
245245
}
246246

247-
tr := newRetry(util.MakeTransport(opts, registryName))
247+
tr := newRetry(util.MakeTransport(opts.RegistryOptions, registryName))
248248
rt := &withUserAgent{t: tr}
249249

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

0 commit comments

Comments
 (0)