postgresql.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. LabelKeyName = "nodb.iwanhae.kr/name"
  13. LabelValuePostgreSQL = "postgresql"
  14. )
  15. type PostgreSQLOpts struct {
  16. Name string
  17. Namespace string
  18. Tag string
  19. User string
  20. Password string
  21. Database string
  22. Memory resource.Quantity
  23. Owner metav1.Object
  24. }
  25. func PostgreSQLPod(opts PostgreSQLOpts) corev1.Pod {
  26. pod := corev1.Pod{
  27. ObjectMeta: metav1.ObjectMeta{
  28. Name: opts.Name,
  29. Namespace: opts.Namespace,
  30. Labels: map[string]string{
  31. LabelKeyName: opts.Name,
  32. LabelKeyType: LabelValuePostgreSQL,
  33. },
  34. },
  35. Spec: corev1.PodSpec{
  36. RestartPolicy: corev1.RestartPolicyAlways,
  37. Containers: []corev1.Container{
  38. {
  39. Name: "postgres",
  40. Image: fmt.Sprintf("postgres:%s", opts.Tag),
  41. Ports: []corev1.ContainerPort{
  42. {Name: "postgres", ContainerPort: 5432, Protocol: corev1.ProtocolTCP},
  43. },
  44. Env: []corev1.EnvVar{
  45. {Name: "POSTGRES_USER", Value: opts.User},
  46. {Name: "POSTGRES_PASSWORD", Value: opts.Password},
  47. {Name: "POSTGRES_DB", Value: opts.Database},
  48. {Name: "PGDATA", Value: "/pgdata"},
  49. },
  50. ResizePolicy: []corev1.ContainerResizePolicy{
  51. {
  52. ResourceName: corev1.ResourceCPU,
  53. RestartPolicy: corev1.NotRequired,
  54. },
  55. {
  56. ResourceName: corev1.ResourceMemory,
  57. RestartPolicy: corev1.RestartContainer,
  58. },
  59. },
  60. Resources: corev1.ResourceRequirements{
  61. Limits: corev1.ResourceList{
  62. corev1.ResourceMemory: opts.Memory,
  63. },
  64. },
  65. StartupProbe: &corev1.Probe{
  66. ProbeHandler: corev1.ProbeHandler{
  67. TCPSocket: &corev1.TCPSocketAction{Port: intstr.FromInt(5432)},
  68. },
  69. InitialDelaySeconds: 3,
  70. TimeoutSeconds: 5,
  71. FailureThreshold: 5,
  72. SuccessThreshold: 1,
  73. },
  74. ReadinessProbe: &corev1.Probe{
  75. ProbeHandler: corev1.ProbeHandler{
  76. TCPSocket: &corev1.TCPSocketAction{Port: intstr.FromInt(5432)},
  77. },
  78. InitialDelaySeconds: 3,
  79. TimeoutSeconds: 5,
  80. FailureThreshold: 5,
  81. SuccessThreshold: 1,
  82. },
  83. VolumeMounts: []corev1.VolumeMount{
  84. {
  85. Name: "pgdata",
  86. MountPath: "/pgdata",
  87. },
  88. },
  89. },
  90. },
  91. Volumes: []corev1.Volume{
  92. {
  93. Name: "pgdata",
  94. VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
  95. },
  96. },
  97. },
  98. }
  99. controllerutil.SetOwnerReference(opts.Owner, &pod, scheme)
  100. return pod
  101. }