Skip to content

Commit 7cfe182

Browse files
committed
feat: added support for pod selector and namespace support.
1 parent 1e2d4b3 commit 7cfe182

File tree

11 files changed

+337
-256
lines changed

11 files changed

+337
-256
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,3 @@ go.work
2323
.idea/
2424
.vscode/
2525
bin/
26-
27-
tls.crt
28-
tls.key

Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
DOCKER_IMAGE := ketches/registry-proxy
22
ALIYUN_IMAGE := registry.cn-hangzhou.aliyuncs.com/ketches/registry-proxy
3-
VERSION := v1.2.0
3+
VERSION := v1.2.1
44

55
.PHONY: build
66
build:
77
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/amd64/registry-proxy main.go
88
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/arm64/registry-proxy main.go
9-
- docker buildx use gobuilder
10-
- docker buildx create --use --name gobuilder
11-
docker buildx build --platform linux/amd64,linux/arm64 -t $(DOCKER_IMAGE):$(VERSION) -t $(ALIYUN_IMAGE):$(VERSION) --push .
9+
docker buildx create --use --name gobuilder 2>/dev/null || docker buildx use gobuilder
10+
docker buildx build --platform linux/amd64,linux/arm64 -t $(DOCKER_IMAGE):$(VERSION) -t $(ALIYUN_IMAGE):$(VERSION) --push .
1211

1312
.PHONY: deploy
1413
deploy:

README.md

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@
55

66
在 Kubernetes 集群中部署 Registry Proxy,自动帮助您使用镜像代理服务拉取新创建的 Pod 中的外网容器镜像(仅限公有镜像)。
77

8-
**适用场景**
8+
**适用场景**
99

10-
1. 无法拉取例如 K8s (registry.k8s.io) 、谷歌 (gcr.io) 等镜像;
11-
2. 龟速拉取例如 GitHub(ghcr.io)、RedHat(quay.io) 等镜像;
10+
1. 无法拉取例如 K8s (`registry.k8s.io`) 、谷歌 (`gcr.io`) 等镜像;
11+
2. 龟速拉取例如 GitHub(`ghcr.io`)、RedHat(`quay.io`) 等镜像;
1212

13-
**代理清单**
13+
**代理清单**
1414

1515
默认镜像代理服务支持的外网镜像仓库:
1616

17-
- docker.io
18-
- registry.k8s.io
19-
- quay.io
20-
- ghcr.io
21-
- gcr.io
22-
- k8s.gcr.io
23-
- docker.cloudsmith.io
17+
- `docker.io`
18+
- `registry.k8s.io`
19+
- `quay.io`
20+
- `ghcr.io`
21+
- `gcr.io`
22+
- `k8s.gcr.io`
23+
- `docker.cloudsmith.io`
2424

2525
## 快速安装
2626

27-
执行以下命令安装 registry-proxyv1.1.0+ 版本):
27+
执行以下命令安装 `registry-proxy``v1.1.0+` 版本):
2828

