Skip to content

Commit

Permalink
feat: add builder cache prune
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Oct 3, 2024
1 parent 2ae5d75 commit 9866440
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
30 changes: 28 additions & 2 deletions docker-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ var logLevel = machineryvars.GetEnv("LOG_LEVEL", "info")
var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "")
var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_SCHEDULE", "22 1 * * *")
var pruneDanglingSchedule = machineryvars.GetEnv("PRUNE_DANGLING_SCHEDULE", "22 1 * * *")
var pruneBuilderCacheSchedule = machineryvars.GetEnv("PRUNE_BUILDER_CACHE_SCHEDULE", "22 1 * * *")
var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */4 * * *")
var updateImagesSchedule = machineryvars.GetEnv("UPDATE_IMAGES_SCHEDULE", "*/15 * * * *")
var pruneImagesUntil = machineryvars.GetEnv("PRUNE_IMAGES_UNTIL", "168h")
var pruneBuilderCacheUntil = machineryvars.GetEnv("PRUNE_BUILDER_CACHE_UNTIL", "168h")

func main() {
cli, err := client.NewClientWithOpts(
Expand All @@ -57,6 +59,7 @@ func main() {
c := cron.New()
pruneImages(cli, c)
pruneDanglingImages(cli, c)
pruneBuilderCache(cli, c)
removeExited(cli, c)
updateImages(cli, c)
c.Start()
Expand Down Expand Up @@ -113,6 +116,29 @@ func pruneDanglingImages(client *client.Client, c *cron.Cron) {
}
}

func pruneBuilderCache(client *client.Client, c *cron.Cron) {
_, err := c.AddFunc(pruneBuilderCacheSchedule, func() {
log.Println("Starting builder cache prune")
ageFilter := filters.NewArgs()
ageFilter.Add("until", pruneBuilderCacheUntil)
builderCacheOpts := types.BuildCachePruneOptions{
Filters: ageFilter,
}

// Cleans up dangling images
pruneReport, pruneErr := client.BuildCachePrune(context.Background(), builderCacheOpts)
if pruneErr != nil {
log.Println(pruneErr)
}
log.Printf("Builder Cache prune complete: %d deleted, %d bytes reclaimed\n",
len(pruneReport.CachesDeleted), pruneReport.SpaceReclaimed)
})

if err != nil {
log.Printf("Error initiating pruneBuilderCache cron: %v\n", err)
}
}

func removeExited(client *client.Client, c *cron.Cron) {
_, err := c.AddFunc(removeExitedSchedule, func() {
log.Println("Starting removeExited")
Expand Down Expand Up @@ -197,7 +223,7 @@ func updateImages(client *client.Client, c *cron.Cron) {
for _, img := range postUpdateImages {
for _, updatedImg := range updatedImages {
if img.ID == updatedImg {
log.Println(fmt.Sprintf("Updated image %s", img.RepoTags))
log.Printf("Updated image %s", img.RepoTags)
}
}
}
Expand All @@ -208,7 +234,7 @@ func updateImages(client *client.Client, c *cron.Cron) {
} else {
imgPluralize = "images"
}
log.Println(fmt.Sprintf("Update images complete | %d %s updated", len(updatedImages), imgPluralize))
log.Printf("Update images complete | %d %s updated", len(updatedImages), imgPluralize)
})

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/uselagoon/lagoon-service-images

go 1.22

0 comments on commit 9866440

Please sign in to comment.