Skip to content

Commit dbb11e7

Browse files
committed
Add custom healthcheck for NATS resources
Signed-off-by: DanielS <[email protected]>
1 parent c39fde7 commit dbb11e7

35 files changed

+813
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
hs = {}
2+
local healthy_cons = { Created=true, Updated=true, Noop=true, SkipCreate=true, SkipUpdate=true }
3+
if obj.status ~= nil then
4+
if obj.status.conditions ~= nil then
5+
for i, condition in ipairs(obj.status.conditions) do
6+
if condition.status == "False" then
7+
hs.status = "Degraded"
8+
hs.message = condition.message
9+
return hs
10+
end
11+
if condition.type == "Ready" and healthy_cons[condition.reason] then
12+
hs.status = "Healthy"
13+
hs.message = condition.message
14+
return hs
15+
end
16+
if condition.type == "Ready" and not healthy_cons[condition.reason] then
17+
hs.status = "Progressing"
18+
hs.message = condition.message
19+
local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d]+)%.%d+Z"
20+
local year, month, day, hour, minute, seconds = condition.lastTransitionTime:match(pattern)
21+
local event_time = os.time{year = year, month = month, day = day, hour = hour,
22+
min = minute, sec = seconds}
23+
if os.difftime(os.time(), event_time) > 30 then
24+
hs.status = "Degraded"
25+
hs.message = "Trying to create resource for more than 30 seconds"
26+
end
27+
return hs
28+
end
29+
end
30+
end
31+
end
32+
hs.status = "Progressing"
33+
hs.message = "Waiting for Nacks"
34+
return hs
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests:
2+
- healthStatus:
3+
status: Progressing
4+
message: Waiting for Nacks
5+
inputPath: test_data/progressing.yaml
6+
- healthStatus:
7+
status: Degraded
8+
message: 'failed to create account'
9+
inputPath: test_data/degraded_failure.yaml
10+
- healthStatus:
11+
status: Degraded
12+
message: 'Trying to create resource for more than 30 seconds'
13+
inputPath: test_data/degraded_stuck.yaml
14+
- healthStatus:
15+
status: Healthy
16+
message: 'Account successfully updated'
17+
inputPath: test_data/healthy_updated.yaml
18+
- healthStatus:
19+
status: Healthy
20+
message: 'Account successfully created'
21+
inputPath: test_data/healthy_created.yaml
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: jetstream.nats.io/v1beta2
2+
kind: Account
3+
metadata:
4+
name: account-name
5+
spec:
6+
name: a
7+
servers:
8+
- nats://nats:4222
9+
tls:
10+
secret:
11+
name: nack-a-tls
12+
ca: "ca.crt"
13+
cert: "tls.crt"
14+
key: "tls.key"
15+
status:
16+
conditions:
17+
- lastTransitionTime: '2025-05-14T14:25:20.89689315Z'
18+
message: >-
19+
failed to create account
20+
reason: Errored
21+
status: 'False'
22+
type: Ready
23+
observedGeneration: 0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: jetstream.nats.io/v1beta2
2+
kind: Account
3+
metadata:
4+
name: account-name
5+
spec:
6+
name: a
7+
servers:
8+
- nats://nats:4222
9+
tls:
10+
secret:
11+
name: nack-a-tls
12+
ca: "ca.crt"
13+
cert: "tls.crt"
14+
key: "tls.key"
15+
status:
16+
conditions:
17+
- lastTransitionTime: '2025-05-14T14:22:01.962295009Z'
18+
message: Creating account
19+
reason: Creating
20+
status: 'True'
21+
type: Ready
22+
observedGeneration: 0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: jetstream.nats.io/v1beta2
2+
kind: Account
3+
metadata:
4+
name: account-name
5+
spec:
6+
name: a
7+
servers:
8+
- nats://nats:4222
9+
tls:
10+
secret:
11+
name: nack-a-tls
12+
ca: "ca.crt"
13+
cert: "tls.crt"
14+
key: "tls.key"
15+
status:
16+
conditions:
17+
- lastTransitionTime: '2025-05-14T14:22:01.962295009Z'
18+
message: Account successfully created
19+
reason: Created
20+
status: 'True'
21+
type: Ready
22+
observedGeneration: 1
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: jetstream.nats.io/v1beta2
2+
kind: Account
3+
metadata:
4+
name: account-name
5+
spec:
6+
name: a
7+
servers:
8+
- nats://nats:4222
9+
tls:
10+
secret:
11+
name: nack-a-tls
12+
ca: "ca.crt"
13+
cert: "tls.crt"
14+
key: "tls.key"
15+
status:
16+
conditions:
17+
- lastTransitionTime: '2025-05-14T14:22:01.962295009Z'
18+
message: Account successfully updated
19+
reason: Updated
20+
status: 'True'
21+
type: Ready
22+
observedGeneration: 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
apiVersion: jetstream.nats.io/v1beta2
2+
kind: Account
3+
metadata:
4+
name: account-name
5+
spec:
6+
name: a
7+
servers:
8+
- nats://nats:4222
9+
tls:
10+
secret:
11+
name: nack-a-tls
12+
ca: "ca.crt"
13+
cert: "tls.crt"
14+
key: "tls.key"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
hs = {}
2+
local healthy_cons = { Created=true, Updated=true, Noop=true, SkipCreate=true, SkipUpdate=true }
3+
if obj.status ~= nil then
4+
if obj.status.conditions ~= nil then
5+
for i, condition in ipairs(obj.status.conditions) do
6+
if condition.status == "False" then
7+
hs.status = "Degraded"
8+
hs.message = condition.message
9+
return hs
10+
end
11+
if condition.type == "Ready" and healthy_cons[condition.reason] then
12+
hs.status = "Healthy"
13+
hs.message = condition.message
14+
return hs
15+
end
16+
if condition.type == "Ready" and not healthy_cons[condition.reason] then
17+
hs.status = "Progressing"
18+
hs.message = condition.message
19+
local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d]+)%.%d+Z"
20+
local year, month, day, hour, minute, seconds = condition.lastTransitionTime:match(pattern)
21+
local event_time = os.time{year = year, month = month, day = day, hour = hour,
22+
min = minute, sec = seconds}
23+
if os.difftime(os.time(), event_time) > 30 then
24+
hs.status = "Degraded"
25+
hs.message = "Trying to create resource for more than 30 seconds"
26+
end
27+
return hs
28+
end
29+
end
30+
end
31+
end
32+
hs.status = "Progressing"
33+
hs.message = "Waiting for Nacks"
34+
return hs
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests:
2+
- healthStatus:
3+
status: Progressing
4+
message: Waiting for Nacks
5+
inputPath: test_data/progressing.yaml
6+
- healthStatus:
7+
status: Degraded
8+
message: 'failed to create consumer'
9+
inputPath: test_data/degraded_failure.yaml
10+
- healthStatus:
11+
status: Degraded
12+
message: 'Trying to create resource for more than 30 seconds'
13+
inputPath: test_data/degraded_stuck.yaml
14+
- healthStatus:
15+
status: Healthy
16+
message: 'Consumer successfully updated'
17+
inputPath: test_data/healthy_updated.yaml
18+
- healthStatus:
19+
status: Healthy
20+
message: 'Consumer successfully created'
21+
inputPath: test_data/healthy_created.yaml
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: jetstream.nats.io/v1beta2
2+
kind: Consumer
3+
metadata:
4+
name: consumer-name
5+
spec:
6+
streamName: mystream
7+
durableName: my-pull-consumer
8+
deliverPolicy: all
9+
filterSubject: orders.received
10+
maxDeliver: 20
11+
ackPolicy: explicit
12+
status:
13+
conditions:
14+
- lastTransitionTime: '2025-05-14T14:25:20.89689315Z'
15+
message: >-
16+
failed to create consumer
17+
reason: Errored
18+
status: 'False'
19+
type: Ready
20+
observedGeneration: 0

0 commit comments

Comments
 (0)