Skip to content

Commit c7bf17f

Browse files
authored
Merge branch 'develop' into develop-main-sync-2jun-1
2 parents 9aed3ad + efe774f commit c7bf17f

File tree

9 files changed

+102
-143
lines changed

9 files changed

+102
-143
lines changed

Wire.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ func InitializeApp() (*App, error) {
303303
pipeline.NewCiMaterialConfigServiceImpl,
304304
wire.Bind(new(pipeline.CiMaterialConfigService), new(*pipeline.CiMaterialConfigServiceImpl)),
305305

306+
app.NewDeploymentEventHandlerImpl,
307+
wire.Bind(new(app.DeploymentEventHandler), new(*app.DeploymentEventHandlerImpl)),
308+
306309
pipeline.NewAppArtifactManagerImpl,
307310
wire.Bind(new(pipeline.AppArtifactManager), new(*pipeline.AppArtifactManagerImpl)),
308311
pipeline.NewDevtronAppCMCSServiceImpl,

env_gen.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

pkg/app/AppService.go

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23-
"net/url"
2423
"strconv"
2524
"time"
2625

@@ -47,7 +46,6 @@ import (
4746

4847
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
4948
"github.com/caarlos0/env"
50-
"github.com/devtron-labs/common-lib/async"
5149
k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
5250
"github.com/devtron-labs/common-lib/utils/k8s/health"
5351
"github.com/devtron-labs/devtron/api/bean"
@@ -126,7 +124,7 @@ type AppServiceImpl struct {
126124
deploymentConfigService common2.DeploymentConfigService
127125
envConfigOverrideReadService read.EnvConfigOverrideService
128126
cdWorkflowRunnerService cd.CdWorkflowRunnerService
129-
asyncRunnable *async.Runnable
127+
deploymentEventHandler DeploymentEventHandler
130128
}
131129

132130
type AppService interface {
@@ -135,7 +133,6 @@ type AppService interface {
135133
GetConfigMapAndSecretJson(appId int, envId int) ([]byte, error)
136134
UpdateCdWorkflowRunnerByACDObject(app *v1alpha1.Application, cdWfrId int, updateTimedOutStatus bool) error
137135
UpdateDeploymentStatusForGitOpsPipelines(app *v1alpha1.Application, applicationClusterId int, statusTime time.Time, isAppStore bool) (bool, bool, *chartConfig.PipelineOverride, error)
138-
WriteCDSuccessEvent(appId int, envId int, override *chartConfig.PipelineOverride)
139136
CreateGitOpsRepo(app *app.App, targetRevision string, userId int32) (gitopsRepoName string, chartGitAttr *commonBean.ChartGitAttribute, err error)
140137

141138
// TODO: move inside reader service
@@ -168,7 +165,7 @@ func NewAppService(
168165
deploymentConfigService common2.DeploymentConfigService,
169166
envConfigOverrideReadService read.EnvConfigOverrideService,
170167
cdWorkflowRunnerService cd.CdWorkflowRunnerService,
171-
asyncRunnable *async.Runnable) *AppServiceImpl {
168+
deploymentEventHandler DeploymentEventHandler) *AppServiceImpl {
172169
appServiceImpl := &AppServiceImpl{
173170
mergeUtil: mergeUtil,
174171
pipelineOverrideRepository: pipelineOverrideRepository,
@@ -199,7 +196,7 @@ func NewAppService(
199196
deploymentConfigService: deploymentConfigService,
200197
envConfigOverrideReadService: envConfigOverrideReadService,
201198
cdWorkflowRunnerService: cdWorkflowRunnerService,
202-
asyncRunnable: asyncRunnable,
199+
deploymentEventHandler: deploymentEventHandler,
203200
}
204201
return appServiceImpl
205202
}
@@ -309,6 +306,7 @@ func (impl *AppServiceImpl) UpdateDeploymentStatusForGitOpsPipelines(app *v1alph
309306
impl.logger.Errorw("error on update cd workflow runner", "CdWorkflowId", pipelineOverride.CdWorkflowId, "status", cdWorkflow2.WorkflowTimedOut, "err", err)
310307
return isSucceeded, isTimelineUpdated, pipelineOverride, err
311308
}
309+
impl.deploymentEventHandler.WriteCDNotificationEventAsync(cdPipeline.AppId, cdPipeline.EnvironmentId, pipelineOverride, eventUtil.Fail)
312310
return isSucceeded, isTimelineUpdated, pipelineOverride, nil
313311
}
314312
if reconciledAt.IsZero() || (kubectlSyncedTimeline != nil && kubectlSyncedTimeline.Id > 0 && reconciledAt.After(kubectlSyncedTimeline.StatusTime)) {
@@ -325,7 +323,7 @@ func (impl *AppServiceImpl) UpdateDeploymentStatusForGitOpsPipelines(app *v1alph
325323
}
326324
if isSucceeded {
327325
impl.logger.Infow("writing cd success event", "gitHash", gitHash, "pipelineOverride", pipelineOverride)
328-
impl.asyncRunnable.Execute(func() { impl.WriteCDSuccessEvent(cdPipeline.AppId, cdPipeline.EnvironmentId, pipelineOverride) })
326+
impl.deploymentEventHandler.WriteCDNotificationEventAsync(cdPipeline.AppId, cdPipeline.EnvironmentId, pipelineOverride, eventUtil.Success)
329327
}
330328
} else {
331329
impl.logger.Debugw("event received for older triggered revision", "gitHash", gitHash)
@@ -773,23 +771,6 @@ func (impl *AppServiceImpl) UpdatePipelineStatusTimelineForApplicationChanges(ap
773771
return isTimelineUpdated, isTimelineTimedOut, kubectlApplySyncedTimeline, nil
774772
}
775773

776-
func (impl *AppServiceImpl) WriteCDSuccessEvent(appId int, envId int, override *chartConfig.PipelineOverride) {
777-
event, _ := impl.eventFactory.Build(eventUtil.Success, &override.PipelineId, appId, &envId, eventUtil.CD)
778-
impl.logger.Debugw("event WriteCDSuccessEvent", "event", event, "override", override)
779-
event = impl.eventFactory.BuildExtraCDData(event, nil, override.Id, bean.CD_WORKFLOW_TYPE_DEPLOY)
780-
_, evtErr := impl.eventClient.WriteNotificationEvent(event)
781-
if evtErr != nil {
782-
impl.logger.Errorw("error in writing event", "event", event, "err", evtErr)
783-
}
784-
}
785-
786-
func (impl *AppServiceImpl) BuildCDSuccessPayload(appName string, environmentName string) *client.Payload {
787-
payload := &client.Payload{}
788-
payload.AppName = appName
789-
payload.EnvName = environmentName
790-
return payload
791-
}
792-
793774
type ValuesOverrideResponse struct {
794775
MergedValues string
795776
ReleaseOverrideJSON string
@@ -887,29 +868,6 @@ type PipelineMaterialInfo struct {
887868
CommitHash string
888869
}
889870

890-
func buildCDTriggerEvent(impl *AppServiceImpl, overrideRequest *bean.ValuesOverrideRequest, pipeline *pipelineConfig.Pipeline,
891-
envOverride *chartConfig.EnvConfigOverride, materialInfo map[string]string, artifact *repository.CiArtifact) client.Event {
892-
event, _ := impl.eventFactory.Build(eventUtil.Trigger, &pipeline.Id, pipeline.AppId, &pipeline.EnvironmentId, eventUtil.CD)
893-
return event
894-
}
895-
896-
func (impl *AppServiceImpl) BuildPayload(overrideRequest *bean.ValuesOverrideRequest, pipeline *pipelineConfig.Pipeline,
897-
envOverride *chartConfig.EnvConfigOverride, materialInfo map[string]string, artifact *repository.CiArtifact) *client.Payload {
898-
payload := &client.Payload{}
899-
payload.AppName = pipeline.App.AppName
900-
payload.PipelineName = pipeline.Name
901-
payload.EnvName = envOverride.Environment.Name
902-
903-
var revision string
904-
for _, v := range materialInfo {
905-
revision = v
906-
break
907-
}
908-
payload.Source = url.PathEscape(revision)
909-
payload.DockerImageUrl = artifact.Image
910-
return payload
911-
}
912-
913871
type ReleaseAttributes struct {
914872
Name string
915873
Tag string

pkg/app/DeploymentEventHandler.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

pkg/app/deploymentEventHandler.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2020-2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package app
18+
19+
import (
20+
"github.com/devtron-labs/common-lib/async"
21+
"github.com/devtron-labs/devtron/api/bean"
22+
client "github.com/devtron-labs/devtron/client/events"
23+
"github.com/devtron-labs/devtron/internal/sql/repository/chartConfig"
24+
util "github.com/devtron-labs/devtron/util/event"
25+
"go.uber.org/zap"
26+
)
27+
28+
type DeploymentEventHandler interface {
29+
WriteCDNotificationEventAsync(appId int, envId int, override *chartConfig.PipelineOverride, eventType util.EventType)
30+
}
31+
32+
type DeploymentEventHandlerImpl struct {
33+
logger *zap.SugaredLogger
34+
eventFactory client.EventFactory
35+
eventClient client.EventClient
36+
asyncRunnable *async.Runnable
37+
}
38+
39+
func NewDeploymentEventHandlerImpl(logger *zap.SugaredLogger, eventClient client.EventClient, eventFactory client.EventFactory, asyncRunnable *async.Runnable) *DeploymentEventHandlerImpl {
40+
deploymentEventHandlerImpl := &DeploymentEventHandlerImpl{
41+
logger: logger,
42+
eventClient: eventClient,
43+
eventFactory: eventFactory,
44+
asyncRunnable: asyncRunnable,
45+
}
46+
return deploymentEventHandlerImpl
47+
}
48+
49+
func (impl *DeploymentEventHandlerImpl) writeCDNotificationEvent(appId int, envId int, override *chartConfig.PipelineOverride, eventType util.EventType) {
50+
event, _ := impl.eventFactory.Build(eventType, &override.PipelineId, appId, &envId, util.CD)
51+
impl.logger.Debugw("event WriteCDNotificationEvent", "event", event, "override", override)
52+
event = impl.eventFactory.BuildExtraCDData(event, nil, override.Id, bean.CD_WORKFLOW_TYPE_DEPLOY)
53+
_, evtErr := impl.eventClient.WriteNotificationEvent(event)
54+
if evtErr != nil {
55+
impl.logger.Errorw("error in writing event", "event", event, "err", evtErr)
56+
}
57+
}
58+
59+
// WriteCDNotificationEventAsync executes WriteCDNotificationEvent in a panic-safe goroutine
60+
func (impl *DeploymentEventHandlerImpl) WriteCDNotificationEventAsync(appId int, envId int, override *chartConfig.PipelineOverride, eventType util.EventType) {
61+
impl.asyncRunnable.Execute(func() {
62+
impl.writeCDNotificationEvent(appId, envId, override, eventType)
63+
})
64+
}

pkg/deployment/trigger/devtronApps/HandlerService.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"time"
2424

25-
"github.com/devtron-labs/common-lib/async"
2625
pubsub "github.com/devtron-labs/common-lib/pubsub-lib"
2726
util5 "github.com/devtron-labs/common-lib/utils/k8s"
2827
bean3 "github.com/devtron-labs/devtron/api/bean"
@@ -168,7 +167,7 @@ type HandlerServiceImpl struct {
168167
ciLogService pipeline.CiLogService
169168
workflowService executor.WorkflowService
170169
blobConfigStorageService pipeline.BlobStorageConfigService
171-
asyncRunnable *async.Runnable
170+
deploymentEventHandler app.DeploymentEventHandler
172171
}
173172

174173
func NewHandlerServiceImpl(logger *zap.SugaredLogger,
@@ -230,7 +229,7 @@ func NewHandlerServiceImpl(logger *zap.SugaredLogger,
230229
ciLogService pipeline.CiLogService,
231230
workflowService executor.WorkflowService,
232231
blobConfigStorageService pipeline.BlobStorageConfigService,
233-
asyncRunnable *async.Runnable) (*HandlerServiceImpl, error) {
232+
deploymentEventHandler app.DeploymentEventHandler) (*HandlerServiceImpl, error) {
234233
impl := &HandlerServiceImpl{
235234
logger: logger,
236235
cdWorkflowCommonService: cdWorkflowCommonService,
@@ -296,7 +295,7 @@ func NewHandlerServiceImpl(logger *zap.SugaredLogger,
296295
ciLogService: ciLogService,
297296
workflowService: workflowService,
298297
blobConfigStorageService: blobConfigStorageService,
299-
asyncRunnable: asyncRunnable,
298+
deploymentEventHandler: deploymentEventHandler,
300299
}
301300
config, err := types.GetCdConfig()
302301
if err != nil {

pkg/deployment/trigger/devtronApps/deployStageHandlerCode.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
util2 "github.com/devtron-labs/devtron/util/event"
2324
"net/http"
2425
"path"
2526
"regexp"
@@ -317,6 +318,12 @@ func (impl *HandlerServiceImpl) ManualCdTrigger(triggerContext bean.TriggerConte
317318
if err != nil {
318319
impl.logger.Errorw("error while updating current runner status to failed", "cdWfr", runner.Id, "err", err)
319320
}
321+
pipelineOverride, err := impl.pipelineOverrideRepository.FindLatestByCdWorkflowId(runner.CdWorkflowId)
322+
if err != nil {
323+
impl.logger.Errorw("error in getting latest pipeline override by cdWorkflowId", "err", err, "cdWorkflowId", runner.CdWorkflowId)
324+
return 0, "", nil, err
325+
}
326+
impl.deploymentEventHandler.WriteCDNotificationEventAsync(pipelineOverride.Pipeline.AppId, pipelineOverride.Pipeline.EnvironmentId, pipelineOverride, util2.Fail)
320327
return 0, "", nil, releaseErr
321328
}
322329

0 commit comments

Comments
 (0)