Skip to content

Commit 1739472

Browse files
adityathebemoshloop
authored andcommitted
feat: notification health
1 parent 51265a1 commit 1739472

5 files changed

+170
-9
lines changed

pkg/health/health_fixtures_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestFixtures(t *testing.T) {
1616
}
1717

1818
for _, file := range files {
19-
// if file != "testdata/Kubernetes/MissionControl/scrapeConfig-minor-delay.yaml" {
19+
// if file != "testdata/Kubernetes/MissionControl/notification-with-error.yaml" {
2020
// continue
2121
// }
2222

pkg/health/health_flanksource.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -176,25 +176,53 @@ func getNotificationHealth(obj *unstructured.Unstructured) (*HealthStatus, error
176176
return nil, err
177177
}
178178

179-
var h Health = HealthUnknown
179+
status := &HealthStatus{
180+
Health: HealthUnknown,
181+
Ready: true,
182+
}
183+
184+
if errorMessage != "" {
185+
status.Message = errorMessage
186+
status.Health = HealthUnhealthy
187+
status.Ready = false
188+
return status, nil
189+
}
190+
180191
if sentCount > 0 {
181-
h = HealthHealthy
192+
status.Health = HealthHealthy
182193
if failedCount > 0 || pendingCount > 0 {
183-
h = HealthWarning
194+
status.Health = HealthWarning
184195
}
185196
} else {
186197
if pendingCount > 0 {
187-
h = HealthWarning
198+
status.Health = HealthWarning
188199
}
189200
if failedCount > 0 {
190-
h = HealthUnhealthy
201+
status.Health = HealthUnhealthy
191202
}
192203
}
193204

194-
status := &HealthStatus{Health: h}
205+
// Check lastFailed timestamp
206+
lastFailedTime, found, err := unstructured.NestedString(obj.Object, "status", "lastFailed")
207+
if err != nil {
208+
return nil, err
209+
}
210+
211+
if found && lastFailedTime != "" {
212+
parsedLastFailedTime, err := time.Parse(time.RFC3339, lastFailedTime)
213+
if err != nil {
214+
return nil, fmt.Errorf("failed to parse lastFailed timestamp: %w", err)
215+
}
195216

196-
if errorMessage != "" {
197-
status.Message = errorMessage
217+
timeSinceLastFailure := time.Since(parsedLastFailedTime)
218+
219+
if timeSinceLastFailure <= 12*time.Hour {
220+
status.Health = HealthWarning
221+
status.Message = fmt.Sprintf("Failed %s ago", duration.HumanDuration(timeSinceLastFailure))
222+
if timeSinceLastFailure <= time.Hour {
223+
status.Health = HealthUnhealthy
224+
}
225+
}
198226
}
199227

200228
return status, nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: mission-control.flanksource.com/v1
2+
kind: Notification
3+
metadata:
4+
annotations:
5+
meta.helm.sh/release-name: playbooks-ai
6+
meta.helm.sh/release-namespace: mc
7+
expected-ready: 'true'
8+
expected-health: 'warning'
9+
expected-message: "Failed 120m ago"
10+
creationTimestamp: "2025-03-11T07:12:44Z"
11+
labels:
12+
app.kubernetes.io/managed-by: Helm
13+
name: notify-recommender-playbook
14+
namespace: mc
15+
resourceVersion: "147557980"
16+
uid: a37a3b53-951d-40f3-a65a-b540950ceb0f
17+
spec:
18+
events:
19+
- config.unhealthy
20+
- config.warning
21+
fallback:
22+
connection: connection://mc/flanksource-slack
23+
filter: tags.cluster == 'homelab'
24+
inhibitions:
25+
- direction: incoming
26+
from: Kubernetes::Pod
27+
to:
28+
- Kubernetes::Deployment
29+
- Kubernetes::ReplicaSet
30+
- Kubernetes::DaemonSet
31+
- Kubernetes::StatefulSet
32+
- direction: outgoing
33+
from: Kubernetes::Node
34+
to:
35+
- Kubernetes::Pod
36+
repeatInterval: 1d
37+
to:
38+
playbook: mc/recommend-playbook
39+
waitFor: 20s
40+
status:
41+
failed: 1
42+
lastSent: "2025-03-14T04:37:50Z"
43+
lastFailed: "@now-2h"
44+
sent: 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
apiVersion: mission-control.flanksource.com/v1
2+
kind: Notification
3+
metadata:
4+
annotations:
5+
meta.helm.sh/release-name: playbooks-ai
6+
meta.helm.sh/release-namespace: mc
7+
expected-ready: 'true'
8+
expected-health: 'unhealthy'
9+
expected-message: "Failed 30m ago"
10+
creationTimestamp: "2025-03-11T07:12:44Z"
11+
labels:
12+
app.kubernetes.io/managed-by: Helm
13+
name: notify-recommender-playbook
14+
namespace: mc
15+
resourceVersion: "147557980"
16+
uid: a37a3b53-951d-40f3-a65a-b540950ceb0f
17+
spec:
18+
events:
19+
- config.unhealthy
20+
- config.warning
21+
fallback:
22+
connection: connection://mc/flanksource-slack
23+
filter: tags.cluster == 'homelab'
24+
inhibitions:
25+
- direction: incoming
26+
from: Kubernetes::Pod
27+
to:
28+
- Kubernetes::Deployment
29+
- Kubernetes::ReplicaSet
30+
- Kubernetes::DaemonSet
31+
- Kubernetes::StatefulSet
32+
- direction: outgoing
33+
from: Kubernetes::Node
34+
to:
35+
- Kubernetes::Pod
36+
repeatInterval: 1d
37+
to:
38+
playbook: mc/recommend-playbook
39+
waitFor: 20s
40+
status:
41+
failed: 1
42+
lastSent: "2025-03-14T04:37:50Z"
43+
lastFailed: "@now-30m"
44+
sent: 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apiVersion: mission-control.flanksource.com/v1
2+
kind: Notification
3+
metadata:
4+
annotations:
5+
meta.helm.sh/release-name: playbooks-ai
6+
meta.helm.sh/release-namespace: mc
7+
expected-ready: 'false'
8+
expected-health: 'unhealthy'
9+
expected-message: "tcp: lookup slack.com: no such host"
10+
creationTimestamp: "2025-03-11T07:12:44Z"
11+
labels:
12+
app.kubernetes.io/managed-by: Helm
13+
name: notify-recommender-playbook
14+
namespace: mc
15+
resourceVersion: "147557980"
16+
uid: a37a3b53-951d-40f3-a65a-b540950ceb0f
17+
spec:
18+
events:
19+
- config.unhealthy
20+
- config.warning
21+
fallback:
22+
connection: connection://mc/flanksource-slack
23+
filter: tags.cluster == 'homelab'
24+
inhibitions:
25+
- direction: incoming
26+
from: Kubernetes::Pod
27+
to:
28+
- Kubernetes::Deployment
29+
- Kubernetes::ReplicaSet
30+
- Kubernetes::DaemonSet
31+
- Kubernetes::StatefulSet
32+
- direction: outgoing
33+
from: Kubernetes::Node
34+
to:
35+
- Kubernetes::Pod
36+
repeatInterval: 1d
37+
to:
38+
playbook: mc/recommend-playbook
39+
waitFor: 20s
40+
status:
41+
failed: 1
42+
lastSent: "2025-03-14T04:37:50Z"
43+
lastFailed: "@now-2h"
44+
sent: 4
45+
error: "tcp: lookup slack.com: no such host"

0 commit comments

Comments
 (0)