blob: 8142855feab89dd62b9ae8111c049b8b2fa353d0 [file] [log] [blame]
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +02001# Deploy a Docker Registry in a cluster.
2
3# This needs an oauth2 secret provisioned, create with:
4# kubectl -n registry create secret generic auth --from-literal=oauth2_secret=...
5# kubectl get secrets rook-ceph-object-user-<ceph-pool>-object-registry -n <ceph-namespace> -o yaml --export | kubectl replace -f - -n registry
6
7local kube = import "../../../kube/kube.libsonnet";
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +02008
9{
10 Environment: {
11 local env = self,
12 local cfg = env.cfg,
13 cfg:: {
14 namespace: "registry",
15 domain: error "domain must be set",
16 storageClassName: error "storageClassName must be set",
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +020017 objectStoreName: error "objectStoreName must be set",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020018 },
19
20 metadata(component):: {
21 namespace: cfg.namespace,
22 labels: {
23 "app.kubernetes.io/name": "registry",
24 "app.kubernetes.io/managed-by": "kubecfg",
25 "app.kubernetes.io/component": component,
26 },
27 },
28
29 namespace: kube.Namespace(cfg.namespace),
30
Sergiusz Bazanskie31d64f2019-10-02 20:59:26 +020031 registryIssuer: kube.Issuer("registry-issuer") {
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020032 metadata+: env.metadata("registry-issuer"),
33 spec: {
34 selfSigned: {},
35 },
36 },
Sergiusz Bazanskie31d64f2019-10-02 20:59:26 +020037 authCertificate: kube.Certificate("auth") {
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020038 metadata+: env.metadata("auth"),
39 spec: {
40 secretName: "auth-internal",
41 duration: "43800h0m0s", // 5 years
42 issuerRef: {
43 name: env.registryIssuer.metadata.name,
44 },
45 commonName: "auth.registry",
46 },
47 },
Sergiusz Bazanskie31d64f2019-10-02 20:59:26 +020048 registryCertificate: kube.Certificate("registry") {
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020049 metadata+: env.metadata("registry"),
50 spec: {
51 secretName: "registry-internal",
52 duration: "43800h0m0s", // 5 years
53 issuerRef: {
54 name: env.registryIssuer.metadata.name,
55 },
56 commonName: "registry.registry",
57 },
58 },
59
60 registryConfig: kube.ConfigMap("registry-config") {
61 metadata+: env.metadata("registry-config"),
62 data: {
63 "config.yml": std.manifestYamlDoc({
64 version: "0.1",
65 log: {
Sergiusz Bazanski5f3a5e02019-09-25 02:51:51 +020066 level: "debug",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020067 fields: {
68 service: "registry",
69 },
70 },
71 storage: {
72 cache: {
73 blobdescriptor: "inmemory",
74 },
75 s3: {
Serge Bazanski3d294842020-08-04 01:34:07 +020076 regionendpoint: "https://object.ceph-waw3.hswaw.net",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020077 bucket: "registry",
Serge Bazanski3d294842020-08-04 01:34:07 +020078 region: "waw-hdd-redunant-3-object:default-placement",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020079 },
80 },
81 http: {
82 addr: ":5000",
83 headers: {
84 "X-Content-Type-Options": ["nosniff"],
85 },
86 tls: {
87 certificate: "/certs/tls.crt",
88 key: "/certs/tls.key",
89 },
90 debug: {
91 addr: "localhost:5001",
92 },
93 },
94 health: {
95 storagedriver: {
96 enabled: true,
97 interval: "10s",
98 threshold: 3,
99 },
100 },
101 auth: {
102 token: {
103 realm: "https://%s/auth" % [cfg.domain],
104 service: "my.docker.registry",
105 issuer: "%s auth server" % [cfg.domain],
106 rootcertbundle: "/authcerts/tls.crt",
107 },
108 },
109 }),
110 },
111 },
112
Serge Bazanski3d294842020-08-04 01:34:07 +0200113 authVolumeClaim: kube.PersistentVolumeClaim("auth-token-storage-3") {
114 metadata+: env.metadata("auth-token-storage-3"),
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200115 spec+: {
116 storageClassName: cfg.storageClassName,
117 accessModes: [ "ReadWriteOnce" ],
118 resources: {
119 requests: {
120 storage: "1Gi",
121 },
122 },
123 },
124 },
125
126 authConfig: kube.ConfigMap("auth-config") {
127 metadata+: env.metadata("auth-config"),
128 data: {
129 "auth_config.yml": std.manifestYamlDoc({
130 server: {
131 addr: ":5001",
132 certificate: "/certs/tls.crt",
133 key: "/certs/tls.key",
134 },
135 token: {
136 issuer: "%s auth server" % [cfg.domain],
137 expiration: 900,
138 },
139 oauth2: {
140 client_id: "registry",
141 client_secret_file: "/secrets/oauth2_secret",
142 authorize_url: "https://sso.hackerspace.pl/oauth/authorize",
143 access_token_url: "https://sso.hackerspace.pl/oauth/token",
144 profile_url: "https://sso.hackerspace.pl/api/1/profile",
145 redirect_url: "https://registry.k0.hswaw.net/oauth2",
146 username_key: "username",
147 token_db: "/data/oauth2_tokens.ldb",
148 registry_url: "https://registry.k0.hswaw.net",
149 },
150 users: {
151 [""]: {}, // '' user are anonymous users.
152 },
153 local data = self,
154 pushers:: [
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200155 { who: ["q3k", "informatic"], what: "vms/*" },
156 { who: ["q3k", "informatic"], what: "app/*" },
157 { who: ["q3k", "informatic"], what: "go/svc/*" },
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200158 { who: ["q3k"], what: "bgpwtf/*" },
159 { who: ["q3k"], what: "devtools/*" },
Sergiusz Bazanski0581bbf2020-05-11 03:21:32 +0200160 { who: ["q3k"], what: "games/factorio/*" },
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +0200161 { who: ["q3k", "informatic"], what: "cluster/*" },
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200162 ],
163 acl: [
164 {
Piotr Dobrowolski10384cd2023-06-20 00:42:15 +0200165 match: {
166 account: "/(%s)/" % std.join("|", p.who),
167 name: p.what,
168 },
169 actions: ["*"],
170 comment: "%s can push to %s" % [std.join(", ", p.who), p.what],
171 }
172 for p in data.pushers
173 ] + [
174 {
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200175 match: {account: "/.+/", name: "${account}/*"},
176 actions: ["*"],
177 comment: "Logged in users have full access to images that are in their 'namespace'",
178 },
179 {
180 match: {account: "/.+/", type: "registry", name: "catalog"},
181 actions: ["*"],
182 comment: "Logged in users can query the catalog.",
183 },
184 {
Piotr Dobrowolski0697e012020-07-02 18:32:24 +0200185 match: {account: "/.*/"},
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200186 actions: ["pull"],
187 comment: "Anyone can pull all images.",
188 },
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200189 ],
190 }),
191 }
192 },
193
194 authDeployment: kube.Deployment("auth") {
195 metadata+: env.metadata("auth"),
196 spec+: {
197 replicas: 1,
198 template+: {
199 spec+: {
200 volumes_: {
201 data: kube.PersistentVolumeClaimVolume(env.authVolumeClaim),
202 config: kube.ConfigMapVolume(env.authConfig),
203 certs: {
204 secret: { secretName: env.authCertificate.spec.secretName },
205 },
206 secrets: {
207 secret: { secretName: "auth" },
208 },
209 },
210 containers_: {
211 auth: kube.Container("auth") {
212 image: "informatic/docker_auth:2019040307",
213 volumeMounts_: {
214 config: { mountPath: "/config" },
215 certs: { mountPath: "/certs" },
216 secrets: { mountPath: "/secrets" },
217 data: { mountPath: "/data" },
218 },
219 },
220 },
221 },
222 },
223 },
224 },
225 authService: kube.Service("auth") {
226 metadata+: env.metadata("auth"),
227 target_pod:: env.authDeployment.spec.template,
228 spec+: {
229 type: "ClusterIP",
230 ports: [
231 { name: "auth", port: 5001, targetPort: 5001, protocol: "TCP" },
232 ],
233 }
234 },
235 registryDeployment: kube.Deployment("docker-registry") {
236 metadata+: env.metadata("docker-registry"),
237 spec+: {
238 replicas: 1,
239 template+: {
240 spec+: {
241 volumes_: {
242 config: kube.ConfigMapVolume(env.registryConfig),
243 certs: {
244 secret: { secretName: env.registryCertificate.spec.secretName },
245 },
246 authcerts: {
247 secret: { secretName: env.authCertificate.spec.secretName },
248 },
249 },
250 containers_: {
251 registry: kube.Container("docker-registry") {
Sergiusz Bazanski5f3a5e02019-09-25 02:51:51 +0200252 image: "registry:2.7.1",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200253 args: ["/config/config.yml"],
254 volumeMounts_: {
255 config: { mountPath: "/config" },
256 certs: { mountPath: "/certs" },
257 authcerts: { mountPath: "/authcerts" },
258 },
259 env_: {
260 REGISTRY_STORAGE_S3_ACCESSKEY: { secretKeyRef: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200261 name: "rook-ceph-object-user-%(objectStorageName)s-registry" % {objectStorageName: cfg.objectStorageName},
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200262 key: "AccessKey"
263 }},
264 REGISTRY_STORAGE_S3_SECRETKEY: { secretKeyRef: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200265 name: "rook-ceph-object-user-%(objectStorageName)s-registry" % {objectStorageName: cfg.objectStorageName},
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200266 key: "SecretKey",
267 }},
268 },
269 },
270 },
271 },
272 },
273 },
274 },
275 registryService: kube.Service("docker-registry") {
276 metadata+: env.metadata("docker-registry"),
277 target_pod:: env.registryDeployment.spec.template,
278 spec+: {
279 type: "ClusterIP",
280 ports: [
281 { name: "registry", port: 5000, targetPort: 5000, protocol: "TCP" },
282 ],
283 }
284 },
285 registryIngress: kube.Ingress("registry") {
286 metadata+: env.metadata("registry") {
287 annotations+: {
288 "kubernetes.io/tls-acme": "true",
Piotr Dobrowolski7e841062023-04-23 11:36:15 +0200289 "cert-manager.io/cluster-issuer": "letsencrypt-prod",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200290 "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
291 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
292 },
293 },
294 spec+: {
295 tls: [
296 {
297 hosts: [cfg.domain],
298 secretName: "registry-tls",
299 },
300 ],
301 rules: [
302 {
303 host: cfg.domain,
304 http: {
305 paths: [
306 { path: "/auth", backend: env.authService.name_port },
307 { path: "/", backend: env.authService.name_port },
308 { path: "/v2/", backend: env.registryService.name_port },
309 ]
310 },
311 }
312 ],
313 },
314 },
315
Sergiusz Bazanskie186c872020-02-21 12:57:02 +0100316 registryStorageUser: kube.CephObjectStoreUser("registry") {
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200317 metadata+: {
Serge Bazanski3d294842020-08-04 01:34:07 +0200318 namespace: "ceph-waw3",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200319 },
320 spec: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200321 store: cfg.objectStorageName,
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200322 displayName: "docker-registry user",
323 },
324 },
325 }
326}