-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
282 lines (241 loc) · 6.75 KB
/
server.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Package server implements the build server command
package server
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/grafana/k6build"
"github.com/grafana/k6build/pkg/builder"
"github.com/grafana/k6build/pkg/catalog"
"github.com/grafana/k6build/pkg/httpserver"
"github.com/grafana/k6build/pkg/server"
"github.com/grafana/k6build/pkg/store"
"github.com/grafana/k6build/pkg/store/client"
"github.com/grafana/k6build/pkg/store/s3"
"github.com/grafana/k6build/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"
)
const (
long = `
Starts a k6build server
API
---
The server exposes an API for building custom k6 binaries.
The API returns the metadata of the custom binary, including an URL for downloading it,
but does not return the binary itself.
For example
curl http://localhost:8000/build -d \
'{
"k6":"v0.50.0",
"dependencies":[
{
"name":"k6/x/kubernetes",
"constraints":">v0.8.0"
}
],
"platform":"linux/amd64"
}' | jq .
{
"artifact": {
"id": "5a241ba6ff643075caadbd06d5a326e5e74f6f10",
"url": "http://localhost:9000/store/5a241ba6ff643075caadbd06d5a326e5e74f6f10/download",
"dependencies": {
"k6": "v0.50.0",
"k6/x/kubernetes": "v0.10.0"
},
"platform": "linux/amd64",
"checksum": "bfdf51ec9279e6d7f91df0a342d0c90ab4990ff1fb0215938505a6894edaf913"
}
}
Note: The build server disables CGO by default but enables it when a dependency requires it.
use --enable-cgo=true to enable CGO support by default.
Metrics
--------
The server exposes prometheus metrics at /metrics
Liveness Probe
--------------
The server exposes a liveness check at /alive
`
example = `
# start the build server using a custom local catalog
k6build server -c /path/to/catalog.json
# start the build server using a custom GOPROXY
k6build server -e GOPROXY=http://localhost:80
# start the build server with a localstack s3 storage backend
# aws credentials are expected in the default location (e.g. env variables)
export AWS_ACCESS_KEY_ID="test"
export AWS_SECRET_ACCESS_KEY="test"
k6build server --s3-endpoint http://localhost:4566 --store-bucket k6build
`
)
type serverConfig struct {
allowBuildSemvers bool
catalogURL string
copyGoEnv bool
enableCgo bool
goEnv map[string]string
port int
s3Bucket string
s3Endpoint string
s3Region string
storeURL string
verbose bool
shutdownTimeout time.Duration
}
// New creates new cobra command for the server command.
func New() *cobra.Command { //nolint:funlen
var (
cfg = serverConfig{}
logLevel string
)
cmd := &cobra.Command{
Use: "server",
Short: "k6 build service",
Long: long,
Example: example,
// prevent the usage help to printed to stderr when an error is reported by a subcommand
SilenceUsage: true,
// this is needed to prevent cobra to print errors reported by subcommands in the stderr
SilenceErrors: true,
RunE: func(cmd *cobra.Command, _ []string) error {
log, err := getLogger(logLevel)
if err != nil {
return err
}
if cfg.enableCgo {
log.Warn("CGO is enabled by default. Use --enable-cgo=false to disable it.")
}
buildSrv, err := cfg.getBuildService(cmd.Context())
if err != nil {
return err
}
apiConfig := server.APIServerConfig{
BuildService: buildSrv,
Log: log,
}
buildAPI := server.NewAPIServer(apiConfig)
srvConfig := httpserver.ServerConfig{
Logger: log,
Port: cfg.port,
EnableMetrics: true,
LivenessProbe: true,
ReadHeaderTimeout: 5 * time.Second,
}
srv := httpserver.NewServer(srvConfig)
srv.Handle("/build", buildAPI)
err = srv.Start(cmd.Context())
if err != nil {
return fmt.Errorf("error serving requests %w", err)
}
return nil
},
}
cmd.Flags().StringVarP(
&cfg.catalogURL,
"catalog",
"c",
catalog.DefaultCatalogURL,
"dependencies catalog. Can be path to a local file or an URL.",
)
cmd.Flags().StringVar(
&cfg.storeURL,
"store-url",
"http://localhost:9000",
"store server url",
)
cmd.Flags().StringVar(&cfg.s3Bucket, "store-bucket", "", "s3 bucket for storing binaries")
cmd.Flags().StringVar(&cfg.s3Endpoint, "s3-endpoint", "", "s3 endpoint")
cmd.Flags().StringVar(&cfg.s3Region, "s3-region", "", "aws region")
cmd.Flags().BoolVarP(&cfg.verbose, "verbose", "v", false, "print build process output")
cmd.Flags().BoolVarP(&cfg.copyGoEnv, "copy-go-env", "g", true, "copy go environment")
cmd.Flags().StringToStringVarP(&cfg.goEnv, "env", "e", nil, "build environment variables")
cmd.Flags().IntVarP(&cfg.port, "port", "p", 8000, "port server will listen")
cmd.Flags().StringVarP(&logLevel, "log-level", "l", "INFO", "log level")
cmd.Flags().BoolVar(&cfg.enableCgo, "enable-cgo", false, "enable CGO for building binaries.")
cmd.Flags().BoolVar(
&cfg.allowBuildSemvers,
"allow-build-semvers",
false,
"allow building versions with build metadata (e.g v0.0.0+build).",
)
cmd.Flags().DurationVar(
&cfg.shutdownTimeout,
"shutdown-timeout",
10*time.Second,
"maximum time to wait for graceful shutdown",
)
return cmd
}
func getLogger(logLevel string) (*slog.Logger, error) {
ll, err := util.ParseLogLevel(logLevel)
if err != nil {
return nil, fmt.Errorf("parsing log level %w", err)
}
return slog.New(
slog.NewTextHandler(
os.Stderr,
&slog.HandlerOptions{
Level: ll,
},
),
), nil
}
func (cfg serverConfig) getBuildService(ctx context.Context) (k6build.BuildService, error) {
store, err := cfg.getStore() //nolint:contextcheck
if err != nil {
return nil, err
}
if cfg.goEnv == nil {
cfg.goEnv = make(map[string]string)
}
cgoEnabled := "0"
if cfg.enableCgo {
cgoEnabled = "1"
}
cfg.goEnv["CGO_ENABLED"] = cgoEnabled
config := builder.Config{
Opts: builder.Opts{
GoOpts: builder.GoOpts{
Env: cfg.goEnv,
CopyGoEnv: cfg.copyGoEnv,
},
Verbose: cfg.verbose,
AllowBuildSemvers: cfg.allowBuildSemvers,
},
Catalog: cfg.catalogURL,
Store: store,
Registerer: prometheus.DefaultRegisterer,
}
builder, err := builder.New(ctx, config)
if err != nil {
return nil, fmt.Errorf("creating local build service %w", err)
}
return builder, nil
}
func (cfg serverConfig) getStore() (store.ObjectStore, error) {
var (
err error
store store.ObjectStore
)
if cfg.s3Bucket != "" {
store, err = s3.New(s3.Config{
Bucket: cfg.s3Bucket,
Endpoint: cfg.s3Endpoint,
Region: cfg.s3Region,
})
if err != nil {
return nil, fmt.Errorf("creating s3 store %w", err)
}
} else {
store, err = client.NewStoreClient(client.StoreClientConfig{
Server: cfg.storeURL,
})
if err != nil {
return nil, fmt.Errorf("creating store %w", err)
}
}
return store, nil
}