Skip to content

Commit 995719f

Browse files
committed
fix: added IsApplicationObjectUpdated logic for application update event
1 parent 3127716 commit 995719f

3 files changed

Lines changed: 90 additions & 40 deletions

File tree

kubewatch/pkg/resource/application/bean.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,3 @@ type applicationDetail struct {
2626
StatusTime time.Time `json:"statusTime"`
2727
ClusterId int `json:"clusterId"`
2828
}
29-
30-
// isNewDeploymentFound checks if a new deployment is found by comparing the history of old and new application objects.
31-
func isNewDeploymentFound(oldAppObj, newAppObj *v1alpha1.Application) bool {
32-
if len(oldAppObj.Status.History) < len(newAppObj.Status.History) {
33-
return true
34-
} else if len(oldAppObj.Status.History) != 0 && len(oldAppObj.Status.History) == len(newAppObj.Status.History) {
35-
return oldAppObj.Status.History.LastRevisionHistory().ID < newAppObj.Status.History.LastRevisionHistory().ID
36-
}
37-
return false
38-
}
39-
40-
// getApplicationLastSyncedResourcesCount returns the count of resources that were last synced in the application.
41-
func getApplicationLastSyncedResourcesCount(appObj *v1alpha1.Application) int {
42-
if appObj.Status.OperationState == nil || appObj.Status.OperationState.SyncResult == nil {
43-
return 0
44-
}
45-
return len(appObj.Status.OperationState.SyncResult.Resources)
46-
}

