Skip to content

Commit 51265a1

Browse files
adityathebemoshloop
authored andcommitted
fix: scrapeconfig stale check when fix-time cron is used
1 parent 9cb4a1c commit 51265a1

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

pkg/health/health_fixtures_test.go

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

1818
for _, file := range files {
19+
// if file != "testdata/Kubernetes/MissionControl/scrapeConfig-minor-delay.yaml" {
20+
// continue
21+
// }
22+
1923
testFixture(t, file)
2024
}
2125
}

pkg/health/health_flanksource.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ func getScrapeConfigHealth(obj *unstructured.Unstructured) (*HealthStatus, error
122122
return nil, fmt.Errorf("failed to parse lastRun timestamp: %w", err)
123123
}
124124

125-
var schedule time.Duration
125+
var nextRuntime time.Time
126126
if scheduleRaw, _, err := unstructured.NestedString(obj.Object, "spec", "schedule"); err != nil {
127127
return nil, fmt.Errorf("failed to parse scraper schedule: %w", err)
128128
} else if scheduleRaw == "" {
129-
schedule = time.Hour // The default schedule
129+
nextRuntime = parsedLastRuntime.Add(time.Hour) // The default schedule
130130
} else {
131131
parsedSchedule, err := cron.ParseStandard(scheduleRaw)
132132
if err != nil {
@@ -137,18 +137,18 @@ func getScrapeConfigHealth(obj *unstructured.Unstructured) (*HealthStatus, error
137137
}, nil
138138
}
139139

140-
schedule = time.Until(parsedSchedule.Next(time.Now()))
140+
nextRuntime = parsedSchedule.Next(parsedLastRuntime)
141141
}
142142

143-
elapsed := time.Since(parsedLastRuntime)
144-
if elapsed > schedule*2 {
145-
status.Health = HealthUnhealthy
143+
// If the ScrapeConfig is few minutes behind the schedule, it's not healthy
144+
if time.Since(nextRuntime) > time.Minute*10 {
146145
status.Status = "Stale"
147-
status.Message = fmt.Sprintf("scraper hasn't run for %s", duration.HumanDuration(elapsed))
148-
} else if elapsed > schedule && status.Health != HealthUnhealthy {
149146
status.Health = HealthWarning
150-
status.Status = "Stale"
151-
status.Message = fmt.Sprintf("scraper hasn't run for %s", duration.HumanDuration(elapsed))
147+
status.Message = fmt.Sprintf("scraper hasn't run for %s", duration.HumanDuration(time.Since(parsedLastRuntime)))
148+
149+
if time.Since(nextRuntime) > time.Hour {
150+
status.Health = HealthUnhealthy
151+
}
152152
}
153153
}
154154

pkg/health/health_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
"@now-5m": _now.Add(-time.Minute * 5).Format(RFC3339Micro),
3434
"@now-10m": _now.Add(-time.Minute * 10).Format(RFC3339Micro),
3535
"@now-15m": _now.Add(-time.Minute * 15).Format(RFC3339Micro),
36+
"@now-30m": _now.Add(-time.Minute * 30).Format(RFC3339Micro),
3637
"@now-1h": _now.Add(-time.Hour).Format(RFC3339Micro),
3738
"@now-2h": _now.Add(-time.Hour * 2).Format(RFC3339Micro),
3839
"@now-4h": _now.Add(-time.Hour * 4).Format(RFC3339Micro),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: configs.flanksource.com/v1
2+
kind: ScrapeConfig
3+
metadata:
4+
uid: 8e6902db-67bc-4546-a93e-5492112205f9
5+
name: ifs-dev-cluster
6+
namespace: mission-control-agent
7+
finalizers:
8+
- scrapeConfig.config.flanksource.com
9+
annotations:
10+
meta.helm.sh/release-name: dev-kubernetes-bundle
11+
meta.helm.sh/release-namespace: mission-control-agent
12+
expected-ready: 'true'
13+
expected-health: 'unhealthy'
14+
expected-message: "scraper hasn't run for 120m"
15+
creationTimestamp: '2024-12-04T12:21:48Z'
16+
spec:
17+
schedule: "10 * * * *"
18+
kubernetes:
19+
- clusterName: ifs-dev-cluster
20+
status:
21+
lastRun:
22+
success: 950
23+
timestamp: "@now-2h"

pkg/health/testdata/Kubernetes/MissionControl/scrapeConfig-minor-delay.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ spec:
4141
status:
4242
lastRun:
4343
success: 1
44-
timestamp: "@now-15m"
44+
timestamp: "@now-30m"

0 commit comments

Comments
 (0)