-
Notifications
You must be signed in to change notification settings - Fork 465
/
GitOpsConfigService.go
522 lines (485 loc) · 16.3 KB
/
GitOpsConfigService.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
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed 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 gitops
import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/client/argocdServer"
"github.com/devtron-labs/devtron/internal/sql/models"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/cluster"
"github.com/devtron-labs/devtron/pkg/pipeline"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/ghodss/yaml"
"github.com/go-pg/pg"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/api/errors"
"net/http"
"strings"
"time"
)
type GitOpsConfigService interface {
CreateGitOpsConfig(config *GitOpsConfigDto) (*GitOpsConfigDto, error)
UpdateGitOpsConfig(config *GitOpsConfigDto) error
GetGitOpsConfigById(id int) (*GitOpsConfigDto, error)
GetAllGitOpsConfig() ([]*GitOpsConfigDto, error)
GetGitOpsConfigByProvider(provider string) (*GitOpsConfigDto, error)
GetGitOpsConfigActive() (*GitOpsConfigDto, error)
}
type GitOpsConfigDto struct {
Id int `json:"id,omitempty"`
Provider string `json:"provider"`
Username string `json:"username"`
Token string `json:"token"`
GitLabGroupId string `json:"gitLabGroupId"`
GitHubOrgId string `json:"gitHubOrgId"`
Host string `json:"host"`
Active bool `json:"active"`
AzureProjectName string `json:"azureProjectName"`
UserId int32 `json:"-"`
}
const GitOpsSecretName = "devtron-gitops-secret"
type GitOpsConfigServiceImpl struct {
logger *zap.SugaredLogger
gitOpsRepository repository.GitOpsConfigRepository
K8sUtil *util.K8sUtil
aCDAuthConfig *user.ACDAuthConfig
clusterService cluster.ClusterService
envService cluster.EnvironmentService
versionService argocdServer.VersionService
gitFactory *util.GitFactory
}
func NewGitOpsConfigServiceImpl(Logger *zap.SugaredLogger, ciHandler pipeline.CiHandler,
gitOpsRepository repository.GitOpsConfigRepository, K8sUtil *util.K8sUtil, aCDAuthConfig *user.ACDAuthConfig,
clusterService cluster.ClusterService, envService cluster.EnvironmentService, versionService argocdServer.VersionService,
gitFactory *util.GitFactory) *GitOpsConfigServiceImpl {
return &GitOpsConfigServiceImpl{
logger: Logger,
gitOpsRepository: gitOpsRepository,
K8sUtil: K8sUtil,
aCDAuthConfig: aCDAuthConfig,
clusterService: clusterService,
envService: envService,
versionService: versionService,
gitFactory: gitFactory,
}
}
func (impl *GitOpsConfigServiceImpl) CreateGitOpsConfig(request *GitOpsConfigDto) (*GitOpsConfigDto, error) {
impl.logger.Debugw("gitops create request", "req", request)
dbConnection := impl.gitOpsRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return nil, err
}
// Rollback tx on error.
defer tx.Rollback()
existingModel, err := impl.gitOpsRepository.GetGitOpsConfigActive()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in creating new gitops config", "error", err)
return nil, err
}
if existingModel != nil && existingModel.Id > 0 {
existingModel.Active = false
existingModel.UpdatedOn = time.Now()
existingModel.UpdatedBy = request.UserId
err = impl.gitOpsRepository.UpdateGitOpsConfig(existingModel, tx)
if err != nil {
impl.logger.Errorw("error in creating new gitops config", "error", err)
return nil, err
}
}
model := &repository.GitOpsConfig{
Provider: strings.ToUpper(request.Provider),
Username: request.Username,
Token: request.Token,
GitHubOrgId: request.GitHubOrgId,
GitLabGroupId: request.GitLabGroupId,
Host: request.Host,
Active: true,
AzureProject: request.AzureProjectName,
AuditLog: models.AuditLog{CreatedBy: request.UserId, CreatedOn: time.Now(), UpdatedOn: time.Now(), UpdatedBy: request.UserId},
}
model, err = impl.gitOpsRepository.CreateGitOpsConfig(model, tx)
if err != nil {
impl.logger.Errorw("error in saving gitops config", "data", model, "err", err)
err = &util.ApiError{
InternalMessage: "gitops config failed to create in db",
UserMessage: "gitops config failed to create in db",
}
return nil, err
}
clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName)
if err != nil {
return nil, err
}
cfg, err := impl.clusterService.GetClusterConfig(clusterBean)
if err != nil {
return nil, err
}
client, err := impl.K8sUtil.GetClient(cfg)
if err != nil {
return nil, err
}
secret, err := impl.K8sUtil.GetSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, GitOpsSecretName, client)
statusError, _ := err.(*errors.StatusError)
if err != nil && statusError.Status().Code != http.StatusNotFound {
impl.logger.Errorw("secret not found", "err", err)
return nil, err
}
data := make(map[string][]byte)
data["username"] = []byte(request.Username)
data["password"] = []byte(request.Token)
if secret == nil {
secret, err = impl.K8sUtil.CreateSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, data, GitOpsSecretName, client)
if err != nil {
impl.logger.Errorw("err on creating secret", "err", err)
return nil, err
}
} else {
secret.Data = data
secret, err = impl.K8sUtil.UpdateSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, secret, client)
if err != nil {
operationComplete := false
retryCount := 0
for !operationComplete && retryCount < 3 {
retryCount = retryCount + 1
secret, err := impl.K8sUtil.GetSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, GitOpsSecretName, client)
if err != nil {
impl.logger.Errorw("secret not found", "err", err)
return nil, err
}
secret.Data = data
secret, err = impl.K8sUtil.UpdateSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, secret, client)
if err != nil {
continue
}
if err == nil {
operationComplete = true
}
}
}
}
operationComplete := false
retryCount := 0
for !operationComplete && retryCount < 3 {
retryCount = retryCount + 1
cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, impl.aCDAuthConfig.ACDConfigMapName, client)
if err != nil {
return nil, err
}
updatedData := impl.updateData(cm.Data, request, GitOpsSecretName, existingModel.Host)
data := cm.Data
data["repository.credentials"] = updatedData["repository.credentials"]
cm.Data = data
_, err = impl.K8sUtil.UpdateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client)
if err != nil {
continue
}
if err == nil {
operationComplete = true
}
}
if !operationComplete {
return nil, fmt.Errorf("resouce version not matched with config map attemped 3 times")
}
err = tx.Commit()
if err != nil {
return nil, err
}
err = impl.gitFactory.Reload()
if err != nil {
return nil, err
}
request.Id = model.Id
return request, nil
}
func (impl *GitOpsConfigServiceImpl) UpdateGitOpsConfig(request *GitOpsConfigDto) error {
impl.logger.Debugw("gitops config update request", "req", request)
dbConnection := impl.gitOpsRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
return err
}
// Rollback tx on error.
defer tx.Rollback()
model, err := impl.gitOpsRepository.GetGitOpsConfigById(request.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for update.", "id", request.Id)
err = &util.ApiError{
InternalMessage: "gitops config update failed, does not exist",
UserMessage: "gitops config update failed, does not exist",
}
return err
}
existingModel, err := impl.gitOpsRepository.GetGitOpsConfigActive()
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in creating new gitops config", "error", err)
return err
}
if request.Active {
if existingModel != nil && existingModel.Id > 0 && existingModel.Id != model.Id {
existingModel.Active = false
existingModel.UpdatedOn = time.Now()
existingModel.UpdatedBy = request.UserId
err = impl.gitOpsRepository.UpdateGitOpsConfig(existingModel, tx)
if err != nil {
impl.logger.Errorw("error in creating new gitops config", "error", err)
return err
}
}
} else {
if existingModel == nil || existingModel.Id == 0 {
return fmt.Errorf("no active config found, please ensure atleast on gitops config active")
}
}
model.Provider = strings.ToUpper(request.Provider)
model.Username = request.Username
model.Token = request.Token
model.GitLabGroupId = request.GitLabGroupId
model.GitHubOrgId = request.GitHubOrgId
model.Host = request.Host
model.Active = request.Active
model.AzureProject = request.AzureProjectName
err = impl.gitOpsRepository.UpdateGitOpsConfig(model, tx)
if err != nil {
impl.logger.Errorw("error in updating team", "data", model, "err", err)
err = &util.ApiError{
InternalMessage: "gitops config failed to update in db",
UserMessage: "gitops config failed to update in db",
}
return err
}
request.Id = model.Id
clusterBean, err := impl.clusterService.FindOne(cluster.ClusterName)
if err != nil {
return err
}
cfg, err := impl.clusterService.GetClusterConfig(clusterBean)
if err != nil {
return err
}
client, err := impl.K8sUtil.GetClient(cfg)
if err != nil {
return err
}
secret, err := impl.K8sUtil.GetSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, GitOpsSecretName, client)
statusError, _ := err.(*errors.StatusError)
if err != nil && statusError.Status().Code != http.StatusNotFound {
impl.logger.Errorw("secret not found", "err", err)
return err
}
data := make(map[string][]byte)
data["username"] = []byte(request.Username)
data["password"] = []byte(request.Token)
if secret == nil {
secret, err = impl.K8sUtil.CreateSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, data, GitOpsSecretName, client)
if err != nil {
impl.logger.Errorw("err on creating secret", "err", err)
return err
}
} else {
secret.Data = data
secret, err = impl.K8sUtil.UpdateSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, secret, client)
if err != nil {
operationComplete := false
retryCount := 0
for !operationComplete && retryCount < 3 {
retryCount = retryCount + 1
secret, err := impl.K8sUtil.GetSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, GitOpsSecretName, client)
if err != nil {
impl.logger.Errorw("secret not found", "err", err)
return err
}
secret.Data = data
secret, err = impl.K8sUtil.UpdateSecret(impl.aCDAuthConfig.ACDConfigMapNamespace, secret, client)
if err != nil {
continue
}
if err == nil {
operationComplete = true
}
}
}
}
operationComplete := false
retryCount := 0
for !operationComplete && retryCount < 3 {
retryCount = retryCount + 1
cm, err := impl.K8sUtil.GetConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, impl.aCDAuthConfig.ACDConfigMapName, client)
if err != nil {
return err
}
updatedData := impl.updateData(cm.Data, request, GitOpsSecretName, existingModel.Host)
data := cm.Data
data["repository.credentials"] = updatedData["repository.credentials"]
cm.Data = data
_, err = impl.K8sUtil.UpdateConfigMap(impl.aCDAuthConfig.ACDConfigMapNamespace, cm, client)
if err != nil {
continue
}
if err == nil {
operationComplete = true
}
}
if !operationComplete {
return fmt.Errorf("resouce version not matched with config map attemped 3 times")
}
err = tx.Commit()
if err != nil {
return err
}
err = impl.gitFactory.Reload()
if err != nil {
return err
}
return nil
}
func (impl *GitOpsConfigServiceImpl) GetGitOpsConfigById(id int) (*GitOpsConfigDto, error) {
model, err := impl.gitOpsRepository.GetGitOpsConfigById(id)
if err != nil {
impl.logger.Errorw("GetGitOpsConfigById, error while get by id", "err", err, "id", id)
return nil, err
}
config := &GitOpsConfigDto{
Id: model.Id,
Provider: model.Provider,
GitHubOrgId: model.GitHubOrgId,
GitLabGroupId: model.GitLabGroupId,
Username: model.Username,
Token: model.Token,
Host: model.Host,
Active: model.Active,
UserId: model.CreatedBy,
AzureProjectName: model.AzureProject,
}
return config, err
}
func (impl *GitOpsConfigServiceImpl) GetAllGitOpsConfig() ([]*GitOpsConfigDto, error) {
models, err := impl.gitOpsRepository.GetAllGitOpsConfig()
if err != nil {
impl.logger.Errorw("GetAllGitOpsConfig, error while fetch all", "err", err)
return nil, err
}
configs := make([]*GitOpsConfigDto, 0)
for _, model := range models {
config := &GitOpsConfigDto{
Id: model.Id,
Provider: model.Provider,
GitHubOrgId: model.GitHubOrgId,
GitLabGroupId: model.GitLabGroupId,
Username: model.Username,
Token: model.Token,
Host: model.Host,
Active: model.Active,
UserId: model.CreatedBy,
AzureProjectName: model.AzureProject,
}
configs = append(configs, config)
}
return configs, err
}
func (impl *GitOpsConfigServiceImpl) GetGitOpsConfigByProvider(provider string) (*GitOpsConfigDto, error) {
model, err := impl.gitOpsRepository.GetGitOpsConfigByProvider(provider)
if err != nil {
impl.logger.Errorw("GetGitOpsConfigByProvider, error while get by name", "err", err, "provider", provider)
return nil, err
}
config := &GitOpsConfigDto{
Id: model.Id,
Provider: model.Provider,
GitHubOrgId: model.GitHubOrgId,
GitLabGroupId: model.GitLabGroupId,
Username: model.Username,
Token: model.Token,
Host: model.Host,
Active: model.Active,
UserId: model.CreatedBy,
AzureProjectName: model.AzureProject,
}
return config, err
}
func (impl *GitOpsConfigServiceImpl) updateData(data map[string]string, request *GitOpsConfigDto, secretName string, existingHost string) map[string]string {
var newRepositories []*RepositoryCredentialsDto
var existingRepositories []*RepositoryCredentialsDto
repoStr := data["repository.credentials"]
if len(repoStr) > 0 {
repoByte, err := yaml.YAMLToJSON([]byte(repoStr))
if err != nil {
panic(err)
}
err = json.Unmarshal(repoByte, &existingRepositories)
if err != nil {
panic(err)
}
}
for _, item := range existingRepositories {
if item.Url != existingHost {
newRepositories = append(newRepositories, item)
}
}
repoData := impl.createRepoElement(secretName, request)
newRepositories = append(newRepositories, repoData)
rb, err := json.Marshal(newRepositories)
if err != nil {
panic(err)
}
repositoriesYamlByte, err := yaml.JSONToYAML(rb)
if err != nil {
panic(err)
}
repositoryCredentials := map[string]string{}
if len(repositoriesYamlByte) > 0 {
repositoryCredentials["repository.credentials"] = string(repositoriesYamlByte)
}
return repositoryCredentials
}
func (impl *GitOpsConfigServiceImpl) createRepoElement(secretName string, request *GitOpsConfigDto) *RepositoryCredentialsDto {
repoData := &RepositoryCredentialsDto{}
usernameSecret := &KeyDto{Name: secretName, Key: "username"}
passwordSecret := &KeyDto{Name: secretName, Key: "password"}
repoData.PasswordSecret = passwordSecret
repoData.UsernameSecret = usernameSecret
repoData.Url = request.Host
return repoData
}
type RepositoryCredentialsDto struct {
Url string `json:"url,omitempty"`
UsernameSecret *KeyDto `json:"usernameSecret,omitempty"`
PasswordSecret *KeyDto `json:"passwordSecret,omitempty"`
}
type KeyDto struct {
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
}
func (impl *GitOpsConfigServiceImpl) GetGitOpsConfigActive() (*GitOpsConfigDto, error) {
model, err := impl.gitOpsRepository.GetGitOpsConfigActive()
if err != nil {
impl.logger.Errorw("GetGitOpsConfigActive, error while getting error", "err", err)
return nil, err
}
config := &GitOpsConfigDto{
Id: model.Id,
Provider: model.Provider,
GitHubOrgId: model.GitHubOrgId,
GitLabGroupId: model.GitLabGroupId,
Active: model.Active,
UserId: model.CreatedBy,
AzureProjectName: model.AzureProject,
}
return config, err
}