138 lines
3.8 KiB
Go
138 lines
3.8 KiB
Go
//
|
|
// Copyright (c) 2012-2018 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 operator
|
|
|
|
import (
|
|
"github.com/operator-framework/operator-sdk/pkg/sdk"
|
|
"github.com/sirupsen/logrus"
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
corev1 "k8s.io/api/core/v1"
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"os"
|
|
)
|
|
|
|
func newPostgresDeployment() *appsv1.Deployment {
|
|
name := "postgres"
|
|
return &appsv1.Deployment{
|
|
TypeMeta: metav1.TypeMeta{
|
|
Kind: "Deployment",
|
|
APIVersion: "apps/v1",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "postgres",
|
|
Namespace: namespace,
|
|
Labels: postgresLabels,
|
|
},
|
|
Spec: appsv1.DeploymentSpec{
|
|
Selector: &metav1.LabelSelector{MatchLabels: postgresLabels},
|
|
Strategy: appsv1.DeploymentStrategy{
|
|
Type: appsv1.DeploymentStrategyType("Recreate"),
|
|
},
|
|
Template: corev1.PodTemplateSpec{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Labels: postgresLabels,
|
|
},
|
|
Spec: corev1.PodSpec{
|
|
Volumes: []corev1.Volume{
|
|
{
|
|
Name: name + "-data",
|
|
VolumeSource: corev1.VolumeSource{
|
|
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
|
|
ClaimName: name + "-data",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
Containers: []corev1.Container{
|
|
{
|
|
Name: name,
|
|
Image: "registry.access.redhat.com/rhscl/postgresql-96-rhel7:1-25",
|
|
ImagePullPolicy: corev1.PullIfNotPresent,
|
|
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,
|
|
},
|
|
}},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// CreatePostgresDeployment creates a deployment with 1 Postgres pod in spec. DB, user and password for Che are created
|
|
// via env variables while DB, user for Keycloak are provisioned in CreatePgJob
|
|
func CreatePostgresDeployment() *appsv1.Deployment {
|
|
k8s := GetK8SConfig()
|
|
deployment := newPostgresDeployment()
|
|
if err := sdk.Create(deployment); err != nil && !errors.IsAlreadyExists(err) {
|
|
logrus.Errorf("Failed to create Postgres deployment : %v", err)
|
|
logrus.Error("Operator is exiting")
|
|
os.Exit(1)
|
|
}
|
|
// wait until deployment is scaled to 1 replica to proceed with other deployments
|
|
k8s.GetDeploymentStatus(deployment)
|
|
return deployment
|
|
}
|