openfaas / faas

OpenFaaS - Serverless Functions Made Simple
https://www.openfaas.com
MIT License
25.12k stars 1.93k forks source link

test gofmt in the dockerfile fails "on my machine". #607

Closed gesch closed 6 years ago

gesch commented 6 years ago

Just as an disclamer, I'm new to openfaas and go, so a user error might be the obvious issue here. I got started with the workshop and got a bit cocky and switched from ruby, which i also don't know much about to go because I'm interessed in go. I'm java developer by breed.

Environment

The environment is a windows 10 laptop, openfaas is running on a azure virtual machine created with the docker-machine shipped with docker toolbox for windows. The azure/openfaas env was installed a couple of days ago just for this test case. swarm is enabled, just the master one, no worker, docker 18.02.0-ce. docker-compose 1.16.1. openfaas version 0.6.4.

context

When creating the template for go everything worked like a charme. Got my template and just changed the sprintf to make sure i can identify it later. manual compile works fine. running the faas build failed because of line 13 in the dockerfile for the go template. ( 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; } ) As mentioned in the error message, i ran gofmt -s -w quite a couple of times. Here i might have failed for some reason. Maybe i didn't know what I'm doing :) However, removing the line in the dockerfile and everything is working fine. I checked the encoding, it is the same. But while writing i got aware of one difference, the original handler.go is using crlf, the new one only the windows style lf. Not sure if this scared of the test.

Expected Behaviour

line 13 accepts the code.

Current Behaviour

line 13 throws an error indicating to run gofmt again.

Possible Solution

shrug

attached the modified handler.go file i editied based on your template. handler.zip

I'm happy to provide more details if needed. The image created without the test is on docker hub, but i guess it's not very helpful as it works fine.

gesch commented 6 years ago

think i added the wrong handler file the first time (the template, not the edited one).

gesch commented 6 years ago

Tried to replace the lf with crlf, test blamed me to run gofmt, which turned them back into LF. So no luck with the line endings.

johnmccabe commented 6 years ago

That handler works fine for me on Windows 10. The line-endings are supposed to be LF (Unix) and not CRLF (Windows) so that attached zip looks OK.

Can you zip up your functions build directory and also paste the output from the command line here. Its kinda hard to tell exactly whats going on for you here.

PS D:\Workspaces\go\src\github.com\openfaas\faas\delme> cat .\whatsup\handler.go
package function

import (
        "fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
        return fmt.Sprintf("Boys who Go. You said: %s", string(req))
}
PS D:\Workspaces\go\src\github.com\openfaas\faas\delme> faas-cli.exe build -f .\whatsup.yml
[0] > Building: whatsup.
Clearing temporary build folder: ./build/whatsup/
Preparing ./whatsup/ ./build/whatsup/function
Building: whatsup with go template. Please wait..
Sending build context to Docker daemon  6.656kB
Step 1/17 : FROM golang:1.8.3-alpine3.6 as builder
 ---> 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.7.6/fwatchdog > /usr/bin/fwatchdog     && chmod +x /usr/bin/fwatchdog     && apk del curl --no-cache
 ---> Using cache
 ---> f6a62e8ff731
Step 3/17 : WORKDIR /go/src/handler
 ---> Using cache
 ---> 34d04ca11229
Step 4/17 : COPY . .
 ---> ab0cae2d39f7
Step 5/17 : 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; }
 ---> Running in 644c802f2734
Removing intermediate container 644c802f2734
 ---> 5286a6e7d0e5
Step 6/17 : RUN CGO_ENABLED=0 GOOS=linux     go build --ldflags "-s -w" -a -installsuffix cgo -o handler . &&     go test $(go list ./... | grep -v /vendor/) -cover
 ---> Running in 996572cd1af7
?       handler [no test files]
?       handler/function        [no test files]
Removing intermediate container 996572cd1af7
 ---> 40efc921a286
Step 7/17 : FROM alpine:3.6
 ---> 77144d8c6bdc
Step 8/17 : RUN apk --no-cache add ca-certificates
 ---> Using cache
 ---> 9411bc373d5b
Step 9/17 : RUN addgroup -S app && adduser -S -g app app
 ---> Using cache
 ---> 562e322b14d7
Step 10/17 : RUN mkdir -p /home/app
 ---> Using cache
 ---> de051c74e6a9
Step 11/17 : RUN chown app /home/app
 ---> Using cache
 ---> 136f9a9c63b8
Step 12/17 : WORKDIR /home/app
 ---> Using cache
 ---> c4be1dad1225
Step 13/17 : COPY --from=builder /go/src/handler/handler    .
 ---> 412c5496672b
Step 14/17 : COPY --from=builder /usr/bin/fwatchdog         .
 ---> e563250ce87f
Step 15/17 : USER app
 ---> Running in d27a34832b40
Removing intermediate container d27a34832b40
 ---> 8b569da8a320
Step 16/17 : ENV fprocess="./handler"
 ---> Running in ecf6caba036b
Removing intermediate container ecf6caba036b
 ---> f7d395e215cf
