-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
653 lines (582 loc) · 22.6 KB
/
config.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package pkg
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"github.com/tidwall/gjson"
"golang.org/x/crypto/bcrypt"
"gopkg.in/yaml.v3"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
func (ic *InstallConfig) VerifyInstallConfig() error {
var err error
errInfo := fmt.Sprintf("verify install config error")
var fieldName, fieldValue string
validate := validator.New()
err = validate.Struct(ic)
if err != nil {
err = fmt.Errorf("validate install config error: %s", err.Error())
return err
}
fieldName = "installMode"
fieldValue = ic.InstallMode
if fieldValue != "docker" && fieldValue != "kubernetes" {
err = fmt.Errorf("%s: %s %s format error: must be docker or kubernetes", errInfo, fieldName, fieldValue)
return err
}
fieldName = "rootDir"
fieldValue = ic.RootDir
if !strings.HasPrefix(fieldValue, "/") {
err = fmt.Errorf("%s: %s %s format error: must start with /", errInfo, fieldName, fieldValue)
return err
}
if strings.HasSuffix(fieldValue, "/") {
err = fmt.Errorf("%s: %s %s format error: can not end with /", errInfo, fieldName, fieldValue)
return err
}
fieldName = "dory.namespace"
fieldValue = ic.Dory.Namespace
if strings.HasPrefix(fieldValue, "/") || strings.HasSuffix(fieldValue, "/") {
err = fmt.Errorf("%s: %s %s format error: can not start or end with /", errInfo, fieldName, fieldValue)
return err
}
if ic.Dory.GitRepo.Type != "gitlab" && ic.Dory.GitRepo.Type != "gitea" {
ic.Dory.GitRepo = GitRepo{}
}
if ic.Dory.ImageRepo.Type != "harbor" {
ic.Dory.ImageRepo = ImageRepo{}
}
if ic.Dory.ArtifactRepo.Type != "nexus" {
ic.Dory.ArtifactRepo = ArtifactRepo{}
}
if ic.Dory.ScanCodeRepo.Type != "sonarqube" {
ic.Dory.ScanCodeRepo = ScanCodeRepo{}
}
if ic.Dory.GitRepo.Internal.Image != "" && ic.Dory.GitRepo.External.ViewUrl != "" {
err = fmt.Errorf("%s: dory.gitRepo.internal and dory.gitRepo.external can not set at the same time", errInfo)
return err
}
if ic.Dory.GitRepo.Internal.Image != "" {
fieldName = "dory.gitRepo.internal.imageDB"
fieldValue = ic.Dory.GitRepo.Internal.ImageDB
if ic.Dory.GitRepo.Type == "gitea" && ic.Dory.GitRepo.Internal.ImageDB == "" {
err = fmt.Errorf("%s: %s %s format error: gitea imageDB can not be empty", errInfo, fieldName, fieldValue)
return err
}
fieldName = "dory.gitRepo.internal.imageNginx"
fieldValue = ic.Dory.GitRepo.Internal.ImageNginx
if ic.Dory.GitRepo.Type == "gitlab" && ic.Dory.GitRepo.Internal.ImageNginx == "" {
err = fmt.Errorf("%s: %s %s format error: gitlab imageNginx can not be empty", errInfo, fieldName, fieldValue)
return err
}
}
if ic.Dory.ArtifactRepo.Internal.Image != "" && ic.Dory.ArtifactRepo.External.ViewUrl != "" {
err = fmt.Errorf("%s: dory.artifactRepo.internal and dory.artifactRepo.external can not set at the same time", errInfo)
return err
}
if ic.Dory.ArtifactRepo.External.ViewUrl != "" && ic.Dory.ArtifactRepo.External.Schema != "http" && ic.Dory.ArtifactRepo.External.Schema != "https" {
err = fmt.Errorf("%s: dory.artifactRepo.external.schema must be http or https", errInfo)
return err
}
if ic.Dory.ArtifactRepo.External.ViewUrl != "" && (ic.Dory.ArtifactRepo.External.ProxyRepo.Pip == "" || ic.Dory.ArtifactRepo.External.ProxyRepo.Go == "" || ic.Dory.ArtifactRepo.External.ProxyRepo.Gradle == "" || ic.Dory.ArtifactRepo.External.ProxyRepo.Maven == "" || ic.Dory.ArtifactRepo.External.ProxyRepo.Npm == "") {
err = fmt.Errorf("%s: dory.artifactRepo.external.proxyRepo required", errInfo)
return err
}
if ic.Dory.ScanCodeRepo.Internal.Image != "" && ic.Dory.ScanCodeRepo.External.ViewUrl != "" {
err = fmt.Errorf("%s: dory.scanCodeRepo.internal and dory.scanCodeRepo.external can not set at the same time", errInfo)
return err
}
if ic.Dory.DemoDatabase.Internal.Image == "" && ic.Dory.DemoDatabase.External.DbUrl == "" {
err = fmt.Errorf("%s: dory.demoDatabase.internal and dory.demoDatabase.external both empty", errInfo)
return err
}
if ic.Dory.DemoDatabase.Internal.Image != "" && ic.Dory.DemoDatabase.External.DbUrl != "" {
err = fmt.Errorf("%s: dory.demoDatabase.internal and dory.demoDatabase.external can not set at the same time", errInfo)
return err
}
if ic.Dory.DemoHost.Internal.Image == "" && ic.Dory.DemoHost.External.HostAddr == "" {
err = fmt.Errorf("%s: dory.demoHost.internal and dory.demoHost.external both empty", errInfo)
return err
}
if ic.Dory.DemoHost.Internal.Image != "" && ic.Dory.DemoHost.External.HostAddr != "" {
err = fmt.Errorf("%s: dory.demoHost.internal and dory.demoHost.external can not set at the same time", errInfo)
return err
}
if ic.Dory.ImageRepo.Internal.Hostname != "" && ic.Dory.ImageRepo.External.Hostname != "" {
err = fmt.Errorf("%s: imageRepo.internal and imageRepo.external can not set at the same time", errInfo)
return err
}
if ic.Dory.ImageRepo.Internal.Hostname != "" {
fieldName = "imageRepo.internal.namespace"
fieldValue = ic.Dory.ImageRepo.Internal.Namespace
if strings.HasPrefix(fieldValue, "/") || strings.HasSuffix(fieldValue, "/") {
err = fmt.Errorf("%s: %s %s format error: can not start or end with /", errInfo, fieldName, fieldValue)
return err
}
if ic.InstallMode == "docker" {
fieldName = "imageRepo.internal.certsDir"
fieldValue = ic.Dory.ImageRepo.Internal.CertsDir
if fieldValue == "" {
err = fmt.Errorf("%s: %s %s format error: installMode is docker, imageRepo.internal.certsDir can not be empty", errInfo, fieldName, fieldValue)
return err
}
if strings.HasPrefix(fieldValue, "/") || strings.HasSuffix(fieldValue, "/") {
err = fmt.Errorf("%s: %s %s format error: can not start or end with /", errInfo, fieldName, fieldValue)
return err
}
fieldName = "imageRepo.internal.dataDir"
fieldValue = ic.Dory.ImageRepo.Internal.DataDir
if fieldValue == "" {
err = fmt.Errorf("%s: %s %s format error: installMode is docker, imageRepo.internal.dataDir can not be empty", errInfo, fieldName, fieldValue)
return err
}
if strings.HasPrefix(fieldValue, "/") || strings.HasSuffix(fieldValue, "/") {
err = fmt.Errorf("%s: %s %s format error: can not start or end with /", errInfo, fieldName, fieldValue)
return err
}
}
arr := strings.Split(ic.Dory.ImageRepo.Internal.Version, ".")
if len(arr) != 3 {
fieldName = "imageRepo.internal.version"
fieldValue = ic.Dory.ImageRepo.Internal.Version
err = fmt.Errorf("%s: %s %s format error: should like v2.4.0", errInfo, fieldName, fieldValue)
return err
}
arr[2] = "0"
ic.Dory.ImageRepo.Internal.VersionBig = strings.Join(arr, ".")
if ic.Dory.ImageRepo.Internal.Password == "" {
ic.Dory.ImageRepo.Internal.Password = RandomString(16, false, "=")
}
if ic.Dory.ImageRepo.Internal.RegistryPassword == "" {
ic.Dory.ImageRepo.Internal.RegistryPassword = RandomString(16, false, "")
}
bs, _ := bcrypt.GenerateFromPassword([]byte(ic.Dory.ImageRepo.Internal.RegistryPassword), 10)
ic.Dory.ImageRepo.Internal.RegistryHtpasswd = string(bs)
}
fieldName = "hostIP"
fieldValue = ic.HostIP
err = ValidateIpAddress(fieldValue)
if err != nil {
err = fmt.Errorf("%s: %s %s format error: %s", errInfo, fieldName, fieldValue, err.Error())
return err
}
if fieldValue == "127.0.0.1" || fieldValue == "localhost" {
err = fmt.Errorf("%s: %s %s format error: can not be 127.0.0.1 or localhost", errInfo, fieldName, fieldValue)
return err
}
var count int
if ic.Kubernetes.PvConfigLocal.LocalPath != "" {
count = count + 1
}
if len(ic.Kubernetes.PvConfigCephfs.CephMonitors) > 0 {
count = count + 1
}
if ic.Kubernetes.PvConfigNfs.NfsServer != "" {
count = count + 1
}
if count != 1 {
err = fmt.Errorf("%s: kubernetes.pvConfigLocal/pvConfigNfs/pvConfigCephfs must set one only", errInfo)
return err
}
if ic.Kubernetes.Runtime != "docker" && ic.Kubernetes.Runtime != "containerd" && ic.Kubernetes.Runtime != "crio" {
fieldName = "kubernetes.runtime"
fieldValue = ic.Kubernetes.Runtime
err = fmt.Errorf("%s: %s %s format error: must be docker or containerd or crio", errInfo, fieldName, fieldValue)
return err
}
if ic.Kubernetes.PvConfigLocal.LocalPath != "" {
if !strings.HasPrefix(ic.Kubernetes.PvConfigLocal.LocalPath, "/") {
fieldName = "kubernetes.pvConfigLocal.localPath"
fieldValue = ic.Kubernetes.PvConfigLocal.LocalPath
err = fmt.Errorf("%s: %s %s format error: must start with /", errInfo, fieldName, fieldValue)
return err
}
}
if len(ic.Kubernetes.PvConfigCephfs.CephMonitors) > 0 {
for _, monitor := range ic.Kubernetes.PvConfigCephfs.CephMonitors {
fieldName = "kubernetes.pvConfigCephfs.cephMonitors"
fieldValue = monitor
arr := strings.Split(monitor, ":")
if len(arr) != 2 {
err = fmt.Errorf("%s: %s %s format error: should like 192.168.0.1:6789", errInfo, fieldName, fieldValue)
return err
}
_, err = strconv.Atoi(arr[1])
if err != nil {
err = fmt.Errorf("%s: %s %s format error: should like 192.168.0.1:6789", errInfo, fieldName, fieldValue)
return err
}
}
if ic.Kubernetes.PvConfigCephfs.CephSecret == "" {
fieldName = "kubernetes.pvConfigCephfs.cephSecret"
fieldValue = ic.Kubernetes.PvConfigCephfs.CephSecret
err = fmt.Errorf("%s: %s %s format error: can not be empty", errInfo, fieldName, fieldValue)
return err
}
if ic.Kubernetes.PvConfigCephfs.CephUser == "" {
fieldName = "kubernetes.pvConfigCephfs.cephUser"
fieldValue = ic.Kubernetes.PvConfigCephfs.CephUser
err = fmt.Errorf("%s: %s %s format error: can not be empty", errInfo, fieldName, fieldValue)
return err
}
if !strings.HasPrefix(ic.Kubernetes.PvConfigCephfs.CephPath, "/") {
fieldName = "kubernetes.pvConfigCephfs.cephPath"
fieldValue = ic.Kubernetes.PvConfigCephfs.CephPath
err = fmt.Errorf("%s: %s %s format error: must start with /", errInfo, fieldName, fieldValue)
return err
}
}
if ic.Kubernetes.PvConfigNfs.NfsServer != "" {
if !strings.HasPrefix(ic.Kubernetes.PvConfigNfs.NfsPath, "/") {
fieldName = "kubernetes.pvConfigNfs.nfsPath"
fieldValue = ic.Kubernetes.PvConfigNfs.NfsPath
err = fmt.Errorf("%s: %s %s format error: must start with /", errInfo, fieldName, fieldValue)
return err
}
}
if ic.Dory.Openldap.Password == "" {
ic.Dory.Openldap.Password = RandomString(16, false, "=")
}
if ic.Dory.Redis.Password == "" {
ic.Dory.Redis.Password = RandomString(16, false, "=")
}
if ic.Dory.Mongo.Password == "" {
ic.Dory.Mongo.Password = RandomString(16, false, "=")
}
if ic.Dory.DemoDatabase.Internal.Password == "" {
ic.Dory.DemoDatabase.Internal.Password = RandomString(16, false, "=")
}
if ic.Dory.DemoDatabase.Internal.UserPassword == "" {
ic.Dory.DemoDatabase.Internal.UserPassword = RandomString(16, false, "=")
}
if ic.Dory.DemoHost.Internal.Password == "" {
ic.Dory.DemoHost.Internal.Password = RandomString(16, false, "=")
}
ic.Dory.NexusInitData = NexusInitData
return err
}
func (ic *InstallConfig) UnmarshalMapValues() (map[string]interface{}, error) {
var err error
errInfo := fmt.Sprintf("unmarshal install config to map error")
bs, _ := yaml.Marshal(ic)
vals := map[string]interface{}{}
err = yaml.Unmarshal(bs, &vals)
if err != nil {
err = fmt.Errorf("%s: %s", errInfo, err.Error())
return vals, err
}
imageRepoInternal := true
imageRepoDomainName := ic.Dory.ImageRepo.Internal.Hostname
imageRepoUsername := "admin"
imageRepoPassword := ic.Dory.ImageRepo.Internal.Password
imageRepoEmail := "admin@example.com"
imageRepoIp := ic.HostIP
if ic.Dory.ImageRepo.Internal.Hostname == "" {
imageRepoInternal = false
imageRepoDomainName = ic.Dory.ImageRepo.External.Hostname
imageRepoUsername = ic.Dory.ImageRepo.External.Username
imageRepoPassword = ic.Dory.ImageRepo.External.Password
imageRepoEmail = ic.Dory.ImageRepo.External.Email
imageRepoIp = ic.Dory.ImageRepo.External.Ip
}
vals["imageRepoInternal"] = imageRepoInternal
vals["imageRepoDomainName"] = imageRepoDomainName
vals["imageRepoUsername"] = imageRepoUsername
vals["imageRepoPassword"] = imageRepoPassword
vals["imageRepoEmail"] = imageRepoEmail
vals["imageRepoIp"] = imageRepoIp
artifactRepoInternal := true
artifactRepoViewUrl := fmt.Sprintf("%s:%d", ic.ViewURL, ic.Dory.ArtifactRepo.Internal.Port)
artifactRepoSchema := "http"
artifactRepoPort := ic.Dory.ArtifactRepo.Internal.Port
artifactRepoPortHub := ic.Dory.ArtifactRepo.Internal.PortHub
artifactRepoPortGcr := ic.Dory.ArtifactRepo.Internal.PortGcr
artifactRepoPortQuay := ic.Dory.ArtifactRepo.Internal.PortQuay
artifactRepoUsername := "admin"
artifactRepoPassword := "Nexus_Pwd_321"
artifactRepoPublicRole := "public-role"
artifactRepoPublicUser := "public-user"
artifactRepoPublicPassword := "public-user"
artifactRepoPublicEmail := "public-user@example.com"
artifactRepoIp := ic.HostIP
if ic.Dory.ArtifactRepo.Internal.Image == "" {
artifactRepoInternal = false
artifactRepoViewUrl = ic.Dory.ArtifactRepo.External.ViewUrl
artifactRepoSchema = ic.Dory.ArtifactRepo.External.Schema
artifactRepoPort = ic.Dory.ArtifactRepo.External.Port
artifactRepoPortHub = ic.Dory.ArtifactRepo.External.PortHub
artifactRepoPortGcr = ic.Dory.ArtifactRepo.External.PortGcr
artifactRepoPortQuay = ic.Dory.ArtifactRepo.External.PortQuay
artifactRepoUsername = ic.Dory.ArtifactRepo.External.Username
artifactRepoPassword = ic.Dory.ArtifactRepo.External.Password
artifactRepoPublicRole = ic.Dory.ArtifactRepo.External.PublicRole
artifactRepoPublicUser = ic.Dory.ArtifactRepo.External.PublicUser
artifactRepoPublicPassword = ic.Dory.ArtifactRepo.External.PublicPassword
artifactRepoPublicEmail = ic.Dory.ArtifactRepo.External.PublicEmail
artifactRepoIp = ic.Dory.ArtifactRepo.External.Hostname
}
vals["artifactRepoInternal"] = artifactRepoInternal
vals["artifactRepoViewUrl"] = artifactRepoViewUrl
vals["artifactRepoSchema"] = artifactRepoSchema
vals["artifactRepoPort"] = artifactRepoPort
vals["artifactRepoPortHub"] = artifactRepoPortHub
vals["artifactRepoPortGcr"] = artifactRepoPortGcr
vals["artifactRepoPortQuay"] = artifactRepoPortQuay
vals["artifactRepoUsername"] = artifactRepoUsername
vals["artifactRepoPassword"] = artifactRepoPassword
vals["artifactRepoPublicRole"] = artifactRepoPublicRole
vals["artifactRepoPublicUser"] = artifactRepoPublicUser
vals["artifactRepoPublicPassword"] = artifactRepoPublicPassword
vals["artifactRepoPublicEmail"] = artifactRepoPublicEmail
vals["artifactRepoIp"] = artifactRepoIp
scanCodeRepoInternal := true
scanCodeRepoViewUrl := fmt.Sprintf("%s:%d", ic.ViewURL, ic.Dory.ScanCodeRepo.Internal.Port)
scanCodeRepoUrl := ""
scanCodeRepoToken := "PLEASE_INPUT_BY_MANUAL"
if ic.Dory.ScanCodeRepo.Internal.Image == "" {
scanCodeRepoInternal = false
scanCodeRepoViewUrl = ic.Dory.ScanCodeRepo.External.ViewUrl
scanCodeRepoUrl = ic.Dory.ScanCodeRepo.External.Url
scanCodeRepoToken = ic.Dory.ScanCodeRepo.External.Token
} else {
if ic.InstallMode == "docker" {
scanCodeRepoUrl = fmt.Sprintf("http://%s:%d", ic.HostIP, ic.Dory.ScanCodeRepo.Internal.Port)
} else {
scanCodeRepoUrl = fmt.Sprintf("http://%s-web:9000", ic.Dory.ScanCodeRepo.Type)
}
}
vals["scanCodeRepoInternal"] = scanCodeRepoInternal
vals["scanCodeRepoViewUrl"] = scanCodeRepoViewUrl
vals["scanCodeRepoUrl"] = scanCodeRepoUrl
vals["scanCodeRepoToken"] = scanCodeRepoToken
demoDatabaseInternal := true
demoDatabaseUrl := fmt.Sprintf("jdbc:mysql://%s:%d/%s", ic.HostIP, ic.Dory.DemoDatabase.Internal.Port, ic.Dory.DemoDatabase.Internal.Database)
demoDatabaseUsername := ic.Dory.DemoDatabase.Internal.User
demoDatabasePassword := ic.Dory.DemoDatabase.Internal.UserPassword
if ic.Dory.DemoDatabase.Internal.Image == "" {
demoDatabaseInternal = false
demoDatabaseUrl = ic.Dory.DemoDatabase.External.DbUrl
demoDatabaseUsername = ic.Dory.DemoDatabase.External.DbUser
demoDatabasePassword = ic.Dory.DemoDatabase.External.DbPassword
}
vals["demoDatabaseInternal"] = demoDatabaseInternal
vals["demoDatabaseUrl"] = demoDatabaseUrl
vals["demoDatabaseUsername"] = demoDatabaseUsername
vals["demoDatabasePassword"] = demoDatabasePassword
demoHostInternal := true
demoHostAddr := ic.HostIP
demoHostPort := ic.Dory.DemoHost.Internal.NodePortSsh
demoHostUser := "root"
demoHostPassword := ic.Dory.DemoHost.Internal.Password
demoHostBecome := false
demoHostBecomeUser := ""
demoHostBecomePassword := ""
demoHostNodePortWeb := ic.Dory.DemoHost.Internal.NodePortWeb
if ic.Dory.DemoHost.Internal.Image == "" {
demoHostInternal = false
demoHostAddr = ic.Dory.DemoHost.External.HostAddr
demoHostPort = ic.Dory.DemoHost.External.HostPort
demoHostUser = ic.Dory.DemoHost.External.HostUser
demoHostPassword = ic.Dory.DemoHost.External.HostPassword
demoHostBecome = ic.Dory.DemoHost.External.HostBecome
demoHostBecomeUser = ic.Dory.DemoHost.External.HostBecomeUser
demoHostBecomePassword = ic.Dory.DemoHost.External.HostBecomePassword
demoHostNodePortWeb = ic.Dory.DemoHost.External.NodePortWeb
}
vals["demoHostInternal"] = demoHostInternal
vals["demoHostAddr"] = demoHostAddr
vals["demoHostPort"] = demoHostPort
vals["demoHostUser"] = demoHostUser
vals["demoHostPassword"] = demoHostPassword
vals["demoHostBecome"] = demoHostBecome
vals["demoHostBecomeUser"] = demoHostBecomeUser
vals["demoHostBecomePassword"] = demoHostBecomePassword
vals["demoHostNodePortWeb"] = demoHostNodePortWeb
gitWebhookUrl := "http://dory-engine:9000"
if (ic.Dory.GitRepo.Type == "gitlab" || ic.Dory.GitRepo.Type == "gitea") && ic.Dory.GitRepo.External.Url != "" && ic.Dory.GitRepo.External.GitWebhookUrl != "" {
gitWebhookUrl = ic.Dory.GitRepo.External.GitWebhookUrl
}
vals["gitWebhookUrl"] = gitWebhookUrl
return vals, err
}
func (ic *InstallConfig) HarborQuery(url, method string, param map[string]interface{}) (string, int, error) {
var err error
var strJson string
var statusCode int
var req *http.Request
var resp *http.Response
var bs []byte
client := &http.Client{
Timeout: time.Second * time.Duration(time.Second*5),
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
domainName := ic.Dory.ImageRepo.Internal.Hostname
username := "admin"
password := ic.Dory.ImageRepo.Internal.Password
if ic.Dory.ImageRepo.Internal.Hostname == "" {
domainName = ic.Dory.ImageRepo.External.Hostname
username = ic.Dory.ImageRepo.External.Username
password = ic.Dory.ImageRepo.External.Password
}
url = fmt.Sprintf("https://%s%s", domainName, url)
if len(param) > 0 {
bs, err = json.Marshal(param)
if err != nil {
return strJson, statusCode, err
}
req, err = http.NewRequest(method, url, bytes.NewReader(bs))
if err != nil {
return strJson, statusCode, err
}
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequest(method, url, nil)
if err != nil {
return strJson, statusCode, err
}
}
req.SetBasicAuth(username, password)
resp, err = client.Do(req)
if err != nil {
return strJson, statusCode, err
}
defer resp.Body.Close()
statusCode = resp.StatusCode
bs, err = ioutil.ReadAll(resp.Body)
if err != nil {
return strJson, statusCode, err
}
strJson = string(bs)
return strJson, statusCode, err
}
func (ic *InstallConfig) HarborProjectAdd(projectName string) error {
var err error
var statusCode int
var strJson string
url := fmt.Sprintf("/api/v2.0/projects")
param := map[string]interface{}{
"project_name": projectName,
"public": true,
}
strJson, statusCode, err = ic.HarborQuery(url, http.MethodPost, param)
if err != nil {
return err
}
errmsg := fmt.Sprintf("%s %s", gjson.Get(strJson, "errors.0.code").String(), gjson.Get(strJson, "errors.0.message").String())
if statusCode < http.StatusOK || statusCode >= http.StatusBadRequest {
err = fmt.Errorf(errmsg)
return err
}
return err
}
func (ic *InstallConfig) KubernetesQuery(url, method string, param map[string]interface{}) (string, int, error) {
var err error
var strJson string
var statusCode int
var req *http.Request
var resp *http.Response
var bs []byte
client := &http.Client{
Timeout: time.Second * time.Duration(time.Second*5),
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
url = fmt.Sprintf("https://%s:%d%s", ic.Kubernetes.Host, ic.Kubernetes.Port, url)
if len(param) > 0 {
bs, err = json.Marshal(param)
if err != nil {
return strJson, statusCode, err
}
req, err = http.NewRequest(method, url, bytes.NewReader(bs))
if err != nil {
return strJson, statusCode, err
}
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequest(method, url, nil)
if err != nil {
return strJson, statusCode, err
}
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ic.Kubernetes.Token))
resp, err = client.Do(req)
if err != nil {
return strJson, statusCode, err
}
defer resp.Body.Close()
statusCode = resp.StatusCode
bs, err = ioutil.ReadAll(resp.Body)
if err != nil {
return strJson, statusCode, err
}
strJson = string(bs)
return strJson, statusCode, err
}
func (ic *InstallConfig) KubernetesPodsGet(namespace string) ([]KubePod, error) {
var err error
var statusCode int
var strJson string
pods := []KubePod{}
url := fmt.Sprintf("/api/v1/namespaces/%s/pods", namespace)
param := map[string]interface{}{}
strJson, statusCode, err = ic.KubernetesQuery(url, http.MethodGet, param)
if err != nil {
return pods, err
}
errmsg := gjson.Get(strJson, "message").String()
if statusCode < http.StatusOK || statusCode >= http.StatusBadRequest {
err = fmt.Errorf(errmsg)
return pods, err
}
var podList KubePodList
err = json.Unmarshal([]byte(strJson), &podList)
if err != nil {
return pods, err
}
pods = podList.Items
return pods, err
}
func (khc *KubernetesHaCluster) VerifyKubernetesHaCluster() error {
var err error
errInfo := fmt.Sprintf("verify kubernetes ha config error")
var fieldName, fieldValue string
validate := validator.New()
err = validate.Struct(khc)
if err != nil {
err = fmt.Errorf("validate kubernetes ha config error: %s", err.Error())
return err
}
fieldName = "virtualIp"
fieldValue = khc.VirtualIp
err = ValidateIpAddress(fieldValue)
if err != nil {
err = fmt.Errorf("%s: %s %s format error: %s", errInfo, fieldName, fieldValue, err.Error())
return err
}
m := map[int]int{}
for _, host := range khc.MasterHosts {
fieldName = "masterHosts.ipAddress"
fieldValue = host.IpAddress
err = ValidateIpAddress(fieldValue)
if err != nil {
err = fmt.Errorf("%s: %s %s format error: %s", errInfo, fieldName, fieldValue, err.Error())
return err
}
_, found := m[host.KeepalivedPriority]
if found {
fieldName = "masterHosts.keepalivedPriority"
fieldValue = fmt.Sprintf("%d", host.KeepalivedPriority)
err = fmt.Errorf("%s: %s %s is duplicated", errInfo, fieldName, fieldValue)
}
m[host.KeepalivedPriority] = host.KeepalivedPriority
}
if khc.KeepAlivedAuthPass == "" {
khc.KeepAlivedAuthPass = RandomString(16, false, "=")
}
return err
}