Add docker transport to push image before final failure

Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com>

Closes: #42
Approved by: rhatdan
This commit is contained in:
TomSweeneyRedHat 2017-11-14 09:55:00 -05:00 committed by Atomic Bot
parent da44ec438d
commit d43f786728
4 changed files with 45 additions and 13 deletions

View File

@ -53,7 +53,7 @@ var (
defaultRuntimeConfig = RuntimeConfig{ defaultRuntimeConfig = RuntimeConfig{
// Leave this empty so containers/storage will use its defaults // Leave this empty so containers/storage will use its defaults
StorageConfig: storage.StoreOptions{}, StorageConfig: storage.StoreOptions{},
ImageDefaultTransport: "docker://", ImageDefaultTransport: DefaultTransport,
InMemoryState: false, InMemoryState: false,
RuntimePath: "/usr/bin/runc", RuntimePath: "/usr/bin/runc",
ConmonPath: "/usr/local/libexec/crio/conmon", ConmonPath: "/usr/local/libexec/crio/conmon",

View File

@ -32,12 +32,6 @@ import (
// Runtime API // Runtime API
const (
// DefaultRegistry is a prefix that we apply to an image name
// to check docker hub first for the image
DefaultRegistry = "docker://"
)
var ( var (
// DockerArchive is the transport we prepend to an image name // DockerArchive is the transport we prepend to an image name
// when saving to docker-archive // when saving to docker-archive
@ -49,7 +43,7 @@ var (
// images to and from a directory // images to and from a directory
DirTransport = "dir" DirTransport = "dir"
// TransportNames are the supported transports in string form // TransportNames are the supported transports in string form
TransportNames = [...]string{DefaultRegistry, DockerArchive, OCIArchive, "ostree:", "dir:"} TransportNames = [...]string{DefaultTransport, DockerArchive, OCIArchive, "ostree:", "dir:"}
// TarballTransport is the transport for importing a tar archive // TarballTransport is the transport for importing a tar archive
// and creating a filesystem image // and creating a filesystem image
TarballTransport = "tarball" TarballTransport = "tarball"
@ -249,7 +243,7 @@ func (k *Image) Decompose() error {
return nil return nil
} }
k.beenDecomposed = true k.beenDecomposed = true
k.Transport = "docker://" k.Transport = k.runtime.config.ImageDefaultTransport
decomposeName := k.Name decomposeName := k.Name
for _, transport := range TransportNames { for _, transport := range TransportNames {
if strings.HasPrefix(k.Name, transport) { if strings.HasPrefix(k.Name, transport) {
@ -460,7 +454,7 @@ func (ips imageDecomposeStruct) returnFQName() string {
return fmt.Sprintf("%s%s/%s:%s", ips.transport, ips.registry, ips.imageName, ips.tag) return fmt.Sprintf("%s%s/%s:%s", ips.transport, ips.registry, ips.imageName, ips.tag)
} }
func getRegistriesToTry(image string, store storage.Store) ([]*pullStruct, error) { func getRegistriesToTry(image string, store storage.Store, defaultTransport string) ([]*pullStruct, error) {
var pStructs []*pullStruct var pStructs []*pullStruct
var imageError = fmt.Sprintf("unable to parse '%s'\n", image) var imageError = fmt.Sprintf("unable to parse '%s'\n", image)
imgRef, err := reference.Parse(image) imgRef, err := reference.Parse(image)
@ -483,7 +477,7 @@ func getRegistriesToTry(image string, store storage.Store) ([]*pullStruct, error
tag, tag,
registry, registry,
hasDomain, hasDomain,
"docker://", defaultTransport,
} }
if pImage.hasRegistry { if pImage.hasRegistry {
// If input has a registry, we have to assume they included an image // If input has a registry, we have to assume they included an image
@ -651,7 +645,7 @@ func (r *Runtime) PullImage(imgName string, options CopyOptions) error {
srcRef, err := alltransports.ParseImageName(imgName) srcRef, err := alltransports.ParseImageName(imgName)
if err != nil { if err != nil {
// could be trying to pull from registry with short name // could be trying to pull from registry with short name
pullStructs, err = getRegistriesToTry(imgName, r.store) pullStructs, err = getRegistriesToTry(imgName, r.store, r.config.ImageDefaultTransport)
if err != nil { if err != nil {
return errors.Wrap(err, "error getting default registries to try") return errors.Wrap(err, "error getting default registries to try")
} }
@ -703,7 +697,20 @@ func (r *Runtime) PushImage(source string, destination string, options CopyOptio
// Get the destination Image Reference // Get the destination Image Reference
dest, err := alltransports.ParseImageName(destination) dest, err := alltransports.ParseImageName(destination)
if err != nil { if err != nil {
return errors.Wrapf(err, "error getting destination imageReference for %q", destination) if hasTransport(destination) {
return errors.Wrapf(err, "error getting destination imageReference for %q", destination)
}
// Try adding the images default transport
destination2 := r.config.ImageDefaultTransport + destination
dest, err = alltransports.ParseImageName(destination2)
if err != nil {
// One last try with docker:// as the transport
destination2 = DefaultTransport + destination
dest, err = alltransports.ParseImageName(destination2)
if err != nil {
return errors.Wrapf(err, "error getting destination imageReference for %q", destination)
}
}
} }
signaturePolicyPath := r.config.SignaturePolicyPath signaturePolicyPath := r.config.SignaturePolicyPath

View File

@ -4,9 +4,17 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
) )
// Runtime API constants
const (
// DefaultTransport is a prefix that we apply to an image name
// to check docker hub first for the image
DefaultTransport = "docker://"
)
// WriteFile writes a provided string to a provided path // WriteFile writes a provided string to a provided path
func WriteFile(content string, path string) error { func WriteFile(content string, path string) error {
baseDir := filepath.Dir(path) baseDir := filepath.Dir(path)
@ -42,3 +50,8 @@ func FuncTimer(funcName string) {
elapsed := time.Since(time.Now()) elapsed := time.Since(time.Now())
fmt.Printf("%s executed in %d ms\n", funcName, elapsed) fmt.Printf("%s executed in %d ms\n", funcName, elapsed)
} }
// hasTransport determines if the image string contains '://', returns bool
func hasTransport(image string) bool {
return strings.Contains(image, "://")
}

View File

@ -72,3 +72,15 @@ function setup() {
echo "$output" echo "$output"
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
} }
@test "kpod push without transport" {
run ${KPOD_BINARY} $KPOD_OPTIONS pull "$ALPINE"
echo "$output"
[ "$status" -eq 0 ]
# TODO: The following should fail until a registry is running in Travis CI.
run ${KPOD_BINARY} $KPOD_OPTIONS push "$ALPINE" localhost:5000/my-alpine
echo "$output"
[ "$status" -ne 0 ]
run ${KPOD_BINARY} $KPOD_OPTIONS rmi "$ALPINE"
echo "$output"
}