Skip to content

Commit

Permalink
Extract common code for getting insecure registry options from imageRef
Browse files Browse the repository at this point in the history
Signed-off-by: Domenico Luciani <[email protected]>
  • Loading branch information
Domenico Luciani committed Sep 13, 2023
1 parent a3ef194 commit ba7971e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
22 changes: 14 additions & 8 deletions cmd/lifecycle/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,7 @@ func (e *exportCmd) initRemoteAppImage(analyzedMD files.Analyzed) (imgutil.Image
opts = append(opts, remote.WithHistory())
}

if len(e.InsecureRegistries) > 0 {
cmd.DefaultLogger.Infof("Found Insecure Registries: %+q", e.InsecureRegistries)
for _, insecureRegistry := range e.InsecureRegistries {
if strings.HasPrefix(e.RunImageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
opts = append(opts, e.getInsecureRegistryOptions(e.RunImageRef)...)

if analyzedMD.PreviousImageRef() != "" {
cmd.DefaultLogger.Infof("Reusing layers from image '%s'", analyzedMD.PreviousImageRef())
Expand Down Expand Up @@ -531,3 +524,16 @@ func (e *exportCmd) hasExtendedLayers() bool {
}
return true
}

func (e *exportCmd) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
var opts []remote.ImageOption
if len(e.InsecureRegistries) > 0 {
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", e.InsecureRegistries)
for _, insecureRegistry := range e.InsecureRegistries {
if strings.HasPrefix(imageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
return opts
}
22 changes: 14 additions & 8 deletions cmd/lifecycle/rebaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,7 @@ func (r *rebaseCmd) setAppImage() error {
remote.FromBaseImage(targetImageRef),
}

if len(r.InsecureRegistries) > 0 {
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", r.InsecureRegistries)
for _, insecureRegistry := range r.InsecureRegistries {
if strings.HasPrefix(targetImageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
opts = append(opts, r.getInsecureRegistryOptions(targetImageRef)...)

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

return nil
}

func (r *rebaseCmd) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
var opts []remote.ImageOption
if len(r.InsecureRegistries) > 0 {
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", r.InsecureRegistries)
for _, insecureRegistry := range r.InsecureRegistries {
if strings.HasPrefix(imageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
return opts
}
22 changes: 14 additions & 8 deletions cmd/lifecycle/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,7 @@ func (r *restoreCmd) pullSparse(imageRef string) (imgutil.Image, error) {
remote.FromBaseImage(imageRef),
}

if len(r.InsecureRegistries) > 0 {
cmd.DefaultLogger.Infof("Found Insecure Registries: %+q", r.InsecureRegistries)
for _, insecureRegistry := range r.InsecureRegistries {
if strings.HasPrefix(imageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
opts = append(opts, r.getInsecureRegistryOptions(imageRef)...)

// get remote image
remoteImage, err := remote.NewImage(imageRef, r.keychain, opts...)
Expand Down Expand Up @@ -299,3 +292,16 @@ func (r *restoreCmd) restore(layerMetadata files.LayersMetadata, group buildpack
}
return nil
}

func (r *restoreCmd) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
var opts []remote.ImageOption
if len(r.InsecureRegistries) > 0 {
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", r.InsecureRegistries)
for _, insecureRegistry := range r.InsecureRegistries {
if strings.HasPrefix(imageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
return opts
}
22 changes: 15 additions & 7 deletions image/remote_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package image
import (
"strings"

"github.com/buildpacks/lifecycle/cmd"

"github.com/buildpacks/imgutil"
"github.com/buildpacks/imgutil/remote"
"github.com/google/go-containerregistry/pkg/authn"
Expand All @@ -24,13 +26,7 @@ func (h *RemoteHandler) InitImage(imageRef string) (imgutil.Image, error) {
remote.FromBaseImage(imageRef),
}

if len(h.insecureRegistries) > 0 {
for _, insecureRegistry := range h.insecureRegistries {
if strings.HasPrefix(imageRef, insecureRegistry) {
options = append(options, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
options = append(options, h.getInsecureRegistryOptions(imageRef)...)

return remote.NewImage(
imageRef,
Expand All @@ -42,3 +38,15 @@ func (h *RemoteHandler) InitImage(imageRef string) (imgutil.Image, error) {
func (h *RemoteHandler) Kind() string {
return RemoteKind
}
func (h *RemoteHandler) getInsecureRegistryOptions(imageRef string) []remote.ImageOption {
var opts []remote.ImageOption
if len(h.insecureRegistries) > 0 {
cmd.DefaultLogger.Warnf("Found Insecure Registries: %+q", h.insecureRegistries)
for _, insecureRegistry := range h.insecureRegistries {
if strings.HasPrefix(imageRef, insecureRegistry) {
opts = append(opts, remote.WithRegistrySetting(insecureRegistry, true, true))
}
}
}
return opts
}

0 comments on commit ba7971e

Please sign in to comment.