Skip to content

Commit f69c337

Browse files
committed
Refactor: Move logic to get default image uri to constants
Right now, to pull default image we pass empty string to `image/PullBundle` function and have logic to get default image URI in case the empty string for URI which is bit confusing. In this PR we are adding `GetDefaultBundleImageRegistryURL` function to constants pkg to get this info and change `image/PullBundle` signature.
1 parent 22d9ec4 commit f69c337

3 files changed

Lines changed: 18 additions & 22 deletions

File tree

pkg/crc/constants/constants.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,18 @@ func GetDefaultMemory(preset crcpreset.Preset) int {
221221
return 9216
222222
}
223223
}
224+
225+
func GetDefaultBundleImageRegistry(preset crcpreset.Preset) string {
226+
return fmt.Sprintf("//%s/%s:%s", RegistryURI, getImageName(preset), version.GetBundleVersion(preset))
227+
}
228+
229+
func getImageName(preset crcpreset.Preset) string {
230+
switch preset {
231+
case crcpreset.Podman:
232+
return "podman-bundle"
233+
case crcpreset.OKD:
234+
return "okd-bundle"
235+
default:
236+
return "openshift-bundle"
237+
}
238+
}

pkg/crc/image/image.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/crc-org/crc/v2/pkg/crc/gpg"
2020
"github.com/crc-org/crc/v2/pkg/crc/logging"
2121
crcpreset "github.com/crc-org/crc/v2/pkg/crc/preset"
22-
"github.com/crc-org/crc/v2/pkg/crc/version"
2322
"github.com/crc-org/crc/v2/pkg/extract"
2423
v1 "github.com/opencontainers/image-spec/specs-go/v1"
2524
)
@@ -28,10 +27,6 @@ type imageHandler struct {
2827
imageURI string
2928
}
3029

31-
func defaultURI(preset crcpreset.Preset) string {
32-
return fmt.Sprintf("//%s/%s:%s", constants.RegistryURI, getImageName(preset), version.GetBundleVersion(preset))
33-
}
34-
3530
func ValidateURI(uri *url.URL) error {
3631
/* For now we are very restrictive on the docker:// URLs we accept, it
3732
* has to contain the bundle version as the tag, and it has to use the
@@ -101,17 +96,6 @@ func getLayerPath(m *v1.Manifest, index int, mediaType string) (string, error) {
10196
return strings.TrimPrefix(m.Layers[index].Digest.String(), "sha256:"), nil
10297
}
10398

104-
func getImageName(preset crcpreset.Preset) string {
105-
switch preset {
106-
case crcpreset.Podman:
107-
return "podman-bundle"
108-
case crcpreset.OKD:
109-
return "okd-bundle"
110-
default:
111-
return "openshift-bundle"
112-
}
113-
}
114-
11599
func getPresetNameE(imageName string) (crcpreset.Preset, error) {
116100
switch imageName {
117101
case "openshift-bundle":
@@ -129,10 +113,7 @@ func GetPresetName(imageName string) crcpreset.Preset {
129113
return preset
130114
}
131115

132-
func PullBundle(preset crcpreset.Preset, imageURI string) (string, error) {
133-
if imageURI == "" {
134-
imageURI = defaultURI(preset)
135-
}
116+
func PullBundle(imageURI string) (string, error) {
136117
imgHandler := imageHandler{
137118
imageURI: strings.TrimPrefix(imageURI, "docker:"),
138119
}

pkg/crc/machine/bundle/metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ func Download(preset crcPreset.Preset, bundleURI string) (string, error) {
350350
case crcPreset.Podman, crcPreset.OKD:
351351
fallthrough
352352
default:
353-
return image.PullBundle(preset, "")
353+
return image.PullBundle(constants.GetDefaultBundleImageRegistry(preset))
354354
}
355355
}
356356
switch {
357357
case strings.HasPrefix(bundleURI, "http://"), strings.HasPrefix(bundleURI, "https://"):
358358
return download.Download(bundleURI, constants.MachineCacheDir, 0644, nil)
359359
case strings.HasPrefix(bundleURI, "docker://"):
360-
return image.PullBundle(preset, bundleURI)
360+
return image.PullBundle(bundleURI)
361361
}
362362
// the `bundleURI` parameter turned out to be a local path
363363
return bundleURI, nil

0 commit comments

Comments
 (0)