Skip to content

Commit 1f2440e

Browse files
committed
Generating Go Bindings with Docker
This patch introduces the ability to generate Go language bindings using Docker with `Dockerfile-go` and a spec file provided at runtime: # builds the container image $ docker build -t csi/golang -f Dockerfile-go . # emits the go bindings to stdout $ cat spec.md | docker run -i csi/golang If the image is run without content available on STDIN then the following error is emitted. $ docker run csi/golang usage: cat spec.md | docker run -i golang/csi $ docker run -i csi/golang usage: cat spec.md | docker run -i golang/csi The image can also be used to emit the generated protobuf instead of the golang bindings: # emits the protobuf to stdout $ cat spec.md | docker run -i csi/golang proto
1 parent 716f53e commit 1f2440e

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

Dockerfile-go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
FROM golang:1.9.3-alpine as build
2+
MAINTAINER Andrew Kutz <[email protected]>
3+
4+
# The versions of the software used in the build image. Please note
5+
# that the version of the protobuf Go packages and their transitive
6+
# dependencies are defined in the local files lib/go/Gopkg.toml and
7+
# lib/go/Gopkg.lock.
8+
ENV PROTOC_VER 3.5.1
9+
ENV DEP_VER 0.4.1
10+
11+
ENV PKGPATH ${GOPATH}/src/csi
12+
RUN mkdir -p ${PKGPATH}
13+
WORKDIR ${PKGPATH}
14+
15+
# Update the package cache and install the required tools.
16+
RUN apk update && apk add unzip curl git
17+
18+
# Download protoc.
19+
ENV PROTOC_ZIP protoc-$PROTOC_VER-linux-x86_64.zip
20+
ENV PROTOC_BIN bin/protoc
21+
RUN curl -LO https://github.com/google/protobuf/releases/download/v${PROTOC_VER}/${PROTOC_ZIP} && \
22+
unzip ${PROTOC_ZIP} && \
23+
chmod 0755 ${PROTOC_BIN} && \
24+
mv ${PROTOC_BIN} /usr/local/bin
25+
26+
# Download dep.
27+
ENV DEP_BIN dep-linux-amd64
28+
RUN curl -LO https://github.com/golang/dep/releases/download/v${DEP_VER}/${DEP_BIN} && \
29+
chmod 0755 ${DEP_BIN} && \
30+
mv ${DEP_BIN} /usr/local/bin/dep
31+
32+
# Copy the Go source file, dependency manifest, and lock file into the image.
33+
COPY lib/go/csi.go .
34+
COPY lib/go/Gopkg.toml .
35+
COPY lib/go/Gopkg.lock .
36+
37+
# Download the Go dependencies.
38+
RUN dep ensure -v
39+
40+
# Build the protogen-go binary.
41+
ENV PROTOC_GEN_GO_BIN protoc-gen-go
42+
RUN go build -o /usr/local/bin/${PROTOC_GEN_GO_BIN} ./vendor/github.com/golang/protobuf/protoc-gen-go
43+
44+
# Start the second build stage.
45+
FROM alpine:3.7
46+
RUN mkdir -p /csi
47+
WORKDIR /csi
48+
49+
# The versions of the software used in the primary image.
50+
ENV GLIBC_VERSION 2.26-r0
51+
52+
# Download and install glibc.
53+
# This step is credited to https://github.com/jeanblanchard/docker-alpine-glibc.
54+
RUN apk update && apk add curl
55+
RUN curl -Lo /etc/apk/keys/sgerrand.rsa.pub https://github.com/raw/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub && \
56+
curl -Lo glibc.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk" && \
57+
curl -Lo glibc-bin.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-bin-${GLIBC_VERSION}.apk" && \
58+
apk add glibc-bin.apk glibc.apk && \
59+
/usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib && \
60+
echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf
61+
62+
# Copy the protoc and protoc-gen-go binaries from the build image.
63+
COPY --from=build /usr/local/bin/protoc* /usr/local/bin/
64+
65+
# Copy the entrypoint script from the local lib/go directory.
66+
COPY lib/go/spec.sh .
67+
RUN chmod 0755 spec.sh
68+
69+
ENTRYPOINT [ "./spec.sh" ]

lib/go/Gopkg.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/go/Gopkg.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
2+
# for detailed Gopkg.toml documentation.
3+
#
4+
# Refer to https://github.com/toml-lang/toml for detailed TOML docs.
5+
6+
# The following packages are required because they're either build
7+
# tools or are dependencies only if the "csi" package is already
8+
# generated. However, if the "csi" package is removed (a valid
9+
# condition), a "dep ensure" will not fetch the dependencies it
10+
# no longer sees in the project's dependency graph. Including the
11+
# dependencies in the "required" list ensures they're always
12+
# downloaded.
13+
required = [
14+
"github.com/golang/protobuf/protoc-gen-go",
15+
"golang.org/x/net/context",
16+
"google.golang.org/grpc"
17+
]
18+
19+
[[constraint]]
20+
branch = "master"
21+
name = "github.com/golang/protobuf"
22+
23+
[[constraint]]
24+
branch = "master"
25+
name = "golang.org/x/net"
26+
27+
[[constraint]]
28+
name = "google.golang.org/grpc"
29+
version = "1.6.0"

lib/go/spec.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
3+
# Check to see if there is any content in STDIN. If nothing is read
4+
# after 1 second, then exit with an error.
5+
if IFS= read -rt 1 line; then
6+
echo "$line" > csi.spec
7+
else
8+
echo "usage: cat spec.md | docker run -i golang/csi"
9+
exit 1
10+
fi
11+
12+
while IFS= read -r line; do
13+
echo "$line" >> csi.spec
14+
done
15+
16+
sed -ne '/```protobuf$/,/```$/ p' < csi.spec | \
17+
sed -e 's@^```.*$@////////@g' > csi.proto
18+
19+
if [ "$1" = "proto" ]; then
20+
cat csi.proto
21+
else
22+
protoc -I . --go_out=plugins=grpc:. csi.proto
23+
cat csi.pb.go
24+
fi

0 commit comments

Comments
 (0)