Skip to content

Generating Go Bindings with Docker #181

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
69 changes: 69 additions & 0 deletions Dockerfile-go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
FROM golang:1.9.3-alpine as build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this dockerfile at the root of the repo, instead of in the lib/go subtree?

MAINTAINER Andrew Kutz <[email protected]>

# The versions of the software used in the build image. Please note
# that the version of the protobuf Go packages and their transitive
# dependencies are defined in the local files lib/go/Gopkg.toml and
# lib/go/Gopkg.lock.
ENV PROTOC_VER 3.5.1
ENV DEP_VER 0.4.1

ENV PKGPATH ${GOPATH}/src/csi
RUN mkdir -p ${PKGPATH}
WORKDIR ${PKGPATH}

# Update the package cache and install the required tools.
RUN apk update && apk add unzip curl git

# Download protoc.
ENV PROTOC_ZIP protoc-$PROTOC_VER-linux-x86_64.zip
ENV PROTOC_BIN bin/protoc
RUN curl -LO https://github.com/google/protobuf/releases/download/v${PROTOC_VER}/${PROTOC_ZIP} && \
unzip ${PROTOC_ZIP} && \
chmod 0755 ${PROTOC_BIN} && \
mv ${PROTOC_BIN} /usr/local/bin

# Download dep.
ENV DEP_BIN dep-linux-amd64
RUN curl -LO https://github.com/golang/dep/releases/download/v${DEP_VER}/${DEP_BIN} && \
chmod 0755 ${DEP_BIN} && \
mv ${DEP_BIN} /usr/local/bin/dep

# Copy the Go source file, dependency manifest, and lock file into the image.
COPY lib/go/csi.go .
COPY lib/go/Gopkg.toml .
COPY lib/go/Gopkg.lock .

# Download the Go dependencies.
RUN dep ensure -v

# Build the protogen-go binary.
ENV PROTOC_GEN_GO_BIN protoc-gen-go
RUN go build -o /usr/local/bin/${PROTOC_GEN_GO_BIN} ./vendor/github.com/golang/protobuf/protoc-gen-go

# Start the second build stage.
FROM alpine:3.7
RUN mkdir -p /csi
WORKDIR /csi

# The versions of the software used in the primary image.
ENV GLIBC_VERSION 2.26-r0

# Download and install glibc.
# This step is credited to https://github.com/jeanblanchard/docker-alpine-glibc.
RUN apk update && apk add curl
RUN curl -Lo /etc/apk/keys/sgerrand.rsa.pub https://github.com/raw/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub && \
curl -Lo glibc.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk" && \
curl -Lo glibc-bin.apk "https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-bin-${GLIBC_VERSION}.apk" && \
apk add glibc-bin.apk glibc.apk && \
/usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib && \
echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf

# Copy the protoc and protoc-gen-go binaries from the build image.
COPY --from=build /usr/local/bin/protoc* /usr/local/bin/

# Copy the entrypoint script from the local lib/go directory.
COPY lib/go/spec.sh .
RUN chmod 0755 spec.sh

ENTRYPOINT [ "./spec.sh" ]
39 changes: 39 additions & 0 deletions lib/go/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions lib/go/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# Refer to https://github.com/toml-lang/toml for detailed TOML docs.

# The following packages are required because they're either build
# tools or are dependencies only if the "csi" package is already
# generated. However, if the "csi" package is removed (a valid
# condition), a "dep ensure" will not fetch the dependencies it
# no longer sees in the project's dependency graph. Including the
# dependencies in the "required" list ensures they're always
# downloaded.
required = [
"github.com/golang/protobuf/protoc-gen-go",
"golang.org/x/net/context",
"google.golang.org/grpc"
]

[[constraint]]
branch = "master"
name = "github.com/golang/protobuf"

[[constraint]]
branch = "master"
name = "golang.org/x/net"

[[constraint]]
name = "google.golang.org/grpc"
version = "1.6.0"
24 changes: 24 additions & 0 deletions lib/go/spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

# Check to see if there is any content in STDIN. If nothing is read
# after 1 second, then exit with an error.
if IFS= read -rt 1 line; then
echo "$line" > csi.spec
else
echo "usage: cat spec.md | docker run -i golang/csi"
exit 1
fi

while IFS= read -r line; do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we replace this while loop with a simple cat >> csi.spec?

echo "$line" >> csi.spec
done

sed -ne '/```protobuf$/,/```$/ p' < csi.spec | \
sed -e 's@^```.*$@////////@g' > csi.proto

if [ "$1" = "proto" ]; then
cat csi.proto
else
protoc -I . --go_out=plugins=grpc:. csi.proto
cat csi.pb.go
fi