This repository has been archived by the owner on May 1, 2020. It is now read-only.
/
deployer.go
351 lines (305 loc) · 12.9 KB
/
deployer.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
Copyright (C) 2019 Synopsys, Inc.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package blackduck
import (
"fmt"
"strings"
"github.com/blackducksoftware/horizon/pkg/components"
"github.com/blackducksoftware/synopsys-operator/pkg/api"
blackduckapi "github.com/blackducksoftware/synopsys-operator/pkg/api/blackduck/v1"
"github.com/blackducksoftware/synopsys-operator/pkg/apps/blackduck/v2/containers"
"github.com/blackducksoftware/synopsys-operator/pkg/util"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
)
// GetPostgresComponents returns the blackduck postgres component list
func (hc *Creater) GetPostgresComponents(blackduck *blackduckapi.Blackduck) (*api.ComponentList, error) {
componentList := &api.ComponentList{}
// Get Containers Flavor
hubContainerFlavor, err := hc.getContainersFlavor(blackduck)
if err != nil {
return nil, err
}
containerCreater := containers.NewCreater(hc.config, hc.kubeConfig, hc.kubeClient, blackduck, hubContainerFlavor, false)
// Get Db creds
var adminPassword, userPassword, postgresPassword string
if blackduck.Spec.ExternalPostgres != nil {
adminPassword, err = util.Base64Decode(blackduck.Spec.ExternalPostgres.PostgresAdminPassword)
if err != nil {
return nil, fmt.Errorf("%v: unable to decode external Postgres adminPassword due to: %+v", blackduck.Spec.Namespace, err)
}
userPassword, err = util.Base64Decode(blackduck.Spec.ExternalPostgres.PostgresUserPassword)
if err != nil {
return nil, fmt.Errorf("%v: unable to decode external Postgres userPassword due to: %+v", blackduck.Spec.Namespace, err)
}
} else {
adminPassword, err = util.Base64Decode(blackduck.Spec.AdminPassword)
if err != nil {
return nil, fmt.Errorf("%v: unable to decode adminPassword due to: %+v", blackduck.Spec.Namespace, err)
}
userPassword, err = util.Base64Decode(blackduck.Spec.UserPassword)
if err != nil {
return nil, fmt.Errorf("%v: unable to decode userPassword due to: %+v", blackduck.Spec.Namespace, err)
}
postgresPassword, err = util.Base64Decode(blackduck.Spec.PostgresPassword)
if err != nil {
return nil, fmt.Errorf("%v: unable to decode postgresPassword due to: %+v", blackduck.Spec.Namespace, err)
}
}
postgres := containerCreater.GetPostgres()
if blackduck.Spec.ExternalPostgres == nil {
postgresDeployment, err := postgres.GetPostgresDeployment()
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, postgresDeployment)
componentList.Services = append(componentList.Services, postgres.GetPostgresService())
}
componentList.ConfigMaps = append(componentList.ConfigMaps, containerCreater.GetPostgresConfigmap())
componentList.Secrets = append(componentList.Secrets, containerCreater.GetPostgresSecret(adminPassword, userPassword, postgresPassword))
return componentList, nil
}
// GetComponents returns the blackduck components
func (hc *Creater) GetComponents(blackduck *blackduckapi.Blackduck) (*api.ComponentList, error) {
componentList := &api.ComponentList{}
// Get the flavor
flavor, err := hc.getContainersFlavor(blackduck)
if err != nil {
return nil, err
}
containerCreater := containers.NewCreater(hc.config, hc.kubeConfig, hc.kubeClient, blackduck, flavor, false)
// Configmap
componentList.ConfigMaps = append(componentList.ConfigMaps, containerCreater.GetConfigmaps()...)
//Secrets
// nginx certificate
cert, key, _ := hc.getTLSCertKeyOrCreate(blackduck)
if !hc.config.DryRun {
secret, err := util.GetSecret(hc.kubeClient, hc.config.Namespace, "blackduck-secret")
if err != nil {
log.Errorf("unable to find Synopsys Operator blackduck-secret in %s namespace due to %+v", hc.config.Namespace, err)
return nil, err
}
// if Black Duck instance level Seal Key is provided, then use it else use the operator level seal key
sealKey := secret.Data["SEAL_KEY"]
if len(blackduck.Spec.SealKey) > 0 {
sealKeyStr, err := util.Base64Decode(blackduck.Spec.SealKey)
if err != nil {
return nil, fmt.Errorf("%v: unable to decode seal key due to: %+v", blackduck.Spec.Namespace, err)
}
sealKey = []byte(sealKeyStr)
}
componentList.Secrets = append(componentList.Secrets, containerCreater.GetSecrets(cert, key, sealKey)...)
} else {
componentList.Secrets = append(componentList.Secrets, containerCreater.GetSecrets(cert, key, []byte{})...)
}
// cfssl
imageName := containerCreater.GetImageTag("blackduck-cfssl")
if len(imageName) > 0 {
cfsslDeployment, err := containerCreater.GetCfsslDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, cfsslDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetCfsslService())
}
// nginx
imageName = containerCreater.GetImageTag("blackduck-nginx")
if len(imageName) > 0 {
nginxDeployment, err := containerCreater.GetWebserverDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, nginxDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetWebServerService())
}
// documentation
imageName = containerCreater.GetImageTag("blackduck-documentation")
if len(imageName) > 0 {
documentationDeployment, err := containerCreater.GetDocumentationDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, documentationDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetDocumentationService())
}
// solr
imageName = containerCreater.GetImageTag("blackduck-solr")
if len(imageName) > 0 {
solrDeployment, err := containerCreater.GetSolrDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, solrDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetSolrService())
}
// registration
imageName = containerCreater.GetImageTag("blackduck-registration")
if len(imageName) > 0 {
registrationDeployment, err := containerCreater.GetRegistrationDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, registrationDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetRegistrationService())
}
// zookeeper
imageName = containerCreater.GetImageTag("blackduck-zookeeper")
if len(imageName) > 0 {
zookeeperDeployment, err := containerCreater.GetZookeeperDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, zookeeperDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetZookeeperService())
}
// jobRunner
imageName = containerCreater.GetImageTag("blackduck-jobrunner")
if len(imageName) > 0 {
jobRunnerDeployment, err := containerCreater.GetJobRunnerDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, jobRunnerDeployment)
}
// hub-scan
imageName = containerCreater.GetImageTag("blackduck-scan")
if len(imageName) > 0 {
scanDeployment, err := containerCreater.GetScanDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, scanDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetScanService())
}
// hub-authentication
imageName = containerCreater.GetImageTag("blackduck-authentication")
if len(imageName) > 0 {
authDeployment, err := containerCreater.GetAuthenticationDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, authDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetAuthenticationService())
}
// webapp-logstash
imageName = containerCreater.GetImageTag("blackduck-webapp")
if len(imageName) > 0 {
webappLogstashDeployment, err := containerCreater.GetWebappLogstashDeployment(imageName, containerCreater.GetImageTag("blackduck-logstash"))
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, webappLogstashDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetWebAppService())
componentList.Services = append(componentList.Services, containerCreater.GetLogStashService())
}
//Upload cache
//As part of Black Duck 2019.4.0, upload cache is part of Black Duck
imageName = containerCreater.GetImageTag("blackduck-upload-cache")
if len(imageName) > 0 {
uploadCacheDeployment, err := containerCreater.GetUploadCacheDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, uploadCacheDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetUploadCacheService())
}
if hc.isBinaryAnalysisEnabled(&blackduck.Spec) {
// Binary Scanner
imageName := containerCreater.GetImageTag("appcheck-worker")
if len(imageName) > 0 {
binaryScannerDeployment, err := containerCreater.GetBinaryScannerDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, binaryScannerDeployment)
}
// Rabbitmq
imageName = containerCreater.GetImageTag("rabbitmq")
if len(imageName) > 0 {
rabbitmqDeployment, err := containerCreater.GetRabbitmqDeployment(imageName)
if err != nil {
return nil, errors.Trace(err)
}
componentList.Deployments = append(componentList.Deployments, rabbitmqDeployment)
componentList.Services = append(componentList.Services, containerCreater.GetRabbitmqService())
}
}
// Add Expose service
if svc := hc.getExposeService(blackduck); svc != nil {
componentList.Services = append(componentList.Services, svc)
}
// Add OpenShift routes
if util.OPENSHIFT == strings.ToUpper(blackduck.Spec.ExposeService) {
route := containerCreater.GetOpenShiftRoute()
if route != nil {
componentList.Routes = []*api.Route{route}
}
}
return componentList, nil
}
func (hc *Creater) getExposeService(bd *blackduckapi.Blackduck) *components.Service {
containerCreater := containers.NewCreater(hc.config, hc.kubeConfig, hc.kubeClient, bd, nil, false)
var svc *components.Service
switch strings.ToUpper(bd.Spec.ExposeService) {
case util.NODEPORT:
svc = containerCreater.GetWebServerNodePortService()
break
case util.LOADBALANCER:
svc = containerCreater.GetWebServerLoadBalancerService()
break
default:
}
return svc
}
// GetPVC returns the PVCs
func (hc *Creater) GetPVC(blackduck *blackduckapi.Blackduck) ([]*components.PersistentVolumeClaim, error) {
containerCreater := containers.NewCreater(hc.config, hc.kubeConfig, hc.kubeClient, blackduck, nil, hc.isBinaryAnalysisEnabled(&blackduck.Spec))
return containerCreater.GetPVCs()
}
func (hc *Creater) getTLSCertKeyOrCreate(blackduck *blackduckapi.Blackduck) (string, string, error) {
if len(blackduck.Spec.Certificate) > 0 && len(blackduck.Spec.CertificateKey) > 0 {
return blackduck.Spec.Certificate, blackduck.Spec.CertificateKey, nil
}
// Cert copy
if len(blackduck.Spec.CertificateName) > 0 && !strings.EqualFold(blackduck.Spec.CertificateName, "default") {
secret, err := util.GetSecret(hc.kubeClient, blackduck.Spec.CertificateName, util.GetResourceName(blackduck.Name, util.BlackDuckName, "webserver-certificate"))
if err == nil {
cert, certok := secret.Data["WEBSERVER_CUSTOM_CERT_FILE"]
key, keyok := secret.Data["WEBSERVER_CUSTOM_KEY_FILE"]
if certok && keyok {
return string(cert), string(key), nil
}
}
}
// default cert
secret, err := util.GetSecret(hc.kubeClient, hc.config.Namespace, "blackduck-certificate")
if err == nil {
data := secret.Data
if len(data) >= 2 {
cert, certok := secret.Data["WEBSERVER_CUSTOM_CERT_FILE"]
key, keyok := secret.Data["WEBSERVER_CUSTOM_KEY_FILE"]
if !certok || !keyok {
util.DeleteSecret(hc.kubeClient, blackduck.Spec.Namespace, util.GetResourceName(blackduck.Name, util.BlackDuckName, "webserver-certificate"))
} else {
return string(cert), string(key), nil
}
}
}
// Default
return CreateSelfSignedCert()
}