-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
107 lines (89 loc) · 2.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/openfaas/go-sdk/builder"
)
var (
image string
handler string
lang string
functionName string
platformsStr string
buildArgsStr string
)
func main() {
flag.StringVar(&image, "image", "", "Docker image name to build")
flag.StringVar(&handler, "handler", "", "Directory with handler for function, e.g. handler.js")
flag.StringVar(&lang, "lang", "", "Language or template to use, e.g. node20")
flag.StringVar(&functionName, "name", "", "Name of the function")
flag.StringVar(&platformsStr, "platforms", "linux/amd64", "Comma separated list of target platforms for multi-arch image builds.")
flag.StringVar(&buildArgsStr, "build-args", "", "Additional build arguments for the docker build in the form of key1=value1,key2=value2")
flag.Parse()
platforms := strings.Split(platformsStr, ",")
buildArgs := parseBuildArgs(buildArgsStr)
// Get the HMAC secret used for payload authentication with the builder API.
payloadSecret, err := os.ReadFile("payload.txt")
if err != nil {
log.Fatal(err)
}
payloadSecret = bytes.TrimSpace(payloadSecret)
// Initialize a new builder client.
builderURL, _ := url.Parse("http://127.0.0.1:8081")
b := builder.NewFunctionBuilder(builderURL, http.DefaultClient, builder.WithHmacAuth(string(payloadSecret)))
// Create the function build context using the provided function handler and language template.
buildContext, err := builder.CreateBuildContext(functionName, handler, lang, []string{})
if err != nil {
log.Fatalf("failed to create build context: %s", err)
}
// Create a temporary file for the build tar.
tarFile, err := os.CreateTemp(os.TempDir(), "build-context-*.tar")
if err != nil {
log.Fatalf("failed to temporary file: %s", err)
}
tarFile.Close()
tarPath := tarFile.Name()
defer os.Remove(tarPath)
// Configuration for the build.
// Set the image name plus optional build arguments and target platforms for multi-arch images.
buildConfig := builder.BuildConfig{
Image: image,
Platforms: platforms,
BuildArgs: buildArgs,
}
// Prepare a tar archive that contains the build config and build context.
if err := builder.MakeTar(tarPath, buildContext, &buildConfig); err != nil {
log.Fatal(err)
}
// Invoke the function builder with the tar archive containing the build config and context
// to build and push the function image.
result, err := b.Build(tarPath)
if err != nil {
log.Fatal(err)
}
log.Printf("Image: %s built.", result.Image)
// Print build logs
log.Println("Build logs:")
for _, logMsg := range result.Log {
fmt.Printf("%s\n", logMsg)
}
}
func parseBuildArgs(str string) map[string]string {
buildArgs := map[string]string{}
if str != "" {
pairs := strings.Split(str, ",")
for _, pair := range pairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 {
buildArgs[kv[0]] = kv[1]
}
}
}
return buildArgs
}