From 6cd36539b2b257f1e3739b0030579519d862e1dc Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 23 Mar 2023 17:29:22 +1100 Subject: [PATCH 01/42] Refactoring dockerhost scripts to golang --- docker-host/main.go | 108 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 25 ++++++++++ go.sum | 84 ++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 docker-host/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/docker-host/main.go b/docker-host/main.go new file mode 100644 index 00000000..100710f5 --- /dev/null +++ b/docker-host/main.go @@ -0,0 +1,108 @@ +package main + +import ( + "context" + "fmt" + "io" + "os" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/client" +) + +const EnvOverrideHost = "DOCKER_HOST" + +var dockerHost = os.Getenv("DOCKER_HOST") +var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") + +func main() { + cli, err := client.NewClientWithOpts( + client.WithHostFromEnv(), + ) + if err != nil { + fmt.Println("Error", err) + } + defer cli.Close() + + if cli.DaemonHost() != dockerHost { + fmt.Sprintf("Could not connect to %s", dockerHost) + } + + pruneImages(cli) + removeExited(cli) + updateImages(cli) +} + +func pruneImages(client *client.Client) { + ageFilter := filters.NewArgs() + danglingFilter := filters.NewArgs() + ageFilter.Add("until", "168") + danglingFilter.Add("dangling", "true") + + // # prune all images older than 7 days or what is specified in the environment variable + client.ImagesPrune(context.Background(), ageFilter) + + // # prune all docker build cache images older than 7 days or what is specified in the environment variable + client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) + + // # after old images are pruned, clean up dangling images + client.ImagesPrune(context.Background(), danglingFilter) + + fmt.Println("Prune complete") +} + +func removeExited(client *client.Client) { + ctx := context.Background() + statusFilter := filters.NewArgs() + statusFilter.Add("status", "exited") + containers, err := client.ContainerList(ctx, types.ContainerListOptions{ + Filters: statusFilter, + }) + if err != nil { + panic(err) + } + + // # remove all exited containers + for _, container := range containers { + _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + Force: true, + RemoveVolumes: true, + }) + } + + fmt.Println("removeExited complete") +} + +func updateImages(client *client.Client) { + ctx := context.Background() + filters := filters.NewArgs() + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + panic(err) + } + + // # Iterates through all images that have the name of the repository we are interested in in it + for i, image := range images { + out, err := client.ImagePull(ctx, image.RepoTags[i], types.ImagePullOptions{}) + if err != nil { + panic(err) + } + defer out.Close() + io.Copy(os.Stdout, out) + } + fmt.Println("Update images complete") +} + +// #Todo +// func updatePushImages(client *client.Client) { +// } + +func getEnv(key, defaultValue string) string { + value := os.Getenv(key) + if len(value) == 0 { + return defaultValue + } + return value +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..afa5843a --- /dev/null +++ b/go.mod @@ -0,0 +1,25 @@ +module lagoon-service-images + +go 1.19 + +require github.com/docker/docker v23.0.1+incompatible + +require ( + github.com/Microsoft/go-winio v0.6.0 // indirect + github.com/docker/distribution v2.8.1+incompatible // indirect + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect + github.com/morikuni/aec v1.0.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.0.2 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/stretchr/testify v1.8.2 // indirect + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect + golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/sys v0.1.0 // indirect + golang.org/x/time v0.3.0 // indirect + golang.org/x/tools v0.1.12 // indirect + gotest.tools/v3 v3.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..c9c6fb1c --- /dev/null +++ b/go.sum @@ -0,0 +1,84 @@ +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg= +github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= +github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v23.0.1+incompatible h1:vjgvJZxprTTE1A37nm+CLNAdwu6xZekyoiVlUZEINcY= +github.com/docker/docker v23.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= +github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= +gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= From 2d2d9de725be1c952593f2fde237a31faa4f0c1c Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 28 Mar 2023 12:47:07 +1100 Subject: [PATCH 02/42] Refactored update-push-images --- docker-host/main.go | 67 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 100710f5..b2ba6fc2 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "io" + "io/ioutil" "os" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -15,6 +17,7 @@ const EnvOverrideHost = "DOCKER_HOST" var dockerHost = os.Getenv("DOCKER_HOST") var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") +var registryHost = getEnv("REGISTRY", "docker-registry.default.svc:5000") func main() { cli, err := client.NewClientWithOpts( @@ -29,9 +32,10 @@ func main() { fmt.Sprintf("Could not connect to %s", dockerHost) } - pruneImages(cli) - removeExited(cli) - updateImages(cli) + // pruneImages(cli) + // removeExited(cli) + // updateImages(cli) + updatePushImages(cli) } func pruneImages(client *client.Client) { @@ -96,8 +100,61 @@ func updateImages(client *client.Client) { } // #Todo -// func updatePushImages(client *client.Client) { -// } +func updatePushImages(client *client.Client) { + ctx := context.Background() + namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") + if err != nil { + fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) + os.Exit(1) + } + token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") + if err != nil { + fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) + os.Exit(1) + } + client.RegistryLogin(ctx, types.AuthConfig{ + Username: "serviceaccount", + Password: "", + RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), + }) + + filters := filters.NewArgs() + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + panic(err) + } + + var imgRepoTags []string + for _, img := range images { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } + + imageNoRespository := "" + + for _, fullImage := range imgRepoTags { + image := strings.Split(fullImage, "/") + + if len(image) == 3 { + imageNoRespository = image[2] + } else { + imageNoRespository = image[1] + } + + // # pull newest version of found image + out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) + if err != nil { + panic(err) + } + defer out.Close() + + // # Tag the image with the openshift registry name and the openshift project this container is running + client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) + + // # Push to the openshift registry + client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) + } +} func getEnv(key, defaultValue string) string { value := os.Getenv(key) From 995fae6c84c143b02a3ab0efb63725c5ed47642f Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 5 Apr 2023 12:27:43 +1000 Subject: [PATCH 03/42] Add cron for each function --- docker-host/Dockerfile | 22 ++++--- go.mod => docker-host/go.mod | 13 ++-- go.sum => docker-host/go.sum | 23 ++++---- docker-host/main.go | 111 ++++++++++++++++++++--------------- 4 files changed, 99 insertions(+), 70 deletions(-) rename go.mod => docker-host/go.mod (69%) rename go.sum => docker-host/go.sum (87%) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index b135bc34..3c7fc431 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,5 +1,16 @@ -FROM uselagoon/commons:latest as commons +FROM golang:1.18-alpine3.17 as builder + +WORKDIR /docker-host + +COPY main.go . +COPY go.mod . +COPY go.sum . + +# compile +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build docker-host . + +FROM uselagoon/commons:latest as commons FROM docker:20.10.22-dind LABEL org.opencontainers.image.authors="The Lagoon Authors" maintainer="The Lagoon Authors" @@ -11,6 +22,8 @@ COPY --from=commons /bin/fix-permissions /bin/ep /bin/docker-sleep /bin/ COPY --from=commons /sbin/tini /sbin/ COPY --from=commons /home /home +COPY --from=builder /docker-host /docker-host + ENV TMPDIR=/tmp \ TMP=/tmp \ HOME=/home \ @@ -29,11 +42,6 @@ ENV DOCKER_HOST=docker-host \ RUN fix-permissions /home -COPY update-push-images.sh /update-push-images.sh -COPY update-images.sh /update-images.sh -COPY prune-images.sh /prune-images.sh -COPY remove-exited.sh /remove-exited.sh - ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}"] +CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}, /docker-host"] \ No newline at end of file diff --git a/go.mod b/docker-host/go.mod similarity index 69% rename from go.mod rename to docker-host/go.mod index afa5843a..dd551976 100644 --- a/go.mod +++ b/docker-host/go.mod @@ -1,8 +1,8 @@ -module lagoon-service-images +module docker-host go 1.19 -require github.com/docker/docker v23.0.1+incompatible +require github.com/docker/docker v23.0.2+incompatible require ( github.com/Microsoft/go-winio v0.6.0 // indirect @@ -15,11 +15,12 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/robfig/cron/v3 v3.0.0 // indirect github.com/stretchr/testify v1.8.2 // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect - golang.org/x/sys v0.1.0 // indirect + golang.org/x/mod v0.9.0 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/sys v0.6.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.1.12 // indirect + golang.org/x/tools v0.7.0 // indirect gotest.tools/v3 v3.4.0 // indirect ) diff --git a/go.sum b/docker-host/go.sum similarity index 87% rename from go.sum rename to docker-host/go.sum index c9c6fb1c..d385b93e 100644 --- a/go.sum +++ b/docker-host/go.sum @@ -6,8 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v23.0.1+incompatible h1:vjgvJZxprTTE1A37nm+CLNAdwu6xZekyoiVlUZEINcY= -github.com/docker/docker v23.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker v23.0.2+incompatible h1:q81C2qQ/EhPm8COZMUGOQYh4qLv4Xu6CXELJ3WK/mlU= +github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= @@ -30,6 +30,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= +github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -44,23 +46,24 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= +golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= @@ -70,8 +73,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= +golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/docker-host/main.go b/docker-host/main.go index b2ba6fc2..73c418e2 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -11,6 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" + "github.com/robfig/cron/v3" ) const EnvOverrideHost = "DOCKER_HOST" @@ -32,74 +33,90 @@ func main() { fmt.Sprintf("Could not connect to %s", dockerHost) } - // pruneImages(cli) - // removeExited(cli) - // updateImages(cli) + pruneImages(cli) + removeExited(cli) + updateImages(cli) updatePushImages(cli) } func pruneImages(client *client.Client) { - ageFilter := filters.NewArgs() - danglingFilter := filters.NewArgs() - ageFilter.Add("until", "168") - danglingFilter.Add("dangling", "true") + c := cron.New() + c.AddFunc("22 1 * * *", func() { + ageFilter := filters.NewArgs() + danglingFilter := filters.NewArgs() + ageFilter.Add("until", "168") + danglingFilter.Add("dangling", "true") - // # prune all images older than 7 days or what is specified in the environment variable - client.ImagesPrune(context.Background(), ageFilter) + // # prune all images older than 7 days or what is specified in the environment variable + client.ImagesPrune(context.Background(), ageFilter) - // # prune all docker build cache images older than 7 days or what is specified in the environment variable - client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) + // # prune all docker build cache images older than 7 days or what is specified in the environment variable + client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) - // # after old images are pruned, clean up dangling images - client.ImagesPrune(context.Background(), danglingFilter) + // # after old images are pruned, clean up dangling images + client.ImagesPrune(context.Background(), danglingFilter) - fmt.Println("Prune complete") + fmt.Println("Prune complete") + }) + c.Start() } func removeExited(client *client.Client) { - ctx := context.Background() - statusFilter := filters.NewArgs() - statusFilter.Add("status", "exited") - containers, err := client.ContainerList(ctx, types.ContainerListOptions{ - Filters: statusFilter, - }) - if err != nil { - panic(err) - } - - // # remove all exited containers - for _, container := range containers { - _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ - Force: true, - RemoveVolumes: true, + c := cron.New() + c.AddFunc("22 */4 * * *", func() { + ctx := context.Background() + statusFilter := filters.NewArgs() + statusFilter.Add("status", "exited") + containers, err := client.ContainerList(ctx, types.ContainerListOptions{ + Filters: statusFilter, }) - } + if err != nil { + panic(err) + } - fmt.Println("removeExited complete") + // # remove all exited containers + for _, container := range containers { + _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + Force: true, + RemoveVolumes: true, + }) + } + + fmt.Println("removeExited complete") + }) + c.Start() } func updateImages(client *client.Client) { - ctx := context.Background() - filters := filters.NewArgs() - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) - if err != nil { - panic(err) - } - - // # Iterates through all images that have the name of the repository we are interested in in it - for i, image := range images { - out, err := client.ImagePull(ctx, image.RepoTags[i], types.ImagePullOptions{}) + c := cron.New() + c.AddFunc("*/15 * * * *", func() { + ctx := context.Background() + filters := filters.NewArgs() + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { panic(err) } - defer out.Close() - io.Copy(os.Stdout, out) - } - fmt.Println("Update images complete") + + var imgRepoTags []string + for _, img := range images { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } + + // # Iterates through all images that have the name of the repository we are interested in in it + for _, image := range imgRepoTags { + out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) + if err != nil { + panic(err) + } + defer out.Close() + io.Copy(os.Stdout, out) + } + fmt.Println("Update images complete") + }) + c.Start() } -// #Todo func updatePushImages(client *client.Client) { ctx := context.Background() namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") From 8de7b96f8a960696c21af83512347edc10b59ee7 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 5 Apr 2023 17:36:28 +1000 Subject: [PATCH 04/42] Removing bash scripts --- docker-host/Dockerfile | 2 +- docker-host/prune-images.sh | 15 --------------- docker-host/remove-exited.sh | 8 -------- docker-host/update-images.sh | 12 ------------ docker-host/update-push-images.sh | 26 -------------------------- 5 files changed, 1 insertion(+), 62 deletions(-) delete mode 100755 docker-host/prune-images.sh delete mode 100755 docker-host/remove-exited.sh delete mode 100755 docker-host/update-images.sh delete mode 100755 docker-host/update-push-images.sh diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 3c7fc431..373501e3 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -44,4 +44,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}, /docker-host"] \ No newline at end of file +CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}", "/docker-host"] \ No newline at end of file diff --git a/docker-host/prune-images.sh b/docker-host/prune-images.sh deleted file mode 100755 index a07a2118..00000000 --- a/docker-host/prune-images.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -set -x -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -# prune all images older than 7 days or what is specified in the environment variable -docker image prune -af --filter "until=${PRUNE_IMAGES_UNTIL:-168h}" - -# prune docker build caches coming from buildkit builds -# prune all docker build cache images older than 7 days or what is specified in the environment variable -docker builder prune -af --filter "until=${PRUNE_IMAGES_UNTIL:-168h}" - -# after old images are pruned, clean up dangling images -docker image prune -f diff --git a/docker-host/remove-exited.sh b/docker-host/remove-exited.sh deleted file mode 100755 index 319810f7..00000000 --- a/docker-host/remove-exited.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -x -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -# remove all exited containers -docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm diff --git a/docker-host/update-images.sh b/docker-host/update-images.sh deleted file mode 100755 index 0de8fbba..00000000 --- a/docker-host/update-images.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -e -set -x - -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -# Iterates through all images that have the name of the repository we are interested in in it -for FULL_IMAGE in $(docker image ls --format "{{.Repository}}:{{.Tag}}" | grep -E "${REPOSITORY_TO_UPDATE}/" | grep -v none); do - # pull newest version of found image - docker pull ${FULL_IMAGE} | cat -done diff --git a/docker-host/update-push-images.sh b/docker-host/update-push-images.sh deleted file mode 100755 index c75b3acf..00000000 --- a/docker-host/update-push-images.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -e -set -x -NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace) - -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -docker login -u=serviceaccount --password-stdin ${REGISTRY} < /var/run/secrets/kubernetes.io/serviceaccount/token - -# Iterates through all images that have the name of the repository we are interested in in it -for FULL_IMAGE in $(docker image ls --format "{{.Repository}}:{{.Tag}}" | grep -E "${REPOSITORY_TO_UPDATE}/" | grep -v none); do - IMAGE=(${FULL_IMAGE//// }) - if [ ${#IMAGE[@]} == "3" ]; then - IMAGE_NO_REPOSITORY=${IMAGE[2]} - else - IMAGE_NO_REPOSITORY=${IMAGE[1]} - fi - - # pull newest version of found image - docker pull ${FULL_IMAGE} | cat - # Tag the image with the openshift registry name and the openshift project this container is running - docker tag ${FULL_IMAGE} ${REGISTRY}/${NAMESPACE}/${IMAGE_NO_REPOSITORY} - # Push to the openshift registry - docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NO_REPOSITORY} -done From 37f8f9c65cfb6bb7672dedcdec2b4a9c54a88db8 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 12 Apr 2023 12:42:20 +1000 Subject: [PATCH 05/42] Updated dockerfile cmd --- docker-host/Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 373501e3..2896a640 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,4 +1,3 @@ - FROM golang:1.18-alpine3.17 as builder WORKDIR /docker-host @@ -44,4 +43,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}", "/docker-host"] \ No newline at end of file +CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR};/docker-host"] \ No newline at end of file From 6a67c2c4812f05db416b75fe99ee1a608d122452 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 13 Apr 2023 17:03:06 +1000 Subject: [PATCH 06/42] Moved cmd directive into go --- docker-host/Dockerfile | 2 +- docker-host/go.mod | 14 ++-- docker-host/go.sum | 12 ++++ docker-host/main.go | 146 +++++++++++++++++++++-------------------- 4 files changed, 95 insertions(+), 79 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 2896a640..f5e7b674 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -43,4 +43,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR};/docker-host"] \ No newline at end of file +CMD ["sh", "-c", "sh", "/docker-host"] diff --git a/docker-host/go.mod b/docker-host/go.mod index dd551976..4fcd3494 100644 --- a/docker-host/go.mod +++ b/docker-host/go.mod @@ -2,7 +2,10 @@ module docker-host go 1.19 -require github.com/docker/docker v23.0.2+incompatible +require ( + github.com/docker/docker v23.0.3+incompatible + github.com/robfig/cron/v3 v3.0.1 +) require ( github.com/Microsoft/go-winio v0.6.0 // indirect @@ -15,12 +18,11 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/robfig/cron/v3 v3.0.0 // indirect github.com/stretchr/testify v1.8.2 // indirect - golang.org/x/mod v0.9.0 // indirect - golang.org/x/net v0.8.0 // indirect - golang.org/x/sys v0.6.0 // indirect + golang.org/x/mod v0.10.0 // indirect + golang.org/x/net v0.9.0 // indirect + golang.org/x/sys v0.7.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.7.0 // indirect + golang.org/x/tools v0.8.0 // indirect gotest.tools/v3 v3.4.0 // indirect ) diff --git a/docker-host/go.sum b/docker-host/go.sum index d385b93e..1863e42a 100644 --- a/docker-host/go.sum +++ b/docker-host/go.sum @@ -8,6 +8,8 @@ github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6 github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/docker v23.0.2+incompatible h1:q81C2qQ/EhPm8COZMUGOQYh4qLv4Xu6CXELJ3WK/mlU= github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker v23.0.3+incompatible h1:9GhVsShNWz1hO//9BNg/dpMnZW25KydO4wtVxWAIbho= +github.com/docker/docker v23.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= @@ -32,6 +34,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -48,12 +52,16 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= +golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -64,6 +72,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= @@ -75,6 +85,8 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= +golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= +golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/docker-host/main.go b/docker-host/main.go index 73c418e2..7319c52c 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -4,9 +4,8 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" - "strings" + "os/exec" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -18,7 +17,9 @@ const EnvOverrideHost = "DOCKER_HOST" var dockerHost = os.Getenv("DOCKER_HOST") var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") -var registryHost = getEnv("REGISTRY", "docker-registry.default.svc:5000") +var REGISTRY = getEnv("REGISTRY", "docker-registry.default.svc:5000") +var BIP = getEnv("BIP", "172.16.0.1/16") +var REGISTRY_MIRROR = getEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") func main() { cli, err := client.NewClientWithOpts( @@ -32,15 +33,21 @@ func main() { if cli.DaemonHost() != dockerHost { fmt.Sprintf("Could not connect to %s", dockerHost) } - - pruneImages(cli) - removeExited(cli) - updateImages(cli) - updatePushImages(cli) + 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + c := cron.New() + pruneImages(cli, c) + removeExited(cli, c) + updateImages(cli, c) + c.Start() + cmd := exec.Command("sh", "-c", command) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Println("could not run command: ", err) + } } -func pruneImages(client *client.Client) { - c := cron.New() +func pruneImages(client *client.Client, c *cron.Cron) { c.AddFunc("22 1 * * *", func() { ageFilter := filters.NewArgs() danglingFilter := filters.NewArgs() @@ -58,11 +65,9 @@ func pruneImages(client *client.Client) { fmt.Println("Prune complete") }) - c.Start() } -func removeExited(client *client.Client) { - c := cron.New() +func removeExited(client *client.Client, c *cron.Cron) { c.AddFunc("22 */4 * * *", func() { ctx := context.Background() statusFilter := filters.NewArgs() @@ -84,11 +89,9 @@ func removeExited(client *client.Client) { fmt.Println("removeExited complete") }) - c.Start() } -func updateImages(client *client.Client) { - c := cron.New() +func updateImages(client *client.Client, c *cron.Cron) { c.AddFunc("*/15 * * * *", func() { ctx := context.Background() filters := filters.NewArgs() @@ -114,64 +117,63 @@ func updateImages(client *client.Client) { } fmt.Println("Update images complete") }) - c.Start() } -func updatePushImages(client *client.Client) { - ctx := context.Background() - namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") - if err != nil { - fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) - os.Exit(1) - } - token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") - if err != nil { - fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) - os.Exit(1) - } - client.RegistryLogin(ctx, types.AuthConfig{ - Username: "serviceaccount", - Password: "", - RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), - }) - - filters := filters.NewArgs() - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) - if err != nil { - panic(err) - } - - var imgRepoTags []string - for _, img := range images { - imgRepoTags = append(imgRepoTags, img.RepoTags...) - } - - imageNoRespository := "" - - for _, fullImage := range imgRepoTags { - image := strings.Split(fullImage, "/") - - if len(image) == 3 { - imageNoRespository = image[2] - } else { - imageNoRespository = image[1] - } - - // # pull newest version of found image - out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) - if err != nil { - panic(err) - } - defer out.Close() - - // # Tag the image with the openshift registry name and the openshift project this container is running - client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) - - // # Push to the openshift registry - client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) - } -} +// func updatePushImages(client *client.Client) { +// ctx := context.Background() +// namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") +// if err != nil { +// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) +// os.Exit(1) +// } +// token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") +// if err != nil { +// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) +// os.Exit(1) +// } +// client.RegistryLogin(ctx, types.AuthConfig{ +// Username: "serviceaccount", +// Password: "", +// RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), +// }) + +// filters := filters.NewArgs() +// filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) +// images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) +// if err != nil { +// panic(err) +// } + +// var imgRepoTags []string +// for _, img := range images { +// imgRepoTags = append(imgRepoTags, img.RepoTags...) +// } + +// imageNoRespository := "" + +// for _, fullImage := range imgRepoTags { +// image := strings.Split(fullImage, "/") + +// if len(image) == 3 { +// imageNoRespository = image[2] +// } else { +// imageNoRespository = image[1] +// } + +// // # pull newest version of found image +// out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) +// if err != nil { +// panic(err) +// } +// defer out.Close() + +// // # Tag the image with the openshift registry name and the openshift project this container is running +// client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) + +// // # Push to the openshift registry +// client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) +// } +// } func getEnv(key, defaultValue string) string { value := os.Getenv(key) From b0c18e79c75fbf3acff179d912141cd552e03555 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 14 Apr 2023 09:53:55 +1000 Subject: [PATCH 07/42] Updated cmd --- docker-host/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 7319c52c..892a8a80 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -33,13 +33,13 @@ func main() { if cli.DaemonHost() != dockerHost { fmt.Sprintf("Could not connect to %s", dockerHost) } - 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + var cmd = exec.Command("sh", "-c", "sh", command) c := cron.New() pruneImages(cli, c) removeExited(cli, c) updateImages(cli, c) c.Start() - cmd := exec.Command("sh", "-c", command) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { From e088a2f567a0d791778afac54aac74dd0cb332cd Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 14 Apr 2023 17:30:02 +1000 Subject: [PATCH 08/42] Updated cmd and added error handling --- docker-host/Dockerfile | 6 +-- docker-host/main.go | 116 ++++++++++++++--------------------------- 2 files changed, 42 insertions(+), 80 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index f5e7b674..39c26d31 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,6 +1,6 @@ FROM golang:1.18-alpine3.17 as builder -WORKDIR /docker-host +WORKDIR /build COPY main.go . COPY go.mod . @@ -21,7 +21,7 @@ COPY --from=commons /bin/fix-permissions /bin/ep /bin/docker-sleep /bin/ COPY --from=commons /sbin/tini /sbin/ COPY --from=commons /home /home -COPY --from=builder /docker-host /docker-host +COPY --from=builder /build/docker-host /docker-host ENV TMPDIR=/tmp \ TMP=/tmp \ @@ -43,4 +43,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh", "/docker-host"] +CMD ["sh", "-c", "/docker-host"] diff --git a/docker-host/main.go b/docker-host/main.go index 892a8a80..601c71b6 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "log" "os" "os/exec" @@ -15,7 +16,7 @@ import ( const EnvOverrideHost = "DOCKER_HOST" -var dockerHost = os.Getenv("DOCKER_HOST") +var dockerHost = getEnv("DOCKER_HOST", "docker-host") var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") var REGISTRY = getEnv("REGISTRY", "docker-registry.default.svc:5000") var BIP = getEnv("BIP", "172.16.0.1/16") @@ -30,45 +31,58 @@ func main() { } defer cli.Close() - if cli.DaemonHost() != dockerHost { + 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + var cmd = exec.Command("sh", "-c", command) + + if dockerHost != cli.DaemonHost() { fmt.Sprintf("Could not connect to %s", dockerHost) } - 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) - var cmd = exec.Command("sh", "-c", "sh", command) c := cron.New() pruneImages(cli, c) removeExited(cli, c) updateImages(cli, c) + fmt.Println("Cronjob start") c.Start() cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + fmt.Println("Cronjob run") if err := cmd.Run(); err != nil { fmt.Println("could not run command: ", err) } } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc("22 1 * * *", func() { + c.AddFunc("/5 * * * *", func() { + log.Println("Starting image prune") + // c.AddFunc("22 1 * * *", func() { ageFilter := filters.NewArgs() danglingFilter := filters.NewArgs() ageFilter.Add("until", "168") danglingFilter.Add("dangling", "true") // # prune all images older than 7 days or what is specified in the environment variable - client.ImagesPrune(context.Background(), ageFilter) - + _, err := client.ImagesPrune(context.Background(), ageFilter) + if err != nil { + log.Println(err) + } // # prune all docker build cache images older than 7 days or what is specified in the environment variable - client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) - + _, buildErr := client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) + if buildErr != nil { + log.Println(err) + } // # after old images are pruned, clean up dangling images - client.ImagesPrune(context.Background(), danglingFilter) - - fmt.Println("Prune complete") + _, pruneErr := client.ImagesPrune(context.Background(), danglingFilter) + if pruneErr != nil { + log.Println(err) + } + log.Println("Prune complete") }) } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc("22 */4 * * *", func() { + c.AddFunc("/6 * * * *", func() { + log.Println("Starting remove exited") + // c.AddFunc("22 */4 * * *", func() { ctx := context.Background() statusFilter := filters.NewArgs() statusFilter.Add("status", "exited") @@ -76,29 +90,33 @@ func removeExited(client *client.Client, c *cron.Cron) { Filters: statusFilter, }) if err != nil { - panic(err) + log.Println(err) } // # remove all exited containers for _, container := range containers { - _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + err := client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ Force: true, RemoveVolumes: true, }) + if err != nil { + log.Println(err) + } } - - fmt.Println("removeExited complete") + log.Println("removeExited complete") }) } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc("*/15 * * * *", func() { + c.AddFunc("/8 * * * *", func() { + log.Println("Starting update images") + // c.AddFunc("*/15 * * * *", func() { ctx := context.Background() filters := filters.NewArgs() filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { - panic(err) + log.Println(err) } var imgRepoTags []string @@ -110,71 +128,15 @@ func updateImages(client *client.Client, c *cron.Cron) { for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) if err != nil { - panic(err) + log.Println(err) } defer out.Close() io.Copy(os.Stdout, out) } - fmt.Println("Update images complete") + log.Println("Update images complete") }) } -// func updatePushImages(client *client.Client) { -// ctx := context.Background() -// namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") -// if err != nil { -// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) -// os.Exit(1) -// } -// token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") -// if err != nil { -// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) -// os.Exit(1) -// } -// client.RegistryLogin(ctx, types.AuthConfig{ -// Username: "serviceaccount", -// Password: "", -// RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), -// }) - -// filters := filters.NewArgs() -// filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) -// images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) -// if err != nil { -// panic(err) -// } - -// var imgRepoTags []string -// for _, img := range images { -// imgRepoTags = append(imgRepoTags, img.RepoTags...) -// } - -// imageNoRespository := "" - -// for _, fullImage := range imgRepoTags { -// image := strings.Split(fullImage, "/") - -// if len(image) == 3 { -// imageNoRespository = image[2] -// } else { -// imageNoRespository = image[1] -// } - -// // # pull newest version of found image -// out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) -// if err != nil { -// panic(err) -// } -// defer out.Close() - -// // # Tag the image with the openshift registry name and the openshift project this container is running -// client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) - -// // # Push to the openshift registry -// client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) -// } -// } - func getEnv(key, defaultValue string) string { value := os.Getenv(key) if len(value) == 0 { From 80125d21e13334ec6ddc598d8334fe42a81bc69d Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 17 Apr 2023 12:39:28 +1000 Subject: [PATCH 09/42] Added api version negotiation --- docker-host/main.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 601c71b6..725f73d8 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -25,6 +25,7 @@ var REGISTRY_MIRROR = getEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.clo func main() { cli, err := client.NewClientWithOpts( client.WithHostFromEnv(), + client.WithAPIVersionNegotiation(), ) if err != nil { fmt.Println("Error", err) @@ -41,23 +42,20 @@ func main() { pruneImages(cli, c) removeExited(cli, c) updateImages(cli, c) - fmt.Println("Cronjob start") c.Start() cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - fmt.Println("Cronjob run") if err := cmd.Run(); err != nil { fmt.Println("could not run command: ", err) } } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc("/5 * * * *", func() { + c.AddFunc("22 1 * * *", func() { log.Println("Starting image prune") - // c.AddFunc("22 1 * * *", func() { ageFilter := filters.NewArgs() danglingFilter := filters.NewArgs() - ageFilter.Add("until", "168") + ageFilter.Add("until", "168h") danglingFilter.Add("dangling", "true") // # prune all images older than 7 days or what is specified in the environment variable @@ -68,21 +66,20 @@ func pruneImages(client *client.Client, c *cron.Cron) { // # 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(err) + log.Println(buildErr) } // # after old images are pruned, clean up dangling images _, pruneErr := client.ImagesPrune(context.Background(), danglingFilter) if pruneErr != nil { - log.Println(err) + log.Println(pruneErr) } log.Println("Prune complete") }) } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc("/6 * * * *", func() { - log.Println("Starting remove exited") - // c.AddFunc("22 */4 * * *", func() { + c.AddFunc("22 */4 * * *", func() { + log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() statusFilter.Add("status", "exited") @@ -108,9 +105,8 @@ func removeExited(client *client.Client, c *cron.Cron) { } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc("/8 * * * *", func() { + c.AddFunc("*/15 * * * *", func() { log.Println("Starting update images") - // c.AddFunc("*/15 * * * *", func() { ctx := context.Background() filters := filters.NewArgs() filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) From dad6ddada4c72d04e4ff678aa5b3d578d7181a24 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 24 Apr 2023 14:38:04 +1000 Subject: [PATCH 10/42] Adding cron schedules + filters as env vars --- docker-host/go.mod | 1 + docker-host/go.sum | 2 ++ docker-host/main.go | 38 ++++++++++++++++++-------------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/docker-host/go.mod b/docker-host/go.mod index 4fcd3494..be56e270 100644 --- a/docker-host/go.mod +++ b/docker-host/go.mod @@ -19,6 +19,7 @@ require ( github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/stretchr/testify v1.8.2 // indirect + github.com/uselagoon/machinery v0.0.7 // indirect golang.org/x/mod v0.10.0 // indirect golang.org/x/net v0.9.0 // indirect golang.org/x/sys v0.7.0 // indirect diff --git a/docker-host/go.sum b/docker-host/go.sum index 1863e42a..ca22704e 100644 --- a/docker-host/go.sum +++ b/docker-host/go.sum @@ -43,6 +43,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/uselagoon/machinery v0.0.7 h1:fBIURDQRVnaB/Gime1Lfp6kgQYmuQpa0gmdfPwo7dkc= +github.com/uselagoon/machinery v0.0.7/go.mod h1:dGAvkxWQkRu/eLQeWLrTl5HvNoFsiXlXInCptHUxzUw= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/docker-host/main.go b/docker-host/main.go index 725f73d8..3e39fa75 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -12,15 +12,21 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" "github.com/robfig/cron/v3" + machineryvars "github.com/uselagoon/machinery/utils/variables" ) const EnvOverrideHost = "DOCKER_HOST" -var dockerHost = getEnv("DOCKER_HOST", "docker-host") -var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") -var REGISTRY = getEnv("REGISTRY", "docker-registry.default.svc:5000") -var BIP = getEnv("BIP", "172.16.0.1/16") -var REGISTRY_MIRROR = getEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") +var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host") +var repo = machineryvars.GetEnv("REPOSITORY_TO_UPDATE", "amazeeio") +var REGISTRY = machineryvars.GetEnv("REGISTRY", "docker-registry.default.svc:5000") +var BIP = machineryvars.GetEnv("BIP", "172.16.0.1/16") +var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") +var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_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 danglingFilter = machineryvars.GetEnv("DANGLING_FILTER", "true") func main() { cli, err := client.NewClientWithOpts( @@ -51,12 +57,12 @@ func main() { } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc("22 1 * * *", func() { + c.AddFunc(pruneImagesSchedule, func() { log.Println("Starting image prune") ageFilter := filters.NewArgs() - danglingFilter := filters.NewArgs() - ageFilter.Add("until", "168h") - danglingFilter.Add("dangling", "true") + pruneDanglingFilter := filters.NewArgs() + ageFilter.Add("until", pruneImagesUntil) + pruneDanglingFilter.Add("dangling", danglingFilter) // # prune all images older than 7 days or what is specified in the environment variable _, err := client.ImagesPrune(context.Background(), ageFilter) @@ -69,7 +75,7 @@ func pruneImages(client *client.Client, c *cron.Cron) { log.Println(buildErr) } // # after old images are pruned, clean up dangling images - _, pruneErr := client.ImagesPrune(context.Background(), danglingFilter) + _, pruneErr := client.ImagesPrune(context.Background(), pruneDanglingFilter) if pruneErr != nil { log.Println(pruneErr) } @@ -78,7 +84,7 @@ func pruneImages(client *client.Client, c *cron.Cron) { } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc("22 */4 * * *", func() { + c.AddFunc(removeExitedSchedule, func() { log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() @@ -105,7 +111,7 @@ func removeExited(client *client.Client, c *cron.Cron) { } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc("*/15 * * * *", func() { + c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() filters := filters.NewArgs() @@ -132,11 +138,3 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println("Update images complete") }) } - -func getEnv(key, defaultValue string) string { - value := os.Getenv(key) - if len(value) == 0 { - return defaultValue - } - return value -} From b85f72a4f814ec1608a64ccf0d44b154d028bb93 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2023 17:28:37 +1000 Subject: [PATCH 11/42] Various improvements --- docker-host/main.go | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 3e39fa75..64bc3610 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/exec" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -18,10 +19,10 @@ import ( const EnvOverrideHost = "DOCKER_HOST" var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host") -var repo = machineryvars.GetEnv("REPOSITORY_TO_UPDATE", "amazeeio") +var repositoryToUpdate = machineryvars.GetEnv("REPOSITORY_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_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") +var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "") var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_SCHEDULE", "22 1 * * *") var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */4 * * *") var updateImagesSchedule = machineryvars.GetEnv("UPDATE_IMAGES_SCHEDULE", "*/15 * * * *") @@ -34,11 +35,17 @@ func main() { client.WithAPIVersionNegotiation(), ) if err != nil { - fmt.Println("Error", err) + log.Println("Error", err) } 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + 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 --storage-opt=overlay2.override_kernel_check=1", BIP) + if REGISTRY != "" { + command = command + fmt.Sprintf(" --insecure-registry=%s", REGISTRY) + } + if REGISTRY_MIRROR != "" { + command = command + fmt.Sprintf(" --registry-mirror=%s", REGISTRY_MIRROR) + } var cmd = exec.Command("sh", "-c", command) if dockerHost != cli.DaemonHost() { @@ -68,6 +75,7 @@ func pruneImages(client *client.Client, c *cron.Cron) { _, err := client.ImagesPrune(context.Background(), ageFilter) if err != nil { 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}) @@ -94,6 +102,7 @@ func removeExited(client *client.Client, c *cron.Cron) { }) if err != nil { log.Println(err) + return } // # remove all exited containers @@ -114,11 +123,11 @@ func updateImages(client *client.Client, c *cron.Cron) { c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() - filters := filters.NewArgs() - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + filters := addFilters(repositoryToUpdate) images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) + return } var imgRepoTags []string @@ -129,12 +138,31 @@ func updateImages(client *client.Client, c *cron.Cron) { // # Iterates through all images that have the name of the repository we are interested in in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) + log.Println("Image to update", image) + if err != nil { log.Println(err) + continue } defer out.Close() - io.Copy(os.Stdout, out) + _, error := io.Copy(os.Stdout, out) + if error != nil { + log.Println(err) + } } log.Println("Update images complete") }) } + +func addFilters(repo string) filters.Args { + filters := filters.NewArgs() + if strings.Contains(repo, "|") { + splitRepos := strings.Split(repo, "|") + for _, repo := range splitRepos { + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + } + } else { + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + } + return filters +} From 1dd265eb809da7335eb71ba8d8cdf3278a4dd438 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 5 May 2023 18:15:55 +1000 Subject: [PATCH 12/42] Updated filter --- docker-host/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 64bc3610..14340d14 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -159,10 +159,10 @@ func addFilters(repo string) filters.Args { if strings.Contains(repo, "|") { splitRepos := strings.Split(repo, "|") for _, repo := range splitRepos { - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) } } else { - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) } return filters } From b8f0d84c5ee91436a5ae4766caf3670b9a34f32e Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 11 May 2023 17:02:23 +1000 Subject: [PATCH 13/42] Updated image filter --- docker-host/Dockerfile | 2 +- docker-host/main.go | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 1d8bfd68..34229a4b 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -35,7 +35,7 @@ RUN apk add --no-cache bash ENV DOCKER_HOST=docker-host \ REGISTRY=docker-registry.default.svc:5000 \ - REPOSITORY_TO_UPDATE=amazeeio \ + REPOSITORIES_TO_UPDATE=amazeeio \ BIP=172.16.0.1/16 \ REGISTRY_MIRROR=https://imagecache.amazeeio.cloud diff --git a/docker-host/main.go b/docker-host/main.go index 14340d14..1e22ca4a 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -19,7 +19,7 @@ import ( const EnvOverrideHost = "DOCKER_HOST" var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host") -var repositoryToUpdate = machineryvars.GetEnv("REPOSITORY_TO_UPDATE", "uselagoon") +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_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "") @@ -35,7 +35,7 @@ func main() { client.WithAPIVersionNegotiation(), ) if err != nil { - log.Println("Error", err) + log.Fatalf("Error", err) } defer cli.Close() @@ -49,7 +49,7 @@ func main() { var cmd = exec.Command("sh", "-c", command) if dockerHost != cli.DaemonHost() { - fmt.Sprintf("Could not connect to %s", dockerHost) + log.Fatalf("Could not connect to %s", dockerHost) } c := cron.New() pruneImages(cli, c) @@ -59,7 +59,7 @@ func main() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { - fmt.Println("could not run command: ", err) + log.Fatalf("could not run command: ", err) } } @@ -123,7 +123,7 @@ func updateImages(client *client.Client, c *cron.Cron) { c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() - filters := addFilters(repositoryToUpdate) + filters := addFilters(repositoriesToUpdate) images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) @@ -156,13 +156,9 @@ func updateImages(client *client.Client, c *cron.Cron) { func addFilters(repo string) filters.Args { filters := filters.NewArgs() - if strings.Contains(repo, "|") { - splitRepos := strings.Split(repo, "|") - for _, repo := range splitRepos { - filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) - } - } else { - filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) + splitRepos := strings.Split(repo, "|") + for _, repo := range splitRepos { + filters.Add("reference", repo) } return filters } From 8e1232286823141e690d752872a44cbab03a3655 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 26 May 2023 11:36:32 +1000 Subject: [PATCH 14/42] Updated dockerfile env to match new filter --- docker-host/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 34229a4b..5635df44 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -35,7 +35,7 @@ RUN apk add --no-cache bash ENV DOCKER_HOST=docker-host \ REGISTRY=docker-registry.default.svc:5000 \ - REPOSITORIES_TO_UPDATE=amazeeio \ + REPOSITORIES_TO_UPDATE=*uselagoon/*:* \ BIP=172.16.0.1/16 \ REGISTRY_MIRROR=https://imagecache.amazeeio.cloud From 7e1f9085512fc4f472266b6c1b8df9915513523e Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Thu, 29 Jun 2023 16:06:07 +1000 Subject: [PATCH 15/42] update docker to v24 --- docker-host/Dockerfile | 2 +- docker-host/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 5635df44..0358c5e2 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -10,7 +10,7 @@ COPY go.sum . RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build docker-host . FROM uselagoon/commons:latest as commons -FROM docker:20.10.24-dind +FROM docker:24.0.2-dind LABEL org.opencontainers.image.authors="The Lagoon Authors" maintainer="The Lagoon Authors" LABEL org.opencontainers.image.source="https://github.com/uselagoon/lagoon-service-images" repository="https://github.com/uselagoon/lagoon-service-images" diff --git a/docker-host/main.go b/docker-host/main.go index 1e22ca4a..76ccf843 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -39,7 +39,7 @@ 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 --storage-opt=overlay2.override_kernel_check=1", BIP) + 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", BIP) if REGISTRY != "" { command = command + fmt.Sprintf(" --insecure-registry=%s", REGISTRY) } From 08b936143a543182ff3b8957bbe5fc2ab6874740 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 15 Aug 2023 09:27:21 +1000 Subject: [PATCH 16/42] Reduce log noise --- docker-host/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-host/main.go b/docker-host/main.go index 76ccf843..3cf2a7d1 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -145,7 +145,7 @@ func updateImages(client *client.Client, c *cron.Cron) { continue } defer out.Close() - _, error := io.Copy(os.Stdout, out) + _, error := io.Copy(io.Discard, out) if error != nil { log.Println(err) } From 245b4fa0a00e13ed31295b4c542f385d4bbe8612 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 18 Aug 2023 17:12:51 +1000 Subject: [PATCH 17/42] Included a variable to set log level --- docker-host/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 3cf2a7d1..337fb7d9 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -22,6 +22,7 @@ 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 logLevel = machineryvars.GetEnv("LOG_LEVEL", "info") var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "") var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_SCHEDULE", "22 1 * * *") var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */4 * * *") @@ -39,7 +40,7 @@ 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", BIP) + 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) } @@ -138,7 +139,7 @@ func updateImages(client *client.Client, c *cron.Cron) { // # Iterates through all images that have the name of the repository we are interested in in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) - log.Println("Image to update", image) + log.Println("Checking update for", image) if err != nil { log.Println(err) From 6be93e1cdb86b2825db46bcb484acee4150e669d Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 22 Aug 2023 15:59:53 +1000 Subject: [PATCH 18/42] Changed log output to only log updated images --- docker-host/main.go | 67 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 337fb7d9..301f47a0 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -125,21 +125,27 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println("Starting update images") ctx := context.Background() filters := addFilters(repositoriesToUpdate) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + preUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) return } + var preUpdateIDs []string + for _, img := range preUpdateImages { + preUpdateIDs = append(preUpdateIDs, img.ID) + } + var imgRepoTags []string - for _, img := range images { - imgRepoTags = append(imgRepoTags, img.RepoTags...) + for _, img := range preUpdateImages { + if img.RepoTags != nil { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } } - // # Iterates through all images that have the name of the repository we are interested in in it + // # Iterates through all images that have the name of the repository we are interested in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) - log.Println("Checking update for", image) if err != nil { log.Println(err) @@ -151,10 +157,59 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println(err) } } - log.Println("Update images complete") + + postUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + log.Println(err) + } + + var postUpdateIDs []string + for _, img := range postUpdateImages { + postUpdateIDs = append(postUpdateIDs, img.ID) + } + + updatedImages := imgComparison(preUpdateIDs, postUpdateIDs) + for _, img := range postUpdateImages { + for _, updatedImg := range updatedImages { + if img.ID == updatedImg { + log.Println(fmt.Sprintf("Updated image %s", img.RepoTags)) + } + } + } + + imgPluralize := "" + if len(updatedImages) == 1 { + imgPluralize = "image" + } else { + imgPluralize = "images" + } + log.Println(fmt.Sprintf("Update images complete | %d %s updated", len(updatedImages), imgPluralize)) }) } +func imgComparison(preUpdate, postUpdate []string) []string { + var updatedImgs []string + + for i := 0; i < 2; i++ { + for _, preUpdateImg := range preUpdate { + found := false + for _, postUpdateImg := range postUpdate { + if preUpdateImg == postUpdateImg { + found = true + break + } + } + if !found { + updatedImgs = append(updatedImgs, preUpdateImg) + } + } + if i == 0 { + preUpdate, postUpdate = postUpdate, preUpdate + } + } + return updatedImgs +} + func addFilters(repo string) filters.Args { filters := filters.NewArgs() splitRepos := strings.Split(repo, "|") From 6188bad6884c60407fcdc4606578252cdf53621a Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:32:45 +1000 Subject: [PATCH 19/42] Refactoring dockerhost scripts to golang --- docker-host/main.go | 108 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 25 ++++++++++ go.sum | 84 ++++++++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 docker-host/main.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/docker-host/main.go b/docker-host/main.go new file mode 100644 index 00000000..100710f5 --- /dev/null +++ b/docker-host/main.go @@ -0,0 +1,108 @@ +package main + +import ( + "context" + "fmt" + "io" + "os" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/client" +) + +const EnvOverrideHost = "DOCKER_HOST" + +var dockerHost = os.Getenv("DOCKER_HOST") +var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") + +func main() { + cli, err := client.NewClientWithOpts( + client.WithHostFromEnv(), + ) + if err != nil { + fmt.Println("Error", err) + } + defer cli.Close() + + if cli.DaemonHost() != dockerHost { + fmt.Sprintf("Could not connect to %s", dockerHost) + } + + pruneImages(cli) + removeExited(cli) + updateImages(cli) +} + +func pruneImages(client *client.Client) { + ageFilter := filters.NewArgs() + danglingFilter := filters.NewArgs() + ageFilter.Add("until", "168") + danglingFilter.Add("dangling", "true") + + // # prune all images older than 7 days or what is specified in the environment variable + client.ImagesPrune(context.Background(), ageFilter) + + // # prune all docker build cache images older than 7 days or what is specified in the environment variable + client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) + + // # after old images are pruned, clean up dangling images + client.ImagesPrune(context.Background(), danglingFilter) + + fmt.Println("Prune complete") +} + +func removeExited(client *client.Client) { + ctx := context.Background() + statusFilter := filters.NewArgs() + statusFilter.Add("status", "exited") + containers, err := client.ContainerList(ctx, types.ContainerListOptions{ + Filters: statusFilter, + }) + if err != nil { + panic(err) + } + + // # remove all exited containers + for _, container := range containers { + _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + Force: true, + RemoveVolumes: true, + }) + } + + fmt.Println("removeExited complete") +} + +func updateImages(client *client.Client) { + ctx := context.Background() + filters := filters.NewArgs() + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + panic(err) + } + + // # Iterates through all images that have the name of the repository we are interested in in it + for i, image := range images { + out, err := client.ImagePull(ctx, image.RepoTags[i], types.ImagePullOptions{}) + if err != nil { + panic(err) + } + defer out.Close() + io.Copy(os.Stdout, out) + } + fmt.Println("Update images complete") +} + +// #Todo +// func updatePushImages(client *client.Client) { +// } + +func getEnv(key, defaultValue string) string { + value := os.Getenv(key) + if len(value) == 0 { + return defaultValue + } + return value +} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..afa5843a --- /dev/null +++ b/go.mod @@ -0,0 +1,25 @@ +module lagoon-service-images + +go 1.19 + +require github.com/docker/docker v23.0.1+incompatible + +require ( + github.com/Microsoft/go-winio v0.6.0 // indirect + github.com/docker/distribution v2.8.1+incompatible // indirect + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect + github.com/morikuni/aec v1.0.0 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.0.2 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/stretchr/testify v1.8.2 // indirect + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect + golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect + golang.org/x/sys v0.1.0 // indirect + golang.org/x/time v0.3.0 // indirect + golang.org/x/tools v0.1.12 // indirect + gotest.tools/v3 v3.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..c9c6fb1c --- /dev/null +++ b/go.sum @@ -0,0 +1,84 @@ +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg= +github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= +github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v23.0.1+incompatible h1:vjgvJZxprTTE1A37nm+CLNAdwu6xZekyoiVlUZEINcY= +github.com/docker/docker v23.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= +github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= +github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= +github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= +gotest.tools/v3 v3.4.0/go.mod h1:CtbdzLSsqVhDgMtKsx03ird5YTGB3ar27v0u/yKBW5g= From 2b2fafe1373773c1d0db8856bda68239a21b3df1 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:32:45 +1000 Subject: [PATCH 20/42] Refactored update-push-images --- docker-host/main.go | 67 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 100710f5..b2ba6fc2 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "io" + "io/ioutil" "os" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -15,6 +17,7 @@ const EnvOverrideHost = "DOCKER_HOST" var dockerHost = os.Getenv("DOCKER_HOST") var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") +var registryHost = getEnv("REGISTRY", "docker-registry.default.svc:5000") func main() { cli, err := client.NewClientWithOpts( @@ -29,9 +32,10 @@ func main() { fmt.Sprintf("Could not connect to %s", dockerHost) } - pruneImages(cli) - removeExited(cli) - updateImages(cli) + // pruneImages(cli) + // removeExited(cli) + // updateImages(cli) + updatePushImages(cli) } func pruneImages(client *client.Client) { @@ -96,8 +100,61 @@ func updateImages(client *client.Client) { } // #Todo -// func updatePushImages(client *client.Client) { -// } +func updatePushImages(client *client.Client) { + ctx := context.Background() + namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") + if err != nil { + fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) + os.Exit(1) + } + token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") + if err != nil { + fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) + os.Exit(1) + } + client.RegistryLogin(ctx, types.AuthConfig{ + Username: "serviceaccount", + Password: "", + RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), + }) + + filters := filters.NewArgs() + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + panic(err) + } + + var imgRepoTags []string + for _, img := range images { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } + + imageNoRespository := "" + + for _, fullImage := range imgRepoTags { + image := strings.Split(fullImage, "/") + + if len(image) == 3 { + imageNoRespository = image[2] + } else { + imageNoRespository = image[1] + } + + // # pull newest version of found image + out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) + if err != nil { + panic(err) + } + defer out.Close() + + // # Tag the image with the openshift registry name and the openshift project this container is running + client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) + + // # Push to the openshift registry + client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) + } +} func getEnv(key, defaultValue string) string { value := os.Getenv(key) From 684fd37e9327c40f5cb1c5f08174d92007434ced Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:00 +1000 Subject: [PATCH 21/42] Add cron for each function --- docker-host/Dockerfile | 22 ++++--- go.mod => docker-host/go.mod | 13 ++-- go.sum => docker-host/go.sum | 23 ++++---- docker-host/main.go | 111 ++++++++++++++++++++--------------- 4 files changed, 99 insertions(+), 70 deletions(-) rename go.mod => docker-host/go.mod (69%) rename go.sum => docker-host/go.sum (87%) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index f859d14c..f8a4c3bb 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,5 +1,16 @@ -FROM uselagoon/commons:latest as commons +FROM golang:1.18-alpine3.17 as builder + +WORKDIR /docker-host + +COPY main.go . +COPY go.mod . +COPY go.sum . + +# compile +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build docker-host . + +FROM uselagoon/commons:latest as commons FROM docker:27.0.3-dind LABEL org.opencontainers.image.authors="The Lagoon Authors" maintainer="The Lagoon Authors" @@ -11,6 +22,8 @@ COPY --from=commons /bin/fix-permissions /bin/ep /bin/docker-sleep /bin/ COPY --from=commons /sbin/tini /sbin/ COPY --from=commons /home /home +COPY --from=builder /docker-host /docker-host + ENV TMPDIR=/tmp \ TMP=/tmp \ HOME=/home \ @@ -29,11 +42,6 @@ ENV DOCKER_HOST=docker-host \ RUN fix-permissions /home -COPY update-push-images.sh /update-push-images.sh -COPY update-images.sh /update-images.sh -COPY prune-images.sh /prune-images.sh -COPY remove-exited.sh /remove-exited.sh - ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --registry-mirror=${REGISTRY_MIRROR}"] +CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}, /docker-host"] diff --git a/go.mod b/docker-host/go.mod similarity index 69% rename from go.mod rename to docker-host/go.mod index afa5843a..dd551976 100644 --- a/go.mod +++ b/docker-host/go.mod @@ -1,8 +1,8 @@ -module lagoon-service-images +module docker-host go 1.19 -require github.com/docker/docker v23.0.1+incompatible +require github.com/docker/docker v23.0.2+incompatible require ( github.com/Microsoft/go-winio v0.6.0 // indirect @@ -15,11 +15,12 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/robfig/cron/v3 v3.0.0 // indirect github.com/stretchr/testify v1.8.2 // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect - golang.org/x/sys v0.1.0 // indirect + golang.org/x/mod v0.9.0 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/sys v0.6.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.1.12 // indirect + golang.org/x/tools v0.7.0 // indirect gotest.tools/v3 v3.4.0 // indirect ) diff --git a/go.sum b/docker-host/go.sum similarity index 87% rename from go.sum rename to docker-host/go.sum index c9c6fb1c..d385b93e 100644 --- a/go.sum +++ b/docker-host/go.sum @@ -6,8 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v23.0.1+incompatible h1:vjgvJZxprTTE1A37nm+CLNAdwu6xZekyoiVlUZEINcY= -github.com/docker/docker v23.0.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker v23.0.2+incompatible h1:q81C2qQ/EhPm8COZMUGOQYh4qLv4Xu6CXELJ3WK/mlU= +github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= @@ -30,6 +30,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= +github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -44,23 +46,24 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= +golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= @@ -70,8 +73,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= +golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/docker-host/main.go b/docker-host/main.go index b2ba6fc2..73c418e2 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -11,6 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" + "github.com/robfig/cron/v3" ) const EnvOverrideHost = "DOCKER_HOST" @@ -32,74 +33,90 @@ func main() { fmt.Sprintf("Could not connect to %s", dockerHost) } - // pruneImages(cli) - // removeExited(cli) - // updateImages(cli) + pruneImages(cli) + removeExited(cli) + updateImages(cli) updatePushImages(cli) } func pruneImages(client *client.Client) { - ageFilter := filters.NewArgs() - danglingFilter := filters.NewArgs() - ageFilter.Add("until", "168") - danglingFilter.Add("dangling", "true") + c := cron.New() + c.AddFunc("22 1 * * *", func() { + ageFilter := filters.NewArgs() + danglingFilter := filters.NewArgs() + ageFilter.Add("until", "168") + danglingFilter.Add("dangling", "true") - // # prune all images older than 7 days or what is specified in the environment variable - client.ImagesPrune(context.Background(), ageFilter) + // # prune all images older than 7 days or what is specified in the environment variable + client.ImagesPrune(context.Background(), ageFilter) - // # prune all docker build cache images older than 7 days or what is specified in the environment variable - client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) + // # prune all docker build cache images older than 7 days or what is specified in the environment variable + client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) - // # after old images are pruned, clean up dangling images - client.ImagesPrune(context.Background(), danglingFilter) + // # after old images are pruned, clean up dangling images + client.ImagesPrune(context.Background(), danglingFilter) - fmt.Println("Prune complete") + fmt.Println("Prune complete") + }) + c.Start() } func removeExited(client *client.Client) { - ctx := context.Background() - statusFilter := filters.NewArgs() - statusFilter.Add("status", "exited") - containers, err := client.ContainerList(ctx, types.ContainerListOptions{ - Filters: statusFilter, - }) - if err != nil { - panic(err) - } - - // # remove all exited containers - for _, container := range containers { - _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ - Force: true, - RemoveVolumes: true, + c := cron.New() + c.AddFunc("22 */4 * * *", func() { + ctx := context.Background() + statusFilter := filters.NewArgs() + statusFilter.Add("status", "exited") + containers, err := client.ContainerList(ctx, types.ContainerListOptions{ + Filters: statusFilter, }) - } + if err != nil { + panic(err) + } - fmt.Println("removeExited complete") + // # remove all exited containers + for _, container := range containers { + _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + Force: true, + RemoveVolumes: true, + }) + } + + fmt.Println("removeExited complete") + }) + c.Start() } func updateImages(client *client.Client) { - ctx := context.Background() - filters := filters.NewArgs() - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) - if err != nil { - panic(err) - } - - // # Iterates through all images that have the name of the repository we are interested in in it - for i, image := range images { - out, err := client.ImagePull(ctx, image.RepoTags[i], types.ImagePullOptions{}) + c := cron.New() + c.AddFunc("*/15 * * * *", func() { + ctx := context.Background() + filters := filters.NewArgs() + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { panic(err) } - defer out.Close() - io.Copy(os.Stdout, out) - } - fmt.Println("Update images complete") + + var imgRepoTags []string + for _, img := range images { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } + + // # Iterates through all images that have the name of the repository we are interested in in it + for _, image := range imgRepoTags { + out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) + if err != nil { + panic(err) + } + defer out.Close() + io.Copy(os.Stdout, out) + } + fmt.Println("Update images complete") + }) + c.Start() } -// #Todo func updatePushImages(client *client.Client) { ctx := context.Background() namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") From f06bd9c0f3d2f1e1141dd411ba5fa32ba8d6db53 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:20 +1000 Subject: [PATCH 22/42] Removing bash scripts --- docker-host/Dockerfile | 2 +- docker-host/prune-images.sh | 15 --------------- docker-host/remove-exited.sh | 8 -------- docker-host/update-images.sh | 12 ------------ docker-host/update-push-images.sh | 26 -------------------------- 5 files changed, 1 insertion(+), 62 deletions(-) delete mode 100755 docker-host/prune-images.sh delete mode 100755 docker-host/remove-exited.sh delete mode 100755 docker-host/update-images.sh delete mode 100755 docker-host/update-push-images.sh diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index f8a4c3bb..40c4031b 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -44,4 +44,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}, /docker-host"] +CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}", "/docker-host"] diff --git a/docker-host/prune-images.sh b/docker-host/prune-images.sh deleted file mode 100755 index a07a2118..00000000 --- a/docker-host/prune-images.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash -set -x -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -# prune all images older than 7 days or what is specified in the environment variable -docker image prune -af --filter "until=${PRUNE_IMAGES_UNTIL:-168h}" - -# prune docker build caches coming from buildkit builds -# prune all docker build cache images older than 7 days or what is specified in the environment variable -docker builder prune -af --filter "until=${PRUNE_IMAGES_UNTIL:-168h}" - -# after old images are pruned, clean up dangling images -docker image prune -f diff --git a/docker-host/remove-exited.sh b/docker-host/remove-exited.sh deleted file mode 100755 index 319810f7..00000000 --- a/docker-host/remove-exited.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -x -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -# remove all exited containers -docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm diff --git a/docker-host/update-images.sh b/docker-host/update-images.sh deleted file mode 100755 index 0de8fbba..00000000 --- a/docker-host/update-images.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -e -set -x - -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -# Iterates through all images that have the name of the repository we are interested in in it -for FULL_IMAGE in $(docker image ls --format "{{.Repository}}:{{.Tag}}" | grep -E "${REPOSITORY_TO_UPDATE}/" | grep -v none); do - # pull newest version of found image - docker pull ${FULL_IMAGE} | cat -done diff --git a/docker-host/update-push-images.sh b/docker-host/update-push-images.sh deleted file mode 100755 index c75b3acf..00000000 --- a/docker-host/update-push-images.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -e -set -x -NAMESPACE=$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace) - -if ! docker -H ${DOCKER_HOST} info &> /dev/null; then - echo "could not connect to ${DOCKER_HOST}"; exit 1 -fi - -docker login -u=serviceaccount --password-stdin ${REGISTRY} < /var/run/secrets/kubernetes.io/serviceaccount/token - -# Iterates through all images that have the name of the repository we are interested in in it -for FULL_IMAGE in $(docker image ls --format "{{.Repository}}:{{.Tag}}" | grep -E "${REPOSITORY_TO_UPDATE}/" | grep -v none); do - IMAGE=(${FULL_IMAGE//// }) - if [ ${#IMAGE[@]} == "3" ]; then - IMAGE_NO_REPOSITORY=${IMAGE[2]} - else - IMAGE_NO_REPOSITORY=${IMAGE[1]} - fi - - # pull newest version of found image - docker pull ${FULL_IMAGE} | cat - # Tag the image with the openshift registry name and the openshift project this container is running - docker tag ${FULL_IMAGE} ${REGISTRY}/${NAMESPACE}/${IMAGE_NO_REPOSITORY} - # Push to the openshift registry - docker push ${REGISTRY}/${NAMESPACE}/${IMAGE_NO_REPOSITORY} -done From 423dd9b5220ab453aedc190dae50c435e45d0c9d Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:37 +1000 Subject: [PATCH 23/42] Updated dockerfile cmd --- docker-host/Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 40c4031b..8bcd7f66 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,4 +1,3 @@ - FROM golang:1.18-alpine3.17 as builder WORKDIR /docker-host @@ -44,4 +43,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR}", "/docker-host"] +CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR};/docker-host"] From 148d8d8005f18765164c35fda11757fd512d2770 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 24/42] Moved cmd directive into go --- docker-host/Dockerfile | 2 +- docker-host/go.mod | 14 ++-- docker-host/go.sum | 12 ++++ docker-host/main.go | 146 +++++++++++++++++++++-------------------- 4 files changed, 95 insertions(+), 79 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 8bcd7f66..77342130 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -43,4 +43,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh /usr/local/bin/dind /usr/local/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.sock --insecure-registry=${REGISTRY} --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=${BIP} --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=${REGISTRY_MIRROR};/docker-host"] +CMD ["sh", "-c", "sh", "/docker-host"] diff --git a/docker-host/go.mod b/docker-host/go.mod index dd551976..4fcd3494 100644 --- a/docker-host/go.mod +++ b/docker-host/go.mod @@ -2,7 +2,10 @@ module docker-host go 1.19 -require github.com/docker/docker v23.0.2+incompatible +require ( + github.com/docker/docker v23.0.3+incompatible + github.com/robfig/cron/v3 v3.0.1 +) require ( github.com/Microsoft/go-winio v0.6.0 // indirect @@ -15,12 +18,11 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/robfig/cron/v3 v3.0.0 // indirect github.com/stretchr/testify v1.8.2 // indirect - golang.org/x/mod v0.9.0 // indirect - golang.org/x/net v0.8.0 // indirect - golang.org/x/sys v0.6.0 // indirect + golang.org/x/mod v0.10.0 // indirect + golang.org/x/net v0.9.0 // indirect + golang.org/x/sys v0.7.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.7.0 // indirect + golang.org/x/tools v0.8.0 // indirect gotest.tools/v3 v3.4.0 // indirect ) diff --git a/docker-host/go.sum b/docker-host/go.sum index d385b93e..1863e42a 100644 --- a/docker-host/go.sum +++ b/docker-host/go.sum @@ -8,6 +8,8 @@ github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6 github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= github.com/docker/docker v23.0.2+incompatible h1:q81C2qQ/EhPm8COZMUGOQYh4qLv4Xu6CXELJ3WK/mlU= github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/docker v23.0.3+incompatible h1:9GhVsShNWz1hO//9BNg/dpMnZW25KydO4wtVxWAIbho= +github.com/docker/docker v23.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= @@ -32,6 +34,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -48,12 +52,16 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= +golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -64,6 +72,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= @@ -75,6 +85,8 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= +golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= +golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/docker-host/main.go b/docker-host/main.go index 73c418e2..7319c52c 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -4,9 +4,8 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" - "strings" + "os/exec" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -18,7 +17,9 @@ const EnvOverrideHost = "DOCKER_HOST" var dockerHost = os.Getenv("DOCKER_HOST") var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") -var registryHost = getEnv("REGISTRY", "docker-registry.default.svc:5000") +var REGISTRY = getEnv("REGISTRY", "docker-registry.default.svc:5000") +var BIP = getEnv("BIP", "172.16.0.1/16") +var REGISTRY_MIRROR = getEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") func main() { cli, err := client.NewClientWithOpts( @@ -32,15 +33,21 @@ func main() { if cli.DaemonHost() != dockerHost { fmt.Sprintf("Could not connect to %s", dockerHost) } - - pruneImages(cli) - removeExited(cli) - updateImages(cli) - updatePushImages(cli) + 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + c := cron.New() + pruneImages(cli, c) + removeExited(cli, c) + updateImages(cli, c) + c.Start() + cmd := exec.Command("sh", "-c", command) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + fmt.Println("could not run command: ", err) + } } -func pruneImages(client *client.Client) { - c := cron.New() +func pruneImages(client *client.Client, c *cron.Cron) { c.AddFunc("22 1 * * *", func() { ageFilter := filters.NewArgs() danglingFilter := filters.NewArgs() @@ -58,11 +65,9 @@ func pruneImages(client *client.Client) { fmt.Println("Prune complete") }) - c.Start() } -func removeExited(client *client.Client) { - c := cron.New() +func removeExited(client *client.Client, c *cron.Cron) { c.AddFunc("22 */4 * * *", func() { ctx := context.Background() statusFilter := filters.NewArgs() @@ -84,11 +89,9 @@ func removeExited(client *client.Client) { fmt.Println("removeExited complete") }) - c.Start() } -func updateImages(client *client.Client) { - c := cron.New() +func updateImages(client *client.Client, c *cron.Cron) { c.AddFunc("*/15 * * * *", func() { ctx := context.Background() filters := filters.NewArgs() @@ -114,64 +117,63 @@ func updateImages(client *client.Client) { } fmt.Println("Update images complete") }) - c.Start() } -func updatePushImages(client *client.Client) { - ctx := context.Background() - namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") - if err != nil { - fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) - os.Exit(1) - } - token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") - if err != nil { - fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) - os.Exit(1) - } - client.RegistryLogin(ctx, types.AuthConfig{ - Username: "serviceaccount", - Password: "", - RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), - }) - - filters := filters.NewArgs() - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) - if err != nil { - panic(err) - } - - var imgRepoTags []string - for _, img := range images { - imgRepoTags = append(imgRepoTags, img.RepoTags...) - } - - imageNoRespository := "" - - for _, fullImage := range imgRepoTags { - image := strings.Split(fullImage, "/") - - if len(image) == 3 { - imageNoRespository = image[2] - } else { - imageNoRespository = image[1] - } - - // # pull newest version of found image - out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) - if err != nil { - panic(err) - } - defer out.Close() - - // # Tag the image with the openshift registry name and the openshift project this container is running - client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) - - // # Push to the openshift registry - client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) - } -} +// func updatePushImages(client *client.Client) { +// ctx := context.Background() +// namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") +// if err != nil { +// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) +// os.Exit(1) +// } +// token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") +// if err != nil { +// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) +// os.Exit(1) +// } +// client.RegistryLogin(ctx, types.AuthConfig{ +// Username: "serviceaccount", +// Password: "", +// RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), +// }) + +// filters := filters.NewArgs() +// filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) +// images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) +// if err != nil { +// panic(err) +// } + +// var imgRepoTags []string +// for _, img := range images { +// imgRepoTags = append(imgRepoTags, img.RepoTags...) +// } + +// imageNoRespository := "" + +// for _, fullImage := range imgRepoTags { +// image := strings.Split(fullImage, "/") + +// if len(image) == 3 { +// imageNoRespository = image[2] +// } else { +// imageNoRespository = image[1] +// } + +// // # pull newest version of found image +// out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) +// if err != nil { +// panic(err) +// } +// defer out.Close() + +// // # Tag the image with the openshift registry name and the openshift project this container is running +// client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) + +// // # Push to the openshift registry +// client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) +// } +// } func getEnv(key, defaultValue string) string { value := os.Getenv(key) From 0a9babac9969a956872dc44208fffedcd5c766a8 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 25/42] Updated cmd --- docker-host/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 7319c52c..892a8a80 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -33,13 +33,13 @@ func main() { if cli.DaemonHost() != dockerHost { fmt.Sprintf("Could not connect to %s", dockerHost) } - 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + var cmd = exec.Command("sh", "-c", "sh", command) c := cron.New() pruneImages(cli, c) removeExited(cli, c) updateImages(cli, c) c.Start() - cmd := exec.Command("sh", "-c", command) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { From 6bfc576095d4cdf9e54678e43b7d90308dd2e858 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 26/42] Updated cmd and added error handling --- docker-host/Dockerfile | 6 +-- docker-host/main.go | 116 ++++++++++++++--------------------------- 2 files changed, 42 insertions(+), 80 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 77342130..c5d91736 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,6 +1,6 @@ FROM golang:1.18-alpine3.17 as builder -WORKDIR /docker-host +WORKDIR /build COPY main.go . COPY go.mod . @@ -21,7 +21,7 @@ COPY --from=commons /bin/fix-permissions /bin/ep /bin/docker-sleep /bin/ COPY --from=commons /sbin/tini /sbin/ COPY --from=commons /home /home -COPY --from=builder /docker-host /docker-host +COPY --from=builder /build/docker-host /docker-host ENV TMPDIR=/tmp \ TMP=/tmp \ @@ -43,4 +43,4 @@ RUN fix-permissions /home ENTRYPOINT ["/sbin/tini", "--", "/lagoon/entrypoints.sh"] -CMD ["sh", "-c", "sh", "/docker-host"] +CMD ["sh", "-c", "/docker-host"] diff --git a/docker-host/main.go b/docker-host/main.go index 892a8a80..601c71b6 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "log" "os" "os/exec" @@ -15,7 +16,7 @@ import ( const EnvOverrideHost = "DOCKER_HOST" -var dockerHost = os.Getenv("DOCKER_HOST") +var dockerHost = getEnv("DOCKER_HOST", "docker-host") var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") var REGISTRY = getEnv("REGISTRY", "docker-registry.default.svc:5000") var BIP = getEnv("BIP", "172.16.0.1/16") @@ -30,45 +31,58 @@ func main() { } defer cli.Close() - if cli.DaemonHost() != dockerHost { + 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + var cmd = exec.Command("sh", "-c", command) + + if dockerHost != cli.DaemonHost() { fmt.Sprintf("Could not connect to %s", dockerHost) } - 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) - var cmd = exec.Command("sh", "-c", "sh", command) c := cron.New() pruneImages(cli, c) removeExited(cli, c) updateImages(cli, c) + fmt.Println("Cronjob start") c.Start() cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + fmt.Println("Cronjob run") if err := cmd.Run(); err != nil { fmt.Println("could not run command: ", err) } } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc("22 1 * * *", func() { + c.AddFunc("/5 * * * *", func() { + log.Println("Starting image prune") + // c.AddFunc("22 1 * * *", func() { ageFilter := filters.NewArgs() danglingFilter := filters.NewArgs() ageFilter.Add("until", "168") danglingFilter.Add("dangling", "true") // # prune all images older than 7 days or what is specified in the environment variable - client.ImagesPrune(context.Background(), ageFilter) - + _, err := client.ImagesPrune(context.Background(), ageFilter) + if err != nil { + log.Println(err) + } // # prune all docker build cache images older than 7 days or what is specified in the environment variable - client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) - + _, buildErr := client.BuildCachePrune(context.Background(), types.BuildCachePruneOptions{Filters: ageFilter}) + if buildErr != nil { + log.Println(err) + } // # after old images are pruned, clean up dangling images - client.ImagesPrune(context.Background(), danglingFilter) - - fmt.Println("Prune complete") + _, pruneErr := client.ImagesPrune(context.Background(), danglingFilter) + if pruneErr != nil { + log.Println(err) + } + log.Println("Prune complete") }) } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc("22 */4 * * *", func() { + c.AddFunc("/6 * * * *", func() { + log.Println("Starting remove exited") + // c.AddFunc("22 */4 * * *", func() { ctx := context.Background() statusFilter := filters.NewArgs() statusFilter.Add("status", "exited") @@ -76,29 +90,33 @@ func removeExited(client *client.Client, c *cron.Cron) { Filters: statusFilter, }) if err != nil { - panic(err) + log.Println(err) } // # remove all exited containers for _, container := range containers { - _ = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + err := client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ Force: true, RemoveVolumes: true, }) + if err != nil { + log.Println(err) + } } - - fmt.Println("removeExited complete") + log.Println("removeExited complete") }) } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc("*/15 * * * *", func() { + c.AddFunc("/8 * * * *", func() { + log.Println("Starting update images") + // c.AddFunc("*/15 * * * *", func() { ctx := context.Background() filters := filters.NewArgs() filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { - panic(err) + log.Println(err) } var imgRepoTags []string @@ -110,71 +128,15 @@ func updateImages(client *client.Client, c *cron.Cron) { for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) if err != nil { - panic(err) + log.Println(err) } defer out.Close() io.Copy(os.Stdout, out) } - fmt.Println("Update images complete") + log.Println("Update images complete") }) } -// func updatePushImages(client *client.Client) { -// ctx := context.Background() -// namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace") -// if err != nil { -// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) -// os.Exit(1) -// } -// token, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") -// if err != nil { -// fmt.Println(fmt.Sprintf("Task failed to read the token, error was: %v", err)) -// os.Exit(1) -// } -// client.RegistryLogin(ctx, types.AuthConfig{ -// Username: "serviceaccount", -// Password: "", -// RegistryToken: fmt.Sprintf("%s:%s", registryHost, token), -// }) - -// filters := filters.NewArgs() -// filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) -// images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) -// if err != nil { -// panic(err) -// } - -// var imgRepoTags []string -// for _, img := range images { -// imgRepoTags = append(imgRepoTags, img.RepoTags...) -// } - -// imageNoRespository := "" - -// for _, fullImage := range imgRepoTags { -// image := strings.Split(fullImage, "/") - -// if len(image) == 3 { -// imageNoRespository = image[2] -// } else { -// imageNoRespository = image[1] -// } - -// // # pull newest version of found image -// out, err := client.ImagePull(ctx, fullImage, types.ImagePullOptions{}) -// if err != nil { -// panic(err) -// } -// defer out.Close() - -// // # Tag the image with the openshift registry name and the openshift project this container is running -// client.ImageTag(ctx, fullImage, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository)) - -// // # Push to the openshift registry -// client.ImagePush(ctx, fmt.Sprintf("%s/%s/%s", registryHost, namespace, imageNoRespository), types.ImagePushOptions{}) -// } -// } - func getEnv(key, defaultValue string) string { value := os.Getenv(key) if len(value) == 0 { From e586b549ccdd425abc9278acfbd1c9fbfc7cdbd3 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 27/42] Added api version negotiation --- docker-host/main.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 601c71b6..725f73d8 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -25,6 +25,7 @@ var REGISTRY_MIRROR = getEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.clo func main() { cli, err := client.NewClientWithOpts( client.WithHostFromEnv(), + client.WithAPIVersionNegotiation(), ) if err != nil { fmt.Println("Error", err) @@ -41,23 +42,20 @@ func main() { pruneImages(cli, c) removeExited(cli, c) updateImages(cli, c) - fmt.Println("Cronjob start") c.Start() cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - fmt.Println("Cronjob run") if err := cmd.Run(); err != nil { fmt.Println("could not run command: ", err) } } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc("/5 * * * *", func() { + c.AddFunc("22 1 * * *", func() { log.Println("Starting image prune") - // c.AddFunc("22 1 * * *", func() { ageFilter := filters.NewArgs() danglingFilter := filters.NewArgs() - ageFilter.Add("until", "168") + ageFilter.Add("until", "168h") danglingFilter.Add("dangling", "true") // # prune all images older than 7 days or what is specified in the environment variable @@ -68,21 +66,20 @@ func pruneImages(client *client.Client, c *cron.Cron) { // # 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(err) + log.Println(buildErr) } // # after old images are pruned, clean up dangling images _, pruneErr := client.ImagesPrune(context.Background(), danglingFilter) if pruneErr != nil { - log.Println(err) + log.Println(pruneErr) } log.Println("Prune complete") }) } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc("/6 * * * *", func() { - log.Println("Starting remove exited") - // c.AddFunc("22 */4 * * *", func() { + c.AddFunc("22 */4 * * *", func() { + log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() statusFilter.Add("status", "exited") @@ -108,9 +105,8 @@ func removeExited(client *client.Client, c *cron.Cron) { } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc("/8 * * * *", func() { + c.AddFunc("*/15 * * * *", func() { log.Println("Starting update images") - // c.AddFunc("*/15 * * * *", func() { ctx := context.Background() filters := filters.NewArgs() filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) From b5042e6b421efa0288c5363a43ab0fb7c9dc4c48 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 28/42] Adding cron schedules + filters as env vars --- docker-host/go.mod | 1 + docker-host/go.sum | 2 ++ docker-host/main.go | 38 ++++++++++++++++++-------------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/docker-host/go.mod b/docker-host/go.mod index 4fcd3494..be56e270 100644 --- a/docker-host/go.mod +++ b/docker-host/go.mod @@ -19,6 +19,7 @@ require ( github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/stretchr/testify v1.8.2 // indirect + github.com/uselagoon/machinery v0.0.7 // indirect golang.org/x/mod v0.10.0 // indirect golang.org/x/net v0.9.0 // indirect golang.org/x/sys v0.7.0 // indirect diff --git a/docker-host/go.sum b/docker-host/go.sum index 1863e42a..ca22704e 100644 --- a/docker-host/go.sum +++ b/docker-host/go.sum @@ -43,6 +43,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/uselagoon/machinery v0.0.7 h1:fBIURDQRVnaB/Gime1Lfp6kgQYmuQpa0gmdfPwo7dkc= +github.com/uselagoon/machinery v0.0.7/go.mod h1:dGAvkxWQkRu/eLQeWLrTl5HvNoFsiXlXInCptHUxzUw= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/docker-host/main.go b/docker-host/main.go index 725f73d8..3e39fa75 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -12,15 +12,21 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" "github.com/robfig/cron/v3" + machineryvars "github.com/uselagoon/machinery/utils/variables" ) const EnvOverrideHost = "DOCKER_HOST" -var dockerHost = getEnv("DOCKER_HOST", "docker-host") -var repo = getEnv("REPOSITORY_TO_UPDATE", "amazeeio") -var REGISTRY = getEnv("REGISTRY", "docker-registry.default.svc:5000") -var BIP = getEnv("BIP", "172.16.0.1/16") -var REGISTRY_MIRROR = getEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") +var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host") +var repo = machineryvars.GetEnv("REPOSITORY_TO_UPDATE", "amazeeio") +var REGISTRY = machineryvars.GetEnv("REGISTRY", "docker-registry.default.svc:5000") +var BIP = machineryvars.GetEnv("BIP", "172.16.0.1/16") +var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") +var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_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 danglingFilter = machineryvars.GetEnv("DANGLING_FILTER", "true") func main() { cli, err := client.NewClientWithOpts( @@ -51,12 +57,12 @@ func main() { } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc("22 1 * * *", func() { + c.AddFunc(pruneImagesSchedule, func() { log.Println("Starting image prune") ageFilter := filters.NewArgs() - danglingFilter := filters.NewArgs() - ageFilter.Add("until", "168h") - danglingFilter.Add("dangling", "true") + pruneDanglingFilter := filters.NewArgs() + ageFilter.Add("until", pruneImagesUntil) + pruneDanglingFilter.Add("dangling", danglingFilter) // # prune all images older than 7 days or what is specified in the environment variable _, err := client.ImagesPrune(context.Background(), ageFilter) @@ -69,7 +75,7 @@ func pruneImages(client *client.Client, c *cron.Cron) { log.Println(buildErr) } // # after old images are pruned, clean up dangling images - _, pruneErr := client.ImagesPrune(context.Background(), danglingFilter) + _, pruneErr := client.ImagesPrune(context.Background(), pruneDanglingFilter) if pruneErr != nil { log.Println(pruneErr) } @@ -78,7 +84,7 @@ func pruneImages(client *client.Client, c *cron.Cron) { } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc("22 */4 * * *", func() { + c.AddFunc(removeExitedSchedule, func() { log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() @@ -105,7 +111,7 @@ func removeExited(client *client.Client, c *cron.Cron) { } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc("*/15 * * * *", func() { + c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() filters := filters.NewArgs() @@ -132,11 +138,3 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println("Update images complete") }) } - -func getEnv(key, defaultValue string) string { - value := os.Getenv(key) - if len(value) == 0 { - return defaultValue - } - return value -} From 70b93334a21d05704e45df7ed3407258a8e7abc9 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 29/42] Various improvements --- docker-host/main.go | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 3e39fa75..64bc3610 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/exec" + "strings" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -18,10 +19,10 @@ import ( const EnvOverrideHost = "DOCKER_HOST" var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host") -var repo = machineryvars.GetEnv("REPOSITORY_TO_UPDATE", "amazeeio") +var repositoryToUpdate = machineryvars.GetEnv("REPOSITORY_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_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "https://imagecache.amazeeio.cloud") +var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "") var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_SCHEDULE", "22 1 * * *") var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */4 * * *") var updateImagesSchedule = machineryvars.GetEnv("UPDATE_IMAGES_SCHEDULE", "*/15 * * * *") @@ -34,11 +35,17 @@ func main() { client.WithAPIVersionNegotiation(), ) if err != nil { - fmt.Println("Error", err) + log.Println("Error", err) } 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 --insecure-registry=%s --insecure-registry=harbor-harbor-core.harbor.svc.cluster.local:80 --bip=%s --storage-driver=overlay2 --storage-opt=overlay2.override_kernel_check=1 --registry-mirror=%s", REGISTRY, BIP, REGISTRY_MIRROR) + 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 --storage-opt=overlay2.override_kernel_check=1", BIP) + if REGISTRY != "" { + command = command + fmt.Sprintf(" --insecure-registry=%s", REGISTRY) + } + if REGISTRY_MIRROR != "" { + command = command + fmt.Sprintf(" --registry-mirror=%s", REGISTRY_MIRROR) + } var cmd = exec.Command("sh", "-c", command) if dockerHost != cli.DaemonHost() { @@ -68,6 +75,7 @@ func pruneImages(client *client.Client, c *cron.Cron) { _, err := client.ImagesPrune(context.Background(), ageFilter) if err != nil { 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}) @@ -94,6 +102,7 @@ func removeExited(client *client.Client, c *cron.Cron) { }) if err != nil { log.Println(err) + return } // # remove all exited containers @@ -114,11 +123,11 @@ func updateImages(client *client.Client, c *cron.Cron) { c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() - filters := filters.NewArgs() - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + filters := addFilters(repositoryToUpdate) images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) + return } var imgRepoTags []string @@ -129,12 +138,31 @@ func updateImages(client *client.Client, c *cron.Cron) { // # Iterates through all images that have the name of the repository we are interested in in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) + log.Println("Image to update", image) + if err != nil { log.Println(err) + continue } defer out.Close() - io.Copy(os.Stdout, out) + _, error := io.Copy(os.Stdout, out) + if error != nil { + log.Println(err) + } } log.Println("Update images complete") }) } + +func addFilters(repo string) filters.Args { + filters := filters.NewArgs() + if strings.Contains(repo, "|") { + splitRepos := strings.Split(repo, "|") + for _, repo := range splitRepos { + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + } + } else { + filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + } + return filters +} From f016bd26b53f9e615dcbfa1632453ad1a0ed978b Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 30/42] Updated filter --- docker-host/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 64bc3610..14340d14 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -159,10 +159,10 @@ func addFilters(repo string) filters.Args { if strings.Contains(repo, "|") { splitRepos := strings.Split(repo, "|") for _, repo := range splitRepos { - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) } } else { - filters.Add("reference", fmt.Sprintf("%s/*:*", repo)) + filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) } return filters } From 66659fec6327ae2fadf3deee2c1db91eb497d067 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 31/42] Updated image filter --- docker-host/Dockerfile | 2 +- docker-host/main.go | 20 ++++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index c5d91736..c3faff16 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -35,7 +35,7 @@ RUN apk add --no-cache bash ENV DOCKER_HOST=docker-host \ REGISTRY=docker-registry.default.svc:5000 \ - REPOSITORY_TO_UPDATE=amazeeio \ + REPOSITORIES_TO_UPDATE=amazeeio \ BIP=172.16.0.1/16 \ REGISTRY_MIRROR=https://imagecache.amazeeio.cloud diff --git a/docker-host/main.go b/docker-host/main.go index 14340d14..1e22ca4a 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -19,7 +19,7 @@ import ( const EnvOverrideHost = "DOCKER_HOST" var dockerHost = machineryvars.GetEnv("DOCKER_HOST", "docker-host") -var repositoryToUpdate = machineryvars.GetEnv("REPOSITORY_TO_UPDATE", "uselagoon") +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_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "") @@ -35,7 +35,7 @@ func main() { client.WithAPIVersionNegotiation(), ) if err != nil { - log.Println("Error", err) + log.Fatalf("Error", err) } defer cli.Close() @@ -49,7 +49,7 @@ func main() { var cmd = exec.Command("sh", "-c", command) if dockerHost != cli.DaemonHost() { - fmt.Sprintf("Could not connect to %s", dockerHost) + log.Fatalf("Could not connect to %s", dockerHost) } c := cron.New() pruneImages(cli, c) @@ -59,7 +59,7 @@ func main() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { - fmt.Println("could not run command: ", err) + log.Fatalf("could not run command: ", err) } } @@ -123,7 +123,7 @@ func updateImages(client *client.Client, c *cron.Cron) { c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() - filters := addFilters(repositoryToUpdate) + filters := addFilters(repositoriesToUpdate) images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) @@ -156,13 +156,9 @@ func updateImages(client *client.Client, c *cron.Cron) { func addFilters(repo string) filters.Args { filters := filters.NewArgs() - if strings.Contains(repo, "|") { - splitRepos := strings.Split(repo, "|") - for _, repo := range splitRepos { - filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) - } - } else { - filters.Add("reference", fmt.Sprintf("*%s/*:*", repo)) + splitRepos := strings.Split(repo, "|") + for _, repo := range splitRepos { + filters.Add("reference", repo) } return filters } From 312543d3d377e4436c92d97884085ffd4b69caae Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 27 Aug 2024 08:34:50 +1000 Subject: [PATCH 32/42] Updated dockerfile env to match new filter --- docker-host/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index c3faff16..b647e34d 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -35,7 +35,7 @@ RUN apk add --no-cache bash ENV DOCKER_HOST=docker-host \ REGISTRY=docker-registry.default.svc:5000 \ - REPOSITORIES_TO_UPDATE=amazeeio \ + REPOSITORIES_TO_UPDATE=*uselagoon/*:* \ BIP=172.16.0.1/16 \ REGISTRY_MIRROR=https://imagecache.amazeeio.cloud From e2def9213966153130a91c30036917e2c5bfab8a Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Tue, 27 Aug 2024 08:35:02 +1000 Subject: [PATCH 33/42] update docker to v24 --- docker-host/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-host/main.go b/docker-host/main.go index 1e22ca4a..76ccf843 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -39,7 +39,7 @@ 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 --storage-opt=overlay2.override_kernel_check=1", BIP) + 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", BIP) if REGISTRY != "" { command = command + fmt.Sprintf(" --insecure-registry=%s", REGISTRY) } From 730dcbf203be260f9d2caa3859ae3b9c5be237da Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 27 Aug 2024 08:35:02 +1000 Subject: [PATCH 34/42] Reduce log noise --- docker-host/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-host/main.go b/docker-host/main.go index 76ccf843..3cf2a7d1 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -145,7 +145,7 @@ func updateImages(client *client.Client, c *cron.Cron) { continue } defer out.Close() - _, error := io.Copy(os.Stdout, out) + _, error := io.Copy(io.Discard, out) if error != nil { log.Println(err) } From fe7d006951347eaa4be7fadb7fcbfd1d1d4bcb70 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 27 Aug 2024 08:35:02 +1000 Subject: [PATCH 35/42] Included a variable to set log level --- docker-host/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 3cf2a7d1..337fb7d9 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -22,6 +22,7 @@ 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 logLevel = machineryvars.GetEnv("LOG_LEVEL", "info") var REGISTRY_MIRROR = machineryvars.GetEnv("REGISTRY_MIRROR", "") var pruneImagesSchedule = machineryvars.GetEnv("PRUNE_SCHEDULE", "22 1 * * *") var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */4 * * *") @@ -39,7 +40,7 @@ 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", BIP) + 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) } @@ -138,7 +139,7 @@ func updateImages(client *client.Client, c *cron.Cron) { // # Iterates through all images that have the name of the repository we are interested in in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) - log.Println("Image to update", image) + log.Println("Checking update for", image) if err != nil { log.Println(err) From 6f9c0e14884c877ab997c2442958e72278c673be Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Tue, 27 Aug 2024 08:35:02 +1000 Subject: [PATCH 36/42] Changed log output to only log updated images --- docker-host/main.go | 67 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 337fb7d9..301f47a0 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -125,21 +125,27 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println("Starting update images") ctx := context.Background() filters := addFilters(repositoriesToUpdate) - images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + preUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) if err != nil { log.Println(err) return } + var preUpdateIDs []string + for _, img := range preUpdateImages { + preUpdateIDs = append(preUpdateIDs, img.ID) + } + var imgRepoTags []string - for _, img := range images { - imgRepoTags = append(imgRepoTags, img.RepoTags...) + for _, img := range preUpdateImages { + if img.RepoTags != nil { + imgRepoTags = append(imgRepoTags, img.RepoTags...) + } } - // # Iterates through all images that have the name of the repository we are interested in in it + // # Iterates through all images that have the name of the repository we are interested in it for _, image := range imgRepoTags { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) - log.Println("Checking update for", image) if err != nil { log.Println(err) @@ -151,10 +157,59 @@ func updateImages(client *client.Client, c *cron.Cron) { log.Println(err) } } - log.Println("Update images complete") + + postUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + if err != nil { + log.Println(err) + } + + var postUpdateIDs []string + for _, img := range postUpdateImages { + postUpdateIDs = append(postUpdateIDs, img.ID) + } + + updatedImages := imgComparison(preUpdateIDs, postUpdateIDs) + for _, img := range postUpdateImages { + for _, updatedImg := range updatedImages { + if img.ID == updatedImg { + log.Println(fmt.Sprintf("Updated image %s", img.RepoTags)) + } + } + } + + imgPluralize := "" + if len(updatedImages) == 1 { + imgPluralize = "image" + } else { + imgPluralize = "images" + } + log.Println(fmt.Sprintf("Update images complete | %d %s updated", len(updatedImages), imgPluralize)) }) } +func imgComparison(preUpdate, postUpdate []string) []string { + var updatedImgs []string + + for i := 0; i < 2; i++ { + for _, preUpdateImg := range preUpdate { + found := false + for _, postUpdateImg := range postUpdate { + if preUpdateImg == postUpdateImg { + found = true + break + } + } + if !found { + updatedImgs = append(updatedImgs, preUpdateImg) + } + } + if i == 0 { + preUpdate, postUpdate = postUpdate, preUpdate + } + } + return updatedImgs +} + func addFilters(repo string) filters.Args { filters := filters.NewArgs() splitRepos := strings.Split(repo, "|") From 2ac9c4f841557b3c41825b1405259d240711956f Mon Sep 17 00:00:00 2001 From: Toby Bellwood Date: Tue, 27 Aug 2024 09:01:11 +1000 Subject: [PATCH 37/42] build: update golang to 1.22 --- .gitignore | 1 + docker-host/Dockerfile | 2 +- docker-host/go.mod | 4 ++-- docker-host/go.sum | 17 ++++------------- docker-host/main.go | 4 ++-- 5 files changed, 10 insertions(+), 18 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..22d0d82f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index b647e34d..39fb3d9a 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.18-alpine3.17 as builder +FROM golang:1.22-alpine3.20 as builder WORKDIR /build diff --git a/docker-host/go.mod b/docker-host/go.mod index be56e270..683e9333 100644 --- a/docker-host/go.mod +++ b/docker-host/go.mod @@ -1,10 +1,11 @@ module docker-host -go 1.19 +go 1.22 require ( github.com/docker/docker v23.0.3+incompatible github.com/robfig/cron/v3 v3.0.1 + github.com/uselagoon/machinery v0.0.7 ) require ( @@ -19,7 +20,6 @@ require ( github.com/opencontainers/image-spec v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/stretchr/testify v1.8.2 // indirect - github.com/uselagoon/machinery v0.0.7 // indirect golang.org/x/mod v0.10.0 // indirect golang.org/x/net v0.9.0 // indirect golang.org/x/sys v0.7.0 // indirect diff --git a/docker-host/go.sum b/docker-host/go.sum index ca22704e..0bc9fdf2 100644 --- a/docker-host/go.sum +++ b/docker-host/go.sum @@ -1,4 +1,5 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg= github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -6,8 +7,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v23.0.2+incompatible h1:q81C2qQ/EhPm8COZMUGOQYh4qLv4Xu6CXELJ3WK/mlU= -github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/docker v23.0.3+incompatible h1:9GhVsShNWz1hO//9BNg/dpMnZW25KydO4wtVxWAIbho= github.com/docker/docker v23.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= @@ -16,8 +15,9 @@ github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4 github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= @@ -32,8 +32,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= -github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -52,28 +50,23 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= -golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -85,8 +78,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= -golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/docker-host/main.go b/docker-host/main.go index 301f47a0..07d21208 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -36,7 +36,7 @@ func main() { client.WithAPIVersionNegotiation(), ) if err != nil { - log.Fatalf("Error", err) + log.Fatalf("Error: %s", err) } defer cli.Close() @@ -60,7 +60,7 @@ func main() { cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("could not run command: ", err) + log.Fatalf("could not run command: %s", err) } } From 9eb61172e2bafa5dc377cb44c3fe754add860033 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Fri, 30 Aug 2024 17:31:00 +1000 Subject: [PATCH 38/42] Initial pass at adding pruneDanglingSchedule & updates go.mod --- docker-host/go.mod | 33 ++++++++----- docker-host/go.sum | 110 ++++++++++++++++++++++++++------------------ docker-host/main.go | 83 ++++++++++++++++++++++----------- 3 files changed, 143 insertions(+), 83 deletions(-) diff --git a/docker-host/go.mod b/docker-host/go.mod index be56e270..b80004c5 100644 --- a/docker-host/go.mod +++ b/docker-host/go.mod @@ -1,29 +1,38 @@ module docker-host -go 1.19 +go 1.21 + +toolchain go1.22.3 require ( - github.com/docker/docker v23.0.3+incompatible + github.com/docker/docker v27.2.0+incompatible github.com/robfig/cron/v3 v3.0.1 + github.com/uselagoon/machinery v0.0.29 ) require ( - github.com/Microsoft/go-winio v0.6.0 // indirect - github.com/docker/distribution v2.8.1+incompatible // indirect - github.com/docker/go-connections v0.4.0 // indirect + github.com/Microsoft/go-winio v0.6.2 // indirect + github.com/containerd/log v0.1.0 // indirect + github.com/distribution/reference v0.6.0 // indirect + github.com/docker/go-connections v0.5.0 // indirect github.com/docker/go-units v0.5.0 // indirect + github.com/felixge/httpsnoop v1.0.4 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect + github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect github.com/morikuni/aec v1.0.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect - github.com/opencontainers/image-spec v1.0.2 // indirect + github.com/opencontainers/image-spec v1.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/stretchr/testify v1.8.2 // indirect - github.com/uselagoon/machinery v0.0.7 // indirect - golang.org/x/mod v0.10.0 // indirect - golang.org/x/net v0.9.0 // indirect - golang.org/x/sys v0.7.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect + go.opentelemetry.io/otel v1.29.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 // indirect + go.opentelemetry.io/otel/metric v1.29.0 // indirect + go.opentelemetry.io/otel/sdk v1.29.0 // indirect + go.opentelemetry.io/otel/trace v1.29.0 // indirect + golang.org/x/sys v0.24.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.8.0 // indirect gotest.tools/v3 v3.4.0 // indirect ) diff --git a/docker-host/go.sum b/docker-host/go.sum index ca22704e..764b4780 100644 --- a/docker-host/go.sum +++ b/docker-host/go.sum @@ -1,83 +1,103 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= -github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg= -github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= +github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= +github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68= -github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= -github.com/docker/docker v23.0.2+incompatible h1:q81C2qQ/EhPm8COZMUGOQYh4qLv4Xu6CXELJ3WK/mlU= -github.com/docker/docker v23.0.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= -github.com/docker/docker v23.0.3+incompatible h1:9GhVsShNWz1hO//9BNg/dpMnZW25KydO4wtVxWAIbho= -github.com/docker/docker v23.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= -github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= -github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= +github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/docker v27.2.0+incompatible h1:Rk9nIVdfH3+Vz4cyI/uhbINhEZ/oLmc+CBXmH6fbNk4= +github.com/docker/docker v27.2.0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c= +github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= -github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= -github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= +github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= -github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/uselagoon/machinery v0.0.7 h1:fBIURDQRVnaB/Gime1Lfp6kgQYmuQpa0gmdfPwo7dkc= -github.com/uselagoon/machinery v0.0.7/go.mod h1:dGAvkxWQkRu/eLQeWLrTl5HvNoFsiXlXInCptHUxzUw= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/uselagoon/machinery v0.0.29 h1:invFIPv1Z1xCt8/1ilbiNDuAEPrb+AUO21BnNG+CX8c= +github.com/uselagoon/machinery v0.0.29/go.mod h1:X0qguIO9skumMhhT0ap5CKHulKgYzy3TiIn+xlwiFQc= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 h1:TT4fX+nBOA/+LUkobKGW1ydGcn+G3vRw9+g5HwCphpk= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0/go.mod h1:L7UH0GbB0p47T4Rri3uHjbpCFYrVrwc1I25QhNPiGK8= +go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw= +go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 h1:dIIDULZJpgdiHz5tXrTgKIMLkus6jEFa7x5SOKcyR7E= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0/go.mod h1:jlRVBe7+Z1wyxFSUs48L6OBQZ5JwH2Hg/Vbl+t9rAgI= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 h1:JAv0Jwtl01UFiyWZEMiJZBiTlv5A50zNs8lsthXqIio= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0/go.mod h1:QNKLmUEAq2QUbPQUfvw4fmv0bgbK7UlOSFCnXyfvSNc= +go.opentelemetry.io/otel/metric v1.29.0 h1:vPf/HFWTNkPu1aYeIsc98l4ktOQaL6LeSoeV2g+8YLc= +go.opentelemetry.io/otel/metric v1.29.0/go.mod h1:auu/QWieFVWx+DmQOUMgj0F8LHWdgalxXqvp7BII/W8= +go.opentelemetry.io/otel/sdk v1.29.0 h1:vkqKjk7gwhS8VaWb0POZKmIEDimRCMsopNYnriHyryo= +go.opentelemetry.io/otel/sdk v1.29.0/go.mod h1:pM8Dx5WKnvxLCb+8lG1PRNIDxu9g9b9g59Qr7hfAAok= +go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4= +go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= +go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0= +go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs= -golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= -golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= -golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -85,16 +105,18 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4= -golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= -golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= -golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd h1:BBOTEWLuuEGQy9n1y9MhVJ9Qt0BDu21X8qZs71/uPZo= +google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd/go.mod h1:fO8wJzT2zbQbAjbIoos1285VfEIYKDDY+Dt+WpTkh6g= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd h1:6TEm2ZxXoQmFWFlt1vNxvVOa1Q0dXFQD1m/rYjXmS0E= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= +google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc= +google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.4.0 h1:ZazjZUfuVeZGLAmlKKuyv3IKP5orXcwtOwDQH6YVr6o= diff --git a/docker-host/main.go b/docker-host/main.go index 301f47a0..046e7827 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -10,7 +10,9 @@ import ( "strings" "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types/image" "github.com/docker/docker/client" "github.com/robfig/cron/v3" machineryvars "github.com/uselagoon/machinery/utils/variables" @@ -25,10 +27,10 @@ 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 pruneImagesSchedule = machineryvars.GetEnv("PRUNE_SCHEDULE", "22 1 * * *") +var pruneDanglingSchedule = machineryvars.GetEnv("PRUNE_DANGLING_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 danglingFilter = machineryvars.GetEnv("DANGLING_FILTER", "true") func main() { cli, err := client.NewClientWithOpts( @@ -36,7 +38,7 @@ func main() { client.WithAPIVersionNegotiation(), ) if err != nil { - log.Fatalf("Error", err) + log.Fatalf("Error: %v", err) } defer cli.Close() @@ -54,51 +56,70 @@ func main() { } c := cron.New() pruneImages(cli, c) + pruneDanglingImages(cli, c) removeExited(cli, c) updateImages(cli, c) c.Start() cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { - log.Fatalf("could not run command: ", err) + log.Fatalf("could not run command: %v", err) } } func pruneImages(client *client.Client, c *cron.Cron) { - c.AddFunc(pruneImagesSchedule, func() { + _, err := c.AddFunc(pruneImagesSchedule, func() { log.Println("Starting image prune") ageFilter := filters.NewArgs() - pruneDanglingFilter := filters.NewArgs() ageFilter.Add("until", pruneImagesUntil) - pruneDanglingFilter.Add("dangling", danglingFilter) - // # prune all images older than 7 days or what is specified in the environment variable - _, err := client.ImagesPrune(context.Background(), ageFilter) + // prune all images older than 7 days or what is specified in the environment variable + pruneReport, err := client.ImagesPrune(context.Background(), ageFilter) if err != nil { log.Println(err) return } - // # prune all docker build cache images older than 7 days or what is specified in the environment variable + // 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) } - // # after old images are pruned, clean up dangling images - _, pruneErr := client.ImagesPrune(context.Background(), pruneDanglingFilter) + log.Printf("Image prune complete: %d images deleted, %d bytes reclaimed\n", + len(pruneReport.ImagesDeleted), pruneReport.SpaceReclaimed) + }) + + if err != nil { + log.Printf("Error initiating pruneImages cron: %v\n", err) + } +} + +func pruneDanglingImages(client *client.Client, c *cron.Cron) { + _, err := c.AddFunc(pruneDanglingSchedule, func() { + log.Println("Starting dangling image prune") + pruneDanglingFilter := filters.NewArgs() + pruneDanglingFilter.Add("dangling", "true") + + // Cleans up dangling images + pruneReport, pruneErr := client.ImagesPrune(context.Background(), pruneDanglingFilter) if pruneErr != nil { log.Println(pruneErr) } - log.Println("Prune complete") + log.Printf("Dangling Image prune complete: %d images deleted, %d bytes reclaimed\n", + len(pruneReport.ImagesDeleted), pruneReport.SpaceReclaimed) }) + + if err != nil { + log.Printf("Error initiating pruneDanglingImages cron: %v\n", err) + } } func removeExited(client *client.Client, c *cron.Cron) { - c.AddFunc(removeExitedSchedule, func() { + _, err := c.AddFunc(removeExitedSchedule, func() { log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() statusFilter.Add("status", "exited") - containers, err := client.ContainerList(ctx, types.ContainerListOptions{ + containers, err := client.ContainerList(ctx, container.ListOptions{ Filters: statusFilter, }) if err != nil { @@ -106,9 +127,9 @@ func removeExited(client *client.Client, c *cron.Cron) { return } - // # remove all exited containers - for _, container := range containers { - err := client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{ + // remove all exited containers + for _, con := range containers { + err := client.ContainerRemove(ctx, con.ID, container.RemoveOptions{ Force: true, RemoveVolumes: true, }) @@ -118,14 +139,18 @@ func removeExited(client *client.Client, c *cron.Cron) { } log.Println("removeExited complete") }) + + if err != nil { + log.Printf("Error initiating removeExited cron: %v\n", err) + } } func updateImages(client *client.Client, c *cron.Cron) { - c.AddFunc(updateImagesSchedule, func() { + _, err := c.AddFunc(updateImagesSchedule, func() { log.Println("Starting update images") ctx := context.Background() - filters := addFilters(repositoriesToUpdate) - preUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + ImgFilters := addFilters(repositoriesToUpdate) + preUpdateImages, err := client.ImageList(ctx, image.ListOptions{Filters: ImgFilters}) if err != nil { log.Println(err) return @@ -143,9 +168,9 @@ func updateImages(client *client.Client, c *cron.Cron) { } } - // # Iterates through all images that have the name of the repository we are interested in it - for _, image := range imgRepoTags { - out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) + // Iterates through all images that have the name of the repository we are interested in it + for _, img := range imgRepoTags { + out, err := client.ImagePull(ctx, img, image.PullOptions{}) if err != nil { log.Println(err) @@ -158,7 +183,7 @@ func updateImages(client *client.Client, c *cron.Cron) { } } - postUpdateImages, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters}) + postUpdateImages, err := client.ImageList(ctx, image.ListOptions{Filters: ImgFilters}) if err != nil { log.Println(err) } @@ -185,6 +210,10 @@ func updateImages(client *client.Client, c *cron.Cron) { } log.Println(fmt.Sprintf("Update images complete | %d %s updated", len(updatedImages), imgPluralize)) }) + + if err != nil { + log.Printf("Error initiating updateImages cron: %v\n", err) + } } func imgComparison(preUpdate, postUpdate []string) []string { @@ -211,10 +240,10 @@ func imgComparison(preUpdate, postUpdate []string) []string { } func addFilters(repo string) filters.Args { - filters := filters.NewArgs() + repoFilters := filters.NewArgs() splitRepos := strings.Split(repo, "|") for _, repo := range splitRepos { - filters.Add("reference", repo) + repoFilters.Add("reference", repo) } - return filters + return repoFilters } From 98df40bae54705e1079a05e6fe7f392d09eb66d2 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Thu, 3 Oct 2024 10:14:52 +1000 Subject: [PATCH 39/42] feat: add builder cache prune --- docker-host/main.go | 51 ++++++++++++++++++++++++++++++++------------- go.mod | 3 +++ 2 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 go.mod diff --git a/docker-host/main.go b/docker-host/main.go index 046e7827..e9cc2fa2 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -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( @@ -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) @@ -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() @@ -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) }) @@ -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 build cache 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") @@ -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) } } } @@ -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 { diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..46163aa3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/uselagoon/lagoon-service-images + +go 1.22 From c482341cb80b0dbf50c2a58f58cec85c36ec40c7 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Fri, 4 Oct 2024 11:38:33 +1000 Subject: [PATCH 40/42] refactor: clean up any containers, not just exited --- docker-host/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-host/main.go b/docker-host/main.go index e9cc2fa2..5441237a 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -33,6 +33,7 @@ var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */ 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") +var removeExitedUntil = machineryvars.GetEnv("REMOVE_EXITED_UNTIL", "168h") func main() { cli, err := client.NewClientWithOpts( @@ -139,7 +140,7 @@ func removeExited(client *client.Client, c *cron.Cron) { log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() - statusFilter.Add("status", "exited") + statusFilter.Add("until", removeExitedUntil) containers, err := client.ContainerList(ctx, container.ListOptions{ Filters: statusFilter, }) From a9df02bf0e2482a62e6f8446b88ea8552eb392aa Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Tue, 8 Oct 2024 11:12:11 +1100 Subject: [PATCH 41/42] chore: fix exited container check filter --- docker-host/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docker-host/main.go b/docker-host/main.go index 5441237a..1c1d1214 100644 --- a/docker-host/main.go +++ b/docker-host/main.go @@ -33,7 +33,6 @@ var removeExitedSchedule = machineryvars.GetEnv("REMOVE_EXITED_SCHEDULE", "22 */ 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") -var removeExitedUntil = machineryvars.GetEnv("REMOVE_EXITED_UNTIL", "168h") func main() { cli, err := client.NewClientWithOpts( @@ -140,7 +139,10 @@ func removeExited(client *client.Client, c *cron.Cron) { log.Println("Starting removeExited") ctx := context.Background() statusFilter := filters.NewArgs() - statusFilter.Add("until", removeExitedUntil) + statusFilter.Add("status", "paused") + statusFilter.Add("status", "exited") + statusFilter.Add("status", "dead") + statusFilter.Add("status", "created") containers, err := client.ContainerList(ctx, container.ListOptions{ Filters: statusFilter, }) From 67efcafc5326399cac6398f452ef9914c68f9e2a Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Mon, 14 Oct 2024 11:08:59 +1100 Subject: [PATCH 42/42] chore: dockerfile AS --- docker-host/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-host/Dockerfile b/docker-host/Dockerfile index 39fb3d9a..4c9eac35 100644 --- a/docker-host/Dockerfile +++ b/docker-host/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.22-alpine3.20 as builder +FROM golang:1.22-alpine3.20 AS builder WORKDIR /build @@ -9,7 +9,7 @@ COPY go.sum . # compile RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build docker-host . -FROM uselagoon/commons:latest as commons +FROM uselagoon/commons:latest AS commons FROM docker:27.0.3-dind LABEL org.opencontainers.image.authors="The Lagoon Authors" maintainer="The Lagoon Authors"