You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-08-13-building-functions-via-api-golang.md
+25-8Lines changed: 25 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: How to Build Functions with the Go SDK for OpenFaaS
3
-
description: Learn how to build functions from source with the Function builder API and Go SDK for OpenFaaS
3
+
description: Learn how to build functions from source with the Function Builder API and Go SDK for OpenFaaS
4
4
date: 2024-08-13
5
5
categories:
6
6
- kubernetes
@@ -18,7 +18,7 @@ In this blog post I'll show you how to build functions using the new `builder` p
18
18
19
19
You can use the [builder package](https://pkg.go.dev/github.com/openfaas/go-sdk/builder) to access the the Function Builder API, which takes source code and builds container images without needing root or Docker. It's designed for SaaS companies who want their users to be able to supply their own code to integrate into their product. It can also be used by a platform or managed services team that manages a multi-tenant OpenFaaS environment.
20
20
21
-
As a general rule, we see customers provide an AWS Lambda-like experience for their users, where code is typed into a text box using a web IDE, and then deployed with a single click.
21
+
Since the Function Builder API has been available, we've mainly seen customers create a user-experience that resembles AWS Lambda, but for their own users. Users open a dashboard hosting a web IDE like Ace, CodeMirror or Monaco and supply code, it's then built and deployed to OpenFaaS via a single click.
22
22
23
23
**Customer spotlight**
24
24
@@ -28,25 +28,42 @@ Whether the *Custom Script* is pre-supplied by the Patchworks team, or created b
28
28
29
29
For an example of the user-experience Patchworks created for their users, see: [Patchworks docs: Adding & testing custom scripts](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/adding-and-testing-custom-scripts#creating-a-new-script)
30
30
31
-
[E2E Networks Limited](https://www.e2enetworks.com/) is an NSE-listed, infrastructure company headquartered in India. They integrated OpenFaaS more directly, and provide an additional tab alongside other services such as Computer, Kubernetes, & VPCs in their cloud dashboard. Users can supply code using predefined OpenFaaS templates, invoke their functions, and monitor the results in one place. E2E Networks also supply a CLI and GitHub Actions integration.
31
+
[E2E Networks Limited](https://www.e2enetworks.com/) is an NSE-listed, infrastructure company headquartered in India. They turned to OpenFaaS to offer customers a Function As A Service (FaaS) offering alongside their more traditional VPC, Compute, Object Storage, and Kubernetes services, with a goal to help customers migrate from existing cloud vendors. Users can supply code using predefined OpenFaaS templates, invoke their functions, and monitor the results in one place. E2E Networks also supply a CLI and GitHub Actions integration.
32
32
33
33
See also: [E2E networks: Function as a Service (FaaS)](https://docs.e2enetworks.com/faas_doc/faas.html)
34
34
35
35
**Contents**
36
36
37
37
* A quick recap on function templates
38
-
* Complete example with Go SDK
38
+
* Complete example with Go SDK and other considerations
39
+
* How to create a tenant namespace and deploy the function
39
40
* How to build multi-arch images
40
41
* How to pass build-arguments
41
42
* Conclusion
42
43
43
44
## A quick recap on function templates
44
45
45
-
OpenFaaS is a serverless platform for building, deploying, [monitoring](https://docs.openfaas.com/openfaas-pro/grafana-dashboards/), [triggering](https://docs.openfaas.com/reference/triggers/) and [scaling](https://docs.openfaas.com/architecture/autoscaling/) functions on Kubernetes. The logical unit is the Open Container Initiative (OCI) image which contains the functioncode and dependencies. The start-up process is generally the optional [watchdog](https://docs.openfaas.com/architecture/watchdog/) which is responsible for starting the process that handles HTTP requests from the OpenFaaS API. Existing code or containers can also be supplied so long as it conforms to the workload contract, and exposes HTTP traffic along with a readiness endpoint.
46
+
OpenFaaS is a serverless platform for building, deploying, [monitoring](https://docs.openfaas.com/openfaas-pro/grafana-dashboards/), [triggering](https://docs.openfaas.com/reference/triggers/), and [scaling](https://docs.openfaas.com/architecture/autoscaling/) functions on Kubernetes. The logical unit is the Open Container Initiative (OCI) image which contains the Operating System (OS), function's source code and all its dependencies. The start-up process is generally the (optional)[watchdog](https://docs.openfaas.com/architecture/watchdog/) which is responsible for starting the process that handles HTTP requests from the OpenFaaS API. Existing code or containers can also be supplied so long as they conform to the OpenFaaS workload contract, and expose HTTP traffic on port 8080.
46
47
47
-
The purpose of a template is to make starting a new function easy, without having to think about Dockerfiles, HTTP frameworks, or dependency management. Templates are stored in Git, and the built-in tooling in `faas-cli` can fetch them and use them to scaffold a new function. Templates can be written for any language, so long as they can execute on Linux, expose a HTTP server, and can be built into a container image using a Dockerfile.
48
+
The purpose of a template is to make creating a new function easy, without having to think about boilerplate code like Dockerfiles, HTTP frameworks, or dependency management. The official templates are fetched from Git repositories, and the built-in tooling in `faas-cli` can download them to scaffold new functions. Templates can be written for any language, so long as they target Linux, expose a HTTP server, and can be built into a container image using a Dockerfile.
48
49
49
-
For the [golang-middleware](https://github.com/openfaas/golang-http-template) template, whilst users only need to work with a `go.mod` and `handler.go` file, if you look at the Git repository, you'll see the code that starts the process, along with the Dockerfile etc:
50
+
Let's take the [golang-middleware](https://github.com/openfaas/golang-http-template) template as an example.
51
+
52
+
This is how the CLI can be used to pull the template and scaffold a new function, notice how the user only needs to edit the `handler.go` file, and the `go.mod` file if they need to add dependencies.
53
+
54
+
```bash
55
+
$ faas-cli template store pull golang-middleware
56
+
57
+
$ faas-cli new --lang golang-middleware hello-world-go
58
+
59
+
$ ls
60
+
hello-world-go.yaml
61
+
hello-world-go/
62
+
hello-world-go/handler.go
63
+
hello-world-go/go.mod
64
+
```
65
+
66
+
If you look at the Git repository, or the local `template` folder, you'll see all the additional files which are kept hidden from the user. This includes main.go that starts the process and HTTP server, implements a graceful shutdown, etc, and the multi-arch Dockerfile:
As a general rule, users will edit the files in the `function` directory, and template authors write and maintain the files outside of that directory.
81
+
In order to reduce the cognitive load, and repetition between functions, code for the user is kept in the `function` directory, and template authors write and maintain the files outside of that directory.
65
82
66
83
Every time a build is performed, the CLI will create a folder such as `build/hello-world`, then it writes the base content of the template into that directory and then copies the user's code into the `function` directory. The `build/hello-world` directory is then used as the build context for the container build.
0 commit comments