-
Notifications
You must be signed in to change notification settings - Fork 55
/
utils.go
156 lines (139 loc) · 4.7 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package providers
import (
"github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1/common"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/config"
obj "github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/object"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/providers/sizing"
"github.com/RedHatInsights/clowder/controllers/cloud.redhat.com/utils"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
)
// MakeLocalDB populates the given deployment object with the local DB struct.
func MakeLocalDB(dd *apps.Deployment, nn types.NamespacedName, baseResource obj.ClowdObject, extraLabels *map[string]string, cfg *config.DatabaseConfig, image string, usePVC bool, dbName string, res *core.ResourceRequirements) {
labels := baseResource.GetLabels()
labels["service"] = "db"
for k, v := range *extraLabels {
labels[k] = v
}
labler := utils.MakeLabeler(nn, labels, baseResource)
labler(dd)
var volSource core.VolumeSource
if usePVC {
volSource = core.VolumeSource{
PersistentVolumeClaim: &core.PersistentVolumeClaimVolumeSource{
ClaimName: nn.Name,
},
}
dd.Spec.Strategy.Type = apps.RecreateDeploymentStrategyType
dd.Spec.Strategy.RollingUpdate = nil
} else {
volSource = core.VolumeSource{
EmptyDir: &core.EmptyDirVolumeSource{},
}
dd.Spec.Strategy.Type = apps.RollingUpdateDeploymentStrategyType
dd.Spec.Strategy.RollingUpdate = &apps.RollingUpdateDeployment{
MaxUnavailable: &intstr.IntOrString{
Type: intstr.String,
StrVal: "25%",
},
MaxSurge: &intstr.IntOrString{
Type: intstr.String,
StrVal: "25%",
},
}
}
dd.Spec.Replicas = common.Int32Ptr(1)
dd.Spec.Selector = &metav1.LabelSelector{MatchLabels: labels}
dd.Spec.Template.Spec.Volumes = []core.Volume{
{
Name: nn.Name,
VolumeSource: volSource,
},
}
dd.Spec.Template.ObjectMeta.Labels = labels
envVars := []core.EnvVar{
{Name: "POSTGRESQL_USER", Value: cfg.Username},
{Name: "POSTGRESQL_PASSWORD", Value: cfg.Password},
{Name: "PGPASSWORD", Value: cfg.AdminPassword}, // Legacy for old db images can likely be removed soon
{Name: "POSTGRESQL_MASTER_USER", Value: cfg.AdminUsername},
{Name: "POSTGRESQL_MASTER_PASSWORD", Value: cfg.AdminPassword},
{Name: "POSTGRESQL_DATABASE", Value: dbName},
}
ports := []core.ContainerPort{{
Name: "database",
ContainerPort: 5432,
Protocol: core.ProtocolTCP,
}}
probeHandler := core.Handler{
Exec: &core.ExecAction{
Command: []string{
"psql",
"-U",
"$(POSTGRESQL_USER)",
"-d",
"$(POSTGRESQL_DATABASE)",
"-c",
"SELECT 1",
},
},
}
livenessProbe := core.Probe{
Handler: probeHandler,
InitialDelaySeconds: 15,
TimeoutSeconds: 2,
PeriodSeconds: 10,
SuccessThreshold: 1,
FailureThreshold: 3,
}
readinessProbe := core.Probe{
Handler: probeHandler,
InitialDelaySeconds: 45,
TimeoutSeconds: 2,
PeriodSeconds: 10,
SuccessThreshold: 1,
FailureThreshold: 3,
}
requestResource := sizing.GetDefaultResourceRequirements()
if res != nil {
requestResource = *res
}
c := core.Container{
Name: nn.Name,
Image: image,
Env: envVars,
LivenessProbe: &livenessProbe,
ReadinessProbe: &readinessProbe,
Ports: ports,
Resources: requestResource,
VolumeMounts: []core.VolumeMount{{
Name: nn.Name,
MountPath: "/var/lib/pgsql/data",
}},
TerminationMessagePath: "/dev/termination-log",
TerminationMessagePolicy: core.TerminationMessageReadFile,
ImagePullPolicy: core.PullIfNotPresent,
}
dd.Spec.Template.Spec.Containers = []core.Container{c}
}
// MakeLocalDBService populates the given service object with the local DB struct.
func MakeLocalDBService(s *core.Service, nn types.NamespacedName, baseResource obj.ClowdObject, extraLabels *map[string]string) {
servicePorts := []core.ServicePort{{
Name: "database",
Port: 5432,
Protocol: "TCP",
TargetPort: intstr.FromInt(5432),
}}
labels := providers.Labels{"service": "db", "app": baseResource.GetClowdName()}
for k, v := range *extraLabels {
labels[k] = v
}
utils.MakeService(s, nn, labels, servicePorts, baseResource, false)
}
// MakeLocalDBPVC populates the given PVC object with the local DB struct.
func MakeLocalDBPVC(pvc *core.PersistentVolumeClaim, nn types.NamespacedName, baseResource obj.ClowdObject, capacity string) {
utils.MakePVC(pvc, nn, providers.Labels{"service": "db", "app": baseResource.GetClowdName()}, capacity, baseResource)
}