Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 1 | # 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 | |
| 7 | local kube = import "../../../kube/kube.libsonnet"; |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 8 | |
| 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 Bazanski | d07861b | 2019-08-08 17:48:25 +0200 | [diff] [blame] | 17 | objectStoreName: error "objectStoreName must be set", |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 18 | }, |
| 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 Bazanski | e31d64f | 2019-10-02 20:59:26 +0200 | [diff] [blame] | 31 | registryIssuer: kube.Issuer("registry-issuer") { |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 32 | metadata+: env.metadata("registry-issuer"), |
| 33 | spec: { |
| 34 | selfSigned: {}, |
| 35 | }, |
| 36 | }, |
Sergiusz Bazanski | e31d64f | 2019-10-02 20:59:26 +0200 | [diff] [blame] | 37 | authCertificate: kube.Certificate("auth") { |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 38 | 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 Bazanski | e31d64f | 2019-10-02 20:59:26 +0200 | [diff] [blame] | 48 | registryCertificate: kube.Certificate("registry") { |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 49 | 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 Bazanski | 5f3a5e0 | 2019-09-25 02:51:51 +0200 | [diff] [blame] | 66 | level: "debug", |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 67 | fields: { |
| 68 | service: "registry", |
| 69 | }, |
| 70 | }, |
| 71 | storage: { |
| 72 | cache: { |
| 73 | blobdescriptor: "inmemory", |
| 74 | }, |
| 75 | s3: { |
Serge Bazanski | 3d29484 | 2020-08-04 01:34:07 +0200 | [diff] [blame] | 76 | regionendpoint: "https://object.ceph-waw3.hswaw.net", |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 77 | bucket: "registry", |
Serge Bazanski | 3d29484 | 2020-08-04 01:34:07 +0200 | [diff] [blame] | 78 | region: "waw-hdd-redunant-3-object:default-placement", |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 79 | }, |
| 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 Bazanski | 3d29484 | 2020-08-04 01:34:07 +0200 | [diff] [blame] | 113 | authVolumeClaim: kube.PersistentVolumeClaim("auth-token-storage-3") { |
| 114 | metadata+: env.metadata("auth-token-storage-3"), |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 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 Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 155 | { who: ["q3k", "informatic"], what: "vms/*" }, |
| 156 | { who: ["q3k", "informatic"], what: "app/*" }, |
| 157 | { who: ["q3k", "informatic"], what: "go/svc/*" }, |
Sergiusz Bazanski | d07861b | 2019-08-08 17:48:25 +0200 | [diff] [blame] | 158 | { who: ["q3k"], what: "bgpwtf/*" }, |
| 159 | { who: ["q3k"], what: "devtools/*" }, |
Sergiusz Bazanski | 0581bbf | 2020-05-11 03:21:32 +0200 | [diff] [blame] | 160 | { who: ["q3k"], what: "games/factorio/*" }, |
Sergiusz Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 161 | { who: ["q3k", "informatic"], what: "cluster/*" }, |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 162 | ], |
| 163 | acl: [ |
| 164 | { |
| 165 | match: {account: "/.+/", name: "${account}/*"}, |
| 166 | actions: ["*"], |
| 167 | comment: "Logged in users have full access to images that are in their 'namespace'", |
| 168 | }, |
| 169 | { |
| 170 | match: {account: "/.+/", type: "registry", name: "catalog"}, |
| 171 | actions: ["*"], |
| 172 | comment: "Logged in users can query the catalog.", |
| 173 | }, |
| 174 | { |
Piotr Dobrowolski | 0697e01 | 2020-07-02 18:32:24 +0200 | [diff] [blame] | 175 | match: {account: "/.*/"}, |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 176 | actions: ["pull"], |
| 177 | comment: "Anyone can pull all images.", |
| 178 | }, |
| 179 | ] + [ |
| 180 | { |
| 181 | match: { |
| 182 | account: "/(%s)/" % std.join("|", p.who), |
| 183 | name: p.what, |
| 184 | }, |
| 185 | actions: ["*"], |
| 186 | comment: "%s can push to %s" % [std.join(", ", p.who), p.what], |
| 187 | } |
| 188 | for p in data.pushers |
| 189 | ], |
| 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 Bazanski | 5f3a5e0 | 2019-09-25 02:51:51 +0200 | [diff] [blame] | 252 | image: "registry:2.7.1", |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 253 | 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 Bazanski | d07861b | 2019-08-08 17:48:25 +0200 | [diff] [blame] | 261 | name: "rook-ceph-object-user-%(objectStorageName)s-registry" % {objectStorageName: cfg.objectStorageName}, |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 262 | key: "AccessKey" |
| 263 | }}, |
| 264 | REGISTRY_STORAGE_S3_SECRETKEY: { secretKeyRef: { |
Sergiusz Bazanski | d07861b | 2019-08-08 17:48:25 +0200 | [diff] [blame] | 265 | name: "rook-ceph-object-user-%(objectStorageName)s-registry" % {objectStorageName: cfg.objectStorageName}, |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 266 | 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", |
| 289 | "certmanager.k8s.io/cluster-issuer": "letsencrypt-prod", |
| 290 | "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 Bazanski | e186c87 | 2020-02-21 12:57:02 +0100 | [diff] [blame] | 316 | registryStorageUser: kube.CephObjectStoreUser("registry") { |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 317 | metadata+: { |
Serge Bazanski | 3d29484 | 2020-08-04 01:34:07 +0200 | [diff] [blame] | 318 | namespace: "ceph-waw3", |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 319 | }, |
| 320 | spec: { |
Sergiusz Bazanski | d07861b | 2019-08-08 17:48:25 +0200 | [diff] [blame] | 321 | store: cfg.objectStorageName, |
Sergiusz Bazanski | 4d61d20 | 2019-07-21 16:56:41 +0200 | [diff] [blame] | 322 | displayName: "docker-registry user", |
| 323 | }, |
| 324 | }, |
| 325 | } |
| 326 | } |