Skip to content

Commit c16b038

Browse files
author
Domenico Luciani
committed
Extract common code for getting insecure registry options from imageRef
Signed-off-by: Domenico Luciani <[email protected]>
1 parent d5371e2 commit c16b038

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

cmd/lifecycle/exporter.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,7 @@ func (e *exportCmd) initRemoteAppImage(analyzedMD files.Analyzed) (imgutil.Image
357357
opts = append(opts, remote.WithHistory())
358358
}
359359

360-
if len(e.InsecureRegistries) > 0 {
361-
cmd.DefaultLogger.Infof("Found Insecure Registries: %+q", e.InsecureRegistries)
362-
for _, insecureRegistry := range e.InsecureRegistries {
363-
if strings.HasPrefix(e.RunImageRef, insecureRegistry) {
364-
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
365-
}
366-
}
367-
}
360+
opts = append(opts, e.getInsecureRegistryOptions(e.RunImageRef)...)
368361

369362
if analyzedMD.PreviousImageRef() != "" {
370363
cmd.DefaultLogger.Infof("Reusing layers from image '%s'", analyzedMD.PreviousImageRef())
@@ -531,3 +524,16 @@ func (e *exportCmd) hasExtendedLayers() bool {
531524
}
532525
return true
533526
}
527+
528+
func (e *exportCmd) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
529+
var opts []remote.ImageOption
530+
if len(e.InsecureRegistries) > 0 {
531+
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", e.InsecureRegistries)
532+
for _, insecureRegistry := range e.InsecureRegistries {
533+
if strings.HasPrefix(imageRef, insecureRegistry) {
534+
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
535+
}
536+
}
537+
}
538+
return opts
539+
}

cmd/lifecycle/rebaser.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,7 @@ func (r *rebaseCmd) setAppImage() error {
170170
remote.FromBaseImage(targetImageRef),
171171
}
172172

173-
if len(r.InsecureRegistries) > 0 {
174-
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", r.InsecureRegistries)
175-
for _, insecureRegistry := range r.InsecureRegistries {
176-
if strings.HasPrefix(targetImageRef, insecureRegistry) {
177-
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
178-
}
179-
}
180-
}
173+
opts = append(opts, r.getInsecureRegistryOptions(targetImageRef)...)
181174

182175
r.appImage, err = remote.NewImage(
183176
targetImageRef,
@@ -217,3 +210,16 @@ func (r *rebaseCmd) setAppImage() error {
217210

218211
return nil
219212
}
213+
214+
func (r *rebaseCmd) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
215+
var opts []remote.ImageOption
216+
if len(r.InsecureRegistries) > 0 {
217+
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", r.InsecureRegistries)
218+
for _, insecureRegistry := range r.InsecureRegistries {
219+
if strings.HasPrefix(imageRef, insecureRegistry) {
220+
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
221+
}
222+
}
223+
}
224+
return opts
225+
}

cmd/lifecycle/restorer.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,7 @@ func (r *restoreCmd) pullSparse(imageRef string) (imgutil.Image, error) {
229229
remote.FromBaseImage(imageRef),
230230
}
231231

232-
if len(r.InsecureRegistries) > 0 {
233-
cmd.DefaultLogger.Infof("Found Insecure Registries: %+q", r.InsecureRegistries)
234-
for _, insecureRegistry := range r.InsecureRegistries {
235-
if strings.HasPrefix(imageRef, insecureRegistry) {
236-
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
237-
}
238-
}
239-
}
232+
opts = append(opts, r.getInsecureRegistryOptions(imageRef)...)
240233

241234
// get remote image
242235
remoteImage, err := remote.NewImage(imageRef, r.keychain, opts...)
@@ -297,3 +290,16 @@ func (r *restoreCmd) restore(layerMetadata files.LayersMetadata, group buildpack
297290
}
298291
return nil
299292
}
293+
294+
func (r *restoreCmd) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
295+
var opts []remote.ImageOption
296+
if len(r.InsecureRegistries) > 0 {
297+
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", r.InsecureRegistries)
298+
for _, insecureRegistry := range r.InsecureRegistries {
299+
if strings.HasPrefix(imageRef, insecureRegistry) {
300+
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
301+
}
302+
}
303+
}
304+
return opts
305+
}

image/remote_handler.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package image
33
import (
44
"strings"
55

6+
"github.com/buildpacks/lifecycle/cmd"
7+
68
"github.com/buildpacks/imgutil"
79
"github.com/buildpacks/imgutil/remote"
810
"github.com/google/go-containerregistry/pkg/authn"
@@ -24,13 +26,7 @@ func (h *RemoteHandler) InitImage(imageRef string) (imgutil.Image, error) {
2426
remote.FromBaseImage(imageRef),
2527
}
2628

27-
if len(h.insecureRegistries) > 0 {
28-
for _, insecureRegistry := range h.insecureRegistries {
29-
if strings.HasPrefix(imageRef, insecureRegistry) {
30-
options = append(options, remote.WithRegistrySetting(insecureRegistry, true, true))
31-
}
32-
}
33-
}
29+
options = append(options, h.getInsecureRegistryOptions(imageRef)...)
3430

3531
return remote.NewImage(
3632
imageRef,
@@ -42,3 +38,15 @@ func (h *RemoteHandler) InitImage(imageRef string) (imgutil.Image, error) {
4238
func (h *RemoteHandler) Kind() string {
4339
return RemoteKind
4440
}
41+
func (h *RemoteHandler) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
42+
var opts []remote.ImageOption
43+
if len(h.insecureRegistries) > 0 {
44+
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", h.insecureRegistries)
45+
for _, insecureRegistry := range h.insecureRegistries {
46+
if strings.HasPrefix(imageRef, insecureRegistry) {
47+
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
48+
}
49+
}
50+
}
51+
return opts
52+
}

0 commit comments

Comments
 (0)