From ee90549179faa34981b385c3679a85cfb571e660 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 4 Mar 2018 13:41:25 +0100 Subject: [PATCH] Rebuild to use crond and pre-compiled binary The build process of the binary patched with the cron runner was too instable as coredns is not properly vendoring their dependencies. This way the binary is stable (provided by coredns) and the cron is taken over by alpine crond. Signed-off-by: Knut Ahlers --- Dockerfile | 25 ++++++------------------ build.sh | 45 ++++++++++++++++++++++++-------------------- cron_generate.go | 44 ------------------------------------------- docker-entrypoint.sh | 12 ++++++++++++ zonefile_cron | 5 +++++ 5 files changed, 48 insertions(+), 83 deletions(-) mode change 100644 => 100755 build.sh delete mode 100644 cron_generate.go create mode 100755 docker-entrypoint.sh create mode 100755 zonefile_cron diff --git a/Dockerfile b/Dockerfile index 807fd0d..b20fefc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,14 @@ -FROM golang:alpine - -ARG COREDNS_VERSION=v1.0.5 - -ADD ./build.sh /usr/local/bin/build.sh -ADD ./cron_generate.go /src/cron_generate.go -RUN set -ex \ - && apk --no-cache add git bash \ - && bash /usr/local/bin/build.sh - FROM alpine LABEL maintainer Knut Ahlers -COPY --from=0 /go/bin/coredns /usr/local/bin/ - -ADD ./requirements.txt /src/requirements.txt -RUN set -ex \ - && apk --no-cache add python3 bind-tools \ - && pip3 install -r /src/requirements.txt - ADD . /src WORKDIR /src +RUN set -ex \ + && apk --no-cache add bash \ + && /src/build.sh + EXPOSE 53/udp 53 VOLUME ["/src/zones"] @@ -29,5 +16,5 @@ VOLUME ["/src/zones"] HEALTHCHECK --interval=30s --timeout=5s \ CMD dig +short @localhost health.server.test TXT || exit 1 -ENTRYPOINT ["/usr/local/bin/coredns"] -CMD ["--"] +ENTRYPOINT ["/src/docker-entrypoint.sh"] +CMD ["coredns"] diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 index ab8a187..8b801eb --- a/build.sh +++ b/build.sh @@ -1,28 +1,33 @@ #!/bin/bash set -euxo pipefail -# Download sourcecode -mkdir -p /go/src/github.com/coredns -git clone https://github.com/coredns/coredns.git /go/src/github.com/coredns/coredns +# Install build utilities +apk --no-cache add curl -# Ensure version pinning -cd /go/src/github.com/coredns/coredns -git reset --hard ${COREDNS_VERSION} +# Install dependencies +apk --no-cache add python3 bind-tools -# Copy cron drop-in -cp /src/cron_generate.go . +# Get latest versions of tools using latestver +COREDNS_VERSION=$(curl -sSfL 'https://lv.luzifer.io/catalog-api/coredns/latest.txt?p=version') +DUMB_INIT_VERSION=$(curl -sSfL 'https://lv.luzifer.io/catalog-api/dumb-init/latest.txt?p=version') -# Get dependencies and build -go get -d -v +[ -z "${COREDNS_VERSION}" ] && { exit 1; } +[ -z "${DUMB_INIT_VERSION}" ] && { exit 1; } -# Force downgrades not being pinned -CWD=$(pwd) -cd ${GOPATH}/src/github.com/mholt/caddy && git checkout -q v0.10.10 -cd ${GOPATH}/src/github.com/miekg/dns && git checkout -q v1.0.4 -cd ${GOPATH}/src/github.com/prometheus/client_golang && git checkout -q v0.8.0 -cd ${GOPATH}/src/golang.org/x/net && git checkout -q release-branch.go1.9 -cd ${GOPATH}/src/golang.org/x/text && git checkout -q e19ae1496984b1c655b8044a65c0300a3c878dd3 -cd "${CWD}" +# Install tools +curl -sSfL https://github.com/coredns/coredns/releases/download/v${COREDNS_VERSION}/coredns_${COREDNS_VERSION}_linux_amd64.tgz | \ + tar -x -z -C /usr/local/bin -# Do the compile -go install +curl -sSfLo /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64 +chmod +x /usr/local/bin/dumb-init + +# Install requirements for python3 scripts +pip3 install -r /src/requirements.txt + +# Create cron to update zones periodically +echo "* * * * * run-parts /etc/periodic/1min" >> /var/spool/cron/crontabs/root +mkdir -p /etc/periodic/1min +ln -s /src/zonefile_cron /etc/periodic/1min/zonefile_cron + +# Cleanup +apk --no-cache del curl diff --git a/cron_generate.go b/cron_generate.go deleted file mode 100644 index 996ddfe..0000000 --- a/cron_generate.go +++ /dev/null @@ -1,44 +0,0 @@ -package main - -import ( - "context" - "os/exec" - "time" - - "github.com/Sirupsen/logrus" - "github.com/robfig/cron" -) - -func init() { - c := cron.New() - c.AddFunc("0 * * * * *", generateZonefiles) - c.Start() - - go generateZonefiles() -} - -func generateZonefiles() { - logger := logrus.WithFields(logrus.Fields{ - "fkt": "generateZonefiles", - }) - - var ( - iw = logger.WriterLevel(logrus.InfoLevel) - ew = logger.WriterLevel(logrus.ErrorLevel) - ) - - defer iw.Close() - defer ew.Close() - - ctx, cancel := context.WithTimeout(context.Background(), 59*time.Second) - defer cancel() - - cmd := exec.CommandContext(ctx, "/usr/bin/python3", "generateZonefiles.py") - cmd.Stdout = iw - cmd.Stderr = ew - cmd.Dir = "/src" - - if err := cmd.Run(); err != nil { - logger.WithError(err).Error("Command execution failed") - } -} diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100755 index 0000000..8bc0e5b --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,12 @@ +#!/usr/local/bin/dumb-init /bin/bash +set -euxo pipefail + +if [ "${1:-}" = 'coredns' ]; then + # Start crond in the background + crond + + # Start coredns + exec "$@" +fi + +exec "$@" diff --git a/zonefile_cron b/zonefile_cron new file mode 100755 index 0000000..3db8609 --- /dev/null +++ b/zonefile_cron @@ -0,0 +1,5 @@ +#!/bin/bash +set -euxo pipefail + +cd /src +exec /usr/bin/python3 generateZonefiles.py