postgresql.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package templates
  2. import (
  3. "fmt"
  4. corev1 "k8s.io/api/core/v1"
  5. "k8s.io/apimachinery/pkg/api/resource"
  6. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  7. "k8s.io/apimachinery/pkg/util/intstr"
  8. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  9. )
  10. const (
  11. LabelKeyType = "nodb.iwanhae.kr/type"
  12. LabelValuePostgreSQL = "postgresql"
  13. )
  14. type PostgreSQLOpts struct {
  15. Name string
  16. Namespace string
  17. Tag string
  18. User string
  19. Password string
  20. Database string
  21. Memory resource.Quantity
  22. Owner metav1.Object
  23. }
  24. func PostgreSQLPod(opts PostgreSQLOpts) corev1.Pod {
  25. pod := corev1.Pod{
  26. ObjectMeta: metav1.ObjectMeta{
  27. Name: opts.Name,
  28. Namespace: opts.Namespace,
  29. Labels: map[string]string{
  30. LabelKeyType: LabelValuePostgreSQL,
  31. },
  32. },
  33. Spec: corev1.PodSpec{
  34. RestartPolicy: corev1.RestartPolicyAlways,
  35. Containers: []corev1.Container{
  36. {
  37. Name: "postgres",
  38. Image: fmt.Sprintf("postgres:%s", opts.Tag),
  39. Ports: []corev1.ContainerPort{
  40. {Name: "postgres", ContainerPort: 5432, Protocol: corev1.ProtocolTCP},
  41. },
  42. Env: []corev1.EnvVar{
  43. {Name: "POSTGRES_USER", Value: opts.User},
  44. {Name: "POSTGRES_PASSWORD", Value: opts.Password},
  45. {Name: "POSTGRES_DB", Value: opts.Database},
  46. {Name: "PGDATA", Value: "/pgdata"},
  47. },
  48. Resources: corev1.ResourceRequirements{
  49. Limits: corev1.ResourceList{
  50. corev1.ResourceMemory: opts.Memory,
  51. },
  52. },
  53. StartupProbe: &corev1.Probe{
  54. ProbeHandler: corev1.ProbeHandler{
  55. TCPSocket: &corev1.TCPSocketAction{Port: intstr.FromInt(5432)},
  56. },
  57. InitialDelaySeconds: 3,
  58. TimeoutSeconds: 5,
  59. FailureThreshold: 5,
  60. SuccessThreshold: 1,
  61. },
  62. ReadinessProbe: &corev1.Probe{
  63. ProbeHandler: corev1.ProbeHandler{
  64. TCPSocket: &corev1.TCPSocketAction{Port: intstr.FromInt(5432)},
  65. },
  66. InitialDelaySeconds: 3,
  67. TimeoutSeconds: 5,
  68. FailureThreshold: 5,
  69. SuccessThreshold: 1,
  70. },
  71. VolumeMounts: []corev1.VolumeMount{
  72. {
  73. Name: "pgdata",
  74. MountPath: "/pgdata",
  75. },
  76. },
  77. },
  78. },
  79. Volumes: []corev1.Volume{
  80. {
  81. Name: "pgdata",
  82. VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
  83. },
  84. },
  85. },
  86. }
  87. controllerutil.SetOwnerReference(opts.Owner, &pod, scheme)
  88. return pod
  89. }