package templates import ( "fmt" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) const ( LabelKeyType = "nodb.iwanhae.kr/type" LabelKeyName = "nodb.iwanhae.kr/name" LabelValuePostgreSQL = "postgresql" ) type PostgreSQLOpts struct { Name string Namespace string Tag string User string Password string Database string Memory resource.Quantity Owner metav1.Object } func PostgreSQLPod(opts PostgreSQLOpts) corev1.Pod { pod := corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: opts.Name, Namespace: opts.Namespace, Labels: map[string]string{ LabelKeyName: opts.Name, LabelKeyType: LabelValuePostgreSQL, }, }, Spec: corev1.PodSpec{ RestartPolicy: corev1.RestartPolicyAlways, Containers: []corev1.Container{ { Name: "postgres", Image: fmt.Sprintf("postgres:%s", opts.Tag), Ports: []corev1.ContainerPort{ {Name: "postgres", ContainerPort: 5432, Protocol: corev1.ProtocolTCP}, }, Env: []corev1.EnvVar{ {Name: "POSTGRES_USER", Value: opts.User}, {Name: "POSTGRES_PASSWORD", Value: opts.Password}, {Name: "POSTGRES_DB", Value: opts.Database}, {Name: "PGDATA", Value: "/pgdata"}, }, ResizePolicy: []corev1.ContainerResizePolicy{ { ResourceName: corev1.ResourceCPU, RestartPolicy: corev1.NotRequired, }, { ResourceName: corev1.ResourceMemory, RestartPolicy: corev1.RestartContainer, }, }, Resources: corev1.ResourceRequirements{ Limits: corev1.ResourceList{ corev1.ResourceMemory: opts.Memory, }, }, StartupProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ TCPSocket: &corev1.TCPSocketAction{Port: intstr.FromInt(5432)}, }, InitialDelaySeconds: 3, TimeoutSeconds: 5, FailureThreshold: 5, SuccessThreshold: 1, }, ReadinessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ TCPSocket: &corev1.TCPSocketAction{Port: intstr.FromInt(5432)}, }, InitialDelaySeconds: 3, TimeoutSeconds: 5, FailureThreshold: 5, SuccessThreshold: 1, }, VolumeMounts: []corev1.VolumeMount{ { Name: "pgdata", MountPath: "/pgdata", }, }, }, }, Volumes: []corev1.Volume{ { Name: "pgdata", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}, }, }, }, } controllerutil.SetOwnerReference(opts.Owner, &pod, scheme) return pod }