-
Notifications
You must be signed in to change notification settings - Fork 94
/
highavailability.go
276 lines (243 loc) · 8.22 KB
/
highavailability.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package component
import (
"fmt"
appsv1 "github.com/openshift/api/apps/v1"
templatev1 "github.com/openshift/api/template/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type HighAvailability struct {
options []string
Options *HighAvailabilityOptions
}
type HighAvailabilityOptions struct {
nonRequiredHighAvailabilityOptions
requiredHighAvailabilityOptions
}
type requiredHighAvailabilityOptions struct {
appLabel string
apicastProductionRedisURL string
apicastStagingRedisURL string
backendRedisQueuesEndpoint string
backendRedisStorageEndpoint string
systemDatabaseURL string
systemRedisURL string
}
type nonRequiredHighAvailabilityOptions struct {
}
const (
HighlyAvailableReplicas = 2
)
var highlyAvailableExternalDatabases = map[string]bool{
"backend-redis": true,
"system-redis": true,
"system-mysql": true,
}
func NewHighAvailability(options []string) *HighAvailability {
ha := &HighAvailability{
options: options,
}
return ha
}
type HighAvailabilityOptionsProvider interface {
GetHighAvailabilityOptions() *HighAvailabilityOptions
}
type CLIHighAvailabilityOptionsProvider struct {
}
func (o *CLIHighAvailabilityOptionsProvider) GetHighAvailabilityOptions() (*HighAvailabilityOptions, error) {
hob := HighAvailabilityOptionsBuilder{}
hob.AppLabel("${APP_LABEL}")
hob.ApicastProductionRedisURL("${APICAST_PRODUCTION_REDIS_URL}")
hob.ApicastStagingRedisURL("${APICAST_STAGING_REDIS_URL}")
hob.BackendRedisQueuesEndpoint("${BACKEND_REDIS_QUEUES_ENDPOINT}")
hob.BackendRedisStorageEndpoint("${BACKEND_REDIS_STORAGE_ENDPOINT}")
hob.SystemDatabaseURL("${SYSTEM_DATABASE_URL}")
hob.SystemRedisURL("${SYSTEM_REDIS_URL}")
res, err := hob.Build()
if err != nil {
return nil, fmt.Errorf("unable to create High Availability Options - %s", err)
}
return res, nil
}
func (ha *HighAvailability) setHAOptions() {
// TODO move this outside this specific method
optionsProvider := CLIHighAvailabilityOptionsProvider{}
haOpts, err := optionsProvider.GetHighAvailabilityOptions()
_ = err
ha.Options = haOpts
}
func (ha *HighAvailability) GetObjects() ([]runtime.RawExtension, error) {
objects := ha.buildObjects()
return objects, nil
}
func (ha *HighAvailability) AssembleIntoTemplate(template *templatev1.Template, otherComponents []Component) {
ha.setHAOptions() // TODO move this outside
ha.addObjectsIntoTemplate(template)
ha.buildParameters(template)
}
func (ha *HighAvailability) addObjectsIntoTemplate(template *templatev1.Template) {
objects := ha.buildObjects()
template.Objects = append(template.Objects, objects...)
}
func (ha *HighAvailability) buildObjects() []runtime.RawExtension {
systemDatabaseSecrets := ha.createSystemDatabaseSecret()
objects := []runtime.RawExtension{
runtime.RawExtension{Object: systemDatabaseSecrets},
}
return objects
}
// TODO check how to postprocess independently of templates
func (ha *HighAvailability) PostProcess(template *templatev1.Template, otherComponents []Component) {
res := template.Objects
ha.setHAOptions() // TODO move this outside
ha.increaseReplicasNumber(res)
res = ha.deleteInternalDatabasesObjects(res)
ha.updateDatabasesURLS(res)
ha.deleteDBRelatedParameters(template)
template.Objects = res
}
func (ha *HighAvailability) PostProcessObjects(objects []runtime.RawExtension) []runtime.RawExtension {
res := objects
ha.increaseReplicasNumber(res)
res = ha.deleteInternalDatabasesObjects(res)
ha.updateDatabasesURLS(res)
return res
}
func (ha *HighAvailability) createSystemDatabaseSecret() *v1.Secret {
return &v1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: SystemSecretSystemDatabaseSecretName,
Labels: map[string]string{
"app": ha.Options.appLabel,
"threescale_component": "system",
},
},
StringData: map[string]string{
SystemSecretSystemDatabaseURLFieldName: ha.Options.systemDatabaseURL,
},
Type: v1.SecretTypeOpaque,
}
}
func (ha *HighAvailability) increaseReplicasNumber(objects []runtime.RawExtension) {
// We do not increase the number of replicas in database DeploymentConfigs
excludedDeploymentConfigs := map[string]bool{
"system-memcache": true,
"system-sphinx": true,
"zync-database": true,
}
for _, rawExtension := range objects {
obj := rawExtension.Object
dc, ok := obj.(*appsv1.DeploymentConfig)
if ok {
if _, isExcluded := excludedDeploymentConfigs[dc.Name]; !isExcluded {
dc.Spec.Replicas = HighlyAvailableReplicas
}
}
}
}
func (ha *HighAvailability) deleteInternalDatabasesObjects(objects []runtime.RawExtension) []runtime.RawExtension {
keepObjects := []runtime.RawExtension{}
for rawExtIdx, rawExtension := range objects {
switch obj := (rawExtension.Object).(type) {
case *appsv1.DeploymentConfig:
if _, ok := highlyAvailableExternalDatabases[obj.ObjectMeta.Name]; !ok {
// We create a new array and add to it the elements that will
//NOT have to be deleted
keepObjects = append(keepObjects, objects[rawExtIdx])
}
case *v1.Service:
if _, ok := highlyAvailableExternalDatabases[obj.ObjectMeta.Name]; !ok {
keepObjects = append(keepObjects, objects[rawExtIdx])
}
case *v1.PersistentVolumeClaim:
if obj.ObjectMeta.Name != "backend-redis-storage" && obj.ObjectMeta.Name != "system-redis-storage" &&
obj.ObjectMeta.Name != "mysql-storage" {
keepObjects = append(keepObjects, objects[rawExtIdx])
}
case *v1.ConfigMap:
if obj.ObjectMeta.Name != "mysql-main-conf" && obj.ObjectMeta.Name != "mysql-extra-conf" &&
obj.ObjectMeta.Name != "redis-config" {
keepObjects = append(keepObjects, objects[rawExtIdx])
}
default:
keepObjects = append(keepObjects, objects[rawExtIdx])
}
}
return keepObjects
}
func (ha *HighAvailability) deleteDBRelatedParameters(template *templatev1.Template) {
keepParams := []templatev1.Parameter{}
dbParamsToDelete := map[string]bool{
"MYSQL_IMAGE": true,
"REDIS_IMAGE": true,
"MYSQL_USER": true,
"MYSQL_PASSWORD": true,
"MYSQL_DATABASE": true,
"MYSQL_ROOT_PASSWORD": true,
}
for paramIdx := range template.Parameters {
paramName := template.Parameters[paramIdx].Name
if _, ok := dbParamsToDelete[paramName]; !ok {
keepParams = append(keepParams, template.Parameters[paramIdx])
}
}
template.Parameters = keepParams
}
func (ha *HighAvailability) updateDatabasesURLS(objects []runtime.RawExtension) {
for rawExtIdx := range objects {
obj := objects[rawExtIdx].Object
secret, ok := obj.(*v1.Secret)
if ok {
switch secret.Name {
case "system-redis":
secret.StringData["URL"] = ha.Options.systemRedisURL
case "apicast-redis":
secret.StringData["PRODUCTION_URL"] = ha.Options.apicastProductionRedisURL
secret.StringData["STAGING_URL"] = ha.Options.apicastStagingRedisURL
case "backend-redis":
secret.StringData["REDIS_STORAGE_URL"] = ha.Options.backendRedisStorageEndpoint
secret.StringData["REDIS_QUEUES_URL"] = ha.Options.backendRedisQueuesEndpoint
}
}
}
}
func (ha *HighAvailability) buildParameters(template *templatev1.Template) {
parameters := []templatev1.Parameter{
templatev1.Parameter{
Name: "BACKEND_REDIS_STORAGE_ENDPOINT",
Description: "Define the external backend-redis storage endpoint to connect to",
Required: true,
},
templatev1.Parameter{
Name: "BACKEND_REDIS_QUEUES_ENDPOINT",
Description: "Define the external backend-redis queues endpoint to connect to",
Required: true,
},
templatev1.Parameter{
Name: "SYSTEM_REDIS_URL",
Description: "Define the external system-redis to connect to",
Required: true,
},
templatev1.Parameter{
Name: "APICAST_STAGING_REDIS_URL",
Description: "Define the external apicast-staging redis to connect to",
Required: true,
},
templatev1.Parameter{
Name: "APICAST_PRODUCTION_REDIS_URL",
Description: "Define the external apicast-staging redis to connect to",
Required: true,
},
templatev1.Parameter{
Name: "SYSTEM_DATABASE_URL",
Description: "Define the external system-mysql to connect to",
Required: true,
},
}
template.Parameters = append(template.Parameters, parameters...)
}