-
Notifications
You must be signed in to change notification settings - Fork 61
/
up.go
529 lines (444 loc) · 15.1 KB
/
up.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package up
import (
"context"
"fmt"
"os"
"slices"
"strconv"
"strings"
"time"
"github.com/MakeNowJust/heredoc"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
kraftcloud "sdk.kraft.cloud"
kcclient "sdk.kraft.cloud/client"
kcinstances "sdk.kraft.cloud/instances"
kcservices "sdk.kraft.cloud/services"
kcvolumes "sdk.kraft.cloud/volumes"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/compose"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/compose/build"
"kraftkit.sh/internal/cli/kraft/cloud/instance/create"
"kraftkit.sh/internal/cli/kraft/cloud/instance/logs"
"kraftkit.sh/internal/cli/kraft/cloud/instance/start"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/log"
"kraftkit.sh/packmanager"
)
type UpOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Composefile string `noattribute:"true"`
Detach bool `local:"true" long:"detach" short:"d" usage:"Run the services in the background"`
Metro string `noattribute:"true"`
NoStart bool `noattribute:"true"`
Project *compose.Project `noattribute:"true"`
Token string `noattribute:"true"`
Wait time.Duration `local:"true" long:"wait" short:"w" usage:"Timeout to wait for the instance to start (ms/s/m/h)"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&UpOptions{}, cobra.Command{
Short: "Deploy services in a compose project to KraftCloud",
Use: "up [FLAGS] [COMPONENT]",
Args: cobra.ArbitraryArgs,
Aliases: []string{"u"},
Long: heredoc.Docf(`
Deploy services in a compose project to KraftCloud
Use an existing %[1]sComposefile%[1]s or %[1]sdocker-compose.yaml%[1]s file to start a
number of services as instances on KraftCloud.
Note that this is an experimental command and not all attributes of the
%[1]sComposefile%[1]s are supported nor are all flags identical.
`, "`"),
Example: heredoc.Doc(`
# Start a KraftCloud deployment fully.
$ kraft cloud compose up
# Start a KraftCloud deployment with two specific components.
$ kraft cloud compose up nginx component
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "kraftcloud-compose",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *UpOptions) Pre(cmd *cobra.Command, args []string) error {
err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token)
if err != nil {
return fmt.Errorf("could not populate metro and token: %w", err)
}
ctx, err := packmanager.WithDefaultUmbrellaManagerInContext(cmd.Context())
if err != nil {
return err
}
cmd.SetContext(ctx)
if cmd.Flag("file").Changed {
opts.Composefile = cmd.Flag("file").Value.String()
}
return nil
}
func (opts *UpOptions) Run(ctx context.Context, args []string) error {
return Up(ctx, opts, args...)
}
func Up(ctx context.Context, opts *UpOptions, args ...string) error {
var err error
if opts.Auth == nil {
opts.Auth, err = config.GetKraftCloudAuthConfig(ctx, opts.Token)
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
}
}
if opts.Client == nil {
opts.Client = kraftcloud.NewClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*opts.Auth)),
)
}
if opts.Project == nil {
workdir, err := os.Getwd()
if err != nil {
return err
}
opts.Project, err = compose.NewProjectFromComposeFile(ctx, workdir, opts.Composefile)
if err != nil {
return err
}
}
if err := opts.Project.Validate(ctx); err != nil {
return err
}
// If no services are specified, start all services.
if len(args) == 0 {
for service := range opts.Project.Services {
args = append(args, service)
}
}
// Build all services if the build flag is set.
if err := build.Build(ctx, &build.BuildOptions{
Auth: opts.Auth,
Composefile: opts.Composefile,
Metro: opts.Metro,
Project: opts.Project,
Token: opts.Token,
Push: true,
}, args...); err != nil {
return err
}
// Preemptively create all the service groups from the compose project's
// supplied networks.
svcResps, err := createServiceGroupsFromNetworks(ctx, opts, args...)
if err != nil {
return err
}
volResps, err := createVolumes(ctx, opts)
if err != nil {
return err
}
instResps := kcclient.ServiceResponse[kcinstances.GetResponseItem]{}
for _, serviceName := range args {
service, ok := opts.Project.Services[serviceName]
if !ok {
return fmt.Errorf("service '%s' not found", serviceName)
}
instResp, err := opts.Client.Instances().WithMetro(opts.Metro).Get(ctx, service.Name)
if err == nil && len(instResp.Data.Entries) == 1 && instResp.Data.Entries[0].Error == nil {
instResps.Data.Entries = append(instResps.Data.Entries, instResp.Data.Entries...)
log.G(ctx).WithField("name", service.Name).Info("service already exists")
continue
}
// There is only 1 network per service, so we can safely iterate over the
// networks and break after the first iteration.
var network string
for n := range service.Networks {
network = n
break
}
// Handle environmental variables.
var env []string
for k, v := range service.Environment {
if *v == "" {
env = append(env, fmt.Sprintf("%s=%s", k, os.Getenv(k)))
} else {
env = append(env, fmt.Sprintf("%s=%s", k, *v))
}
}
// Handle the memory limit and reservation. Since these two concepts do not
// currently exist via the KraftCloud API, pick the limit if it is set as it
// represents the maximum value, otherwise check if the reservation has been
// set.
var memory int
if service.MemLimit > 0 {
memory = int(service.MemLimit)
} else if service.MemReservation > 0 {
memory = int(service.MemReservation)
}
if service.Image == "" {
user := strings.TrimSuffix(strings.TrimPrefix(opts.Auth.User, "robot$"), ".users.kraftcloud")
service.Image = fmt.Sprintf(
"index.unikraft.io/%s/%s:latest",
user,
strings.ReplaceAll(service.Name, "_", "-"),
)
}
log.G(ctx).WithField("image", service.Image).Info("deploying")
var serviceGroup string
if data, ok := svcResps[network]; ok {
serviceGroup = data.Data.Entries[0].UUID
}
var volumes []string
for _, volume := range service.Volumes {
vol, ok := volResps[volume.Source]
if !ok {
continue
}
volumes = append(volumes, fmt.Sprintf("%s:%s", vol.Data.Entries[0].UUID, volume.Target))
}
name := service.Name
if cname := service.ContainerName; len(cname) > 0 {
name = cname
}
instResp, _, err = create.Create(ctx, &create.CreateOptions{
Auth: opts.Auth,
Client: opts.Client,
Env: env,
Image: service.Image,
Memory: uint(memory),
Metro: opts.Metro,
Name: name,
ServiceGroupNameOrUUID: serviceGroup,
Start: false,
Token: opts.Token,
WaitForImage: true,
Volumes: volumes,
}, service.Command...)
if err != nil {
return err
}
instResps.Data.Entries = append(instResps.Data.Entries, instResp.Data.Entries...)
}
var instances []string
for _, inst := range instResps.Data.Entries {
instances = append(instances, inst.Name)
}
if !opts.NoStart {
// Start the instances together, separate from the previous create
// invocation.
if err := start.Start(ctx, &start.StartOptions{
Auth: opts.Auth,
Client: opts.Client,
Metro: opts.Metro,
Token: opts.Token,
Wait: opts.Wait,
}, instances...); err != nil {
return fmt.Errorf("starting instances: %w", err)
}
}
if opts.Detach {
return utils.PrintInstances(ctx, "table", instResps)
}
return logs.Logs(ctx, &logs.LogOptions{
Auth: opts.Auth,
Client: opts.Client,
Metro: opts.Metro,
Follow: true,
Tail: -1,
}, instances...)
}
// createVolumes is used to create volumes for each service in the compose
// project. These volumes are used to persist data across instances.
func createVolumes(ctx context.Context, opts *UpOptions) (map[string]*kcclient.ServiceResponse[kcvolumes.GetResponseItem], error) {
volResps := make(map[string]*kcclient.ServiceResponse[kcvolumes.GetResponseItem])
for alias, volume := range opts.Project.Volumes {
name := strings.ReplaceAll(volume.Name, "_", "-")
volResp, err := opts.Client.Volumes().WithMetro(opts.Metro).Get(ctx, name)
if err != nil {
return nil, fmt.Errorf("getting volume: %w", err)
}
vol, err := volResp.FirstOrErr()
if err != nil && vol != nil && *vol.Error == kcclient.APIHTTPErrorNotFound {
log.G(ctx).WithField("name", name).Info("creating volume")
size := 64
if sentry, ok := volume.DriverOpts["size"]; ok {
parsed, err := humanize.ParseBytes(sentry)
if err != nil {
return nil, fmt.Errorf("parsing volume size: %w", err)
}
size = int(parsed) / 1024 / 1024
}
createResp, err := opts.Client.Volumes().WithMetro(opts.Metro).Create(ctx, name, size)
if err != nil {
return nil, fmt.Errorf("creating volume: %w", err)
}
vol, err := createResp.FirstOrErr()
if err != nil {
return nil, err
}
getResp, err := opts.Client.Volumes().WithMetro(opts.Metro).Get(ctx, vol.UUID)
if err != nil {
return nil, fmt.Errorf("creating volume: %w", err)
}
volResps[alias] = getResp
} else if err != nil {
return nil, err
} else {
log.G(ctx).Warnf("volume '%s' already exists as '%s'", volume.Name, name)
volResps[alias] = volResp
}
}
return volResps, nil
}
// createServiceGroupsFromNetworks is used to map each compose service's
// networks to a service group. Since it is not possible to attach an instance
// to multiple service groups it also acts a checking mechanism to determine if
// the compose project's networks are valid with respect to the capabilities of
// the KraftCloud API.
func createServiceGroupsFromNetworks(ctx context.Context, opts *UpOptions, args ...string) (map[string]*kcclient.ServiceResponse[kcservices.GetResponseItem], error) {
svcResps := make(map[string]*kcclient.ServiceResponse[kcservices.GetResponseItem])
// First, create a map of networks which are used by individual services.
// These represent service groups and we'll create them only if they don't
// exist and use the port mapping of the individual service to hydrate the
// create request for the service group.
svcReqs := make(map[string]kcservices.CreateRequest)
for alias, service := range opts.Project.Services {
if !slices.Contains(args, alias) {
continue
}
if len(service.Networks) > 1 {
return nil, fmt.Errorf("service '%s' has more than one network attached which is not supported", alias)
}
for network := range service.Networks {
// Skip the default network, in the situation where a possible alternative
// network can be supplied, we prefer this. Only later if we detect that
// there are no networks associated with any of the services, we'll create
// a service group with the default network.
if network == "default" && len(service.Networks) > 1 {
continue
}
if _, ok := svcReqs[network]; ok {
continue
}
svcReqs[network] = kcservices.CreateRequest{}
}
}
// If none of the compose services are associated with a network, create a new
// service group for the default network.
if len(svcReqs) == 0 {
svcReqs["default"] = kcservices.CreateRequest{}
}
// Check each network to determine whether it exists as a service group.
for alias, network := range opts.Project.Networks {
service, ok := svcReqs[alias]
if !ok {
continue
}
service.Name = ptr(strings.ReplaceAll(network.Name, "_", "-"))
svcReqs[alias] = service
respSvc, err := opts.Client.Services().WithMetro(opts.Metro).Get(ctx, *service.Name)
if err != nil {
return nil, err
}
svc, err := respSvc.FirstOrErr()
if err != nil && *svc.Error == kcclient.APIHTTPErrorNotFound {
continue
} else if err != nil {
return nil, err
}
log.G(ctx).Warnf("network '%s' already exists as a service group '%s'", network.Name, *service.Name)
delete(svcReqs, alias)
svcResps[alias] = respSvc
}
// Iterate through each service and grab the associated port mappings. This
// will be used later to hydrate the service group create request.
for alias, service := range opts.Project.Services {
if !slices.Contains(args, alias) {
continue
}
var services []kcservices.CreateRequestService
for _, port := range service.Ports {
if port.Protocol != "" && port.Protocol != "tls" && port.Protocol != "tcp" {
return nil, fmt.Errorf("protocol '%s' is not supported", port.Protocol)
}
if port.Published == "443" {
services = append(services,
kcservices.CreateRequestService{
Port: 443,
DestinationPort: ptr(int(port.Target)),
Handlers: []kcservices.Handler{
kcservices.HandlerHTTP,
kcservices.HandlerTLS,
},
},
kcservices.CreateRequestService{
Port: 80,
DestinationPort: ptr(int(443)),
Handlers: []kcservices.Handler{
kcservices.HandlerHTTP,
kcservices.HandlerRedirect,
},
},
)
} else {
published, err := strconv.Atoi(port.Published)
if err != nil {
return nil, fmt.Errorf("invalid external port: %w", err)
}
services = append(services,
kcservices.CreateRequestService{
Port: published,
DestinationPort: ptr(int(port.Target)),
Handlers: []kcservices.Handler{
kcservices.HandlerTLS,
},
},
)
}
}
// Expose all ports (services) to networks (service groups).
for alias, req := range svcReqs {
req.Services = append(req.Services, services...)
if len(service.DomainName) > 0 {
// If the domain contains a period, it is a fully qualified domain name,
// which means we should append a period to the end of the domain name
// to ensure it is a valid domain name for the KraftCloud API.
if strings.Contains(service.DomainName, ".") {
service.DomainName += "."
}
req.Domains = []kcservices.CreateRequestDomain{{
Name: service.DomainName,
}}
}
svcReqs[alias] = req
}
}
// Create all service groups.
for alias, req := range svcReqs {
if len(req.Services) == 0 {
log.G(ctx).
WithField("network", alias).
Warn("no exposed ports: skipping service group creation")
continue
}
log.G(ctx).WithField("network", *req.Name).Info("creating service group")
createResp, err := opts.Client.Services().WithMetro(opts.Metro).Create(ctx, req)
if err != nil {
return nil, fmt.Errorf("creating service group: %w", err)
}
svc, err := createResp.FirstOrErr()
if err != nil {
return nil, err
}
getResp, err := opts.Client.Services().WithMetro(opts.Metro).Get(ctx, svc.UUID)
if err != nil {
return nil, fmt.Errorf("creating service group: %w", err)
}
svcResps[alias] = getResp
}
return svcResps, nil
}
func ptr[T comparable](v T) *T { return &v }