Skip to content
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

Support go modules #194

Merged
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
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

# 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

Choose a reason for hiding this comment

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

I find the GO_REPLACE pattern a bit confusing, have you consider merging go.mod instead? That way the function writer still has a regular go.mod that can be linted / validated. This project seems like it could do the trick: https://github.com/brendanjryan/modmerge

Choose a reason for hiding this comment

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

Or maybe even go the multi-module approach and have the main code can be a module and the function code a different one? If a package name is enforced we might not even need the replace. https://github.com/golang/go/wiki/Modules#faqs--multi-module-repositories has an overview of that approach.

Copy link
Member Author

Choose a reason for hiding this comment

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

This multi-module approach is what we are doing here, but ... go does not follow replaces in modules, so if you need to use a replace in your function handler module, it will not be respected in the parent template module. This GO_REPLACE is a way for us to explicitly allow overriding the replaces in the parent function module.

Copy link
Member

Choose a reason for hiding this comment

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


# 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 .

Choose a reason for hiding this comment

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

Should the $GOFLAGS arg be used here?

Copy link
Member Author

Choose a reason for hiding this comment

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

This ENV variable is read by the go command automatically https://golang.org/cmd/go/#hdr-Environment_variables


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