/
health.go
210 lines (192 loc) · 5.51 KB
/
health.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
package stages
import (
"context"
"fmt"
"path"
kargoapi "github.com/akuity/kargo/api/v1alpha1"
libargocd "github.com/akuity/kargo/internal/argocd"
argocd "github.com/akuity/kargo/internal/controller/argocd/api/v1alpha1"
"github.com/akuity/kargo/internal/git"
)
func (r *reconciler) checkHealth(
ctx context.Context,
currentFreight kargoapi.FreightReference,
argoCDAppUpdates []kargoapi.ArgoCDAppUpdate,
) *kargoapi.Health {
if len(argoCDAppUpdates) == 0 {
return nil
}
h := kargoapi.Health{
// We'll start healthy and degrade as we find issues
Status: kargoapi.HealthStateHealthy,
ArgoCDApps: make([]kargoapi.ArgoCDAppStatus, len(argoCDAppUpdates)),
Issues: []string{},
}
if r.argocdClient == nil && len(argoCDAppUpdates) > 0 {
h.Status = kargoapi.HealthStateUnknown
h.Issues = []string{
"Argo CD integration is disabled on this controller; cannot assess" +
" the health or sync status of Argo CD Applications",
}
return &h
}
for i, update := range argoCDAppUpdates {
namespace := update.AppNamespace
if namespace == "" {
namespace = libargocd.Namespace()
}
h.ArgoCDApps[i] = kargoapi.ArgoCDAppStatus{
Namespace: namespace,
Name: update.AppName,
}
app, err := r.getArgoCDAppFn(ctx, r.argocdClient, namespace, update.AppName)
if err != nil {
h.ArgoCDApps[i].HealthStatus = kargoapi.ArgoCDAppHealthStatus{
Status: kargoapi.ArgoCDAppHealthStateUnknown,
}
h.ArgoCDApps[i].SyncStatus = kargoapi.ArgoCDAppSyncStatus{
Status: kargoapi.ArgoCDAppSyncStateUnknown,
}
h.Status = h.Status.Merge(kargoapi.HealthStateUnknown)
h.Issues = append(
h.Issues,
fmt.Sprintf(
"error finding Argo CD Application %q in namespace %q: %s",
update.AppName,
namespace,
err,
),
)
continue
}
if app == nil {
h.ArgoCDApps[i].HealthStatus = kargoapi.ArgoCDAppHealthStatus{
Status: kargoapi.ArgoCDAppHealthStateUnknown,
}
h.ArgoCDApps[i].SyncStatus = kargoapi.ArgoCDAppSyncStatus{
Status: kargoapi.ArgoCDAppSyncStateUnknown,
}
h.Status = h.Status.Merge(kargoapi.HealthStateUnknown)
h.Issues = append(
h.Issues,
fmt.Sprintf(
"unable to find Argo CD Application %q in namespace %q",
update.AppName,
namespace,
),
)
continue
}
h.ArgoCDApps[i].HealthStatus = kargoapi.ArgoCDAppHealthStatus{
Status: kargoapi.ArgoCDAppHealthState(app.Status.Health.Status),
Message: app.Status.Health.Message,
}
h.ArgoCDApps[i].SyncStatus = kargoapi.ArgoCDAppSyncStatus{
Status: kargoapi.ArgoCDAppSyncState(app.Status.Sync.Status),
Revision: app.Status.Sync.Revision,
Revisions: app.Status.Sync.Revisions,
}
// TODO: We should re-evaluate this soon. It may have been fixed in recent
// versions.
if len(app.Spec.Sources) > 0 {
h.Status = h.Status.Merge(kargoapi.HealthStateUnknown)
h.Issues = append(
h.Issues,
fmt.Sprintf(
"bugs in Argo CD currently prevent a comprehensive assessment of "+
"the health of multi-source Application %q in namespace %q",
update.AppName,
namespace,
),
)
continue
}
stageHealth, issue := stageHealthForAppHealth(app)
h.Status = h.Status.Merge(stageHealth)
if issue != "" {
h.Issues = append(h.Issues, issue)
}
var desiredRevision string
if app.Spec.Source.Chart == "" {
// This source points to a git repository
sourceGitRepoURL := git.NormalizeURL(app.Spec.Source.RepoURL)
for _, commit := range currentFreight.Commits {
if git.NormalizeURL(commit.RepoURL) == sourceGitRepoURL {
if commit.HealthCheckCommit != "" {
desiredRevision = commit.HealthCheckCommit
} else {
desiredRevision = commit.ID
}
}
break
}
} else {
// This source points to a Helm chart
for _, chart := range currentFreight.Charts {
// path.Join accounts for the possibility that chart.Name is empty
if path.Join(chart.RepoURL, chart.Name) == path.Join(app.Spec.Source.RepoURL, app.Spec.Source.Chart) {
desiredRevision = chart.Version
break
}
}
}
if desiredRevision != "" {
stageHealth, issue = stageHealthForAppSync(app, desiredRevision)
h.Status = h.Status.Merge(stageHealth)
if issue != "" {
h.Issues = append(h.Issues, issue)
}
}
}
return &h
}
func stageHealthForAppHealth(
app *argocd.Application,
) (kargoapi.HealthState, string) {
switch app.Status.Health.Status {
case argocd.HealthStatusProgressing, "":
return kargoapi.HealthStateProgressing,
fmt.Sprintf(
"Argo CD Application %q in namespace %q is progressing",
app.Name,
app.Namespace,
)
case argocd.HealthStatusHealthy:
return kargoapi.HealthStateHealthy, ""
default:
return kargoapi.HealthStateUnhealthy,
fmt.Sprintf(
"Argo CD Application %q in namespace %q has health state %q",
app.Name,
app.Namespace,
app.Status.Health.Status,
)
}
}
func stageHealthForAppSync(
app *argocd.Application,
revision string,
) (kargoapi.HealthState, string) {
if revision == "" {
// No specific revision required
return kargoapi.HealthStateHealthy, ""
}
if app.Operation != nil && app.Operation.Sync != nil {
// Still syncing
return kargoapi.HealthStateProgressing, fmt.Sprintf(
"Argo CD Application %q in namespace %q is being synced",
app.Name,
app.Namespace,
)
}
// Done syncing
if app.Status.Sync.Revision != revision {
return kargoapi.HealthStateUnhealthy, fmt.Sprintf(
"Argo CD Application %q in namespace %q is not synced to revision %q",
app.Name,
app.Namespace,
revision,
)
}
return kargoapi.HealthStateHealthy, ""
}