-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathArgoClientWrapperService.go
562 lines (505 loc) · 23.9 KB
/
ArgoClientWrapperService.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
/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 argocdServer
import (
"context"
"encoding/json"
"fmt"
application2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/certificate"
cluster2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/v2/pkg/apiclient/repocreds"
repository2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/caarlos0/env"
"github.com/devtron-labs/common-lib/async"
"github.com/devtron-labs/devtron/client/argocdServer/adapter"
"github.com/devtron-labs/devtron/client/argocdServer/application"
"github.com/devtron-labs/devtron/client/argocdServer/bean"
certificate2 "github.com/devtron-labs/devtron/client/argocdServer/certificate"
"github.com/devtron-labs/devtron/client/argocdServer/cluster"
config2 "github.com/devtron-labs/devtron/client/argocdServer/config"
bean2 "github.com/devtron-labs/devtron/client/argocdServer/repoCredsK8sClient/bean"
repocreds2 "github.com/devtron-labs/devtron/client/argocdServer/repocreds"
"github.com/devtron-labs/devtron/client/argocdServer/repository"
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/config"
"github.com/devtron-labs/devtron/pkg/deployment/gitOps/git"
"github.com/devtron-labs/devtron/util/retryFunc"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
"strings"
"time"
)
type ACDConfig struct {
ArgoCDAutoSyncEnabled bool `env:"ARGO_AUTO_SYNC_ENABLED" envDefault:"true"` // will gradually switch this flag to false in enterprise
RegisterRepoMaxRetryCount int `env:"ARGO_REPO_REGISTER_RETRY_COUNT" envDefault:"3"`
RegisterRepoMaxRetryDelay int `env:"ARGO_REPO_REGISTER_RETRY_DELAY" envDefault:"10"`
SyncRetryCount int `env:"ARGO_SYNC_RETRY_COUNT" envDefault:"1"`
SyncDelaySeconds int `env:"ARGO_SYNC_DELAY_SECONDS" envDefault:"2"`
}
func (config *ACDConfig) IsManualSyncEnabled() bool {
return config.ArgoCDAutoSyncEnabled == false
}
func (config *ACDConfig) IsAutoSyncEnabled() bool {
return config.ArgoCDAutoSyncEnabled == true
}
func GetACDDeploymentConfig() (*ACDConfig, error) {
cfg := &ACDConfig{}
err := env.Parse(cfg)
if err != nil {
return nil, err
}
return cfg, err
}
const (
ErrorOperationAlreadyInProgress = "another operation is already in progress" // this string is returned from argocd
)
type ApplicationClientWrapper interface {
ResourceTree(ctxt context.Context, query *application2.ResourcesQuery) (*v1alpha1.ApplicationTree, error)
GetArgoClient(ctxt context.Context) (application2.ApplicationServiceClient, *grpc.ClientConn, error)
GetApplicationResource(ctx context.Context, query *application2.ApplicationResourceRequest) (*application2.ApplicationResourceResponse, error)
DeleteArgoApp(ctx context.Context, appName string, cascadeDelete bool) (*application2.ApplicationResponse, error)
// GetArgoAppByName fetches an argoCd app by its name
GetArgoAppByName(ctx context.Context, appName string) (*v1alpha1.Application, error)
GetArgoAppByNameWithK8sClient(ctx context.Context, clusterId int, namespace, appName string) (*v1alpha1.Application, error)
DeleteArgoAppWithK8sClient(ctx context.Context, clusterId int, namespace, appName string, cascadeDelete bool) error
SyncArgoCDApplicationAndRefreshWithK8sClient(ctx context.Context, clusterId int, namespace, appName string) error
// UpdateArgoCDSyncModeIfNeeded - if ARGO_AUTO_SYNC_ENABLED=true and app is in manual sync mode or vice versa update app
UpdateArgoCDSyncModeIfNeeded(ctx context.Context, argoApplication *v1alpha1.Application) (err error)
// RegisterGitOpsRepoInArgoWithRetry - register a repository in argo-cd with retry mechanism
RegisterGitOpsRepoInArgoWithRetry(ctx context.Context, gitOpsRepoUrl, targetRevision string, userId int32) error
// PatchArgoCdApp performs a patch operation on an argoCd app
PatchArgoCdApp(ctx context.Context, dto *bean.ArgoCdAppPatchReqDto) error
// IsArgoAppPatchRequired decides weather the v1alpha1.ApplicationSource requires to be updated
IsArgoAppPatchRequired(argoAppSpec *v1alpha1.ApplicationSource, currentGitRepoUrl, currentTargetRevision, currentChartPath string) bool
// GetGitOpsRepoName returns the GitOps repository name, configured for the argoCd app
GetGitOpsRepoNameForApplication(ctx context.Context, appName string) (gitOpsRepoName string, err error)
GetGitOpsRepoURLForApplication(ctx context.Context, appName string) (gitOpsRepoURL string, err error)
}
type RepositoryClientWrapper interface {
RegisterGitOpsRepoInArgoWithRetry(ctx context.Context, gitOpsRepoUrl, targetRevision string, userId int32) error
}
type RepoCredsClientWrapper interface {
CreateRepoCreds(ctx context.Context, query *repocreds.RepoCredsCreateRequest) (*v1alpha1.RepoCreds, error)
AddOrUpdateOCIRegistry(username, password string, uniqueId int, registryUrl, repo string, isPublic bool) error
DeleteOCIRegistry(registryURL, repo string, ociRegistryId int) error
AddChartRepository(request bean2.ChartRepositoryAddRequest) error
UpdateChartRepository(request bean2.ChartRepositoryUpdateRequest) error
DeleteChartRepository(name, url string) error
}
type CertificateClientWrapper interface {
CreateCertificate(ctx context.Context, query *certificate.RepositoryCertificateCreateRequest) (*v1alpha1.RepositoryCertificateList, error)
DeleteCertificate(ctx context.Context, query *certificate.RepositoryCertificateQuery, opts ...grpc.CallOption) (*v1alpha1.RepositoryCertificateList, error)
}
type ClusterClientWrapper interface {
CreateCluster(ctx context.Context, clusterRequest *cluster2.ClusterCreateRequest) (*v1alpha1.Cluster, error)
UpdateCluster(ctx context.Context, clusterRequest *cluster2.ClusterUpdateRequest) (*v1alpha1.Cluster, error)
}
type ArgoClientWrapperService interface {
ClusterClientWrapper
ApplicationClientWrapper
RepositoryClientWrapper
RepoCredsClientWrapper
CertificateClientWrapper
}
type ArgoClientWrapperServiceImpl struct {
acdApplicationClient application.ServiceClient
repositoryService repository.ServiceClient
clusterClient cluster.ServiceClient
repoCredsClient repocreds2.ServiceClient
CertificateClient certificate2.ServiceClient
logger *zap.SugaredLogger
ACDConfig *ACDConfig
gitOpsConfigReadService config.GitOpsConfigReadService
gitOperationService git.GitOperationService
asyncRunnable *async.Runnable
acdConfigGetter config2.ArgoCDConfigGetter
argoK8sClient ArgoK8sClient
*ArgoClientWrapperServiceEAImpl
}
func NewArgoClientWrapperServiceImpl(
acdClient application.ServiceClient,
repositoryService repository.ServiceClient,
clusterClient cluster.ServiceClient,
repocredsClient repocreds2.ServiceClient,
CertificateClient certificate2.ServiceClient,
logger *zap.SugaredLogger,
ACDConfig *ACDConfig, gitOpsConfigReadService config.GitOpsConfigReadService,
gitOperationService git.GitOperationService, asyncRunnable *async.Runnable,
acdConfigGetter config2.ArgoCDConfigGetter,
ArgoClientWrapperServiceEAImpl *ArgoClientWrapperServiceEAImpl,
argoK8sClient ArgoK8sClient,
) *ArgoClientWrapperServiceImpl {
return &ArgoClientWrapperServiceImpl{
acdApplicationClient: acdClient,
repositoryService: repositoryService,
clusterClient: clusterClient,
repoCredsClient: repocredsClient,
CertificateClient: CertificateClient,
logger: logger,
ACDConfig: ACDConfig,
gitOpsConfigReadService: gitOpsConfigReadService,
gitOperationService: gitOperationService,
asyncRunnable: asyncRunnable,
acdConfigGetter: acdConfigGetter,
ArgoClientWrapperServiceEAImpl: ArgoClientWrapperServiceEAImpl,
argoK8sClient: argoK8sClient,
}
}
func (impl *ArgoClientWrapperServiceImpl) ResourceTree(ctxt context.Context, query *application2.ResourcesQuery) (*v1alpha1.ApplicationTree, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, nil
}
return impl.acdApplicationClient.ResourceTree(ctxt, grpcConfig, query)
}
func (impl *ArgoClientWrapperServiceImpl) GetArgoClient(ctxt context.Context) (application2.ApplicationServiceClient, *grpc.ClientConn, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, nil, err
}
return impl.acdApplicationClient.GetArgoClient(ctxt, grpcConfig)
}
func (impl *ArgoClientWrapperServiceImpl) GetApplicationResource(ctx context.Context, query *application2.ApplicationResourceRequest) (*application2.ApplicationResourceResponse, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
resource, err := impl.acdApplicationClient.GetResource(ctx, grpcConfig, query)
if err != nil {
impl.logger.Errorw("error in getting resource", "err", err)
return nil, err
}
return resource, nil
}
func (impl *ArgoClientWrapperServiceImpl) GetArgoApplication(ctx context.Context, query *application2.ApplicationQuery) (*v1alpha1.Application, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
return impl.acdApplicationClient.Get(ctx, grpcConfig, query)
}
func (impl *ArgoClientWrapperServiceImpl) DeleteArgoApp(ctx context.Context, appName string, cascadeDelete bool) (*application2.ApplicationResponse, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
req := &application2.ApplicationDeleteRequest{
Name: &appName,
Cascade: &cascadeDelete,
}
return impl.acdApplicationClient.Delete(ctx, grpcConfig, req)
}
func (impl *ArgoClientWrapperServiceImpl) UpdateArgoCDSyncModeIfNeeded(ctx context.Context, argoApplication *v1alpha1.Application) (err error) {
if isArgoAppSyncModeMigrationNeeded(argoApplication, impl.ACDConfig) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return err
}
syncModeUpdateRequest := createRequestForArgoCDSyncModeUpdateRequest(argoApplication, impl.ACDConfig.IsAutoSyncEnabled())
validate := false
_, err = impl.acdApplicationClient.Update(ctx, grpcConfig, &application2.ApplicationUpdateRequest{Application: syncModeUpdateRequest, Validate: &validate})
if err != nil {
impl.logger.Errorw("error in creating argo pipeline ", "name", argoApplication.Name, "err", err)
return err
}
}
return nil
}
func (impl *ArgoClientWrapperServiceImpl) RegisterGitOpsRepoInArgoWithRetry(ctx context.Context, gitOpsRepoUrl, targetRevision string, userId int32) error {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil
}
callback := func(int) error {
return impl.createRepoInArgoCd(ctx, grpcConfig, gitOpsRepoUrl)
}
argoCdErr := retryFunc.Retry(callback,
impl.isRetryableArgoRepoCreationError,
impl.ACDConfig.RegisterRepoMaxRetryCount,
time.Duration(impl.ACDConfig.RegisterRepoMaxRetryDelay)*time.Second,
impl.logger)
if argoCdErr != nil {
impl.logger.Errorw("error in registering GitOps repository", "repoName", gitOpsRepoUrl, "err", argoCdErr)
return impl.handleArgoRepoCreationError(ctx, argoCdErr, grpcConfig, gitOpsRepoUrl, targetRevision, userId)
}
impl.logger.Infow("gitOps repo registered in argo", "repoName", gitOpsRepoUrl)
return nil
}
func (impl *ArgoClientWrapperServiceImpl) CreateRepoCreds(ctx context.Context, query *repocreds.RepoCredsCreateRequest) (*v1alpha1.RepoCreds, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
return impl.repoCredsClient.CreateRepoCreds(ctx, grpcConfig, query)
}
func (impl *ArgoClientWrapperServiceImpl) AddOrUpdateOCIRegistry(username, password string, uniqueId int, registryUrl, repo string, isPublic bool) error {
return impl.ArgoClientWrapperServiceEAImpl.AddOrUpdateOCIRegistry(username, password, uniqueId, registryUrl, repo, isPublic)
}
func (impl *ArgoClientWrapperServiceImpl) DeleteOCIRegistry(registryURL, repo string, ociRegistryId int) error {
return impl.ArgoClientWrapperServiceEAImpl.DeleteOCIRegistry(registryURL, repo, ociRegistryId)
}
func (impl *ArgoClientWrapperServiceImpl) AddChartRepository(request bean2.ChartRepositoryAddRequest) error {
return impl.ArgoClientWrapperServiceEAImpl.AddChartRepository(request)
}
func (impl *ArgoClientWrapperServiceImpl) UpdateChartRepository(request bean2.ChartRepositoryUpdateRequest) error {
return impl.ArgoClientWrapperServiceEAImpl.UpdateChartRepository(request)
}
func (impl *ArgoClientWrapperServiceImpl) DeleteChartRepository(name, url string) error {
return impl.ArgoClientWrapperServiceEAImpl.DeleteChartRepository(name, url)
}
func (impl *ArgoClientWrapperServiceImpl) GetArgoAppByName(ctx context.Context, appName string) (*v1alpha1.Application, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
argoApplication, err := impl.acdApplicationClient.Get(ctx, grpcConfig, &application2.ApplicationQuery{Name: &appName})
if err != nil {
impl.logger.Errorw("err in getting argo app by name", "app", appName)
return nil, err
}
return argoApplication, nil
}
func (impl *ArgoClientWrapperServiceImpl) GetArgoAppByNameWithK8sClient(ctx context.Context, clusterId int, namespace, appName string) (*v1alpha1.Application, error) {
k8sConfig, err := impl.acdConfigGetter.GetK8sConfigWithClusterIdAndNamespace(clusterId, namespace)
if err != nil {
impl.logger.Errorw("error in getting k8s config", "err", err)
return nil, err
}
argoApplication, err := impl.argoK8sClient.GetArgoApplication(k8sConfig, appName)
if err != nil {
impl.logger.Errorw("err in getting argo app by name", "app", appName)
return nil, err
}
return argoApplication, nil
}
func (impl *ArgoClientWrapperServiceImpl) DeleteArgoAppWithK8sClient(ctx context.Context, clusterId int, namespace, appName string, cascadeDelete bool) error {
k8sConfig, err := impl.acdConfigGetter.GetK8sConfigWithClusterIdAndNamespace(clusterId, namespace)
if err != nil {
impl.logger.Errorw("error in getting k8s config", "err", err)
return err
}
err = impl.argoK8sClient.DeleteArgoApplication(ctx, k8sConfig, appName, cascadeDelete)
if err != nil {
impl.logger.Errorw("err in getting argo app by name", "app", appName)
return err
}
return nil
}
func (impl *ArgoClientWrapperServiceImpl) SyncArgoCDApplicationAndRefreshWithK8sClient(ctx context.Context, clusterId int, namespace, appName string) error {
k8sConfig, err := impl.acdConfigGetter.GetK8sConfigWithClusterIdAndNamespace(clusterId, namespace)
if err != nil {
impl.logger.Errorw("error in getting k8s config", "err", err)
return err
}
if impl.ACDConfig.IsManualSyncEnabled() {
impl.logger.Debugw("syncing ArgoCd app using k8s as manual sync is enabled", "argoAppName", appName)
err = impl.argoK8sClient.SyncArgoApplication(ctx, k8sConfig, appName)
if err != nil {
if errors.Is(err, ErrAnotherOperationInProgress) {
impl.logger.Infow("Another operation is already in progress, terminating app")
err = impl.argoK8sClient.TerminateApp(ctx, k8sConfig, appName)
if err != nil && !errors.Is(err, ErrNoOperationInProgress) {
impl.logger.Errorw("error in terminating argo application", "appName", appName, "err", err)
return err
}
callback := func(int) error {
err = impl.argoK8sClient.SyncArgoApplication(ctx, k8sConfig, appName)
if err != nil {
return err
}
return nil
}
shouldRetry := func(err error) bool {
return errors.Is(err, ErrAnotherOperationInProgress)
}
err = retryFunc.Retry(callback,
shouldRetry,
impl.ACDConfig.SyncRetryCount,
time.Second*time.Duration(impl.ACDConfig.SyncDelaySeconds),
impl.logger,
)
return err
}
impl.logger.Errorw("err in syncing argo application", "app", appName, "err", err)
return err
}
}
runnableFunc := func() {
impl.logger.Debugw("syncing ArgoCd app using k8s as manual sync is enabled", "argoAppName", appName)
// running ArgoCd app refresh in asynchronous mode
refreshErr := impl.argoK8sClient.RefreshApp(context.Background(), k8sConfig, appName, bean.RefreshTypeNormal)
if refreshErr != nil {
impl.logger.Errorw("error in refreshing argo app", "argoAppName", appName, "err", refreshErr)
}
}
impl.asyncRunnable.Execute(runnableFunc)
return nil
}
func (impl *ArgoClientWrapperServiceImpl) IsArgoAppPatchRequired(argoAppSpec *v1alpha1.ApplicationSource, currentGitRepoUrl, currentTargetRevision, currentChartPath string) bool {
if argoAppSpec == nil {
// if argo app spec is nil, then no need to patch
// this means the argo app object is in corrupted state
impl.logger.Warnw("received argo app spec is nil, skipping for patch request...")
return false
}
return (len(currentGitRepoUrl) != 0 && argoAppSpec.RepoURL != currentGitRepoUrl) ||
argoAppSpec.Path != currentChartPath ||
argoAppSpec.TargetRevision != currentTargetRevision
}
func (impl *ArgoClientWrapperServiceImpl) PatchArgoCdApp(ctx context.Context, dto *bean.ArgoCdAppPatchReqDto) error {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return err
}
patchReq := adapter.GetArgoCdPatchReqFromDto(dto)
reqbyte, err := json.Marshal(patchReq)
if err != nil {
impl.logger.Errorw("error in creating patch", "err", err)
return err
}
reqString := string(reqbyte)
_, err = impl.acdApplicationClient.Patch(ctx, grpcConfig, &application2.ApplicationPatchRequest{Patch: &reqString, Name: &dto.ArgoAppName, PatchType: &dto.PatchType})
if err != nil {
impl.logger.Errorw("error in patching argo pipeline ", "name", dto.ArgoAppName, "patch", reqString, "err", err)
return err
}
return nil
}
func (impl *ArgoClientWrapperServiceImpl) GetGitOpsRepoNameForApplication(ctx context.Context, appName string) (gitOpsRepoName string, err error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return gitOpsRepoName, err
}
acdApplication, err := impl.acdApplicationClient.Get(ctx, grpcConfig, &application2.ApplicationQuery{Name: &appName})
if err != nil {
impl.logger.Errorw("no argo app exists", "acdAppName", appName, "err", err)
return gitOpsRepoName, err
}
// safety checks nil pointers
if acdApplication != nil && acdApplication.Spec.Source != nil {
gitOpsRepoUrl := acdApplication.Spec.Source.RepoURL
gitOpsRepoName = impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(gitOpsRepoUrl)
return gitOpsRepoName, nil
}
return gitOpsRepoName, fmt.Errorf("unable to get any ArgoCd application '%s'", appName)
}
func (impl *ArgoClientWrapperServiceImpl) GetGitOpsRepoURLForApplication(ctx context.Context, appName string) (gitOpsRepoName string, err error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return gitOpsRepoName, err
}
acdApplication, err := impl.acdApplicationClient.Get(ctx, grpcConfig, &application2.ApplicationQuery{Name: &appName})
if err != nil {
impl.logger.Errorw("no argo app exists", "acdAppName", appName, "err", err)
return gitOpsRepoName, err
}
// safety checks nil pointers
if acdApplication != nil && acdApplication.Spec.Source != nil {
gitOpsRepoUrl := acdApplication.Spec.Source.RepoURL
return gitOpsRepoUrl, nil
}
return "", fmt.Errorf("unable to get any ArgoCd application '%s'", appName)
}
// createRepoInArgoCd is the wrapper function to Create Repository in ArgoCd
func (impl *ArgoClientWrapperServiceImpl) createRepoInArgoCd(ctx context.Context, grpcConfig *bean.ArgoGRPCConfig, gitOpsRepoUrl string) error {
repo := &v1alpha1.Repository{
Repo: gitOpsRepoUrl,
}
repo, err := impl.repositoryService.Create(ctx, grpcConfig, &repository2.RepoCreateRequest{Repo: repo, Upsert: true})
if err != nil {
impl.logger.Errorw("error in creating argo Repository", "err", err)
return err
}
return nil
}
// isRetryableArgoRepoCreationError returns whether to retry or not, based on the error returned from callback func
// In createRepoInArgoCd, we will retry only if the error matches to bean.ArgoRepoSyncDelayErr
func (impl *ArgoClientWrapperServiceImpl) isRetryableArgoRepoCreationError(argoCdErr error) bool {
return strings.Contains(argoCdErr.Error(), bean.ArgoRepoSyncDelayErr)
}
// handleArgoRepoCreationError - manages the error thrown while performing createRepoInArgoCd
func (impl *ArgoClientWrapperServiceImpl) handleArgoRepoCreationError(ctx context.Context, argoCdErr error, grpcConfig *bean.ArgoGRPCConfig, gitOpsRepoUrl string, targetRevision string, userId int32) error {
emptyRepoErrorMessages := bean.EmptyRepoErrorList
isEmptyRepoError := false
for _, errMsg := range emptyRepoErrorMessages {
if strings.Contains(argoCdErr.Error(), errMsg) {
isEmptyRepoError = true
}
}
if isEmptyRepoError {
// - found empty repository, create some file in repository
gitOpsRepoName := impl.gitOpsConfigReadService.GetGitOpsRepoNameFromUrl(gitOpsRepoUrl)
err := impl.gitOperationService.CreateReadmeInGitRepo(ctx, gitOpsRepoName, targetRevision, userId)
if err != nil {
impl.logger.Errorw("error in creating file in git repo", "err", err)
return err
}
}
// try to register with after creating readme file
return impl.createRepoInArgoCd(ctx, grpcConfig, gitOpsRepoUrl)
}
func (impl *ArgoClientWrapperServiceImpl) CreateCluster(ctx context.Context, clusterRequest *cluster2.ClusterCreateRequest) (*v1alpha1.Cluster, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
cluster, err := impl.clusterClient.Create(ctx, grpcConfig, clusterRequest)
if err != nil {
return nil, err
}
return cluster, nil
}
func (impl *ArgoClientWrapperServiceImpl) UpdateCluster(ctx context.Context, clusterRequest *cluster2.ClusterUpdateRequest) (*v1alpha1.Cluster, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
impl.logger.Errorw("error in getting grpc config", "err", err)
return nil, err
}
cluster, err := impl.clusterClient.Update(ctx, grpcConfig, clusterRequest)
if err != nil {
return nil, err
}
return cluster, nil
}
func (impl *ArgoClientWrapperServiceImpl) CreateCertificate(ctx context.Context, query *certificate.RepositoryCertificateCreateRequest) (*v1alpha1.RepositoryCertificateList, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
return nil, err
}
return impl.CertificateClient.CreateCertificate(ctx, grpcConfig, query)
}
func (impl *ArgoClientWrapperServiceImpl) DeleteCertificate(ctx context.Context, query *certificate.RepositoryCertificateQuery, opts ...grpc.CallOption) (*v1alpha1.RepositoryCertificateList, error) {
grpcConfig, err := impl.acdConfigGetter.GetGRPCConfig()
if err != nil {
return nil, err
}
return impl.CertificateClient.DeleteCertificate(ctx, grpcConfig, query, opts...)
}