blob: a791acf8f547aba6f1cd7122876f335731f788eb [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";
8local cm = import "cert-manager.libsonnet";
9
10{
11 Environment: {
12 local env = self,
13 local cfg = env.cfg,
14 cfg:: {
15 namespace: "registry",
16 domain: error "domain must be set",
17 storageClassName: error "storageClassName must be set",
Sergiusz Bazanskid07861b2019-08-08 17:48:25 +020018 objectStoreName: error "objectStoreName must be set",
Sergiusz Bazanski4d61d202019-07-21 16:56:41 +020019 },
20
21 metadata(component):: {
22 namespace: cfg.namespace,
23 labels: {
24 "app.kubernetes.io/name": "registry",
25 "app.kubernetes.io/managed-by": "kubecfg",
26 "app.kubernetes.io/component": component,
27 },
28 },
29
30 namespace: kube.Namespace(cfg.namespace),
31
32 registryIssuer: cm.Issuer("registry-issuer") {
33 metadata+: env.metadata("registry-issuer"),
34 spec: {
35 selfSigned: {},
36 },
37 },
38 authCertificate: cm.Certificate("auth") {
39 metadata+: env.metadata("auth"),
40 spec: {
41 secretName: "auth-internal",
42 duration: "43800h0m0s", // 5 years
43 issuerRef: {
44 name: env.registryIssuer.metadata.name,
45 },
46 commonName: "auth.registry",
47 },
48 },
49 registryCertificate: cm.Certificate("registry") {
50 metadata+: env.metadata("registry"),
51 spec: {
52 secretName: "registry-internal",
53 duration: "43800h0m0s", // 5 years
54 issuerRef: {
55 name: env.registryIssuer.metadata.name,
56 },
57 commonName: "registry.registry",
58 },
59 },
60
61 registryConfig: kube.ConfigMap("registry-config") {
62 metadata+: env.metadata("registry-config"),
63 data: {
64 "config.yml": std.manifestYamlDoc({
65 version: "0.1",
66 log: {
67 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 {
174 match: {account: ""},
175 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") {
251 image: "registry:2",
252 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
315 registryStorageUser: kube._Object("ceph.rook.io/v1", "CephObjectStoreUser", "registry") {
316 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}