Skip to content

Commit

Permalink
Enable go module support for go environment (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
life1347 committed Jul 10, 2019
1 parent 9e7dd5a commit 58722f8
Show file tree
Hide file tree
Showing 46 changed files with 131 additions and 6,539 deletions.
24 changes: 24 additions & 0 deletions environments/go/Dockerfile-1.12
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ARG GO_VERSION=1.12.7

FROM ubuntu:18.04 AS base
WORKDIR /
RUN apt update && apt install -y ca-certificates && rm -rf /var/lib/apt/lists/*

FROM golang:${GO_VERSION} AS builder

ENV GOPATH /usr
ENV APP ${GOPATH}/src/github.com/fission/fission/environments/go

WORKDIR ${APP}

ADD context ${APP}/context
ADD server.go ${APP}

RUN go get
RUN go build -a -o /server server.go

FROM base
COPY --from=builder /server /

ENTRYPOINT ["/server"]
EXPOSE 8888
14 changes: 14 additions & 0 deletions environments/go/builder/Dockerfile-1.12
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ARG BUILDER_IMAGE=fission/builder
ARG GO_VERSION=1.12.7

FROM ${BUILDER_IMAGE}

FROM golang:${GO_VERSION}

ENV GOPATH /usr
ENV GO111MODULE on
WORKDIR ${GOPATH}

COPY --from=0 /builder /builder
ADD build.sh /usr/local/bin/build

18 changes: 17 additions & 1 deletion environments/go/builder/build.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/bin/sh
#!/bin/bash

set -eux

srcDir=${GOPATH}/src/$(basename ${SRC_PKG})

trap "rm -rf ${srcDir}" EXIT

# http://ask.xmodulo.com/compare-two-version-numbers.html
version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }

if [ -d ${SRC_PKG} ]
then
echo "Building in directory ${srcDir}"
Expand All @@ -18,4 +21,17 @@ then
fi

cd ${srcDir}

if [ -f "go.mod" ]; then
if [ ! -z ${GOLANG_VERSION} ] && version_ge ${GOLANG_VERSION} "1.12"; then
go mod download
else
echo "Please update fission/go-builder and fission/go-env image to the latest version to support go module"
exit 1
fi
else
# still need to do this; otherwise, go will complain "cannot find main module".
go mod init
fi

go build -buildmode=plugin -i -o ${DEPLOY_PKG} .
35 changes: 35 additions & 0 deletions examples/go/module-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Go module usage

1. Initialize your project

```bash
$ go mod init "<module>"
```

For example,

```bash
$ go mod init "github.com/fission/fission/examples/go/go-module-example"
```

2. Add dependencies

* See [here](https://github.com/golang/go/wiki/Modules#daily-workflow)

3. Verify

```bash
$ go mod verify
```

4. Archive and create package as usual

```bash
$ zip -r go.zip .
adding: go.mod (deflated 26%)
adding: go.sum (deflated 1%)
adding: README.md (deflated 37%)
adding: main.go (deflated 30%)

$ fission pkg create --env go --src go.zip
```
3 changes: 3 additions & 0 deletions examples/go/module-example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/fission/fission/examples/go/go-module-example

require github.com/golang/example v0.0.0-20170904185048-46695d81d1fa
1 change: 1 addition & 0 deletions examples/go/module-example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/golang/example v0.0.0-20170904185048-46695d81d1fa/go.mod h1:tO/5UvQ/uKigUjQBPqzstj6uxd3fUIjddi19DxGJeWg=
File renamed without changes.
202 changes: 0 additions & 202 deletions examples/go/vendor-example/vendor/github.com/golang/example/LICENSE

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 58722f8

Please sign in to comment.