Skip to content

Commit 86ef105

Browse files
committed
Support Insecure Registries
Signed-off-by: Prashant Rewar <[email protected]>
1 parent 2df4445 commit 86ef105

File tree

8 files changed

+60
-13
lines changed

8 files changed

+60
-13
lines changed

internal/build/lifecycle_execution.go

+16
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ func (l *LifecycleExecution) Create(ctx context.Context, buildCache, launchCache
328328
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
329329
}
330330

331+
for _, reg := range l.opts.InsecureRegistries {
332+
flags = append(flags, "-insecure-registry", reg)
333+
}
334+
331335
if l.opts.PreviousImage != "" {
332336
if l.opts.Image == nil {
333337
return errors.New("image can't be nil")
@@ -481,6 +485,10 @@ func (l *LifecycleExecution) Restore(ctx context.Context, buildCache Cache, kani
481485
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
482486
}
483487

488+
for _, reg := range l.opts.InsecureRegistries {
489+
flags = append(flags, "-insecure-registry", reg)
490+
}
491+
484492
// for kaniko
485493
kanikoCacheBindOp := NullOp()
486494
if (l.platformAPI.AtLeast("0.10") && l.hasExtensionsForBuild()) ||
@@ -586,6 +594,10 @@ func (l *LifecycleExecution) Analyze(ctx context.Context, buildCache, launchCach
586594
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
587595
}
588596

597+
for _, reg := range l.opts.InsecureRegistries {
598+
flags = append(flags, "-insecure-registry", reg)
599+
}
600+
589601
if l.opts.PreviousImage != "" {
590602
if l.opts.Image == nil {
591603
return errors.New("image can't be nil")
@@ -795,6 +807,10 @@ func (l *LifecycleExecution) Export(ctx context.Context, buildCache, launchCache
795807
flags = append(flags, "-uid", strconv.Itoa(l.opts.UID))
796808
}
797809

810+
for _, reg := range l.opts.InsecureRegistries {
811+
flags = append(flags, "-insecure-registry", reg)
812+
}
813+
798814
cacheBindOp := NullOp()
799815
switch buildCache.Type() {
800816
case cache.Image:

internal/build/lifecycle_executor.go

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type LifecycleOptions struct {
103103
SBOMDestinationDir string
104104
CreationTime *time.Time
105105
Keychain authn.Keychain
106+
InsecureRegistries []string
106107
}
107108

108109
func NewLifecycleExecutor(logger logging.Logger, docker DockerClient) *LifecycleExecutor {

internal/commands/build.go

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type BuildFlags struct {
5757
DateTime string
5858
PreBuildpacks []string
5959
PostBuildpacks []string
60+
InsecureRegistries []string
6061
}
6162

6263
var macAddressRegex = regexp.MustCompile(`^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$`)
@@ -203,6 +204,7 @@ func Build(logger logging.Logger, cfg config.Config, packClient PackClient) *cob
203204
PreviousInputImage: inputPreviousImage,
204205
LayoutRepoDir: cfg.LayoutRepositoryDir,
205206
},
207+
InsecureRegistries: flags.InsecureRegistries,
206208
}); err != nil {
207209
return errors.Wrap(err, "failed to build")
208210
}
@@ -236,6 +238,7 @@ func buildCommandFlags(cmd *cobra.Command, buildFlags *BuildFlags, cfg config.Co
236238
cmd.Flags().StringVarP(&buildFlags.AppPath, "path", "p", "", "Path to app dir or zip-formatted file (defaults to current working directory)")
237239
cmd.Flags().StringSliceVarP(&buildFlags.Buildpacks, "buildpack", "b", nil, "Buildpack to use. One of:\n a buildpack by id and version in the form of '<buildpack>@<version>',\n path to a buildpack directory (not supported on Windows),\n path/URL to a buildpack .tar or .tgz file, or\n a packaged buildpack image name in the form of '<hostname>/<repo>[:<tag>]'"+stringSliceHelp("buildpack"))
238240
cmd.Flags().StringSliceVarP(&buildFlags.Extensions, "extension", "", nil, "Extension to use. One of:\n an extension by id and version in the form of '<extension>@<version>',\n path to an extension directory (not supported on Windows),\n path/URL to an extension .tar or .tgz file, or\n a packaged extension image name in the form of '<hostname>/<repo>[:<tag>]'"+stringSliceHelp("extension"))
241+
cmd.Flags().StringSliceVarP(&buildFlags.InsecureRegistries, "insecure-registry", "", nil, "List of insecure registries")
239242
cmd.Flags().StringVarP(&buildFlags.Builder, "builder", "B", cfg.DefaultBuilder, "Builder image")
240243
cmd.Flags().Var(&buildFlags.Cache, "cache",
241244
`Cache options used to define cache techniques for build process.

internal/commands/rebase.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Rebase(logger logging.Logger, cfg config.Config, pack PackClient) *cobra.Co
5151
cmd.Flags().StringVar(&policy, "pull-policy", "", "Pull policy to use. Accepted values are always, never, and if-not-present. The default is always")
5252
cmd.Flags().StringVar(&opts.ReportDestinationDir, "report-output-dir", "", "Path to export build report.toml.\nOmitting the flag yield no report file.")
5353
cmd.Flags().BoolVar(&opts.Force, "force", false, "Perform rebase operation without target validation (only available for API >= 0.12)")
54-
54+
cmd.Flags().StringSliceVarP(&opts.InsecureRegistries, "insecure-registry", "", nil, "List of insecure registries")
5555
AddHelpFlag(cmd, "rebase")
5656
return cmd
5757
}

pkg/client/build.go

+3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ type BuildOptions struct {
212212

213213
// Configuration to export to OCI layout format
214214
LayoutConfig *LayoutConfig
215+
216+
InsecureRegistries []string
215217
}
216218

217219
func (b *BuildOptions) Layout() bool {
@@ -555,6 +557,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
555557
CreationTime: opts.CreationTime,
556558
Layout: opts.Layout(),
557559
Keychain: c.keychain,
560+
InsecureRegistries: opts.InsecureRegistries,
558561
}
559562

560563
switch {

pkg/client/client.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ type Client struct {
102102
lifecycleExecutor LifecycleExecutor
103103
buildpackDownloader BuildpackDownloader
104104

105-
experimental bool
106-
registryMirrors map[string]string
107-
version string
105+
experimental bool
106+
registryMirrors map[string]string
107+
version string
108+
insecureRegistries []string
108109
}
109110

110111
// Option is a type of function that mutate settings on the client.
@@ -187,6 +188,13 @@ func WithRegistryMirrors(registryMirrors map[string]string) Option {
187188
}
188189
}
189190

191+
// WithInsecureRegistries sets insecure registry to pull images from.
192+
func WithInsecureRegistries(insecureRegistries []string) Option {
193+
return func(c *Client) {
194+
c.insecureRegistries = insecureRegistries
195+
}
196+
}
197+
190198
// WithKeychain sets keychain of credentials to image registries
191199
func WithKeychain(keychain authn.Keychain) Option {
192200
return func(c *Client) {
@@ -231,7 +239,7 @@ func NewClient(opts ...Option) (*Client, error) {
231239
}
232240

233241
if client.imageFetcher == nil {
234-
client.imageFetcher = image.NewFetcher(client.logger, client.docker, image.WithRegistryMirrors(client.registryMirrors), image.WithKeychain(client.keychain))
242+
client.imageFetcher = image.NewFetcher(client.logger, client.docker, image.WithRegistryMirrors(client.registryMirrors), image.WithKeychain(client.keychain), image.WithInsecureRegistries(client.insecureRegistries))
235243
}
236244

237245
if client.imageFactory == nil {

pkg/client/rebase.go

+7
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,14 @@ type RebaseOptions struct {
4646
// Pass-through force flag to lifecycle rebase command to skip target data
4747
// validated (will not have any effect if API < 0.12).
4848
Force bool
49+
50+
InsecureRegistries []string
4951
}
5052

5153
// Rebase updates the run image layers in an app image.
5254
// This operation mutates the image specified in opts.
5355
func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
56+
var flags = []string{"rebase"}
5457
imageRef, err := c.parseTagReference(opts.RepoName)
5558
if err != nil {
5659
return errors.Wrapf(err, "invalid image name '%s'", opts.RepoName)
@@ -112,6 +115,10 @@ func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
112115
return err
113116
}
114117

118+
for _, reg := range opts.InsecureRegistries {
119+
flags = append(flags, "-insecure-registry", reg)
120+
}
121+
115122
c.logger.Infof("Rebasing %s on run image %s", style.Symbol(appImage.Name()), style.Symbol(baseImage.Name()))
116123
rebaser := &phase.Rebaser{Logger: c.logger, PlatformAPI: build.SupportedPlatformAPIVersions.Latest(), Force: opts.Force}
117124
report, err := rebaser.Rebase(appImage, baseImage, appImage.Name(), nil)

pkg/image/fetcher.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ func WithRegistryMirrors(registryMirrors map[string]string) FetcherOption {
4242
}
4343
}
4444

45+
// WithInsecureRegistries supply your own insecure registries.
46+
func WithInsecureRegistries(insecureRegistries []string) FetcherOption {
47+
return func(c *Fetcher) {
48+
c.insecureRegistries = insecureRegistries
49+
}
50+
}
51+
4552
func WithKeychain(keychain authn.Keychain) FetcherOption {
4653
return func(c *Fetcher) {
4754
c.keychain = keychain
@@ -54,17 +61,19 @@ type DockerClient interface {
5461
}
5562

5663
type Fetcher struct {
57-
docker DockerClient
58-
logger logging.Logger
59-
registryMirrors map[string]string
60-
keychain authn.Keychain
64+
docker DockerClient
65+
logger logging.Logger
66+
registryMirrors map[string]string
67+
keychain authn.Keychain
68+
insecureRegistries []string
6169
}
6270

6371
type FetchOptions struct {
64-
Daemon bool
65-
Platform string
66-
PullPolicy PullPolicy
67-
LayoutOption LayoutOption
72+
Daemon bool
73+
Platform string
74+
PullPolicy PullPolicy
75+
LayoutOption LayoutOption
76+
InsecureRegistries []string
6877
}
6978

7079
func NewFetcher(logger logging.Logger, docker DockerClient, opts ...FetcherOption) *Fetcher {

0 commit comments

Comments
 (0)