blob: d9094032471975949403813f689102321813de05 [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: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +020076 regionendpoint: "https://object.ceph-waw2.hswaw.net",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020077 bucket: "registry",
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +020078 region: "waw-hdd-redunant-2-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
113 authVolumeClaim: kube.PersistentVolumeClaim("auth-token-storage") {
114 metadata+: env.metadata("auth-token-storage"),
115 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 Bazanskib13b7ff2019-08-29 20:12:24 +0200160 { who: ["q3k", "informatic"], what: "cluster/*" },
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200161 ],
162 acl: [
163 {
164 match: {account: "/.+/", name: "${account}/*"},
165 actions: ["*"],
166 comment: "Logged in users have full access to images that are in their 'namespace'",
167 },
168 {
169 match: {account: "/.+/", type: "registry", name: "catalog"},
170 actions: ["*"],
171 comment: "Logged in users can query the catalog.",
172 },
173 {
Piotr Dobrowolski0697e012020-07-02 18:32:24 +0200174 match: {account: "/.*/"},
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200175 actions: ["pull"],
176 comment: "Anyone can pull all images.",
177 },
178 ] + [
179 {
180 match: {
181 account: "/(%s)/" % std.join("|", p.who),
182 name: p.what,
183 },
184 actions: ["*"],
185 comment: "%s can push to %s" % [std.join(", ", p.who), p.what],
186 }
187 for p in data.pushers
188 ],
189 }),
190 }
191 },
192
193 authDeployment: kube.Deployment("auth") {
194 metadata+: env.metadata("auth"),
195 spec+: {
196 replicas: 1,
197 template+: {
198 spec+: {
199 volumes_: {
200 data: kube.PersistentVolumeClaimVolume(env.authVolumeClaim),
201 config: kube.ConfigMapVolume(env.authConfig),
202 certs: {
203 secret: { secretName: env.authCertificate.spec.secretName },
204 },
205 secrets: {
206 secret: { secretName: "auth" },
207 },
208 },
209 containers_: {
210 auth: kube.Container("auth") {
211 image: "informatic/docker_auth:2019040307",
212 volumeMounts_: {
213 config: { mountPath: "/config" },
214 certs: { mountPath: "/certs" },
215 secrets: { mountPath: "/secrets" },
216 data: { mountPath: "/data" },
217 },
218 },
219 },
220 },
221 },
222 },
223 },
224 authService: kube.Service("auth") {
225 metadata+: env.metadata("auth"),
226 target_pod:: env.authDeployment.spec.template,
227 spec+: {
228 type: "ClusterIP",
229 ports: [
230 { name: "auth", port: 5001, targetPort: 5001, protocol: "TCP" },
231 ],
232 }
233 },
234 registryDeployment: kube.Deployment("docker-registry") {
235 metadata+: env.metadata("docker-registry"),
236 spec+: {
237 replicas: 1,
238 template+: {
239 spec+: {
240 volumes_: {
241 config: kube.ConfigMapVolume(env.registryConfig),
242 certs: {
243 secret: { secretName: env.registryCertificate.spec.secretName },
244 },
245 authcerts: {
246 secret: { secretName: env.authCertificate.spec.secretName },
247 },
248 },
249 containers_: {
250 registry: kube.Container("docker-registry") {
Sergiusz Bazanski5f3a5e02019-09-25 02:51:51 +0200251 image: "registry:2.7.1",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200252 args: ["/config/config.yml"],
253 volumeMounts_: {
254 config: { mountPath: "/config" },
255 certs: { mountPath: "/certs" },
256 authcerts: { mountPath: "/authcerts" },
257 },
258 env_: {
259 REGISTRY_STORAGE_S3_ACCESSKEY: { secretKeyRef: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200260 name: "rook-ceph-object-user-%(objectStorageName)s-registry" % {objectStorageName: cfg.objectStorageName},
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200261 key: "AccessKey"
262 }},
263 REGISTRY_STORAGE_S3_SECRETKEY: { secretKeyRef: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200264 name: "rook-ceph-object-user-%(objectStorageName)s-registry" % {objectStorageName: cfg.objectStorageName},
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200265 key: "SecretKey",
266 }},
267 },
268 },
269 },
270 },
271 },
272 },
273 },
274 registryService: kube.Service("docker-registry") {
275 metadata+: env.metadata("docker-registry"),
276 target_pod:: env.registryDeployment.spec.template,
277 spec+: {
278 type: "ClusterIP",
279 ports: [
280 { name: "registry", port: 5000, targetPort: 5000, protocol: "TCP" },
281 ],
282 }
283 },
284 registryIngress: kube.Ingress("registry") {
285 metadata+: env.metadata("registry") {
286 annotations+: {
287 "kubernetes.io/tls-acme": "true",
288 "certmanager.k8s.io/cluster-issuer": "letsencrypt-prod",
289 "nginx.ingress.kubernetes.io/backend-protocol": "HTTPS",
290 "nginx.ingress.kubernetes.io/proxy-body-size": "0",
291 },
292 },
293 spec+: {
294 tls: [
295 {
296 hosts: [cfg.domain],
297 secretName: "registry-tls",
298 },
299 ],
300 rules: [
301 {
302 host: cfg.domain,
303 http: {
304 paths: [
305 { path: "/auth", backend: env.authService.name_port },
306 { path: "/", backend: env.authService.name_port },
307 { path: "/v2/", backend: env.registryService.name_port },
308 ]
309 },
310 }
311 ],
312 },
313 },
314
Sergiusz Bazanskie186c872020-02-21 12:57:02 +0100315 registryStorageUser: kube.CephObjectStoreUser("registry") {
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200316 metadata+: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200317 namespace: "ceph-waw2",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200318 },
319 spec: {
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +0200320 store: cfg.objectStorageName,
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +0200321 displayName: "docker-registry user",
322 },
323 },
324 }
325}