From be49f7c93d8ce82f43429cfbcfcba370a3aa1814 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Fri, 24 Dec 2021 17:49:38 +0800 Subject: [PATCH] Add image filter and update README --- README.md | 11 +++++-- hack/image-filter.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100755 hack/image-filter.sh diff --git a/README.md b/README.md index b26203d..de23cc7 100644 --- a/README.md +++ b/README.md @@ -58,14 +58,21 @@ k8s.gcr.io/coredns/coredns => k8s-gcr.m.daocloud.io/coredns/coredns ## 最佳实践 * 通过 加速 安装 kubeadm ``` bash -# 使用 kubeadm 安装的时候指定 --image-repository 参数, 指定安装的镜像前缀 kubeadm config images pull --image-repository k8s-gcr.m.daocloud.io ``` * 通过 加速 安装 kind - ``` bash kind create cluster --name kind --image docker.m.daocloud.io/kindest/node:v1.22.1 ``` +* 通过 加速 部署 应用(这里以 Ingress 为例) + +``` bash +wget -o image-filter.sh https://github.com/DaoCloud/public-image-mirror/raw/main/hack/image-filter.sh && chmod +x image-filter.sh + +wget -o deploy.yaml https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/baremetal/deploy.yaml + +cat ./deploy.yaml | ./image-filter.sh | kubectl apply -f - +``` diff --git a/hack/image-filter.sh b/hack/image-filter.sh new file mode 100755 index 0000000..31c951a --- /dev/null +++ b/hack/image-filter.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +if [[ "${#}" -ne 0 && "$1" != "-" ]]; then + tmp=$(cat "${1}") +else + tmp=$(cat) +fi + +domains=( + cr.l5d.io + docker.elastic.co + docker.io + gcr.io + ghcr.io + k8s.gcr.io + mcr.microsoft.com + nvcr.io + quay.io + registry.jujucharms.com + rocks.canonical.com +) + +SUFFIX=${SUFFIX:-"m.daocloud.io"} + +function replace_domain() { + local domain="${1}" + IFS='.' read -ra arr <<<"${domain}" + local len=${#arr[@]} + if [[ "${len}" -eq 2 ]]; then + echo "${arr[0]}.${SUFFIX}" + return 0 + fi + + if [[ "${arr[0]}" == "registry" ]]; then + echo "${arr[1]}.${SUFFIX}" + return 0 + fi + if [[ "${arr[0]}" == "docker" ]]; then + echo "${arr[1]}.${SUFFIX}" + return 0 + fi + if [[ "${arr[0]}" == "cr" ]]; then + echo "${arr[1]}.${SUFFIX}" + return 0 + fi + + if [[ "${arr[0]}" =~ cr$ ]]; then + echo "${arr[0]}.${SUFFIX}" + return 0 + fi + + unset 'arr[${#arr[@]}-1]' + + IFS='-' + echo "${arr[*]}.${SUFFIX}" + + unset IFS + return 0 +} + +function replace_all() { + tmp="${1}" + for line in ${domains[*]}; do + key="${line}" + val="$(replace_domain ${line})" + if [[ "${key}" == "" || "${val}" == "" ]]; then + continue + fi + tmp="$(echo "${tmp}" | sed -e "s# ${key}# ${val}#g")" + done + + # Remove sha256 hash + tmp="$(echo "${tmp}" | sed -e "s#@sha256:[0-9a-f]\{64\}##g")" + echo "${tmp}" +} + + +replace_all "${tmp}"