Skip to content

Instantly share code, notes, and snippets.

@ganesshkumar
Last active December 21, 2017 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ganesshkumar/8c1918a9cbc1ddf7f87cff77e5dda8ec to your computer and use it in GitHub Desktop.
Save ganesshkumar/8c1918a9cbc1ddf7f87cff77e5dda8ec to your computer and use it in GitHub Desktop.
OpenFaaS

I am following this post,Build a Serverless Golang Function with OpenFaaS , to create a function in go.

You can find my function to generate identicon on Github.

  • Make sure you are using the branch go-template if you are cloning the repo.
  • The master brach is a standalone repo and it is not based on go template.

Setting up the fuction

Switch to your function folder

$ cd $FUNCTIONS_FOLDER

Create a new function from the go template

$ faas-cli new --lang go identicon

Checking the contents

$ ls
identicon     identicon.yml template

Cloning the identicon function from Github

$ rm -rf identicon

$ git clone git@github.com:ganesshkumar/openfaas-identicon.git identicon

# Use the go-template branch
$ cd identicon && \
  git branch go-template && \
  cd $FUNCTIONS_FOLDER

Using 3rd party dependencies

As suggested in the post, I am using dep to manage my dependencies in the function folder. It creates a vendor folder in my function. I have checked in the vendor folder as of now.

Issue 1: gofmt

When I build the function by executing faas-cli build -f identicon.yml I get the following error

[0] > Building: identicon.
Clearing temporary build folder: ./build/identicon/
Preparing ./identicon/ ./build/identicon/function
Building: identicon with go template. Please wait..
Sending build context to Docker daemon  28.03MB
Step 1/17 : FROM golang:1.8.3-alpine3.6
 ---> fd1ada53b403
Step 2/17 : RUN apk --no-cache add curl     && echo "Pulling watchdog binary from Github."     && curl -sSL https://github.com/openfaas/faas/releases/download/0.6.9/fwatchdog > /usr/bin/fwatchdog     && chmod +x /usr/bin/fwatchdog     && apk del curl --no-cache
 ---> Using cache
 ---> 4968e804e575
Step 3/17 : WORKDIR /go/src/handler
 ---> Using cache
 ---> 43978f256c92
Step 4/17 : COPY . .
 ---> 0dd91378fd75
Step 5/17 : RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }
 ---> Running in 611d3278df2e
./function/vendor/golang.org/x/image/draw/go1_9.go:41:13: expected type, found '='
./function/vendor/golang.org/x/image/draw/go1_9.go:44:12: expected type, found '='
./function/vendor/golang.org/x/image/draw/go1_9.go:47:9: expected type, found '='
./function/vendor/golang.org/x/image/draw/go1_9.go:57:16: expected type, found '='
Run "gofmt -s -w" on your Golang code
The command '/bin/sh -c test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }' returned a non-zero code: 1
2017/12/21 16:49:10 ERROR - Could not execute command: [docker build -t identicon .]

As Alex explained, the gofmt is finding issues in the vendor folder of the function $FUNCTIONS_FOLDER/identicon/vendor

Expected Behavior The vendor folder inside the functions should also be ignored

Possible Fix Repalce the line 13, in the go template's Dockerfile to

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; }

to make it exclude the function's vendor folder from gofmt check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment