che-operator/pkg/deploy/deployment_postgres.go

136 lines
3.8 KiB
Go

//
// Copyright (c) 2012-2019 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package deploy
import (
orgv1 "github.com/eclipse/che-operator/pkg/apis/org/v1"
"github.com/eclipse/che-operator/pkg/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func NewPostgresDeployment(cr *orgv1.CheCluster, chePostgresPassword string, isOpenshift bool) *appsv1.Deployment {
chePostgresUser := util.GetValue(cr.Spec.Database.ChePostgresUser, "pgche")
chePostgresDb := util.GetValue(cr.Spec.Database.ChePostgresDb, "dbche")
postgresAdminPassword := util.GeneratePasswd(12)
postgresImage := util.GetValue(cr.Spec.Database.PostgresImage, DefaultPostgresImage)
pullPolicy := corev1.PullPolicy(util.GetValue(string(cr.Spec.Database.PostgresImagePullPolicy), DefaultPullPolicyFromDockerImage(postgresImage)))
name := "postgres"
labels := GetLabels(cr, name)
deployment := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "postgres",
Namespace: cr.Namespace,
Labels: labels,
},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{MatchLabels: labels},
Strategy: appsv1.DeploymentStrategy{
Type: appsv1.DeploymentStrategyType("Recreate"),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: name + "-data",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: name + "-data",
},
},
},
},
Containers: []corev1.Container{
{
Name: name,
Image: postgresImage,
ImagePullPolicy: pullPolicy,
Ports: []corev1.ContainerPort{
{
Name: name,
ContainerPort: 5432,
Protocol: "TCP",
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("512Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("1Gi"),
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: name + "-data",
MountPath: "/var/lib/pgsql/data",
},
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{
"/bin/sh",
"-i",
"-c",
"psql -h 127.0.0.1 -U " + chePostgresUser + " -q -d " + chePostgresDb + " -c 'SELECT 1'",
},
},
},
InitialDelaySeconds: 15,
FailureThreshold: 10,
SuccessThreshold: 1,
TimeoutSeconds: 5,
},
Env: []corev1.EnvVar{
{
Name: "POSTGRESQL_USER",
Value: chePostgresUser,
},
{
Name: "POSTGRESQL_PASSWORD",
Value: chePostgresPassword,
},
{
Name: "POSTGRESQL_DATABASE",
Value: chePostgresDb,
},
{
Name: "POSTGRESQL_ADMIN_PASSWORD",
Value: postgresAdminPassword,
},
}},
},
},
},
},
}
if ! isOpenshift {
var runAsUser int64 = 26
deployment.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext {
RunAsUser: &runAsUser,
FSGroup: &runAsUser,
}
}
return &deployment
}