Skip to content

Commit 675f544

Browse files
LucasRoesleralexellis
authored andcommitted
Add short script to move vendor folder around
**What** - Add Dockerfile testp to mov ethe vendor folder up from the function volder to function root. This allows the user to control the vendor folder and avoid collisions/validation errors from Go in modules mode - This new flow allows dynamic downloading of Go modules _or_ disabling Go modules and only using vendor. You can not use vendor _and_ modules at the same time due to how Go will validate that the vendor folder and the go.mod are insync Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
1 parent 886beea commit 675f544

File tree

6 files changed

+35
-136
lines changed

6 files changed

+35
-136
lines changed

template/golang-http/Dockerfile

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,54 @@ ARG TARGETARCH
88

99
RUN apk --no-cache add git
1010

11-
ENV CGO_ENABLED=0
12-
1311
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
1412
RUN chmod +x /usr/bin/fwatchdog
1513

14+
ENV CGO_ENABLED=0
15+
1616
RUN mkdir -p /go/src/handler
1717
WORKDIR /go/src/handler
1818
COPY . .
1919

20+
ARG GO111MODULE="off"
21+
ARG GOPROXY=""
22+
ARG GOFLAGS=""
23+
24+
# Add user overrides to the root go.mod, which is the only place "replace" can be used
25+
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
26+
RUN if [ -d ./function/vendor ]; then \
27+
echo "moving handler vendor" && \
28+
mv -f ./function/vendor . && go mod tidy; \
29+
else \
30+
echo "using modules or vendor not found "; \
31+
fi
32+
2033
# Run a gofmt and exclude all vendored code.
2134
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; }
2235

23-
ARG GO111MODULE="off"
24-
ARG GOPROXY=""
2536

37+
WORKDIR /go/src/handler/function
38+
39+
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
40+
41+
WORKDIR /go/src/handler
2642
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
2743
go build --ldflags "-s -w" -a -installsuffix cgo -o handler .
28-
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test handler/function/... -cover
2944

3045
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13
3146
# Add non root user and certs
3247
RUN apk --no-cache add ca-certificates \
3348
&& addgroup -S app && adduser -S -g app app
34-
# Split instructions so that buildkit can run & cache
49+
# Split instructions so that buildkit can run & cache
3550
# the previous command ahead of time.
3651
RUN mkdir -p /home/app \
3752
&& chown app /home/app
3853

3954
WORKDIR /home/app
4055

41-
COPY --from=build /go/src/handler/handler .
42-
COPY --from=build /usr/bin/fwatchdog .
43-
COPY --from=build /go/src/handler/function/ .
44-
45-
RUN chown -R app /home/app
56+
COPY --from=build --chown=app /go/src/handler/handler .
57+
COPY --from=build --chown=app /usr/bin/fwatchdog .
58+
COPY --from=build --chown=app /go/src/handler/function/ .
4659

4760
USER app
4861

template/golang-http/vendor/github.com/openfaas/templates-sdk/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

template/golang-http/vendor/github.com/openfaas/templates-sdk/go-http/README.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

template/golang-http/vendor/github.com/openfaas/templates-sdk/go-http/handler.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

template/golang-http/vendor/modules.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

template/golang-middleware/Dockerfile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@ RUN mkdir -p /go/src/handler
1717
WORKDIR /go/src/handler
1818
COPY . .
1919

20+
ARG GO111MODULE="off"
21+
ARG GOPROXY=""
22+
ARG GOFLAGS=""
23+
2024
# Add user overrides to the root go.mod, which is the only place "replace" can be used
2125
RUN cat function/GO_REPLACE.txt >> ./go.mod || exit 0
26+
RUN if [ -d ./function/vendor ] && [ "${GO111MODULE}" == "off" ]; then \
27+
echo "moving handler vendor" && \
28+
mv -f ./function/vendor .; \
29+
else \
30+
echo "using modules or vendor not found "; \
31+
fi
2232

2333
# Run a gofmt and exclude all vendored code.
2434
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; }
2535

26-
ARG GO111MODULE="off"
27-
ARG GOPROXY=""
28-
ARG GOFLAGS=""
29-
3036
WORKDIR /go/src/handler/function
3137

3238
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
@@ -39,7 +45,7 @@ FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.13
3945
# Add non root user and certs
4046
RUN apk --no-cache add ca-certificates \
4147
&& addgroup -S app && adduser -S -g app app
42-
# Split instructions so that buildkit can run & cache
48+
# Split instructions so that buildkit can run & cache
4349
# the previous command ahead of time.
4450
RUN mkdir -p /home/app \
4551
&& chown app /home/app

0 commit comments

Comments
 (0)