|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "archive/tar" |
5 | 4 | "bytes"
|
6 |
| - "crypto/sha256" |
7 |
| - "encoding/hex" |
8 |
| - "encoding/json" |
9 | 5 | "flag"
|
10 | 6 | "fmt"
|
11 |
| - "io" |
12 |
| - "io/ioutil" |
13 | 7 | "log"
|
14 | 8 | "net/http"
|
| 9 | + "net/url" |
15 | 10 | "os"
|
16 |
| - "os/exec" |
17 |
| - "path" |
18 |
| - "path/filepath" |
19 | 11 | "strings"
|
20 | 12 |
|
21 |
| - hmac "github.com/alexellis/hmac/v2" |
| 13 | + "github.com/openfaas/go-sdk/builder" |
22 | 14 | )
|
23 | 15 |
|
24 |
| -type buildConfig struct { |
25 |
| - Image string `json:"image"` |
26 |
| - BuildArgs map[string]string `json:"buildArgs,omitempty"` |
27 |
| -} |
28 |
| - |
29 |
| -type buildResult struct { |
30 |
| - Log []string `json:"log"` |
31 |
| - Image string `json:"image"` |
32 |
| - Status string `json:"status"` |
33 |
| -} |
34 |
| - |
35 |
| -const ConfigFileName = "com.openfaas.docker.config" |
36 |
| - |
37 | 16 | var (
|
38 |
| - image string |
39 |
| - handler string |
40 |
| - lang string |
| 17 | + image string |
| 18 | + handler string |
| 19 | + lang string |
| 20 | + functionName string |
| 21 | + platformsStr string |
| 22 | + buildArgsStr string |
41 | 23 | )
|
42 | 24 |
|
43 | 25 | func main() {
|
44 | 26 | flag.StringVar(&image, "image", "", "Docker image name to build")
|
45 | 27 | flag.StringVar(&handler, "handler", "", "Directory with handler for function, e.g. handler.js")
|
46 |
| - flag.StringVar(&lang, "lang", "", "Language or template to use, e.g. node17") |
| 28 | + flag.StringVar(&lang, "lang", "", "Language or template to use, e.g. node20") |
| 29 | + flag.StringVar(&functionName, "name", "", "Name of the function") |
| 30 | + flag.StringVar(&platformsStr, "platforms", "linux/amd64", "Comma separated list of target platforms for multi-arch image builds.") |
| 31 | + flag.StringVar(&buildArgsStr, "build-args", "", "Additional build arguments for the docker build in the form of key1=value1,key2=value2") |
47 | 32 | flag.Parse()
|
48 | 33 |
|
49 |
| - tempDir, err := os.MkdirTemp(os.TempDir(), "builder-*") |
50 |
| - if err != nil { |
51 |
| - log.Fatal(err) |
52 |
| - } |
53 |
| - defer os.RemoveAll(tempDir) |
54 |
| - |
55 |
| - tarPath := path.Join(tempDir, "req.tar") |
56 |
| - fmt.Println(tarPath) |
57 |
| - |
58 |
| - if err := shrinkwrap(image, handler, lang); err != nil { |
59 |
| - log.Fatal(err) |
60 |
| - } |
61 |
| - |
62 |
| - if err := makeTar(buildConfig{Image: image}, path.Join("build", "context"), tarPath); err != nil { |
63 |
| - log.Fatalf("Failed to create tar file: %s", err) |
64 |
| - } |
| 34 | + platforms := strings.Split(platformsStr, ",") |
| 35 | + buildArgs := parseBuildArgs(buildArgsStr) |
65 | 36 |
|
66 |
| - res, err := callBuilder(tarPath) |
| 37 | + // Get the HMAC secret used for payload authentication with the builder API. |
| 38 | + payloadSecret, err := os.ReadFile("payload.txt") |
67 | 39 | if err != nil {
|
68 |
| - log.Fatalf("Failed to call builder API: %s", err) |
69 |
| - } |
70 |
| - defer res.Body.Close() |
71 |
| - |
72 |
| - data, _ := io.ReadAll(res.Body) |
73 |
| - |
74 |
| - result := buildResult{} |
75 |
| - if err := json.Unmarshal(data, &result); err != nil { |
76 |
| - log.Fatalf("Failed to unmarshal build result: %s", err) |
| 40 | + log.Fatal(err) |
77 | 41 | }
|
| 42 | + payloadSecret = bytes.TrimSpace(payloadSecret) |
78 | 43 |
|
79 |
| - if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusAccepted { |
80 |
| - log.Fatalf("Unable to build image %s: %s", image, result.Status) |
81 |
| - } |
| 44 | + // Initialize a new builder client. |
| 45 | + builderURL, _ := url.Parse("http://127.0.0.1:8081") |
| 46 | + b := builder.NewFunctionBuilder(builderURL, http.DefaultClient, builder.WithHmacAuth(string(payloadSecret))) |
82 | 47 |
|
83 |
| - log.Printf("Success building image: %s", result.Image) |
84 |
| -} |
85 |
| - |
86 |
| -func shrinkwrap(image, handler, lang string) error { |
87 |
| - buildCmd := exec.Command( |
88 |
| - "faas-cli", |
89 |
| - "build", |
90 |
| - "--lang", |
91 |
| - lang, |
92 |
| - "--handler", |
93 |
| - handler, |
94 |
| - "--name", |
95 |
| - "context", |
96 |
| - "--image", |
97 |
| - image, |
98 |
| - "--shrinkwrap", |
99 |
| - ) |
100 |
| - |
101 |
| - err := buildCmd.Start() |
102 |
| - if err != nil { |
103 |
| - return fmt.Errorf("cannot start faas-cli build: %t", err) |
104 |
| - } |
105 |
| - |
106 |
| - err = buildCmd.Wait() |
| 48 | + // Create the function build context using the provided function handler and language template. |
| 49 | + buildContext, err := builder.CreateBuildContext(functionName, handler, lang, []string{}) |
107 | 50 | if err != nil {
|
108 |
| - return fmt.Errorf("failed to shrinkwrap handler") |
109 |
| - } |
110 |
| - |
111 |
| - return nil |
112 |
| -} |
113 |
| - |
114 |
| -func makeTar(buildConfig buildConfig, base, tarPath string) error { |
115 |
| - configBytes, _ := json.Marshal(buildConfig) |
116 |
| - if err := ioutil.WriteFile(path.Join(base, ConfigFileName), configBytes, 0664); err != nil { |
117 |
| - return err |
| 51 | + log.Fatalf("failed to create build context: %s", err) |
118 | 52 | }
|
119 | 53 |
|
120 |
| - tarFile, err := os.Create(tarPath) |
| 54 | + // Create a temporary file for the build tar. |
| 55 | + tarFile, err := os.CreateTemp(os.TempDir(), "build-context-*.tar") |
121 | 56 | if err != nil {
|
122 |
| - return err |
| 57 | + log.Fatalf("failed to temporary file: %s", err) |
123 | 58 | }
|
| 59 | + tarFile.Close() |
124 | 60 |
|
125 |
| - tarWriter := tar.NewWriter(tarFile) |
126 |
| - defer tarWriter.Close() |
127 |
| - |
128 |
| - err = filepath.Walk(base, func(path string, f os.FileInfo, pathErr error) error { |
129 |
| - if pathErr != nil { |
130 |
| - return pathErr |
131 |
| - } |
132 |
| - |
133 |
| - targetFile, err := os.Open(path) |
134 |
| - if err != nil { |
135 |
| - return err |
136 |
| - } |
137 |
| - |
138 |
| - header, err := tar.FileInfoHeader(f, f.Name()) |
139 |
| - if err != nil { |
140 |
| - return err |
141 |
| - } |
142 |
| - |
143 |
| - header.Name = strings.TrimPrefix(path, base) |
144 |
| - if header.Name != fmt.Sprintf("/%s", ConfigFileName) { |
145 |
| - header.Name = filepath.Join("context", header.Name) |
146 |
| - } |
147 |
| - |
148 |
| - header.Name = strings.TrimPrefix(header.Name, "/") |
| 61 | + tarPath := tarFile.Name() |
| 62 | + defer os.Remove(tarPath) |
149 | 63 |
|
150 |
| - if err := tarWriter.WriteHeader(header); err != nil { |
151 |
| - return err |
152 |
| - } |
153 |
| - |
154 |
| - if f.Mode().IsDir() { |
155 |
| - return nil |
156 |
| - } |
157 |
| - |
158 |
| - _, err = io.Copy(tarWriter, targetFile) |
159 |
| - return err |
160 |
| - }) |
161 |
| - |
162 |
| - return err |
163 |
| -} |
164 |
| - |
165 |
| -func callBuilder(tarPath string) (*http.Response, error) { |
166 |
| - payloadSecret, err := os.ReadFile("payload.txt") |
167 |
| - if err != nil { |
168 |
| - return nil, err |
| 64 | + // Configuration for the build. |
| 65 | + // Set the image name plus optional build arguments and target platforms for multi-arch images. |
| 66 | + buildConfig := builder.BuildConfig{ |
| 67 | + Image: image, |
| 68 | + Platforms: platforms, |
| 69 | + BuildArgs: buildArgs, |
169 | 70 | }
|
170 | 71 |
|
171 |
| - tarFile, err := os.Open(tarPath) |
172 |
| - if err != nil { |
173 |
| - return nil, err |
| 72 | + // Prepare a tar archive that contains the build config and build context. |
| 73 | + if err := builder.MakeTar(tarPath, buildContext, &buildConfig); err != nil { |
| 74 | + log.Fatal(err) |
174 | 75 | }
|
175 |
| - defer tarFile.Close() |
176 | 76 |
|
177 |
| - tarFileBytes, err := ioutil.ReadAll(tarFile) |
| 77 | + // Invoke the function builder with the tar archive containing the build config and context |
| 78 | + // to build and push the function image. |
| 79 | + result, err := b.Build(tarPath) |
178 | 80 | if err != nil {
|
179 |
| - return nil, err |
| 81 | + log.Fatal(err) |
180 | 82 | }
|
181 | 83 |
|
182 |
| - digest := hmac.Sign(tarFileBytes, bytes.TrimSpace(payloadSecret), sha256.New) |
183 |
| - fmt.Println(hex.EncodeToString(digest)) |
| 84 | + log.Printf("Image: %s built.", result.Image) |
184 | 85 |
|
185 |
| - r, err := http.NewRequest(http.MethodPost, "http://127.0.0.1:8081/build", bytes.NewReader(tarFileBytes)) |
186 |
| - if err != nil { |
187 |
| - return nil, err |
| 86 | + // Print build logs |
| 87 | + log.Println("Build logs:") |
| 88 | + for _, logMsg := range result.Log { |
| 89 | + fmt.Printf("%s\n", logMsg) |
188 | 90 | }
|
| 91 | +} |
189 | 92 |
|
190 |
| - r.Header.Set("X-Build-Signature", "sha256="+hex.EncodeToString(digest)) |
191 |
| - r.Header.Set("Content-Type", "application/octet-stream") |
| 93 | +func parseBuildArgs(str string) map[string]string { |
| 94 | + buildArgs := map[string]string{} |
192 | 95 |
|
193 |
| - res, err := http.DefaultClient.Do(r) |
194 |
| - if err != nil { |
195 |
| - return nil, err |
| 96 | + if str != "" { |
| 97 | + pairs := strings.Split(str, ",") |
| 98 | + for _, pair := range pairs { |
| 99 | + kv := strings.SplitN(pair, "=", 2) |
| 100 | + if len(kv) == 2 { |
| 101 | + buildArgs[kv[0]] = kv[1] |
| 102 | + } |
| 103 | + } |
196 | 104 | }
|
197 | 105 |
|
198 |
| - return res, nil |
| 106 | + return buildArgs |
199 | 107 | }
|
0 commit comments