From 62d4871c4bb13145c5522a79ec4c439426c861ce Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Tue, 23 Oct 2018 10:01:08 -0500 Subject: [PATCH 1/6] Tag build container if running tests Previously, the second docker build would not actually use any images from the docker cache. If testing, build and tag the build container first, then build the final container. If not testing, only build once. Signed-off-by: Jose A. Rivera --- build.sh | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/build.sh b/build.sh index c7014b2e7..e29d5166c 100755 --- a/build.sh +++ b/build.sh @@ -11,6 +11,20 @@ RUN_TESTS=${RUN_TESTS:-1} VERSION="$(git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')" BUILDDATE="$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')" +# If running tests, create build container to extract profile data later +if [ "$RUN_TESTS" -ne 0 ]; then + rm -f profile.cov + $DOCKER_CMD build \ + -t glusterfs-csi-driver-build \ + --target build \ + --build-arg RUN_TESTS="$RUN_TESTS" \ + --build-arg version="$VERSION" \ + --build-arg builddate="$BUILDDATE" \ + -f pkg/glusterfs/Dockerfile \ + . \ + || exit 1 +fi + #-- Build final container $DOCKER_CMD build \ -t glusterfs-csi-driver \ @@ -21,15 +35,8 @@ $DOCKER_CMD build \ . \ || exit 1 +# If running tests, extract profile data if [ "$RUN_TESTS" -ne 0 ]; then - rm -f profile.cov - $DOCKER_CMD build \ - -t glusterfs-csi-driver-build \ - --target build \ - --build-arg RUN_TESTS="$RUN_TESTS" \ - -f pkg/glusterfs/Dockerfile \ - . \ - && \ $DOCKER_CMD run --rm glusterfs-csi-driver-build \ cat /profile.cov > profile.cov fi From f613e3863aa2b89cfa35b3ac62a66d41eb86453d Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Tue, 23 Oct 2018 10:03:16 -0500 Subject: [PATCH 2/6] Script tool installation, linting, and tests Move the linting and test commands out into of the Dockerfile into scripts, using some existing scripting in the repo. This makes the Dockerfile cleaner and allows the commands to be run outside the container build for development purposes. Signed-off-by: Jose A. Rivera --- .travis.yml | 2 +- build.sh | 10 ++ pkg/glusterfs/Dockerfile | 45 ++++----- scripts/install-go-tools.sh | 35 +++++++ scripts/lint-go.sh | 12 +++ scripts/{pre-commit.sh => lint-text.sh} | 0 test/100-gotest.sh => scripts/test-go.sh | 13 +-- test.sh | 115 ----------------------- test/001-testtest.sh | 8 -- test/020-test-makefiles.sh | 21 ----- 10 files changed, 84 insertions(+), 177 deletions(-) create mode 100755 scripts/install-go-tools.sh create mode 100755 scripts/lint-go.sh rename scripts/{pre-commit.sh => lint-text.sh} (100%) rename test/100-gotest.sh => scripts/test-go.sh (72%) delete mode 100755 test.sh delete mode 100755 test/001-testtest.sh delete mode 100755 test/020-test-makefiles.sh diff --git a/.travis.yml b/.travis.yml index 6257d2367..246760509 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ install: script: # Lint text-like files - - scripts/pre-commit.sh --require-all + - scripts/lint-text.sh --require-all # Build container w/ Docker - docker version - ./build.sh diff --git a/build.sh b/build.sh index e29d5166c..142989c87 100755 --- a/build.sh +++ b/build.sh @@ -11,6 +11,10 @@ RUN_TESTS=${RUN_TESTS:-1} VERSION="$(git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')" BUILDDATE="$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')" +GO_DEP_VERSION="${GO_DEP_VERSION}" +GO_METALINTER_VERSION="${GO_METALINTER_VERSION:-v2.0.11}" +GO_METALINTER_THREADS=${GO_METALINTER_THREADS:-4} + # If running tests, create build container to extract profile data later if [ "$RUN_TESTS" -ne 0 ]; then rm -f profile.cov @@ -18,6 +22,9 @@ if [ "$RUN_TESTS" -ne 0 ]; then -t glusterfs-csi-driver-build \ --target build \ --build-arg RUN_TESTS="$RUN_TESTS" \ + --build-arg GO_DEP_VERSION="$GO_DEP_VERSION" \ + --build-arg GO_METALINTER_VERSION="$GO_METALINTER_VERSION" \ + --build-arg GO_METALINTER_THREADS="$GO_METALINTER_THREADS" \ --build-arg version="$VERSION" \ --build-arg builddate="$BUILDDATE" \ -f pkg/glusterfs/Dockerfile \ @@ -29,6 +36,9 @@ fi $DOCKER_CMD build \ -t glusterfs-csi-driver \ --build-arg RUN_TESTS="$RUN_TESTS" \ + --build-arg GO_DEP_VERSION="$GO_DEP_VERSION" \ + --build-arg GO_METALINTER_VERSION="$GO_METALINTER_VERSION" \ + --build-arg GO_METALINTER_THREADS="$GO_METALINTER_THREADS" \ --build-arg version="$VERSION" \ --build-arg builddate="$BUILDDATE" \ -f pkg/glusterfs/Dockerfile \ diff --git a/pkg/glusterfs/Dockerfile b/pkg/glusterfs/Dockerfile index 34fb031da..eea32dcfa 100644 --- a/pkg/glusterfs/Dockerfile +++ b/pkg/glusterfs/Dockerfile @@ -15,47 +15,40 @@ FROM openshift/origin-release:golang-1.10 AS build ENV GOPATH="/go/" \ - SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/" + SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/" \ + SCRIPTSDIR="${SRCDIR}scripts/" RUN yum install -y \ git -# Install dep -RUN mkdir -p /go/bin -RUN curl -L https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - -# Install gometalinter -ARG GO_METALINTER_VERSION=v2.0.11 -RUN curl -L 'https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh' \ - | bash -s -- -b "${GOPATH}bin" "${GO_METALINTER_VERSION}" +# Install go tools +ARG GO_DEP_VERSION= +ARG GO_METALINTER_VERSION=latest +COPY scripts/install-go-tools.sh "${SCRIPTSDIR}" +RUN mkdir -p /go/bin; ${SCRIPTSDIR}/install-go-tools.sh # Vendor dependencies COPY Gopkg.lock Gopkg.toml "${SRCDIR}" WORKDIR "${SRCDIR}" RUN /go/bin/dep ensure -v -vendor-only -# Build executable +# Copy source directory COPY . "${SRCDIR}" -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /glusterfs-csi-driver cmd/glusterfs/main.go - -# Ensure the binary is statically linked -RUN ldd /glusterfs-csi-driver | grep -q "not a dynamic executable" # Run tests ARG RUN_TESTS=1 -RUN [ $RUN_TESTS -eq 0 ] || { \ - set -o pipefail \ - && gometalinter -j4 --sort=path --sort=line --sort=column \ - --enable="gofmt" \ - --exclude="method NodeGetId should be NodeGetID" \ - --deadline 9m --vendor --debug ./... \ - |& stdbuf -oL awk '/linter took/ || !/^DEBUG/'; \ - } -RUN [ $RUN_TESTS -eq 0 ] || { \ - GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)"; \ - go test -covermode=count -coverprofile=/profile.cov $GOPACKAGES; \ - } +ARG GO_METALINTER_THREADS=1 +ENV TEST_COVERAGE=stdout \ + GO_COVER_OUT=/profile.cov +RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/lint-go.sh +RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/test-go.sh + +# Build executable +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /glusterfs-csi-driver cmd/glusterfs/main.go + +# Ensure the binary is statically linked +RUN ldd /glusterfs-csi-driver | grep -q "not a dynamic executable" #-- Final container FROM centos:7.5.1804 diff --git a/scripts/install-go-tools.sh b/scripts/install-go-tools.sh new file mode 100755 index 000000000..669626c20 --- /dev/null +++ b/scripts/install-go-tools.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +GOPATH=$(go env GOPATH) +GOBINDIR="${GOPATH}/bin" + +install_dep() { + DEPVER="${GO_DEP_VERSION}" + if type dep >/dev/null 2>&1; then + local version + version=$(dep version | awk '/^ version/{print $3}') + if [[ "${version}" == "${DEPVER}" || ${version} > ${DEPVER} ]]; then + echo "dep ${DEPVER} or greater is already installed" + return + fi + fi + + echo "Installing dep. Version: ${DEPVER:-latest}" + export INSTALL_DIRECTORY="${GOBINDIR}" + export DEP_RELEASE_TAG="${DEPVER}" + curl -L https://raw.githubusercontent.com/golang/dep/master/install.sh | sh +} + +install_gometalinter() { + GMLVER="${GO_METALINTER_VERSION}" + if type gometalinter >/dev/null 2>&1; then + echo "gometalinter already installed" + return + fi + + echo "Installing gometalinter. Version: ${GMLVER}" + curl -L https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh | bash -s -- -b "${GOBINDIR}" "${GMLVER}" +} + +install_dep +install_gometalinter diff --git a/scripts/lint-go.sh b/scripts/lint-go.sh new file mode 100755 index 000000000..a20dbccb7 --- /dev/null +++ b/scripts/lint-go.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -o pipefail + +if [[ -x "$(command -v gometalinter)" ]]; then + # shellcheck disable=SC2086,SC2068 + gometalinter -j ${GO_METALINTER_THREADS:-1} --sort path --sort line --sort column --deadline=9m \ + --enable="gofmt" --exclude "method NodeGetId should be NodeGetID" \ + --vendor --debug ${@-./...} |& stdbuf -oL grep "linter took\\|:warning:\\|:error:" +else + echo "WARNING: gometalinter not found, skipping lint tests" >&2 +fi diff --git a/scripts/pre-commit.sh b/scripts/lint-text.sh similarity index 100% rename from scripts/pre-commit.sh rename to scripts/lint-text.sh diff --git a/test/100-gotest.sh b/scripts/test-go.sh similarity index 72% rename from test/100-gotest.sh rename to scripts/test-go.sh index aa28380aa..d1fc954fe 100755 --- a/test/100-gotest.sh +++ b/scripts/test-go.sh @@ -1,17 +1,18 @@ #!/bin/bash GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)" +COVERFILE="${GO_COVER_OUT:-profile.cov}" + # no special options, exec to go test w/ all pkgs -if [[ ${CSI_TEST_EXITFIRST} != "yes" && -z ${CSI_TEST_COVERAGE} ]]; then +if [[ ${TEST_EXITFIRST} != "yes" && -z ${TEST_COVERAGE} ]]; then # shellcheck disable=SC2086 exec go test ${GOPACKAGES} fi # our options are set so we need to handle each go package one # at at time -if [[ ${CSI_TEST_COVERAGE} ]]; then +if [[ ${TEST_COVERAGE} ]]; then GOTESTOPTS="-covermode=count -coverprofile=cover.out" - COVERFILE=packagecover.out echo "mode: count" > "${COVERFILE}" fi @@ -24,17 +25,17 @@ for gopackage in ${GOPACKAGES}; do # Append to coverfile grep -v "^mode: count" cover.out >> "${COVERFILE}" fi - if [[ ${CSI_TEST_COVERAGE} = "stdout" && -f cover.out ]]; then + if [[ ${TEST_COVERAGE} = "stdout" && -f cover.out ]]; then go tool cover -func=cover.out fi - if [[ ${CSI_TEST_COVERAGE} = "html" && -f cover.out ]]; then + if [[ ${TEST_COVERAGE} = "html" && -f cover.out ]]; then mkdir -p coverage fn="coverage/${gopackage////-}.html" echo " * generating coverage html: ${fn}" go tool cover -html=cover.out -o "${fn}" fi rm -f cover.out - if [[ ${failed} -ne 0 && ${CSI_TEST_EXITFIRST} = "yes" ]]; then + if [[ ${failed} -ne 0 && ${TEST_EXITFIRST} = "yes" ]]; then exit ${failed} fi done diff --git a/test.sh b/test.sh deleted file mode 100755 index 212e2e93f..000000000 --- a/test.sh +++ /dev/null @@ -1,115 +0,0 @@ -#!/bin/bash - -# main test runner -# Executes all executable scripts under the test dir -# in sorted order. - -FAILURES=() - -vecho () { - if [[ "${verbose}" = "yes" ]] ; then - echo "$*" - fi -} - -run_test() { - cmd="${1}" - vecho "-- Running: ${tname} --" - "${cmd}" - sts=$? - if [[ ${sts} -ne 0 ]]; then - vecho "failed ${cmd} [${sts}]" - FAILURES+=("${cmd}") - if [[ "${exitfirst}" = "yes" ]]; then - exit 1 - fi - fi -} - -summary() { - if [[ ${#FAILURES[@]} -gt 0 ]]; then - echo "ERROR: failing tests:" - for i in "${!FAILURES[@]}"; do - echo " ${FAILURES[i]}" - done - exit 1 - else - echo "all tests passed" - exit 0 - fi -} - -show_help() { - echo "$0 [options]" - echo " Options:" - echo " -c|--coverage TYPE Run tests with given coverage type" - echo " -v|--verbose Print verbose output" - echo " -x|--exitfirst Exit on first test failure" - echo " -h|--help Display help" - echo "" - echo " Coverage Types:" - echo " html - Generate html files (one per package) in the" - echo " coverage directory." - echo " stdout - Print coverage information to the console." - echo " summary - Generate ONLY a packagecover.out to record" - echo " coverage stats for all tests run.*" - echo " * All modes generate the package cover information," - echo " summary mode disables all additional output." -} - -CLI="$(getopt -o c:xvh --long coverage:,exitfirst,verbose,help -n "$0" -- "$@")" -eval set -- "${CLI}" -while true ; do - case "$1" in - -c|--coverage) - coverage="$2" - case ${coverage} in - stdout|html|summary);; - *) - echo "error: invalid coverage type ${coverage}." - echo " need one of: stdout, html, summary" - exit 2 - ;; - esac - shift - shift - ;; - -x|--exitfirst) - exitfirst=yes - shift - ;; - -v|--verbose) - verbose=yes - shift - ;; - -h|--help) - show_help - exit 0 - ;; - --) - shift - break - ;; - *) - echo "unknown option" >&2 - exit 2 - ;; - esac -done - -trap summary EXIT - -SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)" - -# environment vars exported for test scripts -# (this way test scripts dont need cli parsing, we do it here) -export CSI_TEST_EXITFIRST=${exitfirst} -export CSI_TEST_SCRIPT_DIR="${SCRIPT_DIR}" -export CSI_TEST_COVERAGE=${coverage} - -cd "${SCRIPT_DIR}" || exit 1 -for tname in $(find ./test | sort) ; do - if [[ ${tname} =~ .*\.sh$ && -f ${tname} && -x ${tname} ]]; then - run_test "${tname}" - fi -done diff --git a/test/001-testtest.sh b/test/001-testtest.sh deleted file mode 100755 index 30e43c902..000000000 --- a/test/001-testtest.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# This "test" exists merely to exercise the test "framework" - -if [ "$CSI_TEST_TEST" ]; then - exit "$CSI_TEST_TEST" -fi -exit 0 diff --git a/test/020-test-makefiles.sh b/test/020-test-makefiles.sh deleted file mode 100755 index 2a0d7f457..000000000 --- a/test/020-test-makefiles.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -# Check for Makefile syntax/style. -# Currently only checks for consistent -# use of tabs (instead of spaces) for indentation. - -SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)" - -BASE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" - -FOUND=$(find "${BASE_DIR}" -name Makefile | \ - grep -v ./vendor | \ - xargs grep -n -E "^[[:space:]]* [[:space:]]*[^[:space:]]") - -if [[ -n "${FOUND}" ]]; then - echo "Found spaces in Makefiles:" - echo "${FOUND}" - exit 1 -fi - -exit 0 From 0a6d7fdbd26bd3426f8662c3a70ac40708f3dd81 Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Wed, 24 Oct 2018 13:12:19 -0500 Subject: [PATCH 3/6] Display Docker version on all builds --- .travis.yml | 1 - build.sh | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 246760509..a45eb9560 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,5 +18,4 @@ script: # Lint text-like files - scripts/lint-text.sh --require-all # Build container w/ Docker - - docker version - ./build.sh diff --git a/build.sh b/build.sh index 142989c87..74e4115a2 100755 --- a/build.sh +++ b/build.sh @@ -15,6 +15,10 @@ GO_DEP_VERSION="${GO_DEP_VERSION}" GO_METALINTER_VERSION="${GO_METALINTER_VERSION:-v2.0.11}" GO_METALINTER_THREADS=${GO_METALINTER_THREADS:-4} +# Print Docker version +echo "=== Docker Version ===" +$DOCKER_CMD version + # If running tests, create build container to extract profile data later if [ "$RUN_TESTS" -ne 0 ]; then rm -f profile.cov From 7c95d9eee4bcbd90b1090b4cd01dea982d58b088 Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Thu, 25 Oct 2018 12:39:39 -0500 Subject: [PATCH 4/6] Tweak build logic / labeling Signed-off-by: Jose A. Rivera --- build.sh | 45 ++++++++++++++++++++-------------------- pkg/glusterfs/Dockerfile | 19 ++++++++++++----- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/build.sh b/build.sh index 74e4115a2..7e131d137 100755 --- a/build.sh +++ b/build.sh @@ -15,42 +15,41 @@ GO_DEP_VERSION="${GO_DEP_VERSION}" GO_METALINTER_VERSION="${GO_METALINTER_VERSION:-v2.0.11}" GO_METALINTER_THREADS=${GO_METALINTER_THREADS:-4} +build_args=() +build_args+=( --build-arg "RUN_TESTS=$RUN_TESTS" ) +build_args+=( --build-arg "GO_DEP_VERSION=$GO_DEP_VERSION" ) +build_args+=( --build-arg "GO_METALINTER_VERSION=$GO_METALINTER_VERSION" ) +build_args+=( --build-arg "GO_METALINTER_THREADS=$GO_METALINTER_THREADS" ) +build_args+=( --build-arg "version=$VERSION" ) +build_args+=( --build-arg "builddate=$BUILDDATE" ) + # Print Docker version echo "=== Docker Version ===" $DOCKER_CMD version -# If running tests, create build container to extract profile data later -if [ "$RUN_TESTS" -ne 0 ]; then - rm -f profile.cov - $DOCKER_CMD build \ - -t glusterfs-csi-driver-build \ - --target build \ - --build-arg RUN_TESTS="$RUN_TESTS" \ - --build-arg GO_DEP_VERSION="$GO_DEP_VERSION" \ - --build-arg GO_METALINTER_VERSION="$GO_METALINTER_VERSION" \ - --build-arg GO_METALINTER_THREADS="$GO_METALINTER_THREADS" \ - --build-arg version="$VERSION" \ - --build-arg builddate="$BUILDDATE" \ - -f pkg/glusterfs/Dockerfile \ - . \ - || exit 1 -fi +# Run container build +$DOCKER_CMD build \ + -t glusterfs-csi-driver-build \ + --target build \ + "${build_args[@]}" \ + -f pkg/glusterfs/Dockerfile \ + . \ +|| exit 1 #-- Build final container $DOCKER_CMD build \ -t glusterfs-csi-driver \ - --build-arg RUN_TESTS="$RUN_TESTS" \ - --build-arg GO_DEP_VERSION="$GO_DEP_VERSION" \ - --build-arg GO_METALINTER_VERSION="$GO_METALINTER_VERSION" \ - --build-arg GO_METALINTER_THREADS="$GO_METALINTER_THREADS" \ - --build-arg version="$VERSION" \ - --build-arg builddate="$BUILDDATE" \ + --target driver \ + "${build_args[@]}" \ -f pkg/glusterfs/Dockerfile \ . \ || exit 1 # If running tests, extract profile data if [ "$RUN_TESTS" -ne 0 ]; then - $DOCKER_CMD run --rm glusterfs-csi-driver-build \ + rm -f profile.cov + $DOCKER_CMD run glusterfs-csi-driver-build \ cat /profile.cov > profile.cov fi + +$DOCKER_CMD rmi glusterfs-csi-driver-build diff --git a/pkg/glusterfs/Dockerfile b/pkg/glusterfs/Dockerfile index eea32dcfa..8f1454a58 100644 --- a/pkg/glusterfs/Dockerfile +++ b/pkg/glusterfs/Dockerfile @@ -11,8 +11,9 @@ # limitations under the License. -#-- Build phase -FROM openshift/origin-release:golang-1.10 AS build +#-- Create build environment + +FROM openshift/origin-release:golang-1.10 AS make_env ENV GOPATH="/go/" \ SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/" \ @@ -32,10 +33,14 @@ COPY Gopkg.lock Gopkg.toml "${SRCDIR}" WORKDIR "${SRCDIR}" RUN /go/bin/dep ensure -v -vendor-only -# Copy source directory -COPY . "${SRCDIR}" +# Copy source directories +COPY cmd/ "${SRCDIR}/cmd" +COPY pkg/ "${SRCDIR}/pkg" +COPY scripts/ "${SCRIPTSDIR}" + +#-- Test phase -# Run tests +FROM make_env AS test ARG RUN_TESTS=1 ARG GO_METALINTER_THREADS=1 ENV TEST_COVERAGE=stdout \ @@ -44,6 +49,10 @@ ENV TEST_COVERAGE=stdout \ RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/lint-go.sh RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/test-go.sh +#-- Build phase + +FROM test AS build + # Build executable RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /glusterfs-csi-driver cmd/glusterfs/main.go From fde8c089a2e7f1be19fa2ade1285c3bc231f8f0b Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Thu, 25 Oct 2018 12:40:08 -0500 Subject: [PATCH 5/6] Add more build parameters Signed-off-by: Jose A. Rivera --- build.sh | 19 +++++++++++++++++-- pkg/glusterfs/Dockerfile | 5 ++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index 7e131d137..e0f9e1dbc 100755 --- a/build.sh +++ b/build.sh @@ -2,12 +2,24 @@ set -e +# Set driver name +DRIVER="${DRIVER:-glusterfs-csi-driver}" + +# Set which docker repo to tag +REPO="${REPO:-gluster/}" + +# Base image to use for final container images +FINAL_BASE="${FINAL_BASE:-centos:7.5.1804}" + # Allow overriding default docker command DOCKER_CMD=${DOCKER_CMD:-docker} # Allow disabling tests during build RUN_TESTS=${RUN_TESTS:-1} +# Cleanup build context when done +RM_BUILD=${RM_BUILD:-1} + VERSION="$(git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')" BUILDDATE="$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')" @@ -20,6 +32,7 @@ build_args+=( --build-arg "RUN_TESTS=$RUN_TESTS" ) build_args+=( --build-arg "GO_DEP_VERSION=$GO_DEP_VERSION" ) build_args+=( --build-arg "GO_METALINTER_VERSION=$GO_METALINTER_VERSION" ) build_args+=( --build-arg "GO_METALINTER_THREADS=$GO_METALINTER_THREADS" ) +build_args+=( --build-arg "FINAL_BASE=$FINAL_BASE" ) build_args+=( --build-arg "version=$VERSION" ) build_args+=( --build-arg "builddate=$BUILDDATE" ) @@ -38,7 +51,7 @@ $DOCKER_CMD build \ #-- Build final container $DOCKER_CMD build \ - -t glusterfs-csi-driver \ + -t "${REPO}${DRIVER}" \ --target driver \ "${build_args[@]}" \ -f pkg/glusterfs/Dockerfile \ @@ -52,4 +65,6 @@ if [ "$RUN_TESTS" -ne 0 ]; then cat /profile.cov > profile.cov fi -$DOCKER_CMD rmi glusterfs-csi-driver-build +if [ "$RM_BUILD" -ne 0 ]; then + $DOCKER_CMD rmi -f glusterfs-csi-driver-build +fi diff --git a/pkg/glusterfs/Dockerfile b/pkg/glusterfs/Dockerfile index 8f1454a58..d4155a3dc 100644 --- a/pkg/glusterfs/Dockerfile +++ b/pkg/glusterfs/Dockerfile @@ -10,6 +10,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +# The base image to use for the final images +ARG FINAL_BASE=centos #-- Create build environment @@ -60,7 +62,8 @@ RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /glust RUN ldd /glusterfs-csi-driver | grep -q "not a dynamic executable" #-- Final container -FROM centos:7.5.1804 + +FROM ${FINAL_BASE} AS driver # Install dependencies RUN yum -y install centos-release-gluster && \ From 34c7af9da7558cf54a61065a7887cc01d6dabf7c Mon Sep 17 00:00:00 2001 From: "Jose A. Rivera" Date: Tue, 30 Oct 2018 11:50:43 -0500 Subject: [PATCH 6/6] Fix for buildah Signed-off-by: Jose A. Rivera --- build.sh | 40 ++++++++++++---------------------------- pkg/glusterfs/Dockerfile | 19 +++++++------------ scripts/test-go.sh | 2 +- 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/build.sh b/build.sh index e0f9e1dbc..adf93ec93 100755 --- a/build.sh +++ b/build.sh @@ -8,18 +8,17 @@ DRIVER="${DRIVER:-glusterfs-csi-driver}" # Set which docker repo to tag REPO="${REPO:-gluster/}" -# Base image to use for final container images -FINAL_BASE="${FINAL_BASE:-centos:7.5.1804}" - # Allow overriding default docker command -DOCKER_CMD=${DOCKER_CMD:-docker} +RUNTIME_CMD=${RUNTIME_CMD:-docker} + +build="build" +if [[ "${RUNTIME_CMD}" == "buildah" ]]; then + build="bud" +fi # Allow disabling tests during build RUN_TESTS=${RUN_TESTS:-1} -# Cleanup build context when done -RM_BUILD=${RM_BUILD:-1} - VERSION="$(git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')" BUILDDATE="$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')" @@ -32,27 +31,16 @@ build_args+=( --build-arg "RUN_TESTS=$RUN_TESTS" ) build_args+=( --build-arg "GO_DEP_VERSION=$GO_DEP_VERSION" ) build_args+=( --build-arg "GO_METALINTER_VERSION=$GO_METALINTER_VERSION" ) build_args+=( --build-arg "GO_METALINTER_THREADS=$GO_METALINTER_THREADS" ) -build_args+=( --build-arg "FINAL_BASE=$FINAL_BASE" ) build_args+=( --build-arg "version=$VERSION" ) build_args+=( --build-arg "builddate=$BUILDDATE" ) # Print Docker version -echo "=== Docker Version ===" -$DOCKER_CMD version +echo "=== $RUNTIME_CMD version ===" +$RUNTIME_CMD version -# Run container build -$DOCKER_CMD build \ - -t glusterfs-csi-driver-build \ - --target build \ - "${build_args[@]}" \ - -f pkg/glusterfs/Dockerfile \ - . \ -|| exit 1 - -#-- Build final container -$DOCKER_CMD build \ +#-- Build container +$RUNTIME_CMD $build \ -t "${REPO}${DRIVER}" \ - --target driver \ "${build_args[@]}" \ -f pkg/glusterfs/Dockerfile \ . \ @@ -61,10 +49,6 @@ $DOCKER_CMD build \ # If running tests, extract profile data if [ "$RUN_TESTS" -ne 0 ]; then rm -f profile.cov - $DOCKER_CMD run glusterfs-csi-driver-build \ - cat /profile.cov > profile.cov -fi - -if [ "$RM_BUILD" -ne 0 ]; then - $DOCKER_CMD rmi -f glusterfs-csi-driver-build + $RUNTIME_CMD run --entrypoint cat "${REPO}${DRIVER}" \ + /profile.cov > profile.cov fi diff --git a/pkg/glusterfs/Dockerfile b/pkg/glusterfs/Dockerfile index d4155a3dc..0016290f3 100644 --- a/pkg/glusterfs/Dockerfile +++ b/pkg/glusterfs/Dockerfile @@ -10,12 +10,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -# The base image to use for the final images -ARG FINAL_BASE=centos - #-- Create build environment -FROM openshift/origin-release:golang-1.10 AS make_env +FROM docker.io/openshift/origin-release:golang-1.10 as build ENV GOPATH="/go/" \ SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/" \ @@ -42,28 +39,26 @@ COPY scripts/ "${SCRIPTSDIR}" #-- Test phase -FROM make_env AS test ARG RUN_TESTS=1 ARG GO_METALINTER_THREADS=1 ENV TEST_COVERAGE=stdout \ - GO_COVER_OUT=/profile.cov + GO_COVER_DIR=/build/ +RUN mkdir /build RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/lint-go.sh RUN [ $RUN_TESTS -eq 0 ] || ${SCRIPTSDIR}/test-go.sh #-- Build phase -FROM test AS build - # Build executable -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /glusterfs-csi-driver cmd/glusterfs/main.go +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-extldflags "-static"' -o /build/glusterfs-csi-driver cmd/glusterfs/main.go # Ensure the binary is statically linked -RUN ldd /glusterfs-csi-driver | grep -q "not a dynamic executable" +RUN ldd /build/glusterfs-csi-driver | grep -q "not a dynamic executable" #-- Final container -FROM ${FINAL_BASE} AS driver +FROM docker.io/centos:7.5.1804 as final # Install dependencies RUN yum -y install centos-release-gluster && \ @@ -73,7 +68,7 @@ RUN yum -y install centos-release-gluster && \ rpm -qa | grep gluster | tee /gluster-rpm-versions.txt # Copy glusterfs-csi-driver from build phase -COPY --from=build /glusterfs-csi-driver /glusterfs-csi-driver +COPY --from=build /build / # The version of the driver (git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2') ARG version="(unknown)" diff --git a/scripts/test-go.sh b/scripts/test-go.sh index d1fc954fe..de3ecd2fb 100755 --- a/scripts/test-go.sh +++ b/scripts/test-go.sh @@ -1,7 +1,7 @@ #!/bin/bash GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)" -COVERFILE="${GO_COVER_OUT:-profile.cov}" +COVERFILE="${GO_COVER_DIR}profile.cov" # no special options, exec to go test w/ all pkgs if [[ ${TEST_EXITFIRST} != "yes" && -z ${TEST_COVERAGE} ]]; then