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

revert golang-http and keep golang-middleware #81

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ test
test.yml
.DS_STORE
.vscode
.idea
6 changes: 0 additions & 6 deletions go.work

This file was deleted.

2 changes: 1 addition & 1 deletion template/golang-http/.dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
function/vendor/github.com/openfaas/go-function-sdk
function/vendor/github.com/openfaas-incubator/go-function-sdk
8 changes: 4 additions & 4 deletions template/golang-http/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

RUN apk --no-cache add git
RUN apk --no-cache add git build-base
mrwormhole marked this conversation as resolved.
Show resolved Hide resolved

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog


RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .
Expand All @@ -20,8 +19,9 @@ ARG GO111MODULE="on"
ARG GOPROXY=""
ARG GOFLAGS=""
ARG CGO_ENABLED=0
ENV CGO_ENABLED=${CGO_ENABLED}

# Lift the vendor and go.mod to the main package, cleanup any relative references
RUN sh modules-cleanup.sh

# 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; }
Expand All @@ -32,7 +32,7 @@ RUN mkdir -p /go/src/handler/function/static
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover

WORKDIR /go/src/handler
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed CGO_ENABLED from here for consistency with other dockerfiles

RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.16.2 as ship
Expand Down
6 changes: 5 additions & 1 deletion template/golang-http/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module handler

go 1.18

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

@mrwormhole mrwormhole Aug 27, 2022

Choose a reason for hiding this comment

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

this part is copied from this repository times

https://github.com/openfaas/golang-http-template/tree/eb793ff6f2749e1ebb02f4c235a78d7602102d7e

happy to discuss this, if giving path makes download faster

replace handler/function => ./function

require (
github.com/openfaas/templates-sdk v0.0.0-20200723092016-0ebf61253625
Copy link
Member

Choose a reason for hiding this comment

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

i think we want to use the current sha here instead of reverting this value, this keeps the template-sdk up-to-date

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea makes perfect sense, only thing I was not sure about is the difference between 2020 and 2022 in this repository in terms of reliability. Will do the change

)
4 changes: 2 additions & 2 deletions template/golang-http/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03 h1:wMIW4ddCuogcuXcFO77BPSMI33s3QTXqLTOHY6mLqFw=
github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03/go.mod h1:2vlqdjIdqUjZphguuCAjoMz6QRPm2O8UT0TaAjd39S8=
github.com/openfaas/templates-sdk v0.0.0-20200723092016-0ebf61253625 h1:6JSt10GDCOw0F67bWnqZ6AYg92pbqCcchTu181aT1w0=
github.com/openfaas/templates-sdk v0.0.0-20200723092016-0ebf61253625/go.mod h1:JWcVHdzlHcR7nLuaDL88Mz68wOqDvOn0CLO6t27OMhk=
6 changes: 0 additions & 6 deletions template/golang-http/go.work

This file was deleted.

157 changes: 157 additions & 0 deletions template/golang-http/modules-cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/usr/bin/env sh

set -e

GO111MODULE=$(go env GO111MODULE)

# move_vendor will copy the function's vendor folder,
# if it exists.
move_vendor() {
if [ ! -d ./function/vendor ]; then
echo "vendor not found"
return
fi

echo "moving function vendor"
mv -f ./function/vendor .
}