2929
```bash
3030
export VERSION=$(curl -s https://api.github.com/repos/ketches/registry-proxy/releases/latest | jq -r .tag_name)
@@ -37,9 +37,9 @@ kubectl apply -f https://ghproxy.ketches.cn/https://raw.githubusercontent.com/ke
3737

3838
## 配置
3939

40-
registry-proxy 安装后自动创建 ConfigMap `registry-proxy-config`,ConfigMap 内容为默认配置,可以通过修改 ConfigMap 来修改默认配置。
40+
`registry-proxy` 安装后自动创建 ConfigMap `registry-proxy-config`,ConfigMap 内容为默认配置,可以通过修改 ConfigMap 来修改默认配置。
4141

42-
默认配置
42+
### 默认配置
4343

4444
```yaml
4545
apiVersion: v1
@@ -65,28 +65,53 @@ data:
6565
- registry-proxy
6666
includeNamespaces:
6767
- *
68+
podSelector: {}
69+
namespaceSelector: {}
6870
```
6971
70-
> Notes:
72+
### 配置说明
73+
74+
**enabled:**
75+
76+
是否开启 registry-proxy 代理功能,boolean 类型,默认为 `true`, 可选值为 `true` 或 `false`;
77+
78+
**proxies:**
79+
80+
镜像代理地址,键为需要代理的镜像地址,值为代理地址,键值对形式,默认使用 [ketches/cloudflare-registry-proxy](https://github.com/ketches/cloudflare-registry-proxy) 镜像代理服务;
81+
82+
> Note:
7183
>
72-
> 1. 默认使用 [ketches/cloudflare-registry-proxy](https://github.com/ketches/cloudflare-registry-proxy) 镜像代理服务;
73-
> 2. `enabled` 为 `true` 时,开启 registry-proxy 代理功能,为 `false` 时,关闭 registry-proxy 代理功能;
74-
> 2. 默认排除 `kube-system`、`kube-public`、`kube-node-lease`、`registry-proxy` 命名空间下的 Pod 容器镜像代理;
75-
> 3. 修改上述配置实时生效,无需重启 registry-proxy;
76-
> 4. 可以自定义代理地址,例如:`docker.io: docker.m.daocloud.io`;
77-
> 5. 可以去除代理地址,免去代理;
78-
> 6. 可以增加代理地址,例如:`mcr.microsoft.com: mcr.dockerproxy.com`;
79-
> 7. 可以通过向 [ketches/cloudflare-registry-proxy](https://github.com/ketches/cloudflare-registry-proxy) 项目 [提交 Issue](https://github.com/ketches/cloudflare-registry-proxy/issues/new) 来申请添加新的国外镜像代理服务
84+
> 可以通过向 [ketches/cloudflare-registry-proxy](https://github.com/ketches/cloudflare-registry-proxy) 项目 [提交 Issue](https://github.com/ketches/cloudflare-registry-proxy/issues/new) 来申请添加新的国外镜像代理服务
85+
86+
**excludeNamespaces:**
87+
88+
排除的命名空间,数组形式,默认排除 `kube-system`、`kube-public`、`kube-node-lease`、`registry-proxy` 命名空间下的 Pod 容器镜像代理;
89+
90+
**includeNamespaces:**
91+
92+
包含的命名空间,数组形式,默认 (`*`) 来包含被排除以外的所有命名空间下的 Pod 容器镜像代理;
93+
94+
**podSelector:**
95+
96+
Pod 选择器,键值对形式,默认为空,支持 Pod 选择器,例如:`app: nginx`;
97+
98+
**namespaceSelector:**
99+
100+
命名空间选择器,键值对形式,默认为空,支持命名空间选择器,例如:`owner: johndoe`;
80101

81102
## 实现原理
82103

83104
使用 Mutating Webhook 准入控制器实现。 当集群中 Pod 创建时,Mutating Webhook 的工作流程如下:
84105

85-
1. 判断 Pod 是否属于排除的命名空间,如果是,结束流程;
86-
2. 判断 Pod 是否属于包含的命名空间,如果不是,结束流程;
87-
3. 依次判断 Pod 中的容器镜像是否匹配代理仓库,如果是,替换为代理镜像;
106+
1. 当 Pod 创建时,Mutating Webhook 会拦截请求;
107+
2. 通过 `enabled` 参数判断是否开启代理功能,如果开启,继续流程;
108+
3. 通过 `excludeNamespaces` 和 `includeNamespaces` 参数判断 Pod 所在命名空间是否需要代理,如果是,继续流程;
109+
4. 通过 `namespaceSelector` 参数命名空间标签,如果匹配,继续流程;
110+
5. 通过 `podSelector` 参数匹配 Pod 标签,如果匹配,继续流程;
111+
6. 通过 `proxies` 参数判断 Pod 中的容器镜像是否需要代理,如果是,继续流程;
112+
7. 通过 `proxies` 参数替换容器镜像地址,完成代理,Mutating Webhook 响应请求,结束流程。
88113

89-
![202311071243391](https://fs.poneding.com/images/202311071243391.png)
114+
![202410211328755.png](https://images.poneding.com/2024/10/202410211328755.png)
90115

91116
## 使用示例
92117

@@ -99,7 +124,7 @@ kubectl apply -f https://raw.githubusercontent.com/ketches/registry-proxy/$VERSI
99124
kubectl apply -f https://ghproxy.ketches.cn/https://raw.githubusercontent.com/ketches/registry-proxy/$VERSION/examples/dockerhub-nginx.yaml
100125
```
101126

102-
示例中的 Pod 镜像为 `nginx:latest`,经过 registry-proxy 自动代理后,容器镜像变为 `docker.ketches.cn/library/nginx:latest`。
127+
示例中的 Pod 镜像为 `nginx:latest`,经过 `registry-proxy` 自动代理后,容器镜像变为 `docker.ketches.cn/library/nginx:latest`。
103128

104129
验证:
105130

@@ -109,7 +134,7 @@ kubectl get pod dockerhub-nginx -o=jsonpath='{.spec.containers[*].image}'
109134

110135
## 卸载&清理
111136

112-
**卸载 registry-proxy**:
137+
**卸载**:
113138

