openfaas / golang-http-template

Golang templates for OpenFaaS using HTTP extensions
https://www.openfaas.com/
MIT License
106 stars 57 forks source link

Cannot publish Function after updating template #75

Closed willfore closed 2 years ago

willfore commented 2 years ago

Needed to make use of Go 1.17 modules. Followed guide to upgrade the template:

Inside of func folder did the following:

rm -rf template/golang-http template/golang-middleware

Install new templates faas-cli template store pull golang-http

Tidy modules go mod tidy

Try to publish function and get the following error:

#24 1.350 handler.go:17:2: ambiguous import: found package github.com/openfaas/templates-sdk/go-http in multiple modules:
#24 1.350   github.com/openfaas/templates-sdk v0.0.0-20200723110415-a699ec277c12 (/go/pkg/mod/github.com/openfaas/templates-sdk@v0.0.0-20200723110415-a699ec277c12/go-http)
#24 1.350   github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03 (/go/pkg/mod/github.com/openfaas/templates-sdk/go-http@v0.0.0-20220408082716-5981c545cb03)
#24 ERROR: process "/bin/sh -c GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover" did not complete successfully: exit code: 1

Expected Behaviour

Upgrade template and publish app

Your Environment

Server: Docker Engine - Community Engine: Version: 20.10.12 API version: 1.41 (minimum version 1.12) Go version: go1.16.12 Git commit: 459d0df Built: Mon Dec 13 11:43:07 2021 OS/Arch: linux/arm64 Experimental: false containerd: Version: 1.4.12 GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc: Version: 1.0.2 GitCommit: v1.0.2-0-g52b36a2 docker-init: Version: 0.19.0 GitCommit: de40ad0

alexellis commented 2 years ago

Hi Billy,

The version of the template you have has other changes, which means the import should be updated for your function to point at github.com/openfaas/templates-sdk/go-http

Here is an example of what it should look like now:

package function

import (
    "fmt"
    "net/http"

    handler "github.com/openfaas/templates-sdk/go-http"
)

// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
    var err error

    message := fmt.Sprintf("Body: %s", string(req.Body))

    return handler.Response{
        Body:       []byte(message),
        StatusCode: http.StatusOK,
    }, err
}

This then gives:

$ go mod tidy

go: finding module for package github.com/openfaas/templates-sdk/go-http
go: downloading github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03
go: downloading github.com/openfaas/templates-sdk v0.0.0-20220408082716-5981c545cb03
go: found github.com/openfaas/templates-sdk/go-http in github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03

This is my go.mod file for a test function:

module handler/function

go 1.18

require github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03

Let me know if this resolves the problem you're facing.

Alternatively, you can always pin an older version of a template if that suits your needs (#branch #tag #release):

faas-cli template pull https://github.com/openfaas/golang-http-template#0.4.0
Fetch templates from repository: https://github.com/openfaas/golang-http-template at 0.4.0
2022/05/02 15:40:15 Attempting to expand templates from https://github.com/openfaas/golang-http-template
2022/05/02 15:40:16 Fetched 2 template(s) : [golang-http golang-middleware] from https://github.com/openfaas/golang-http-template

Alex

willfore commented 2 years ago

I made the suggested changes after go mod tidy I have the following in go.mod

module myfunc

go 1.18

require (
    github.com/google/uuid v1.3.0
    github.com/honeybadger-io/honeybadger-go v0.5.0
    github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03
    golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
)

require (
    github.com/StackExchange/wmi v1.2.1 // indirect
    github.com/go-ole/go-ole v1.2.5 // indirect
    github.com/pborman/uuid v1.2.0 // indirect
    github.com/shirou/gopsutil v2.18.12+incompatible // indirect
    golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)

When publishing now I run into

`#26 [build 13/13] RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --ldflags "-s -w" -a -installsuffix cgo -o handler .

26 0.136 main.go:16:2: package handler/function is not in GOROOT (/usr/local/go/src/handler/function)

26 ERROR: process "/bin/sh -c CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build --ldflags \"-s -w\" -a -installsuffix cgo -o handler ." did not complete successfully: exit code: 1`

I have GO111MODULE set to on

willfore commented 2 years ago

changing module name from myfunc to handler/function fixed the issue.

alexellis commented 2 years ago

OK to close the issue?

willfore commented 2 years ago

All good.

alexellis commented 2 years ago

/lock: resolved

alexellis commented 2 years ago

If anyone else has similar issues, feel free to open your own issue on GitHub.