database-ss.yaml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. {{- if eq .Values.database.type "internal" -}}
  2. {{- $database := .Values.persistence.persistentVolumeClaim.database -}}
  3. apiVersion: apps/v1
  4. kind: StatefulSet
  5. metadata:
  6. name: "{{ template "harbor.database" . }}"
  7. namespace: {{ .Release.Namespace | quote }}
  8. labels:
  9. {{ include "harbor.labels" . | indent 4 }}
  10. component: database
  11. app.kubernetes.io/component: database
  12. spec:
  13. replicas: 1
  14. serviceName: "{{ template "harbor.database" . }}"
  15. selector:
  16. matchLabels:
  17. {{ include "harbor.matchLabels" . | indent 6 }}
  18. component: database
  19. template:
  20. metadata:
  21. labels:
  22. {{ include "harbor.labels" . | indent 8 }}
  23. component: database
  24. app.kubernetes.io/component: database
  25. {{- if .Values.database.podLabels }}
  26. {{ toYaml .Values.database.podLabels | indent 8 }}
  27. {{- end }}
  28. annotations:
  29. checksum/secret: {{ include (print $.Template.BasePath "/database/database-secret.yaml") . | sha256sum }}
  30. {{- if .Values.database.podAnnotations }}
  31. {{ toYaml .Values.database.podAnnotations | indent 8 }}
  32. {{- end }}
  33. spec:
  34. securityContext:
  35. runAsUser: 999
  36. fsGroup: 999
  37. {{- if .Values.database.internal.serviceAccountName }}
  38. serviceAccountName: {{ .Values.database.internal.serviceAccountName }}
  39. {{- end -}}
  40. {{- with .Values.imagePullSecrets }}
  41. imagePullSecrets:
  42. {{- toYaml . | nindent 8 }}
  43. {{- end }}
  44. automountServiceAccountToken: {{ .Values.database.internal.automountServiceAccountToken | default false }}
  45. terminationGracePeriodSeconds: 120
  46. initContainers:
  47. # with "fsGroup" set, each time a volume is mounted, Kubernetes must recursively chown() and chmod() all the files and directories inside the volume
  48. # this causes the postgresql reports the "data directory /var/lib/postgresql/data/pgdata has group or world access" issue when using some CSIs e.g. Ceph
  49. # use this init container to correct the permission
  50. # as "fsGroup" applied before the init container running, the container has enough permission to execute the command
  51. - name: "data-permissions-ensurer"
  52. image: {{ .Values.database.internal.image.repository }}:{{ .Values.database.internal.image.tag }}
  53. imagePullPolicy: {{ .Values.imagePullPolicy }}
  54. {{- if not (empty .Values.containerSecurityContext) }}
  55. securityContext: {{ .Values.containerSecurityContext | toYaml | nindent 10 }}
  56. {{- end }}
  57. command: ["/bin/sh"]
  58. args: ["-c", "chmod -R 700 /var/lib/postgresql/data/pgdata || true"]
  59. {{- if .Values.database.internal.initContainer.permissions.resources }}
  60. resources:
  61. {{ toYaml .Values.database.internal.initContainer.permissions.resources | indent 10 }}
  62. {{- end }}
  63. volumeMounts:
  64. - name: database-data
  65. mountPath: /var/lib/postgresql/data
  66. subPath: {{ $database.subPath }}
  67. {{- with .Values.database.internal.extrInitContainers }}
  68. {{- toYaml . | nindent 6 }}
  69. {{- end }}
  70. containers:
  71. - name: database
  72. image: {{ .Values.database.internal.image.repository }}:{{ .Values.database.internal.image.tag }}
  73. imagePullPolicy: {{ .Values.imagePullPolicy }}
  74. {{- if not (empty .Values.containerSecurityContext) }}
  75. securityContext: {{ .Values.containerSecurityContext | toYaml | nindent 10 }}
  76. {{- end }}
  77. livenessProbe:
  78. exec:
  79. command:
  80. - /docker-healthcheck.sh
  81. initialDelaySeconds: 300
  82. periodSeconds: 10
  83. timeoutSeconds: {{ .Values.database.internal.livenessProbe.timeoutSeconds }}
  84. readinessProbe:
  85. exec:
  86. command:
  87. - /docker-healthcheck.sh
  88. initialDelaySeconds: 1
  89. periodSeconds: 10
  90. timeoutSeconds: {{ .Values.database.internal.readinessProbe.timeoutSeconds }}
  91. {{- if .Values.database.internal.resources }}
  92. resources:
  93. {{ toYaml .Values.database.internal.resources | indent 10 }}
  94. {{- end }}
  95. envFrom:
  96. - secretRef:
  97. name: "{{ template "harbor.database" . }}"
  98. env:
  99. # put the data into a sub directory to avoid the permission issue in k8s with restricted psp enabled
  100. # more detail refer to https://github.com/goharbor/harbor-helm/issues/756
  101. - name: PGDATA
  102. value: "/var/lib/postgresql/data/pgdata"
  103. {{- with .Values.database.internal.extraEnvVars }}
  104. {{- toYaml . | nindent 10 }}
  105. {{- end }}
  106. volumeMounts:
  107. - name: database-data
  108. mountPath: /var/lib/postgresql/data
  109. subPath: {{ $database.subPath }}
  110. - name: shm-volume
  111. mountPath: /dev/shm
  112. volumes:
  113. - name: shm-volume
  114. emptyDir:
  115. medium: Memory
  116. sizeLimit: {{ .Values.database.internal.shmSizeLimit }}
  117. {{- if not .Values.persistence.enabled }}
  118. - name: "database-data"
  119. emptyDir: {}
  120. {{- else if $database.existingClaim }}
  121. - name: "database-data"
  122. persistentVolumeClaim:
  123. claimName: {{ $database.existingClaim }}
  124. {{- end -}}
  125. {{- with .Values.database.internal.nodeSelector }}
  126. nodeSelector:
  127. {{ toYaml . | indent 8 }}
  128. {{- end }}
  129. {{- with .Values.database.internal.affinity }}
  130. affinity:
  131. {{ toYaml . | indent 8 }}
  132. {{- end }}
  133. {{- with .Values.database.internal.tolerations }}
  134. tolerations:
  135. {{ toYaml . | indent 8 }}
  136. {{- end }}
  137. {{- if .Values.database.internal.priorityClassName }}
  138. priorityClassName: {{ .Values.database.internal.priorityClassName }}
  139. {{- end }}
  140. {{- if and .Values.persistence.enabled (not $database.existingClaim) }}
  141. volumeClaimTemplates:
  142. - apiVersion: v1
  143. kind: PersistentVolumeClaim
  144. metadata:
  145. name: "database-data"
  146. labels:
  147. {{ include "harbor.legacy.labels" . | indent 8 }}
  148. annotations:
  149. {{- range $key, $value := $database.annotations }}
  150. {{ $key }}: {{ $value | quote }}
  151. {{- end }}
  152. spec:
  153. accessModes: [{{ $database.accessMode | quote }}]
  154. {{- if $database.storageClass }}
  155. {{- if (eq "-" $database.storageClass) }}
  156. storageClassName: ""
  157. {{- else }}
  158. storageClassName: "{{ $database.storageClass }}"
  159. {{- end }}
  160. {{- end }}
  161. resources:
  162. requests:
  163. storage: {{ $database.size | quote }}
  164. {{- end -}}
  165. {{- end -}}