Skip to content

Commit

Permalink
Support go modules
Browse files Browse the repository at this point in the history
**What**
- Add support for nested go modules by supporting the GO_REPLACE.txt
  file and the build args for GO111MODULE and GOFLAGS

Signed-off-by: Lucas Roesler <[email protected]>
  • Loading branch information
LucasRoesler authored and alexellis committed Feb 4, 2020
1 parent 465644c commit 360077e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
27 changes: 18 additions & 9 deletions template/go-armhf/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
FROM openfaas/classic-watchdog:0.18.1 as watchdog
FROM golang:1.12-alpine3.11 as builder
FROM golang:1.13-alpine3.11 as builder

# Allows you to add additional packages via build-arg
ARG ADDITIONAL_PACKAGE
ARG CGO_ENABLED=0
ARG GO111MODULE="off"
ARG GOPROXY=""
ARG GOFLAGS=""

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

ENV CGO_ENABLED=0

WORKDIR /go/src/handler
COPY . .

# Add user overrides to the root go.mod, which is the only place "replace" can be used
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0

This comment has been minimized.

Copy link
@mabuaisha

mabuaisha Mar 17, 2020

@LucasRoesler @alexellis right now if you tried to run faas-cli build -yaml gofunction.yml you will face any issue that that GO_REPLACE does not exist cat: can't open 'function/GO_REPLACE.txt': No such file or directory

Any thoughts on this please

This comment has been minimized.

Copy link
@LucasRoesler

LucasRoesler Mar 19, 2020

Author Member

@mabuaisha can you report this as a bug, instead of a line comment of a commit. I will test this, but the || exit 0 should prevent this from failing

This comment has been minimized.

Copy link
@mabuaisha

mabuaisha Mar 19, 2020

@LucasRoesler thanks for reply, will open an issue on that

This comment has been minimized.

Copy link
@LucasRoesler

LucasRoesler Mar 19, 2020

Author Member

I just ran this template, the logs do have that error, but it does not break the build

#13 [builder 6/11] RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
#13 0.283 cat: can't open 'function/GO_REPLACE.txt': No such file or directory
#13 DONE 0.3s
...

#22 [stage-2 7/7] RUN chown -R app /home/app
#22 DONE 0.7s

#26 exporting to image
#26 exporting layers
#26 exporting layers 0.2s done
#26 writing image sha256:654564d2ebe8691ba6764687c3453940f013a919f895f4e275941a072ab6ae11 done
#26 naming to docker.io/library/heythere:latest done
#26 DONE 0.2s
Image: heythere:latest built.
[0] < Building heythere done in 27.51s.
[0] Worker done.

Total build time: 27.51s
➜  modtest

As you can see in this screenshot the resulting docker image works as expected

image

This comment has been minimized.

Copy link
@alexellis

alexellis Mar 19, 2020

Member

This is not an error, otherwise the whole process would have stopped. It's just a warning @mabuaisha like @LucasRoesler has explained and demonstrated above.

This comment has been minimized.

Copy link
@mabuaisha

mabuaisha Mar 19, 2020

@alexellis I did have an error which stop the whole build process, maybe could be related to the latest version of docker, because I used the latest updated version 19.03.8. Not sure if this is related or not

This comment has been minimized.

Copy link
@alexellis

alexellis Mar 19, 2020

Member

A commit message isn't really the place for this type of discussion. Feel free to raise an issue if you have steps to reproduce the issue consistently.


# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

WORKDIR /go/src/handler/function

RUN go test ./... -cover

WORKDIR /go/src/handler

RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
go test $(go list ./... | grep -v /vendor/) -cover
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .

FROM alpine:3.11
RUN apk --no-cache add \
ca-certificates

# Add non root user
RUN addgroup -S app && adduser -S -g app app
RUN mkdir -p /home/app
RUN apk --no-cache add ca-certificates \
&& addgroup -S app && adduser -S -g app app \
&& mkdir -p /home/app \
&& chown app /home/app

WORKDIR /home/app

Expand Down
5 changes: 5 additions & 0 deletions template/go-armhf/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module handler

go 1.13

replace handler/function => ./function
3 changes: 2 additions & 1 deletion template/go-armhf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package main

import (
"fmt"
"handler/function"
"io/ioutil"
"log"
"os"

"handler/function"
)

func main() {
Expand Down
27 changes: 18 additions & 9 deletions template/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
FROM openfaas/classic-watchdog:0.18.1 as watchdog
FROM golang:1.12-alpine3.11 as builder
FROM golang:1.13-alpine3.11 as builder

# Allows you to add additional packages via build-arg
ARG ADDITIONAL_PACKAGE
ARG CGO_ENABLED=0
ARG GO111MODULE="off"
ARG GOPROXY=""
ARG GOFLAGS=""

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog

ENV CGO_ENABLED=0

WORKDIR /go/src/handler
COPY . .

# Add user overrides to the root go.mod, which is the only place "replace" can be used
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0

# Run a gofmt and exclude all vendored code.
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

WORKDIR /go/src/handler/function

RUN go test ./... -cover

WORKDIR /go/src/handler

RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
go test $(go list ./... | grep -v /vendor/) -cover
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .

FROM alpine:3.11
RUN apk --no-cache add \
ca-certificates

# Add non root user
RUN addgroup -S app && adduser -S -g app app
RUN mkdir -p /home/app
RUN apk --no-cache add ca-certificates \
&& addgroup -S app && adduser -S -g app app \
&& mkdir -p /home/app \
&& chown app /home/app

WORKDIR /home/app

Expand Down
5 changes: 5 additions & 0 deletions template/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module handler

go 1.13

replace handler/function => ./function
3 changes: 2 additions & 1 deletion template/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package main

import (
"fmt"
"handler/function"
"io/ioutil"
"log"
"os"

"handler/function"
)

func main() {
Expand Down

0 comments on commit 360077e

Please sign in to comment.