migrate-job.yaml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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, mandatory PII key, DB/Redis
  14. # passwords) mounted as 0400 tmpfs files under `/run/secrets` (read through the
  15. # `*_FILE` variables) instead of env vars. It runs the same mutable `:latest`
  16. # image reference as the Deployment.
  17. apiVersion: batch/v1
  18. kind: Job
  19. metadata:
  20. name: d-oauth-migrate
  21. namespace: d-oauth
  22. labels:
  23. app.kubernetes.io/name: d-oauth-server
  24. app.kubernetes.io/part-of: d-oauth
  25. spec:
  26. backoffLimit: 3
  27. activeDeadlineSeconds: 300
  28. ttlSecondsAfterFinished: 3600
  29. template:
  30. metadata:
  31. labels:
  32. app.kubernetes.io/name: d-oauth-server
  33. app.kubernetes.io/part-of: d-oauth
  34. app.kubernetes.io/component: migrate
  35. spec:
  36. restartPolicy: Never
  37. automountServiceAccountToken: false
  38. securityContext:
  39. runAsNonRoot: true
  40. runAsUser: 65532
  41. runAsGroup: 65532
  42. fsGroup: 65532
  43. fsGroupChangePolicy: OnRootMismatch
  44. seccompProfile:
  45. type: RuntimeDefault
  46. containers:
  47. - name: migrate
  48. # Same image reference as the Deployment: mutable `:latest`.
  49. image: ghcr.io/syavne/d-oauth-server:latest
  50. imagePullPolicy: IfNotPresent
  51. # Overrides the image ENTRYPOINT (["/d-oauth-server","serve"]).
  52. command: ["/d-oauth-server", "migrate"]
  53. envFrom:
  54. - configMapRef:
  55. name: d-oauth-config
  56. env:
  57. # Same file-only secret rule as the Deployment; the DSNs come from
  58. # the ConfigMap.
  59. - name: D_OAUTH_DATABASE__PASSWORD_FILE
  60. value: /run/secrets/database-password
  61. - name: D_OAUTH_REDIS__PASSWORD_FILE
  62. value: /run/secrets/redis-password
  63. - name: D_OAUTH_TOKEN_PEPPER_FILE
  64. value: /run/secrets/token-pepper
  65. - name: D_OAUTH_TOTP_ENCRYPTION_KEY_FILE
  66. value: /run/secrets/totp-encryption-key
  67. - name: D_OAUTH_PII_ENCRYPTION_KEY_FILE
  68. value: /run/secrets/pii-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
  107. - key: pii-encryption-key
  108. path: pii-encryption-key