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 aa194c8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
51 changes: 36 additions & 15 deletions docker-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ const EnvOverrideHost = "DOCKER_HOST"

var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host")
var repositoriesToUpdate = machineryvars.GetEnv("REPOSITORIES_TO_UPDATE", "uselagoon")
var REGISTRY = machineryvars.GetEnv("REGISTRY", "docker-registry.default.svc:5000")
var BIP = machineryvars.GetEnv("BIP", "172.16.0.1/16")
var registry = machineryvars.GetEnv("REGISTRY", "docker-registry.default.svc:5000")
var bip = machineryvars.GetEnv("BIP", "172.16.0.1/16")
var logLevel = machineryvars.GetEnv("LOG_LEVEL", "info")
var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "")
var registryMirror = 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 @@ -42,12 +44,12 @@ func main() {
}
defer cli.Close()

var command = fmt.Sprintf("/usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --bip=%s --storage-driver=overlay2 --tls=false --log-level=%s", BIP, logLevel)
if REGISTRY != "" {
command = command + fmt.Sprintf(" --insecure-registry=%s", REGISTRY)
var command = fmt.Sprintf("/usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --bip=%s --storage-driver=overlay2 --tls=false --log-level=%s", bip, logLevel)
if registry != "" {
command = command + fmt.Sprintf(" --insecure-registry=%s", registry)
}
if REGISTRY_MIRROR != "" {
command = command + fmt.Sprintf(" --registry-mirror=%s", REGISTRY_MIRROR)
if registryMirror != "" {
command = command + fmt.Sprintf(" --registry-mirror=%s", registryMirror)
}
var cmd = exec.Command("sh", "-c", command)

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 All @@ -79,11 +82,6 @@ func pruneImages(client *client.Client, c *cron.Cron) {
log.Println(err)
return
}
// prune all docker build cache images older than 7 days or what is specified in the environment variable
_, buildErr := client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter})
if buildErr != nil {
log.Println(buildErr)
}
log.Printf("Image prune complete: %d images deleted, %d bytes reclaimed\n",
len(pruneReport.ImagesDeleted), pruneReport.SpaceReclaimed)
})
Expand Down Expand Up @@ -113,6 +111,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 +218,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 +229,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 aa194c8

Please sign in to comment.