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

Add Go template capability for creating functions #145

Merged
merged 4 commits into from
Oct 6, 2017
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
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ WORKDIR /go/src/github.com/openfaas/faas-cli
COPY . .

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

RUN VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \
&& GIT_COMMIT=$(git rev-list -1 HEAD) \
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli .
RUN go test ./... -cover
&& CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w -X github.com/openfaas/faas-cli/commands.GitCommit=${GIT_COMMIT} -X github.com/openfaas/faas-cli/commands.Version=${VERSION}" -a -installsuffix cgo -o faas-cli . \
&& go test $(go list ./... | grep -v /vendor/ | grep -v /template/) -cover

FROM alpine:latest
RUN apk --no-cache add ca-certificates
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

build:
./build.sh

build_redist:
./build_redist.sh

unit_test:
go test $(shell go list ./... | grep -v /vendor/ | grep -v /template/) -cover
2 changes: 1 addition & 1 deletion builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func BuildImage(image string, handler string, functionName string, language string, nocache bool, squash bool) {

switch language {
case "node", "python", "ruby", "csharp", "python3":
case "node", "python", "ruby", "csharp", "python3", "go":
tempPath := createBuildTemplate(functionName, handler, language)

fmt.Printf("Building: %s with %s template. Please wait..\n", image, language)
Expand Down
1 change: 1 addition & 0 deletions commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func runNewFunction(cmd *cobra.Command, args []string) {
- ruby
- csharp
- Dockerfile
- go

Or alternatively create a folder containing a Dockerfile, then pick
the "Dockerfile" lang type in your YAML file.
Expand Down
36 changes: 36 additions & 0 deletions template/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM golang:1.8.3-alpine

RUN apk --no-cache add curl \
&& echo "Pulling watchdog binary from Github." \
&& curl -sSL https://github.com/openfaas/faas/releases/download/0.6.5/fwatchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& apk del curl --no-cache

Copy link
Contributor

Choose a reason for hiding this comment

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

one quick suggestion, some other pull requests that are pending are opting to run the functions as non-root users. some examples here by @ericstoekl #91.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, I should know that I was one of the guilty parties behind caninothazroot.

Not even following my own advice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have just added an additional commit which corrects this issue

WORKDIR /go/src/handler
COPY . .

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

RUN CGO_ENABLED=0 GOOS=linux \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
go test ./... -cover

FROM alpine:latest
Copy link
Member

@alexellis alexellis Oct 6, 2017

Choose a reason for hiding this comment

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

Please pin to 3.6 - tracking :latest might cause problems when Alpine pushes 3.7.

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 chown app /home/app

WORKDIR /home/app

COPY --from=0 /go/src/handler/handler .
COPY --from=0 /usr/bin/fwatchdog .

USER app

ENV fprocess="./handler"

CMD ["./fwatchdog"]
10 changes: 10 additions & 0 deletions template/go/function/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package function

import (
"fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
}
12 changes: 12 additions & 0 deletions template/go/function/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package function

import "testing"

func TestHandleReturnsCorrectResponse(t *testing.T) {
expected := "Hello, Go. You said: Hello World"
resp := Handle([]byte("Hello World"))

if resp != expected {
t.Fatalf("Expected: %v, Got: %v", expected, resp)
}
}
19 changes: 19 additions & 0 deletions template/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

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

"handler/function"
)

func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Unable to read standard input: %s", err.Error())
}

fmt.Println(function.Handle(input))
}