# cleanup_gomod will move the function's go module
cleanup_gomod() {

# Nothing to do when modules is explicitly off
# the z prefix protects against any SH wonkiness
# see https://stackoverflow.com/a/18264223
if [ "z$GO111MODULE" = "zoff" ]; then
echo "modules disabled, skipping go.mod cleanup"
return
fi

if [ ! -f ./function/go.mod ]; then
echo "module not initialized, skipping go.mod cleanup"
return
fi

echo "cleaning up go.mod"

# Copy the user's go.mod
mv -f ./function/go.mod .
mv -f ./function/go.sum .

# Clean up the go.mod

# Cleanup any sub-module replacements.
# This requires modifying any replace that points to "./*",
# the user has will use this to reference sub-modules instead
# of sub-packages, which we cleanup below.
echo "cleanup local replace statements"
# 1. Replace references to the local folder with `./function`
sed -i 's/=> \.\//=> \.\/function\//' go.mod

# Remove any references to the handler/function module.
# It is ok to just remove it because we will replace it later.
#
# Note that these references may or may not exist. We expect the
# go.mod to have a replace statement _if_ developer has subpackages
# in their handler. In this case they will need a this replace statement
#
# replace handler/function => ./
#
# `go mod` will then add a line that looks like
#
# handler/function v0.0.0-00010101000000-000000000000
#
# both of these lines need to be replaced, this grep selects everything
# _except_ those offending lines.
grep -v "\shandler/function" go.mod >gomod2
mv gomod2 go.mod

# Now update the go.mod
#
# 1. use replace so that imports of handler/function use the local code
# 2. we need to rename the module to handler because our main.go assumes
# this is the package name
go mod edit \
-replace=handler/function=./function \
-module handler

if [ "$DEBUG" -eq 1 ]; then
cat go.mod
echo ""
fi
}

# cleanup_vendor_modulestxt will cleanup the modules.txt file in the vendor folder
# this file is needed when modules are enabled and it must be in sync with the
# go.mod. To function correctly we need to modify the references to handler/function,
# if they exist.
cleanup_vendor_modulestxt() {
if [ ! -d ./vendor ]; then
echo "no vendor found, skipping modules.txt cleanup"
return
fi

# Nothing to do when modules is explicitly off
# the z prefix protects against any SH wonkiness
# see https://stackoverflow.com/a/18264223
if [ "z$GO111MODULE" = "zoff" ]; then
echo "modules disabled, skipping modules.txt cleanup"
return
fi

echo "cleanup vendor/modules.txt"

# just in case
touch "./vendor/modules.txt"

# when vendored, we need to do similar edits to the vendor/modules.txt
# as we did to the go.mod

# 1. we need to replace any possible copy of the handler code
rm -rf vendor/handler &&

# 2. in modules.txt, we remove existing references to the handler/function
# we reconstruct these in the last step
grep -v "\shandler/function" ./vendor/modules.txt >modulestext
mv modulestext ./vendor/modules.txt

# 3. Handle any other local replacements.
# any replace that points to `./**` needs to be udpat echo "cleanup local replace statements"
sed -i 's/=> \.\//=> \.\/function\//' ./vendor/modules.txt

# 4. To make the modules.txt consistent with the new go.mod,
# we add the mising replace to the vendor/modules.txt
echo "## explicit" >>./vendor/modules.txt
echo "# handler/function => ./function" >>./vendor/modules.txt

if [ "$DEBUG" -eq 1 ]; then
cat ./vendor/modules.txt
echo ""
fi
}

# has_local_replacement checks if the file contains local go module replacement
has_local_replacement() {
return "$(grep -E -c '=> \./\S+' "$1")"
}

remove_workspaces() {
if [ -f ./function/go.work ]; then
echo "cleaning function workspace"
rm -f ./function/go.work
return
fi

echo "no work spaces found"
}

################
# main
################
remove_workspaces

move_vendor

cleanup_gomod

cleanup_vendor_modulestxt
5 changes: 1 addition & 4 deletions template/golang-middleware/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

RUN apk --no-cache add git
RUN apk --no-cache add git build-base

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog


RUN mkdir -p /go/src/handler
WORKDIR /go/src/handler
COPY . .
Expand All @@ -20,7 +19,6 @@ ARG GO111MODULE="on"
ARG GOPROXY=""
ARG GOFLAGS=""
ARG CGO_ENABLED=0
ENV CGO_ENABLED=${CGO_ENABLED}

# 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; }
Expand All @@ -35,7 +33,6 @@ RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .

FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.16.2 as ship

# Add non root user and certs
RUN apk --no-cache add ca-certificates \
&& addgroup -S app && adduser -S -g app app
Expand Down