114139
```bash
115140
# uninstall v1.0.0 version for example
@@ -131,22 +156,13 @@ kubectl delete -f https://ghproxy.ketches.cn/https://raw.githubusercontent.com/k
131156

132157
## 代理参考
133158

134-
### Docker Hub 镜像代理
135-
136-
常规镜像代理
137-
138-
- ketches/registry-proxy:latest => docker.ketches.cn/ketches/registry-proxy:latest
139-
140-
根镜像代理
141-
142-
- nginx:latest => docker.ketches.cn/library/nginx:latest
143-
144-
### Kubernetes 镜像代理
145-
146-
常规镜像代理
147-
148-
- registry.k8s.io/ingress-nginx/controller:v1.8.2 => k8s.ketches.cn/ingress-nginx/controller:v1.8.2
159+
| 镜像地址 | 代理地址 |
160+
| --- | --- |
161+
| nginx:latest | docker.ketches.cn/library/nginx:latest |
162+
| ketches/registry-proxy:latest | docker.ketches.cn/ketches/registry-proxy:latest |
163+
| registry.k8s.io/pause:3.9 | k8s.ketches.cn/pause:3.9 |
164+
| registry.k8s.io/ingress-nginx/controller:v1.8.2 | k8s.ketches.cn/ingress-nginx/controller:v1.8.2 |
149165

150-
根镜像代理
166+
## ✨ Stars
151167

152-
- registry.k8s.io/pause:3.9 => k8s.ketches.cn/pause:3.9
168+
[![Stars](https://starchart.cc/ketches/registry-proxy.svg)](https://starchart.cc/ketches/registry-proxy)

deploy/manifests.yaml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ kind: ClusterRole
1616
metadata:
1717
name: registry-proxy
1818
rules:
19-
- apiGroups: [""]
20-
resources: ["namespaces", "configmaps", "secrets"]
21-
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
22-
- apiGroups: ["admissionregistration.k8s.io"]
23-
resources: ["mutatingwebhookconfigurations"]
24-
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
19+
- apiGroups: [""]
20+
resources: ["namespaces", "configmaps", "secrets"]
21+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
22+
- apiGroups: ["admissionregistration.k8s.io"]
23+
resources: ["mutatingwebhookconfigurations"]
24+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
2525

2626
---
2727
apiVersion: rbac.authorization.k8s.io/v1
@@ -33,9 +33,9 @@ roleRef:
3333
kind: ClusterRole
3434
name: registry-proxy
3535
subjects:
36-
- kind: ServiceAccount
37-
name: registry-proxy
38-
namespace: registry-proxy
36+
- kind: ServiceAccount
37+
name: registry-proxy
38+
namespace: registry-proxy
3939

4040
---
4141
apiVersion: apps/v1
@@ -54,18 +54,18 @@ spec:
5454
spec:
5555
serviceAccountName: registry-proxy
5656
containers:
57-
- name: registry-proxy
58-
image: registry.cn-hangzhou.aliyuncs.com/ketches/registry-proxy:v1.2.0
59-
imagePullPolicy: Always
60-
resources:
61-
requests:
62-
memory: "64Mi"
63-
cpu: "50m"
64-
limits:
65-
memory: "256Mi"
66-
cpu: "200m"
67-
ports:
68-
- containerPort: 443
57+
- name: registry-proxy
58+
image: registry.cn-hangzhou.aliyuncs.com/ketches/registry-proxy:v1.2.1
59+
imagePullPolicy: Always
60+
resources:
61+
requests:
62+
memory: "64Mi"
63+
cpu: "50m"
64+
limits:
65+
memory: "256Mi"
66+
cpu: "200m"
67+
ports:
68+
- containerPort: 443
6969

7070
---
7171
apiVersion: v1
@@ -77,6 +77,6 @@ spec:
7777
selector:
7878
app: registry-proxy
7979
ports:
80-
- port: 443
81-
targetPort: 443
82-
type: ClusterIP
80+
- port: 443
81+
targetPort: 443
82+
type: ClusterIP

examples/dockerhub-nginx.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ spec:
1111
resources:
1212
limits:
1313
memory: "256Mi"
14-
cpu: "200m"
14+
cpu: "200m"

go.mod

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,66 @@
11
module github.com/ketches/registry-proxy
22

3-
go 1.21.3
3+
go 1.23.2
44

55
require (
66
github.com/containers/image v3.0.2+incompatible
77
gopkg.in/yaml.v3 v3.0.1
8-
k8s.io/api v0.28.2
9-
k8s.io/apimachinery v0.28.2
10-
k8s.io/client-go v0.28.2
11-
sigs.k8s.io/controller-runtime v0.16.2
8+
k8s.io/api v0.31.1
9+
k8s.io/apimachinery v0.31.1
10+
k8s.io/client-go v0.31.1
11+
sigs.k8s.io/controller-runtime v0.19.0
1212
)
1313

1414
require (
1515
github.com/beorn7/perks v1.0.1 // indirect
16-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
17-
github.com/davecgh/go-spew v1.1.1 // indirect
18-
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
19-
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
20-
github.com/fsnotify/fsnotify v1.6.0 // indirect
21-
github.com/go-logr/logr v1.2.4 // indirect
16+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
17+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
18+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
19+
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
20+
github.com/fsnotify/fsnotify v1.7.0 // indirect
21+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
22+
github.com/go-logr/logr v1.4.2 // indirect
2223
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2324
github.com/go-openapi/jsonreference v0.20.2 // indirect
24-
github.com/go-openapi/swag v0.22.3 // indirect
25+
github.com/go-openapi/swag v0.22.4 // indirect
2526
github.com/gogo/protobuf v1.3.2 // indirect
2627
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
27-
github.com/golang/protobuf v1.5.3 // indirect
28+
github.com/golang/protobuf v1.5.4 // indirect
2829
github.com/google/gnostic-models v0.6.8 // indirect
29-
github.com/google/go-cmp v0.5.9 // indirect
30+
github.com/google/go-cmp v0.6.0 // indirect
3031
github.com/google/gofuzz v1.2.0 // indirect
31-
github.com/google/uuid v1.3.0 // indirect
32+
github.com/google/uuid v1.6.0 // indirect
3233
github.com/imdario/mergo v0.3.6 // indirect
3334
github.com/josharian/intern v1.0.0 // indirect
3435
github.com/json-iterator/go v1.1.12 // indirect
3536
github.com/mailru/easyjson v0.7.7 // indirect
36-
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
3737
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3838
github.com/modern-go/reflect2 v1.0.2 // indirect
3939
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4040
github.com/opencontainers/go-digest v1.0.0 // indirect
4141
github.com/pkg/errors v0.9.1 // indirect
42-
github.com/prometheus/client_golang v1.16.0 // indirect
43-
github.com/prometheus/client_model v0.4.0 // indirect
44-
github.com/prometheus/common v0.44.0 // indirect
45-
github.com/prometheus/procfs v0.10.1 // indirect
42+
github.com/prometheus/client_golang v1.19.1 // indirect
43+
github.com/prometheus/client_model v0.6.1 // indirect
44+
github.com/prometheus/common v0.55.0 // indirect
45+
github.com/prometheus/procfs v0.15.1 // indirect
4646
github.com/spf13/pflag v1.0.5 // indirect
47-
github.com/stretchr/testify v1.8.3 // indirect
48-
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
49-
golang.org/x/net v0.13.0 // indirect
50-
golang.org/x/oauth2 v0.8.0 // indirect
51-
golang.org/x/sys v0.11.0 // indirect
52-
golang.org/x/term v0.10.0 // indirect
53-
golang.org/x/text v0.11.0 // indirect
47+
github.com/x448/float16 v0.8.4 // indirect
48+
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
49+
golang.org/x/net v0.26.0 // indirect
50+
golang.org/x/oauth2 v0.21.0 // indirect
51+
golang.org/x/sys v0.21.0 // indirect
52+
golang.org/x/term v0.21.0 // indirect
53+
golang.org/x/text v0.16.0 // indirect
5454
golang.org/x/time v0.3.0 // indirect
5555
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
56-
google.golang.org/appengine v1.6.7 // indirect
57-
google.golang.org/protobuf v1.30.0 // indirect
56+
google.golang.org/protobuf v1.34.2 // indirect
5857
gopkg.in/inf.v0 v0.9.1 // indirect
5958
gopkg.in/yaml.v2 v2.4.0 // indirect
60-
k8s.io/apiextensions-apiserver v0.28.0 // indirect
61-
k8s.io/component-base v0.28.1 // indirect
62-
k8s.io/klog/v2 v2.100.1 // indirect
63-
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
64-
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
59+
k8s.io/apiextensions-apiserver v0.31.0 // indirect
60+
k8s.io/klog/v2 v2.130.1 // indirect
61+
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
62+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
6563
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
66-
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
67-
sigs.k8s.io/yaml v1.3.0 // indirect
64+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
65+
sigs.k8s.io/yaml v1.4.0 // indirect
6866
)

0 commit comments

Comments
 (0)