Skip to content

Commit

Permalink
Use Database.PeriodicCleanup()
Browse files Browse the repository at this point in the history
  • Loading branch information
jrauh01 committed Jul 15, 2024
1 parent 1d51623 commit 9d58c84
Showing 1 changed file with 35 additions and 102 deletions.
137 changes: 35 additions & 102 deletions cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/metrics"
"github.com/icinga/icinga-kubernetes/pkg/periodic"
schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1"
"github.com/icinga/icinga-kubernetes/pkg/sync"
syncv1 "github.com/icinga/icinga-kubernetes/pkg/sync/v1"
Expand All @@ -27,7 +26,6 @@ import (
"k8s.io/klog/v2"
"os"
"strings"
"time"
)

func main() {
Expand Down Expand Up @@ -221,110 +219,45 @@ func main() {
return s.Run(ctx)
})

errs := make(chan error, 1)
defer close(errs)

defer periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, 0, -1)

_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "event",
PK: "uuid",
Column: "created",
}, 5000, olderThan,
)
if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}

return
}
}, periodic.Immediate()).Stop()

defer periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, -1, 0)

_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "prometheus_cluster_metric",
PK: "(cluster_uuid, timestamp, category, name)",
Column: "timestamp",
}, 10000, olderThan,
)
if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}

return
}
}, periodic.Immediate()).Stop()

defer periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, -1, 0)

_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "prometheus_node_metric",
PK: "(node_uuid, timestamp, category, name)",
Column: "timestamp",
}, 10000, olderThan,
)
if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "event",
PK: "uuid",
Column: "created",
})
})

return
}
}, periodic.Immediate()).Stop()

defer periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, -1, 0)

_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "prometheus_pod_metric",
PK: "(pod_uuid, timestamp, category, name)",
Column: "timestamp",
}, 10000, olderThan,
)
if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_cluster_metric",
PK: "(cluster_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

return
}
}, periodic.Immediate()).Stop()

defer periodic.Start(ctx, time.Hour, func(tick periodic.Tick) {
olderThan := tick.Time.AddDate(0, -1, 0)

_, err := db.CleanupOlderThan(
ctx, database.CleanupStmt{
Table: "prometheus_container_metric",
PK: "(container_uuid, timestamp, category, name)",
Column: "timestamp",
}, 10000, olderThan,
)
if err != nil {
select {
case errs <- err:
case <-ctx.Done():
}
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_node_metric",
PK: "(node_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

return
}
}, periodic.Immediate()).Stop()
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_pod_metric",
PK: "(pod_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

com.ErrgroupReceive(ctx, g, errs)
g.Go(func() error {
return db.PeriodicCleanup(ctx, database.CleanupStmt{
Table: "prometheus_container_metric",
PK: "(container_uuid, timestamp, category, name)",
Column: "timestamp",
})
})

if err := g.Wait(); err != nil {
klog.Fatal(err)
Expand Down

0 comments on commit 9d58c84

Please sign in to comment.