-
Notifications
You must be signed in to change notification settings - Fork 16
/
applicationhealthcomment_controller.go
175 lines (153 loc) · 6.79 KB
/
applicationhealthcomment_controller.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
/*
Copyright 2021.
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 controller
import (
"context"
"slices"
"time"
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/gitops-engine/pkg/health"
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
argocdcommenterv1 "github.com/int128/argocd-commenter/api/v1"
"github.com/int128/argocd-commenter/internal/argocd"
"github.com/int128/argocd-commenter/internal/controller/eventfilter"
"github.com/int128/argocd-commenter/internal/notification"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
var (
// When an application is synced but the health status is not changed,
// the controller will evaluate the health status after this time.
requeueTimeToEvaluateHealthStatusAfterSyncOperation = 3 * time.Second
)
// ApplicationHealthCommentReconciler reconciles a change of Application object.
// It creates a comment when the health status is changed.
type ApplicationHealthCommentReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Notification notification.Client
}
//+kubebuilder:rbac:groups=argoproj.io,resources=applications,verbs=get;watch;list
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;watch;list
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=argocdcommenter.int128.github.io,resources=applicationhealths,verbs=get;list;watch;create;update;patch
//+kubebuilder:rbac:groups=argocdcommenter.int128.github.io,resources=applicationhealths/status,verbs=get;update;patch
func (r *ApplicationHealthCommentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
var app argocdv1alpha1.Application
if err := r.Get(ctx, req.NamespacedName, &app); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if !app.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
}
var appHealth argocdcommenterv1.ApplicationHealth
if err := r.Client.Get(ctx, req.NamespacedName, &appHealth); err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "unable to get the ApplicationHealth")
return ctrl.Result{}, err
}
appHealth.ObjectMeta = metav1.ObjectMeta{
Namespace: req.Namespace,
Name: req.Name,
}
if err := ctrl.SetControllerReference(&app, &appHealth, r.Scheme); err != nil {
logger.Error(err, "unable to set the controller reference to the ApplicationHealth")
return ctrl.Result{}, err
}
if err := r.Client.Create(ctx, &appHealth); err != nil {
logger.Error(err, "unable to create an ApplicationHealth")
return ctrl.Result{}, err
}
logger.Info("created an ApplicationHealth")
}
sourceRevisions := argocd.GetSourceRevisions(app)
if len(sourceRevisions) == 0 {
return ctrl.Result{}, nil
}
currentRevision := sourceRevisions[0].Revision
if appHealth.Status.LastHealthyRevision == currentRevision {
logger.Info("current revision is already healthy", "revision", currentRevision)
return ctrl.Result{}, nil
}
// Evaluate the health status only if the sync operation is succeeded.
phase := argocd.GetSyncOperationPhase(app)
if phase != synccommon.OperationSucceeded {
return ctrl.Result{}, nil
}
syncOperationFinishedAt := argocd.GetSyncOperationFinishedAt(app)
if syncOperationFinishedAt == nil {
return ctrl.Result{}, nil
}
// If this controller is run just after the sync operation,
// it will evaluate the health status after a few seconds.
// https://github.com/int128/argocd-commenter/issues/1044
if time.Since(syncOperationFinishedAt.Time) < requeueTimeToEvaluateHealthStatusAfterSyncOperation {
logger.Info("Requeue later to evaluate the health status", "after", requeueTimeToEvaluateHealthStatusAfterSyncOperation,
"syncOperationFinishedAt", syncOperationFinishedAt)
return ctrl.Result{RequeueAfter: requeueTimeToEvaluateHealthStatusAfterSyncOperation}, nil
}
argocdURL, err := argocd.GetExternalURL(ctx, r.Client, req.Namespace)
if err != nil {
logger.Info("unable to determine Argo CD URL", "error", err)
}
if err := r.Notification.CreateCommentsOnHealthChanged(ctx, app, argocdURL); err != nil {
r.Recorder.Eventf(&app, corev1.EventTypeWarning, "CreateCommentError",
"unable to create a comment on health status %s: %s", app.Status.Health.Status, err)
} else {
r.Recorder.Eventf(&app, corev1.EventTypeNormal, "CreatedComment",
"created a comment on health status %s", app.Status.Health.Status)
}
if app.Status.Health.Status != health.HealthStatusHealthy {
return ctrl.Result{}, nil
}
patch := client.MergeFrom(appHealth.DeepCopy())
appHealth.Status.LastHealthyRevision = currentRevision
if err := r.Client.Status().Patch(ctx, &appHealth, patch); err != nil {
logger.Error(err, "unable to patch lastHealthyRevision")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
r.Recorder.Eventf(&appHealth, corev1.EventTypeNormal, "UpdatedLastHealthyRevision",
"patched lastHealthyRevision to %s", currentRevision)
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ApplicationHealthCommentReconciler) SetupWithManager(mgr ctrl.Manager) error {
r.Recorder = mgr.GetEventRecorderFor("application-health-comment")
return ctrl.NewControllerManagedBy(mgr).
Named("applicationHealthComment").
For(&argocdv1alpha1.Application{}).
WithEventFilter(eventfilter.ApplicationChanged(filterApplicationHealthStatusForComment)).
Complete(r)
}
func filterApplicationHealthStatusForComment(appOld, appNew argocdv1alpha1.Application) bool {
// When the health status is changed
healthOld, healthNew := appOld.Status.Health.Status, appNew.Status.Health.Status
if healthOld != healthNew && slices.Contains(notification.HealthStatusesForComment, healthNew) {
return true
}
// When an application is synced but the health status is not changed,
// the controller will evaluate the health status after sync.
phaseOld, phaseNew := argocd.GetSyncOperationPhase(appOld), argocd.GetSyncOperationPhase(appNew)
if phaseOld != phaseNew && phaseNew == synccommon.OperationSucceeded {
return true
}
return false
}