Add OpenFaaS Clear Linux based GO template

Signed-off-by: Sophia Gong <sophia.gong@intel.com>
This commit is contained in:
Sophia Gong
2019-12-09 08:55:49 +00:00
committed by qzheng527
parent 5b87521388
commit f20becda68
6 changed files with 113 additions and 0 deletions
+23
View File
@@ -93,6 +93,29 @@ Steps could refer to
Then you can invoke your python function by OpenFaas UI or faas-cli.
### go-clearlinux
1. mkdir test && cd test
2. `cp -r "<your-dockerfiles-path>"/FaaS/OpenFaaS/template/. template/`
3. `faas-cli new --lang go-clearlinux go-openfaas --prefix="<your-docker-username-here>"`
Files tree as below.
>
├── go-openfaas
│   ├── handler.go
├── go-openfaas.yml
└── template
* Put the required Clear Linux bundles in the "bundles.txt".
For example,
`echo "openblas" >> go-openfaas/bundles.txt`
`echo "wget" >> go-openfaas/bundles.txt`
4. `faas-cli up -f go-openfaas.yml`
Then you can invoke your go function by OpenFaas UI or faas-cli.
## Proxy and Clear Linux mirror
When working behind a Proxy, you can pass proxy settings to faas-cli commands by "--build-arg".
@@ -0,0 +1,56 @@
FROM openfaas/classic-watchdog:0.18.1 as watchdog
FROM clearlinux/golang as builder
# Allows you to add additional packages via build-arg
ARG CGO_ENABLED=0
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog
WORKDIR /go/src/handler
COPY . .
# install bundles if required
# PLEASE input bundle line by line
COPY bundles.txt .
RUN for bundle in $(cat bundles.txt); do \
swupd bundle-add $bundle $swupd_args; \
done
# 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; }
RUN CGO_ENABLED=${CGO_ENABLED} GOOS=linux \
go build --ldflags "-s -w" -a -installsuffix cgo -o handler . && \
go test $(go list ./... | grep -v /vendor/) -cover
FROM clearlinux:base
# Add non root user
RUN groupadd app && useradd -g app app
RUN mkdir -p /home/app
WORKDIR /home/app
# install bundles if required
# PLEASE input bundle line by line
COPY bundles.txt .
RUN for bundle in $(cat bundles.txt); do \
swupd bundle-add $bundle $swupd_args; \
done
COPY --from=builder /usr/bin/fwatchdog .
COPY --from=builder /go/src/handler/function/ .
COPY --from=builder /go/src/handler/handler .
RUN chown -R app /home/app
USER app
ENV fprocess="./handler"
EXPOSE 8080
HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
CMD ["./fwatchdog"]
@@ -0,0 +1,10 @@
package function
import (
"fmt"
)
// Handle a serverless request
func Handle(req []byte) string {
return fmt.Sprintf("Hello, Go. You said: %s", string(req))
}
@@ -0,0 +1,22 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package main
import (
"fmt"
"handler/function"
"io/ioutil"
"log"
"os"
)
func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Unable to read standard input: %s", err.Error())
}
fmt.Println(function.Handle(input))
}
@@ -0,0 +1,2 @@
language: go
fprocess: ./handler