From 7d62943178e803663b4939666377b75a21d3e537 Mon Sep 17 00:00:00 2001
From: Derek McGowan <derek@mcgstyle.net>
Date: Fri, 30 Jan 2015 14:37:44 -0800
Subject: [PATCH] Add push fallback to v1 for the official registry

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
---
 graph/push.go | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/graph/push.go b/graph/push.go
index 60f469d909..b8eebf078e 100644
--- a/graph/push.go
+++ b/graph/push.go
@@ -2,6 +2,7 @@ package graph
 
 import (
 	"bytes"
+	"errors"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -18,6 +19,8 @@ import (
 	"github.com/docker/libtrust"
 )
 
+var ErrV2RegistryUnavailable = errors.New("error v2 registry unavailable")
+
 // Retrieve the all the images to be uploaded in the correct order
 func (s *TagStore) getImageList(localRepo map[string]string, requestedTag string) ([]string, map[string][]string, error) {
 	var (
@@ -261,6 +264,10 @@ func (s *TagStore) pushV2Repository(r *registry.Session, eng *engine.Engine, out
 
 	endpoint, err := r.V2RegistryEndpoint(repoInfo.Index)
 	if err != nil {
+		if repoInfo.Index.Official {
+			log.Infof("Unable to push to V2 registry, falling back to v1: %s", err)
+			return ErrV2RegistryUnavailable
+		}
 		return fmt.Errorf("error getting registry endpoint: %s", err)
 	}
 	auth, err := r.GetV2Authorization(endpoint, repoInfo.RemoteName, false)
@@ -428,8 +435,9 @@ func (s *TagStore) CmdPush(job *engine.Job) engine.Status {
 			return engine.StatusOK
 		}
 
-		// error out, no fallback to V1
-		return job.Errorf("Error pushing to registry: %s", err)
+		if err != ErrV2RegistryUnavailable {
+			return job.Errorf("Error pushing to registry: %s", err)
+		}
 	}
 
 	if err != nil {