Skip to content

Commit d5e2b08

Browse files
committed
Merge remote-tracking branch 'origin/feat-flux-cd' into feat-flux-cd
2 parents 442adbc + 588c3cf commit d5e2b08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+6853
-14
lines changed

internal/sql/repository/chartConfig/PipelineOverrideRepository.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
2424
"github.com/devtron-labs/devtron/internal/util"
2525
"github.com/devtron-labs/devtron/pkg/sql"
26+
util2 "github.com/devtron-labs/devtron/util"
2627
"github.com/go-pg/pg"
2728
"github.com/go-pg/pg/orm"
2829
"github.com/juju/errors"
@@ -68,6 +69,7 @@ type PipelineOverrideRepository interface {
6869
GetByPipelineIdAndReleaseNo(pipelineId, releaseNo int) (pipelineOverrides []*PipelineOverride, err error)
6970
GetAllRelease(appId, environmentId int) (pipelineOverrides []*PipelineOverride, err error)
7071
FindByPipelineTriggerGitHash(gitHash string) (pipelineOverride *PipelineOverride, err error)
72+
FindByPipelineLikeTriggerGitHash(gitHash string) (pipelineOverride *PipelineOverride, err error)
7173
GetLatestRelease(appId, environmentId int) (pipelineOverrides *PipelineOverride, err error)
7274
GetLatestReleaseForAppIds(appIds []int, envId int) (pipelineOverrides []*PipelineConfigOverrideMetadata, err error)
7375
FindById(id int) (*PipelineOverride, error)
@@ -288,6 +290,16 @@ func (impl PipelineOverrideRepositoryImpl) FindByPipelineTriggerGitHash(gitHash
288290
return pipelineOverride, err
289291
}
290292

293+
func (impl PipelineOverrideRepositoryImpl) FindByPipelineLikeTriggerGitHash(gitHash string) (pipelineOverride *PipelineOverride, err error) {
294+
pipelineOverride = &PipelineOverride{}
295+
err = impl.dbConnection.Model(pipelineOverride).
296+
Column("pipeline_override.*", "Pipeline", "CiArtifact").
297+
Where("pipeline_override.git_hash LIKE ?", util2.GetLIKEClauseQueryParamEnd(gitHash)).
298+
Order("id DESC").Limit(1).
299+
Select()
300+
return pipelineOverride, err
301+
}
302+
291303
func (impl PipelineOverrideRepositoryImpl) FindById(id int) (*PipelineOverride, error) {
292304
var pipelineOverride PipelineOverride
293305
err := impl.dbConnection.Model(&pipelineOverride).

pkg/app/status/PipelineStatusTimelineService.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ type PipelineTimelineDetailDto struct {
9393
StatusFetchCount int `json:"statusFetchCount"`
9494
WfrStatus string `json:"wfrStatus"`
9595
DeploymentAppDeleteRequest bool `json:"deploymentAppDeleteRequest"`
96-
DeploymentAppType string `json:"deploymentAppType"`
9796
}
9897

9998
type PipelineStatusTimelineDto struct {
@@ -294,7 +293,6 @@ func (impl *PipelineStatusTimelineServiceImpl) FetchTimelines(appId, envId, wfrI
294293
StatusFetchCount: statusFetchCount,
295294
WfrStatus: wfrStatus,
296295
DeploymentAppDeleteRequest: wfr.CdWorkflow.Pipeline.DeploymentAppDeleteRequest,
297-
DeploymentAppType: deploymentAppType,
298296
}
299297
return timelineDetail, nil
300298
}

pkg/deployment/trigger/devtronApps/deployStageHandlerCodeFlux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ func (impl *HandlerServiceImpl) CreateHelmRelease(ctx context.Context, fluxCdSpe
276276
},
277277
Spec: helmv2.HelmReleaseSpec{
278278
Interval: metav1.Duration{Duration: helmReleaseReconcileInterval}, //TODO
279+
DriftDetection: &helmv2.DriftDetection{
280+
Mode: helmv2.DriftDetectionEnabled,
281+
},
279282
Chart: &helmv2.HelmChartTemplate{
280283
Spec: helmv2.HelmChartTemplateSpec{
281284
ReconcileStrategy: "Revision",
@@ -310,6 +313,9 @@ func (impl *HandlerServiceImpl) UpdateHelmRelease(ctx context.Context, fluxCdSpe
310313
impl.logger.Errorw("error in getting helm release", "name", name, "namespace", namespace, "err", err)
311314
return nil, fmt.Errorf("failed to get HelmRelease: %w", err)
312315
}
316+
existing.Spec.DriftDetection = &helmv2.DriftDetection{
317+
Mode: helmv2.DriftDetectionEnabled,
318+
}
313319
existing.Spec.Interval = metav1.Duration{Duration: helmReleaseReconcileInterval}
314320
existing.Spec.Chart = &helmv2.HelmChartTemplate{
315321
Spec: helmv2.HelmChartTemplateSpec{

pkg/workflow/dag/WorkflowDagExecutor.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,13 @@ type WorkflowDagExecutor interface {
112112
UpdateWorkflowRunnerStatusForDeployment(appIdentifier *helmBean.AppIdentifier, wfr *pipelineConfig.CdWorkflowRunner, skipReleaseNotFound bool) bool
113113

114114
BuildCiArtifactRequestForWebhook(event pipeline.ExternalCiWebhookDto) (*bean2.CiArtifactWebhookRequest, error)
115-
UpdateWorkflowRunnerStatusForFluxDeployment(appIdentifier *bean8.FluxAppIdentifier, wfr *pipelineConfig.CdWorkflowRunner, hash string) bool
115+
UpdateWorkflowRunnerStatusForFluxDeployment(appIdentifier *bean8.FluxAppIdentifier, wfr *pipelineConfig.CdWorkflowRunner, pipelineOverride *chartConfig.PipelineOverride) bool
116116
}
117117

118118
type WorkflowDagExecutorImpl struct {
119119
logger *zap.SugaredLogger
120120
pipelineRepository pipelineConfig.PipelineRepository
121+
pipelineOverrideRepository chartConfig.PipelineOverrideRepository
121122
cdWorkflowRepository pipelineConfig.CdWorkflowRepository
122123
ciArtifactRepository repository.CiArtifactRepository
123124
enforcerUtil rbac.EnforcerUtil
@@ -159,6 +160,7 @@ type WorkflowDagExecutorImpl struct {
159160
}
160161

161162
func NewWorkflowDagExecutorImpl(Logger *zap.SugaredLogger, pipelineRepository pipelineConfig.PipelineRepository,
163+
pipelineOverrideRepository chartConfig.PipelineOverrideRepository,
162164
cdWorkflowRepository pipelineConfig.CdWorkflowRepository,
163165
ciArtifactRepository repository.CiArtifactRepository,
164166
enforcerUtil rbac.EnforcerUtil,
@@ -193,6 +195,7 @@ func NewWorkflowDagExecutorImpl(Logger *zap.SugaredLogger, pipelineRepository pi
193195
) *WorkflowDagExecutorImpl {
194196
wde := &WorkflowDagExecutorImpl{logger: Logger,
195197
pipelineRepository: pipelineRepository,
198+
pipelineOverrideRepository: pipelineOverrideRepository,
196199
cdWorkflowRepository: cdWorkflowRepository,
197200
ciArtifactRepository: ciArtifactRepository,
198201
enforcerUtil: enforcerUtil,
@@ -1299,7 +1302,8 @@ func (impl *WorkflowDagExecutorImpl) BuildCiArtifactRequestForWebhook(event pipe
12991302
return request, nil
13001303
}
13011304

1302-
func (impl *WorkflowDagExecutorImpl) UpdateWorkflowRunnerStatusForFluxDeployment(appIdentifier *bean8.FluxAppIdentifier, wfr *pipelineConfig.CdWorkflowRunner, hash string) bool {
1305+
func (impl *WorkflowDagExecutorImpl) UpdateWorkflowRunnerStatusForFluxDeployment(appIdentifier *bean8.FluxAppIdentifier, wfr *pipelineConfig.CdWorkflowRunner,
1306+
pipelineOverride *chartConfig.PipelineOverride) bool {
13031307
fluxAppDetail, err := impl.fluxApplicationService.GetFluxAppDetail(context.Background(), appIdentifier)
13041308
if err != nil {
13051309
impl.logger.Errorw("error in getting helm app release status", "appIdentifier", appIdentifier, "err", err)
@@ -1310,7 +1314,7 @@ func (impl *WorkflowDagExecutorImpl) UpdateWorkflowRunnerStatusForFluxDeployment
13101314
wfr.FinishedOn = time.Now()
13111315
return true
13121316
}
1313-
if !matchShortHash(fluxAppDetail.LastObservedVersion, hash) {
1317+
if !impl.checkIfFluxPipelineEventIsValid(fluxAppDetail.LastObservedVersion, pipelineOverride) {
13141318
return false
13151319
}
13161320
wfr.FinishedOn = time.Now()
@@ -1331,24 +1335,39 @@ func (impl *WorkflowDagExecutorImpl) UpdateWorkflowRunnerStatusForFluxDeployment
13311335
return true
13321336
}
13331337

1334-
// matchShortHash compares the short Git hash embedded in the version string
1338+
func (impl *WorkflowDagExecutorImpl) checkIfFluxPipelineEventIsValid(lastObservedVersion string, pipelineOverride *chartConfig.PipelineOverride) bool {
1339+
gitHash := getShortHash(lastObservedVersion)
1340+
if !strings.HasPrefix(pipelineOverride.GitHash, gitHash) {
1341+
pipelineOverrideByHash, err := impl.pipelineOverrideRepository.FindByPipelineLikeTriggerGitHash(gitHash)
1342+
if err != nil {
1343+
impl.logger.Errorw("error on update application status", "gitHash", gitHash, "err", err)
1344+
return false
1345+
}
1346+
if pipelineOverrideByHash == nil || pipelineOverrideByHash.CommitTime.Before(pipelineOverride.CommitTime) {
1347+
// we have received trigger hash which is committed before this apps actual gitHash stored by us
1348+
// this means that the hash stored by us will be synced later, so we will drop this event
1349+
return false
1350+
}
1351+
}
1352+
return true
1353+
}
1354+
1355+
// getShortHash gets the short Git hash embedded in the version string
13351356
// with the beginning of the full Git commit hash.
13361357
//
13371358
// version: expected format like "4.22.1+<shortHash>.<buildNumber>"
13381359
// fullHash: expected to be a full 40-character Git commit SHA
1339-
//
1340-
// Returns true if the short hash is a prefix of the full hash.
1341-
func matchShortHash(version, fullHash string) bool {
1360+
func getShortHash(version string) string {
13421361
// Split version string at '+' to extract metadata
13431362
parts := strings.Split(version, "+")
13441363
if len(parts) < 2 {
1345-
return false // No metadata found
1364+
return "" // No metadata found
13461365
}
13471366

13481367
// Metadata might look like "2b6c6b2.2" → short hash + build number
13491368
metaParts := strings.Split(parts[1], ".")
13501369
shortHash := metaParts[0] // Take only the short hash before the dot
13511370

13521371
// Compare short hash with prefix of full hash
1353-
return strings.HasPrefix(fullHash, shortHash)
1372+
return shortHash
13541373
}

pkg/workflow/status/WorkflowStatusService.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,16 @@ func (impl *WorkflowStatusServiceImpl) CheckFluxAppStatusPeriodicallyAndUpdateIn
620620
Name: wfr.CdWorkflow.Pipeline.DeploymentAppName,
621621
IsKustomizeApp: false,
622622
}
623+
pipeline := wfr.CdWorkflow.Pipeline
623624

624-
pipelineOverride, err := impl.pipelineOverrideRepository.FindLatestByCdWorkflowId(wfr.CdWorkflowId)
625+
// getting latest pipelineOverride for app (by appId and envId)
626+
pipelineOverride, err := impl.pipelineOverrideRepository.FindLatestByAppIdAndEnvId(pipeline.AppId, pipeline.EnvironmentId, bean3.FluxCd)
625627
if err != nil {
626-
impl.logger.Errorw("error in getting latest pipeline override by cdWorkflowId", "err", err, "cdWorkflowId", wfr.CdWorkflowId)
628+
impl.logger.Errorw("error in getting latest pipelineOverride by appId and envId", "err", err, "appId", pipeline.AppId, "envId", pipeline.EnvironmentId)
627629
return err
628630
}
629631

630-
if isWfrUpdated := impl.workflowDagExecutor.UpdateWorkflowRunnerStatusForFluxDeployment(appIdentifier, wfr, pipelineOverride.GitHash); !isWfrUpdated {
632+
if isWfrUpdated := impl.workflowDagExecutor.UpdateWorkflowRunnerStatusForFluxDeployment(appIdentifier, wfr, pipelineOverride); !isWfrUpdated {
631633
continue
632634
}
633635

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj
22+
.vscode/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"server":{"deployment":{"image_tag":"{{.Tag}}","image":"{{.Name}}"}},"pipelineName": "{{.PipelineName}}","releaseVersion":"{{.ReleaseVersion}}","deploymentType": "{{.DeploymentType}}", "app": "{{.App}}", "env": "{{.Env}}", "appMetrics": {{.AppMetrics}}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
appVersion: "1.0"
3+
description: A Helm chart for Kubernetes
4+
name: reference-chart_5-2-0
5+
version: 5.2.0

0 commit comments

Comments
 (0)