Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docker hub proxy #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions docker_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@
# gcr.io/xxx/yyy:zzz -> gcr.azk8s.cn/xxx/yyy:zzz, for example gcr.io/google_containers/kube-apiserver:v1.14.1
# k8s.gcr.io/xxx:yyy => gcr.io/google-containers/xxx:yyy -> gcr.azk8s.cn/google-containers/xxx:yyy, for example k8s.gcr.io/kube-apiserver:v1.14.1
# quay.io/xxx/yyy:zzz -> quay.azk8s.cn/xxx/yyy:zzz, for example quay.io/coreos/flannel:v0.10.0-amd64
# xxx:yyy -> dockerhub.azk8s.cn/library/xxx:yyy

converts = [
{
'prefix': 'gcr.io',
'replace': lambda x: x.replace('gcr.io', 'gcr.azk8s.cn'),
},
{
'prefix': 'k8s.gcr.io',
'replace': lambda x: x.replace('k8s.gcr.io', 'gcr.azk8s.cn/google-containers'),
},
{
'prefix': 'quay.io',
'replace': lambda x: x.replace('quay.io', 'quay.azk8s.cn'),
}
]
converts = {
'gcr.io': 'gcr.azk8s.cn',
'k8s.gcr.io': 'gcr.azk8s.cn/google-containers',
'quay.io': 'quay.azk8s.cn',
}

def proxy_image(image):
repo = image.split("/")[0]
if repo in converts:
return image.replace(repo, converts[repo])
# docker hub
return 'dockerhub.azk8s.cn/library/' + image

def execute_sys_cmd(cmd):
result = os.system(cmd)
Expand All @@ -39,24 +38,18 @@ def usage():
sys.exit(-1)

image = sys.argv[2]
imageArray = image.split("/")

newImage = ''
for cvt in converts:
if imageArray[0] == cvt['prefix']:
newImage = cvt['replace'](image)
break
newImage = proxy_image(image)
if newImage:
print("-- pull {image} from {newimage} instead --".format(image=image, newimage=newImage))
cmd = "docker pull {image}".format(image=newImage)
execute_sys_cmd(cmd)

cmd = "docker tag {newImage} {image}".format(newImage=newImage, image=image)
execute_sys_cmd(cmd)

cmd = "docker rmi {newImage}".format(newImage=newImage)
execute_sys_cmd(cmd)

print("-- pull {image} done --".format(image=image))
sys.exit(0)
else:
Expand Down