Skip to content

Commit

Permalink
image: Add several parsing related helpers
Browse files Browse the repository at this point in the history
In the next commits, we'll want to parse/validate docker:// URLs,
this commit adds several helpers to make it easier to go from
docker://quay.io/crcont/podman-bundle:4.1.1 to the expected bundle name
(crc_podman_libvirt_4.1.1_amd64.crcbundle)

The requirements on the docker:// URL are very strict, hopefully in the
future we'll relax them, and allow stuff like
quay.io/myorg/my-custom-bundle:latest , and crc will be able to figure
out the corresponding bundle name through the use of labels, or through
an intermediate metadata file.
  • Loading branch information
cfergeau authored and praveenkumar committed Sep 19, 2022
1 parent c9c2aaf commit a9c0b4c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/crc/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -30,6 +32,21 @@ func defaultURI(preset crcpreset.Preset) string {
return fmt.Sprintf("//%s/%s:%s", constants.RegistryURI, getImageName(preset), version.GetCRCVersion())
}

func ValidateURI(uri *url.URL) error {
/* For now we are very restrictive on the docker:// URLs we accept, it
* has to contain the bundle version as the tag, and it has to use the
* same image names as the official registry (openshift-bundle,
* okd-bundle, podman-bundle). In future releases, we'll make this more
* flexible
*/
imageAndTag := strings.Split(path.Base(uri.Path), ":")
if len(imageAndTag) != 2 {
return fmt.Errorf("invalid %s registry URL, tag is required (such as docker://quay.io/crcont/openshift-bundle:4.11.0)", uri)
}
_, err := getPresetNameE(imageAndTag[0])
return err
}

func (img *imageHandler) policyContext() (*signature.PolicyContext, error) {
policy := &signature.Policy{Default: []signature.PolicyRequirement{signature.NewPRInsecureAcceptAnything()}}
policyContext, err := signature.NewPolicyContext(policy)
Expand Down Expand Up @@ -95,6 +112,23 @@ func getImageName(preset crcpreset.Preset) string {
}
}

func getPresetNameE(imageName string) (crcpreset.Preset, error) {
switch imageName {
case "openshift-bundle":
return crcpreset.OpenShift, nil
case "okd-bundle":
return crcpreset.OKD, nil
case "podman-bundle":
return crcpreset.Podman, nil
default:
return crcpreset.OpenShift, fmt.Errorf("invalid image name '%s' (Should be openshift-bundle, okd-bundle or podman-bundle)", imageName)
}
}
func GetPresetName(imageName string) crcpreset.Preset {
preset, _ := getPresetNameE(imageName)
return preset
}

func PullBundle(preset crcpreset.Preset, imageURI string) error {
if imageURI == "" {
imageURI = defaultURI(preset)
Expand Down

0 comments on commit a9c0b4c

Please sign in to comment.