Skip to content

Commit

Permalink
Merge pull request #65 from JohnStrunk/36-multi-stage-docker
Browse files Browse the repository at this point in the history
Fully containerized build
  • Loading branch information
humblec authored Oct 23, 2018
2 parents 8d6a608 + b6a5909 commit e5df356
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 174 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
vendor
profile.cov
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build is our output directory for go build
build
# go coverage data
profile.cov

# Remove any tarfiles in the repo
*.gz
Expand Down
37 changes: 1 addition & 36 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,14 @@ addons:
- realpath
- ruby

language: go

env:
global:
- GO_METALINTER_VERSION="v2.0.11"
- GLUSTERFS_CSI_DOCKERFILE="pkg/glusterfs/Dockerfile"

go:
# Kubernetes minimum_go_version can be found in this file:
# https://github.com/kubernetes/kubernetes/blob/master/hack/lib/golang.sh
- stable
- "1.10.x" # used by OCP releases
- "1.11.x" # used by kubernetes master

install:
- gem install asciidoctor mdl
- pip install --user --upgrade pip
- pip install --user yamllint
# install dep
- >
curl -L
https://raw.githubusercontent.com/golang/dep/master/install.sh
| sh
# install gometalinter
- >
curl -L
'https://raw.githubusercontent.com/alecthomas/gometalinter/master/scripts/install.sh'
| bash -s -- -b ${TRAVIS_HOME}/gopath/bin "${GO_METALINTER_VERSION}"

script:
# Lint text-like files
- scripts/pre-commit.sh --require-all
- make vendor-install
# Lint golang sources
- >
set -o pipefail;
gometalinter -j1 --sort=path --sort=line --sort=column
--exclude="method NodeGetId should be NodeGetID"
--deadline 9m --vendor --debug ./...
|& stdbuf -oL awk '/linter took/ || !/^DEBUG/'
# Build the code & run tests
- make
- make test
# Build container w/ Docker
- docker version
- docker build -t gluster-csi-driver -f "${GLUSTERFS_CSI_DOCKERFILE}" .
- ./build.sh
34 changes: 0 additions & 34 deletions Makefile

This file was deleted.

23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ systems.

## Building GlusterFS CSI driver

This repository consists of Dockerfile for GlusterFS CSI dirver to build on
CentOS distribution. Once you clone the repository, to build the image, run the
following command:
This repository contains the source and a Dockerfile to build the GlusterFS CSI
driver. The driver is built as a multi-stage container build. This requires a
relatively recent version of Docker or Buildah.

1. Get inside the repository directory
Docker packages can be obtained for
[CentOS](https://docs.docker.com/install/linux/docker-ce/centos/),
[Fedora](https://docs.docker.com/install/linux/docker-ce/fedora/) or other
distributions.

```
[root@localhost]#cd gluster-csi-driver
```
To build, ensure docker is installed, and run:

1. Compile and create a binary
1. Get inside the repository directory

```
[root@localhost]#make
[root@localhost]# cd gluster-csi-driver
```

3) Build a docker image based on the binary compiled above
1. Build the glusterfs-csi-driver container

```
[root@localhost]#docker build -t glusterfs-csi-driver -f pkg/glusterfs/Dockerfile .
[root@localhost]# ./build.sh
```

## Testing GlusterFS CSI driver
Expand Down
35 changes: 35 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /bin/bash

set -e

# Allow overriding default docker command
DOCKER_CMD=${DOCKER_CMD:-docker}

# Allow disabling tests during build
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')"

#-- Build final container
$DOCKER_CMD build \
-t glusterfs-csi-driver \
--build-arg RUN_TESTS="$RUN_TESTS" \
--build-arg version="$VERSION" \
--build-arg builddate="$BUILDDATE" \
-f pkg/glusterfs/Dockerfile \
. \
|| exit 1

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
37 changes: 9 additions & 28 deletions extras/centos-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,12 @@
# if anything fails, we'll abort
set -e

REQ_GO_VERSION='1.10.2'
# install Go
if ! yum -y install "golang >= $REQ_GO_VERSION"
then
# not the right version, install manually
# download URL comes from https://golang.org/dl/
curl -O https://storage.googleapis.com/golang/go${REQ_GO_VERSION}.linux-amd64.tar.gz
tar xzf go${REQ_GO_VERSION}.linux-amd64.tar.gz -C /usr/local
export PATH=$PATH:/usr/local/go/bin
fi

# also needs git, hg, bzr, svn gcc and make
yum -y install git mercurial bzr subversion gcc make

export CSISRC=$GOPATH/src/github.com/gluster/gluster-csi-driver
cd "$CSISRC"

# install the build and test requirements
./scripts/install-reqs.sh

# install vendored dependencies
make vendor-install

# verify build
make glusterfs-csi-driver

# run tests
make test TESTOPTIONS=-v
# Install buildah & Docker
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum -y install buildah docker-ce
systemctl start docker

# Build containers
./build.sh
81 changes: 73 additions & 8 deletions pkg/glusterfs/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
# Copyright 2018 The Gluster CSI Authors.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0
# Licensed under GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
# You may obtain a copy of the License at
# https://opensource.org/licenses/lgpl-3.0.html

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


#-- Build phase
FROM openshift/origin-release:golang-1.10 AS build

ENV GOPATH="/go/" \
SRCDIR="/go/src/github.com/gluster/gluster-csi-driver/"

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}"

# Vendor dependencies
COPY Gopkg.lock Gopkg.toml "${SRCDIR}"
WORKDIR "${SRCDIR}"
RUN /go/bin/dep ensure -v -vendor-only

# Build executable
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; \
}


#-- Final container
FROM centos:7.5.1804

# Copy glusterfs-csi-driver from build directory
COPY ./build/glusterfs-csi-driver /glusterfs-csi-driver
# Install dependencies
RUN yum -y install centos-release-gluster && \
yum -y install glusterfs-fuse && \
yum clean all -y && \
rm -rf /var/cache/yum && \
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

# The version of the driver (git describe --dirty --always --tags | sed 's/-/./2' | sed 's/-/./2')
ARG version="(unknown)"
# Container build time (date -u '+%Y-%m-%dT%H:%M:%S.%NZ')
ARG builddate="(unknown)"

RUN yum -y install glusterfs-fuse && yum clean all -y
LABEL build-date="${builddate}"
LABEL io.k8s.description="FUSE-based CSI driver for Gluster file access"
LABEL name="glusterfs-csi-driver"
LABEL Summary="FUSE-based CSI driver for Gluster file access"
LABEL vcs-type="git"
LABEL vcs-url="https://github.com/gluster/gluster-csi-driver"
LABEL vendor="gluster.org"
LABEL version="${version}"

ENTRYPOINT ["/glusterfs-csi-driver"]
35 changes: 0 additions & 35 deletions scripts/check-go.sh

This file was deleted.

20 changes: 0 additions & 20 deletions scripts/check-reqs.sh

This file was deleted.

1 change: 1 addition & 0 deletions scripts/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ run_check '.*\.md' mdl --style scripts/mdl-style.rb

# Install via: dnf install shellcheck
run_check '.*\.(ba)?sh' shellcheck
run_check '.*\.(ba)?sh' bash -n

# Install via: pip install yamllint
run_check '.*\.ya?ml' yamllint -s -d "{extends: default, rules: {line-length: {allow-non-breakable-inline-mappings: true}}}"
Expand Down

0 comments on commit e5df356

Please sign in to comment.