Step 17/17 : CMD ["./fwatchdog"]
 ---> Running in d94f09fe4fe2
Removing intermediate container d94f09fe4fe2
 ---> 740d67f53d99
Successfully built 740d67f53d99
Successfully tagged whatsup:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
Image: whatsup built.
[0] < Builder done.
johnmccabe commented 6 years ago

Can you also just confirm that the function built successfully (faas-cli.exe build rather than go build) before you changed the handler?

gesch commented 6 years ago

Thats the build directory: hello-go.zip and the matching output

$ faas build -f hello-go.yml
[0] > Building hello-go.
Clearing temporary build folder: ./build/hello-go/
Preparing ./hello-go/ ./build/hello-go/function
Building: sdadmin/hello-go with go template. Please wait..
Sending build context to Docker daemon  6.656kB
Step 1/17 : FROM golang:1.8.3-alpine3.6 as builder
 ---> 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.7.6/fwatchdog > /usr/bin/fwatchdog     && chmod +x /usr/bin/fwatchdog     && apk del curl --no-cache
 ---> Using cache
 ---> 30467ae1d6b0
Step 3/17 : WORKDIR /go/src/handler
 ---> Using cache
 ---> ad4b7b55e4ea
Step 4/17 : COPY . .
 ---> 59251457c26a
Step 5/17 : 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; }
 ---> Running in b32453aafcca
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/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }' returned a non-zero code: 1
2018/03/25 10:35:56 ERROR - Could not execute command: [docker build -t sdadmin/hello-go .]

GEsch@SDHAMW064 MINGW64 /c/workspace/openfaas/faas (0.7.3)
$  faas-cli.exe build -f hello-go.yml
[0] > Building hello-go.
Clearing temporary build folder: ./build/hello-go/
Preparing ./hello-go/ ./build/hello-go/function
Building: sdadmin/hello-go with go template. Please wait..
Sending build context to Docker daemon  6.656kB
Step 1/17 : FROM golang:1.8.3-alpine3.6 as builder
 ---> 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.7.6/fwatchdog > /usr/bin/fwatchdog     && chmod +x /usr/bin/fwatchdog     && apk del curl --no-cache
 ---> Using cache
 ---> 30467ae1d6b0
Step 3/17 : WORKDIR /go/src/handler
 ---> Using cache
 ---> ad4b7b55e4ea
Step 4/17 : COPY . .
 ---> Using cache
 ---> 59251457c26a
Step 5/17 : 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; }
 ---> Running in 4847a6a748c2
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/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }' returned a non-zero code: 1
2018/03/25 10:40:54 ERROR - Could not execute command: [docker build -t sdadmin/hello-go .]:

not sure if this preserves the line endings properly, but just for reference:

GEsch@SDHAMW064 MINGW64 /c/workspace/openfaas/faas (0.7.3)
$ cat ./hello-go/handler.go
package function

import (
        "fmt"
)

// Handle a serverless request
func Handle(req []byte) string {
        return fmt.Sprintf("Boys who Go. You said: %s", string(req))
}
johnmccabe commented 6 years ago

@gesch the line endings in main.go are CRLF, the test step is searching for all go files and its actually this which is resulting in the output you're seeing.

For reference, what editor are you using, and if you're committing this to a local git repo is it configured it to automatically change line endings (git config core.autocrlf)?

gesch commented 6 years ago

This solved the issue, i didn't check the unchanged files provided by the template. Sorry for bothering you :)

alexellis commented 6 years ago

@gesch which editor are you using?

John - thank you for the support with this issue.

gesch commented 6 years ago

@alexellis using latest version of visual studio code, which is good and bad at the same time. The editor fixes the encoding automatically for all edited files. But this way i was not aware of the issue at all for the other files cloned from the repository.

alexellis commented 6 years ago

@gesch what is the status of this issue?

Are you using a work-around that needs to be documented or is this resolved?

gesch commented 6 years ago

Hi Alex,

Sorry if if I missed something here. The quick chat in slack solved my problem immediately. The ticket is closed and I guessed that's the final state. I'm not aware of all the steps in your workflow :)

Best regards and thanks for your help,

Guido


From: Alex Ellis notifications@github.com Sent: Monday, April 16, 2018 11:48:05 PM To: openfaas/faas Cc: Esch, Guido; Mention Subject: Re: [openfaas/faas] test gofmt in the dockerfile fails "on my machine". (#607)

@geschhttps://github.com/gesch what is the status of this issue?

Are you using a work-around that needs to be documented or is this resolved?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/openfaas/faas/issues/607#issuecomment-381760904, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AGkI4dASX-V-hM_XDlJIeI1SCnB0zTsdks5tpRGVgaJpZM4S56Xb.

alexellis commented 6 years ago

What did you learn from the chat in Slack that resolved this for you?

gesch commented 6 years ago

a) windows is not suitable for go development b) changing git config parameters solved the problem in the short term. However this will break other projects. I will create a couple .gitattributes for my own git repository to make sure, go line endings stay LF as they should. Not sure if this attributes are usable for the openfaas repo.