migrate-job.yaml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Exactly-once schema migration Job (task 46).
  2. #
  3. # Run BEFORE the Deployment rolls (CI applies the Job, waits for completion,
  4. # then rolls the Deployment). `migrate` applies forward-only sqlx migrations
  5. # under Postgres advisory lock, so concurrent starts serialize and the schema
  6. # is applied exactly once — replicas MUST NOT migrate (they share one
  7. # database; per-replica migration races and double-applies). The Deployment's
  8. # readiness gate (`_sqlx_migrations` version match) keeps new replicas out of
  9. # traffic until this Job succeeds.
  10. #
  11. # Hardened like the Deployment: non-root 65532,
  12. # read-only rootfs, ALL capabilities dropped, `automountServiceAccountToken:
  13. # false`, and every secret (token pepper, TOTP key, DB/Redis passwords) mounted
  14. # as 0400 tmpfs files under `/run/secrets` (read through the `*_FILE`
  15. # variables) instead of env vars. The image carries the same placeholder
  16. # digest; `pin-digest` rewrites the real digest in `kustomization.yaml` for
  17. # every workload.
  18. apiVersion: batch/v1
  19. kind: Job
  20. metadata:
  21. name: d-oauth-migrate
  22. namespace: d-oauth
  23. labels:
  24. app.kubernetes.io/name: d-oauth-server
  25. app.kubernetes.io/part-of: d-oauth
  26. spec:
  27. backoffLimit: 3
  28. activeDeadlineSeconds: 300
  29. ttlSecondsAfterFinished: 3600
  30. template:
  31. metadata:
  32. labels:
  33. app.kubernetes.io/name: d-oauth-server
  34. app.kubernetes.io/part-of: d-oauth
  35. app.kubernetes.io/component: migrate
  36. spec:
  37. restartPolicy: Never
  38. automountServiceAccountToken: false
  39. securityContext:
  40. runAsNonRoot: true
  41. runAsUser: 65532
  42. runAsGroup: 65532
  43. fsGroup: 65532
  44. fsGroupChangePolicy: OnRootMismatch
  45. seccompProfile:
  46. type: RuntimeDefault
  47. containers:
  48. - name: migrate
  49. # Same pinning rule as the Deployment: placeholder digest, never a
  50. # shippable `:latest`; CI resolves it via `kustomization.yaml`.
  51. image: ghcr.io/d-oauth/d-oauth-server@sha256:0000000000000000000000000000000000000000000000000000000000000000
  52. imagePullPolicy: IfNotPresent
  53. # Overrides the image ENTRYPOINT (["/d-oauth-server","serve"]).
  54. command: ["/d-oauth-server", "migrate"]
  55. envFrom:
  56. - configMapRef:
  57. name: d-oauth-config
  58. env:
  59. # Same file-only secret rule as the Deployment; the DSNs come from
  60. # the ConfigMap.
  61. - name: D_OAUTH_DATABASE__PASSWORD_FILE
  62. value: /run/secrets/database-password
  63. - name: D_OAUTH_REDIS__PASSWORD_FILE
  64. value: /run/secrets/redis-password
  65. - name: D_OAUTH_TOKEN_PEPPER_FILE
  66. value: /run/secrets/token-pepper
  67. - name: D_OAUTH_TOTP_ENCRYPTION_KEY_FILE
  68. value: /run/secrets/totp-encryption-key
  69. resources:
  70. requests:
  71. cpu: 50m
  72. memory: 64Mi
  73. limits:
  74. cpu: 500m
  75. memory: 256Mi
  76. securityContext:
  77. allowPrivilegeEscalation: false
  78. readOnlyRootFilesystem: true
  79. capabilities:
  80. drop:
  81. - ALL
  82. volumeMounts:
  83. - name: certs
  84. mountPath: /certs
  85. readOnly: true
  86. - name: secrets
  87. mountPath: /run/secrets
  88. readOnly: true
  89. volumes:
  90. - name: certs
  91. secret:
  92. secretName: d-oauth-tls
  93. defaultMode: 0o400
  94. - name: secrets
  95. secret:
  96. secretName: d-oauth-secrets
  97. defaultMode: 0o400
  98. items:
  99. - key: database-password
  100. path: database-password
  101. - key: redis-password
  102. path: redis-password
  103. - key: token-pepper
  104. path: token-pepper
  105. - key: totp-encryption-key
  106. path: totp-encryption-key