kubewatch/pkg/resource/application/handler.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,33 +60,16 @@ func (impl *InformerImpl) GetSharedInformer(clusterLabels *informerBean.ClusterL
6060
statusTime := time.Now()
6161
if oldApp, ok := old.(*applicationBean.Application); ok {
6262
if newApp, ok := new.(*applicationBean.Application); ok {
63-
if isNewDeploymentFound(oldApp, newApp) {
63+
// Check if the application has a new deployment history
64+
if isNewDeploymentHistoryFound(oldApp, newApp) {
6465
impl.logger.Debugw("ARGO_CD_APPLICATION: new deployment detected", "appName", newApp.Name, "status", newApp.Status.Health.Status)
6566
impl.sendAppUpdate(clusterLabels.ClusterId, newApp, statusTime)
6667
} else {
67-
impl.logger.Debugw("ARGO_CD_APPLICATION: old deployment detected for update", "appName", oldApp.Name, "status", oldApp.Status.Health.Status)
68-
oldRevision := oldApp.Status.Sync.Revision
69-
newRevision := newApp.Status.Sync.Revision
70-
oldStatus := string(oldApp.Status.Health.Status)
71-
newStatus := string(newApp.Status.Health.Status)
72-
newSyncStatus := string(newApp.Status.Sync.Status)
73-
oldSyncStatus := string(oldApp.Status.Sync.Status)
74-
oldAppLastSyncedResourcesCount := getApplicationLastSyncedResourcesCount(oldApp)
75-
newAppLastSyncedResourcesCount := getApplicationLastSyncedResourcesCount(newApp)
76-
if (oldRevision != newRevision) ||
77-
(oldStatus != newStatus) ||
78-
(newSyncStatus != oldSyncStatus) ||
79-
(oldAppLastSyncedResourcesCount != newAppLastSyncedResourcesCount) {
68+
if IsApplicationObjectUpdated(impl.logger, oldApp, newApp) {
8069
impl.sendAppUpdate(clusterLabels.ClusterId, newApp, statusTime)
81-
impl.logger.Debugw("ARGO_CD_APPLICATION: send update event for application object", "appName", oldApp.Name, "oldRevision", oldRevision, "newRevision",
82-
newRevision, "oldStatus", oldStatus, "newStatus", newStatus,
83-
"newSyncStatus", newSyncStatus, "oldSyncStatus", oldSyncStatus,
84-
"oldAppLastSyncedResourcesCount", oldAppLastSyncedResourcesCount, "newAppLastSyncedResourcesCount", newAppLastSyncedResourcesCount)
70+
impl.logger.Debugw("ARGO_CD_APPLICATION: send update event for application object", "appName", oldApp.Name)
8571
} else {
86-
impl.logger.Debugw("ARGO_CD_APPLICATION: skip updating event for application object", "appName", oldApp.Name, "oldRevision", oldRevision, "newRevision",
87-
newRevision, "oldStatus", oldStatus, "newStatus", newStatus,
88-
"newSyncStatus", newSyncStatus, "oldSyncStatus", oldSyncStatus,
89-
"oldAppLastSyncedResourcesCount", oldAppLastSyncedResourcesCount, "newAppLastSyncedResourcesCount", newAppLastSyncedResourcesCount)
72+
impl.logger.Debugw("ARGO_CD_APPLICATION: skip updating event for application object", "appName", oldApp.Name)
9073
}
9174
}
9275
} else {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 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 application
18+
19+
import (
20+
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
21+
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
22+
"go.uber.org/zap"
23+
)
24+
25+
// isNewDeploymentHistoryFound checks
26+
// if a new deployment is found by
27+
// comparing the history of old and new application objects.
28+
func isNewDeploymentHistoryFound(oldAppObj, newAppObj *v1alpha1.Application) bool {
29+
if len(oldAppObj.Status.History) < len(newAppObj.Status.History) {
30+
return true
31+
} else if len(oldAppObj.Status.History) != 0 && len(oldAppObj.Status.History) == len(newAppObj.Status.History) {
32+
return oldAppObj.Status.History.LastRevisionHistory().ID < newAppObj.Status.History.LastRevisionHistory().ID
33+
}
34+
return false
35+
}
36+
37+
// getApplicationLastSyncedResourcesCount returns the count of resources that were last synced in the application.
38+
func getApplicationLastSyncedResourcesCount(appObj *v1alpha1.Application) int {
39+
if appObj.Status.OperationState == nil || appObj.Status.OperationState.SyncResult == nil {
40+
return 0
41+
}
42+
return len(appObj.Status.OperationState.SyncResult.Resources)
43+
}
44+
45+
func getApplicationOperationRevision(appObj *v1alpha1.Application) string {
46+
if appObj.Status.OperationState == nil || appObj.Status.OperationState.Operation.Sync == nil {
47+
return ""
48+
}
49+
return appObj.Status.OperationState.Operation.Sync.Revision
50+
}
51+
52+
func getApplicationOperationPhase(appObj *v1alpha1.Application) synccommon.OperationPhase {
53+
if appObj.Status.OperationState == nil {
54+
return ""
55+
}
56+
return appObj.Status.OperationState.Phase
57+
}
58+
59+
func IsApplicationObjectUpdated(logger *zap.SugaredLogger, oldAppObj, newAppObj *v1alpha1.Application) bool {
60+
// Check if the application sync revision has changed
61+
oldRevision := oldAppObj.Status.Sync.Revision
62+
newRevision := newAppObj.Status.Sync.Revision
63+
// Check if the operation revision has changed
64+
oldOperationRevision := getApplicationOperationRevision(oldAppObj)
65+
newOperationRevision := getApplicationOperationRevision(newAppObj)
66+
// Check if the health status has changed
67+
oldStatus := string(oldAppObj.Status.Health.Status)
68+
newStatus := string(newAppObj.Status.Health.Status)
69+
// Check if the operation phase has changed
70+
oldOperationPhase := getApplicationOperationPhase(oldAppObj)
71+
newOperationPhase := getApplicationOperationPhase(newAppObj)
72+
// Check if the last synced resources count has changed
73+
oldAppLastSyncedResourcesCount := getApplicationLastSyncedResourcesCount(oldAppObj)
74+
newAppLastSyncedResourcesCount := getApplicationLastSyncedResourcesCount(newAppObj)
75+
76+
logger.Debugw("oldRevision", oldRevision, "newRevision", newRevision,
77+
"oldOperationRevision", oldOperationRevision, "newOperationRevision", newOperationRevision,
78+
"oldStatus", oldStatus, "newStatus", newStatus,
79+
"oldOperationPhase", oldOperationPhase, "newOperationPhase", newOperationPhase,
80+
"oldAppLastSyncedResourcesCount", oldAppLastSyncedResourcesCount, "newAppLastSyncedResourcesCount", newAppLastSyncedResourcesCount)
81+
82+
return (oldRevision != newRevision) || (newOperationRevision != oldOperationRevision) ||
83+
(oldStatus != newStatus) || (oldOperationPhase != newOperationPhase) ||
84+
(oldAppLastSyncedResourcesCount != newAppLastSyncedResourcesCount)
85+
}

0 commit comments

Comments
 (0)