Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: Rewrite Dockerhost scripts in Go #25

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6cd3653
Refactoring dockerhost scripts to golang
CGoodwin90 Mar 23, 2023
2d2d9de
Refactored update-push-images
CGoodwin90 Mar 28, 2023
995fae6
Add cron for each function
CGoodwin90 Apr 5, 2023
8de7b96
Removing bash scripts
CGoodwin90 Apr 5, 2023
37f8f9c
Updated dockerfile cmd
CGoodwin90 Apr 12, 2023
6a67c2c
Moved cmd directive into go
CGoodwin90 Apr 13, 2023
b0c18e7
Updated cmd
CGoodwin90 Apr 13, 2023
e088a2f
Updated cmd and added error handling
CGoodwin90 Apr 14, 2023
80125d2
Added api version negotiation
CGoodwin90 Apr 17, 2023
14c2756
Merged conflicts
CGoodwin90 Apr 19, 2023
dad6dda
Adding cron schedules + filters as env vars
CGoodwin90 Apr 24, 2023
b85f72a
Various improvements
CGoodwin90 May 5, 2023
1dd265e
Updated filter
CGoodwin90 May 5, 2023
b8f0d84
Updated image filter
CGoodwin90 May 11, 2023
8e12322
Updated dockerfile env to match new filter
CGoodwin90 May 26, 2023
7e1f908
update docker to v24
tobybellwood Jun 29, 2023
08b9361
Reduce log noise
CGoodwin90 Aug 14, 2023
245b4fa
Included a variable to set log level
CGoodwin90 Aug 18, 2023
6be93e1
Changed log output to only log updated images
CGoodwin90 Aug 22, 2023
6188bad
Refactoring dockerhost scripts to golang
CGoodwin90 Aug 26, 2024
2b2fafe
Refactored update-push-images
CGoodwin90 Aug 26, 2024
684fd37
Add cron for each function
CGoodwin90 Aug 26, 2024
f06bd9c
Removing bash scripts
CGoodwin90 Aug 26, 2024
423dd9b
Updated dockerfile cmd
CGoodwin90 Aug 26, 2024
148d8d8
Moved cmd directive into go
CGoodwin90 Aug 26, 2024
0a9baba
Updated cmd
CGoodwin90 Aug 26, 2024
6bfc576
Updated cmd and added error handling
CGoodwin90 Aug 26, 2024
e586b54
Added api version negotiation
CGoodwin90 Aug 26, 2024
b5042e6
Adding cron schedules + filters as env vars
CGoodwin90 Aug 26, 2024
70b9333
Various improvements
CGoodwin90 Aug 26, 2024
f016bd2
Updated filter
CGoodwin90 Aug 26, 2024
66659fe
Updated image filter
CGoodwin90 Aug 26, 2024
312543d
Updated dockerfile env to match new filter
CGoodwin90 Aug 26, 2024
e2def92
update docker to v24
tobybellwood Aug 26, 2024
730dcbf
Reduce log noise
CGoodwin90 Aug 26, 2024
fe7d006
Included a variable to set log level
CGoodwin90 Aug 26, 2024
6f9c0e1
Changed log output to only log updated images
CGoodwin90 Aug 26, 2024
2ac9c4f
build: update golang to 1.22
tobybellwood Aug 26, 2024
cbf34f1
Merged main
CGoodwin90 Aug 30, 2024
9eb6117
Initial pass at adding pruneDanglingSchedule & updates go.mod
CGoodwin90 Aug 30, 2024
2ae5d75
Updated Dockerfile
CGoodwin90 Aug 30, 2024
98df40b
feat: add builder cache prune
shreddedbacon Oct 3, 2024
c482341
refactor: clean up any containers, not just exited
shreddedbacon Oct 4, 2024
a9df02b
chore: fix exited container check filter
shreddedbacon Oct 8, 2024
67efcaf
chore: dockerfile AS
shreddedbacon Oct 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions docker-host/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
FROM uselagoon/commons:latest as commons
FROM golang:1.18-alpine3.17 as builder

WORKDIR /build

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.24-dind

LABEL org.opencontainers.image.authors="The Lagoon Authors" maintainer="The Lagoon Authors"
Expand All @@ -11,6 +21,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 /build/docker-host /docker-host

ENV TMPDIR=/tmp \
TMP=/tmp \
HOME=/home \
Expand All @@ -23,17 +35,12 @@ RUN apk add --no-cache bash

ENV DOCKER_HOST=docker-host \
REGISTRY=docker-registry.default.svc:5000 \
REPOSITORY_TO_UPDATE=amazeeio \
REPOSITORIES_TO_UPDATE=*uselagoon/*:* \
BIP=172.16.0.1/16 \
REGISTRY_MIRROR=https://imagecache.amazeeio.cloud

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", "/docker-host"]
29 changes: 29 additions & 0 deletions docker-host/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module docker-host

go 1.19

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
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
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
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.8.0 // indirect
gotest.tools/v3 v3.4.0 // indirect
)
101 changes: 101 additions & 0 deletions docker-host/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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.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=
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/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/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.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/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=
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.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=
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=
164 changes: 164 additions & 0 deletions docker-host/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package main

import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"

"github.com/docker/docker/api/types"
"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 = 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_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 * * * *")
var pruneImagesUntil = machineryvars.GetEnv("PRUNE_IMAGES_UNTIL", "168h")
var danglingFilter = machineryvars.GetEnv("DANGLING_FILTER", "true")

func main() {
cli, err := client.NewClientWithOpts(
client.WithHostFromEnv(),
client.WithAPIVersionNegotiation(),
)
if err != nil {
log.Fatalf("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 --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() {
log.Fatalf("Could not connect to %s", dockerHost)
}
c := cron.New()
pruneImages(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)
}
}

func pruneImages(client *client.Client, c *cron.Cron) {
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)
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})
if buildErr != nil {
log.Println(buildErr)
}
// # after old images are pruned, clean up dangling images
_, pruneErr := client.ImagesPrune(context.Background(), pruneDanglingFilter)
if pruneErr != nil {
log.Println(pruneErr)
}
log.Println("Prune complete")
})
}

func removeExited(client *client.Client, c *cron.Cron) {
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{
Filters: statusFilter,
})
if err != nil {
log.Println(err)
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
return
}

// # remove all exited containers
for _, container := range containers {
err := client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{
Force: true,
RemoveVolumes: true,
})
if err != nil {
log.Println(err)
}
}
log.Println("removeExited complete")
})
}

func updateImages(client *client.Client, c *cron.Cron) {
c.AddFunc(updateImagesSchedule, func() {
log.Println("Starting update images")
ctx := context.Background()
filters := addFilters(repositoriesToUpdate)
images, err := client.ImageList(ctx, types.ImageListOptions{Filters: filters})
if err != nil {
log.Println(err)
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
return
}

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{})
log.Println("Image to update", image)
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
log.Println(err)
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
continue
}
defer out.Close()
_, error := io.Copy(os.Stdout, out)
CGoodwin90 marked this conversation as resolved.
Show resolved Hide resolved
if error != nil {
log.Println(err)
}
}
log.Println("Update images complete")
})
}

func addFilters(repo string) filters.Args {
filters := filters.NewArgs()
splitRepos := strings.Split(repo, "|")
for _, repo := range splitRepos {
filters.Add("reference", repo)
}
return filters
}
15 changes: 0 additions & 15 deletions docker-host/prune-images.sh

This file was deleted.

8 changes: 0 additions & 8 deletions docker-host/remove-exited.sh

This file was deleted.

12 changes: 0 additions & 12 deletions docker-host/update-images.sh

This file was deleted.

Loading