2024-01-28 15:31:58 +01:00
|
|
|
package pods
|
|
|
|
|
|
|
|
import (
|
2024-02-03 11:11:10 +01:00
|
|
|
"log"
|
2024-01-28 15:31:58 +01:00
|
|
|
"time"
|
2024-02-03 11:11:10 +01:00
|
|
|
|
|
|
|
"github.com/containers/podman/v4/pkg/bindings/volumes"
|
|
|
|
"github.com/spf13/viper"
|
2024-01-28 15:31:58 +01:00
|
|
|
)
|
|
|
|
|
2024-02-03 11:11:10 +01:00
|
|
|
// GarbageCollector is a goroutine that cleans up old Containers
|
2024-01-29 16:45:51 +01:00
|
|
|
func GarbageCollector() error {
|
2024-02-03 11:11:10 +01:00
|
|
|
if viper.GetBool("timeout_on_restart") {
|
|
|
|
timeoutExistingContainers()
|
|
|
|
}
|
2024-01-28 15:31:58 +01:00
|
|
|
for {
|
2024-01-29 16:45:51 +01:00
|
|
|
err := Cleanup()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-28 15:31:58 +01:00
|
|
|
time.Sleep(time.Minute * 10)
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 11:11:10 +01:00
|
|
|
|
|
|
|
func timeoutExistingContainers() {
|
|
|
|
var oldContainers []string
|
|
|
|
|
|
|
|
for _, container := range containerList() {
|
|
|
|
oldContainers = append(oldContainers, container.ID)
|
|
|
|
}
|
|
|
|
OldContainers = append(OldContainers, oldContainers...)
|
|
|
|
log.Println("old Containers: ", oldContainers)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func pruneVolumes() error {
|
|
|
|
|
|
|
|
results, err := volumes.Prune(Socket, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resultLen := len(results)
|
|
|
|
for i, result := range results {
|
|
|
|
log.Printf("[%d/%d] %s %d MB", i, resultLen, result.Id, result.Size/1024/1024)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|