-
Notifications
You must be signed in to change notification settings - Fork 62
/
start.go
293 lines (248 loc) · 7.51 KB
/
start.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
// 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 start
import (
"context"
"errors"
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
machineapi "kraftkit.sh/api/machine/v1alpha1"
networkapi "kraftkit.sh/api/network/v1alpha1"
volumeapi "kraftkit.sh/api/volume/v1alpha1"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/cli/kraft/logs"
"kraftkit.sh/internal/cli/kraft/utils"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/machine/network"
mplatform "kraftkit.sh/machine/platform"
"kraftkit.sh/machine/volume"
)
type StartOptions struct {
All bool `long:"all" usage:"Start all machines"`
Detach bool `long:"detach" short:"d" usage:"Run in background"`
NoPrefix bool `long:"no-prefix" usage:"When starting multiple machines, do not prefix each log line with the name"`
Platform string `noattribute:"true"`
Remove bool `long:"rm" usage:"Automatically remove the unikernel when it shutsdown"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&StartOptions{}, cobra.Command{
Short: "Start one or more machines",
Use: "start [FLAGS] MACHINE [MACHINE [...]]",
Aliases: []string{},
Long: "Start one or more machines",
Example: heredoc.Doc(`
# Start a machine
$ kraft start my-machine
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "run",
},
})
if err != nil {
panic(err)
}
cmd.Flags().VarP(
cmdfactory.NewEnumFlag[mplatform.Platform](
mplatform.Platforms(),
mplatform.Platform("auto"),
),
"plat",
"p",
"Set the platform virtual machine monitor driver. Set to 'auto' to detect the guest's platform and 'host' to use the host platform.",
)
return cmd
}
func (opts *StartOptions) Pre(cmd *cobra.Command, args []string) error {
if len(args) == 0 && !opts.All {
return fmt.Errorf("please supply a machine ID or name or use the --all flag")
}
opts.Platform = cmd.Flag("plat").Value.String()
return nil
}
func (opts *StartOptions) Run(ctx context.Context, args []string) error {
return Start(ctx, opts, args...)
}
// Start a set of machines by name.
func Start(ctx context.Context, opts *StartOptions, machineNames ...string) error {
var err error
platform := mplatform.PlatformUnknown
var machineController machineapi.MachineService
if opts.Platform == "auto" {
machineController, err = mplatform.NewMachineV1alpha1ServiceIterator(ctx)
} else {
if opts.Platform == "host" {
platform, _, err = mplatform.Detect(ctx)
if err != nil {
return err
}
} else {
var ok bool
platform, ok = mplatform.PlatformsByName()[opts.Platform]
if !ok {
return fmt.Errorf("unknown platform driver: %s", opts.Platform)
}
}
strategy, ok := mplatform.Strategies()[platform]
if !ok {
return fmt.Errorf("unsupported platform driver: %s (contributions welcome!)", platform.String())
}
machineController, err = strategy.NewMachineV1alpha1(ctx)
}
if err != nil {
return err
}
var machines []machineapi.Machine
if opts.All {
knownMachines, err := machineController.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}
machines = knownMachines.Items
} else {
for _, name := range machineNames {
machine, err := machineController.Get(ctx, &machineapi.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
})
if err != nil {
return err
}
machines = append(machines, *machine)
}
}
var errGroup []error
loggedMachines := []string{}
volumeController, err := volume.NewVolumeV1alpha1ServiceIterator(ctx)
if err != nil {
return fmt.Errorf("instantiating volume service controller iterator: %w", err)
}
for _, machine := range machines {
machine := machine // Go closures
// Check if the machine's requested ports are not already in use by an
// existing running machine.
if err := utils.CheckPorts(ctx, machineController, &machine); err != nil {
return err
}
log.G(ctx).
WithField("machine", machine.Name).
Trace("starting")
if _, err := machineController.Start(ctx, &machine); err != nil {
return err
}
for _, vol := range machine.Spec.Volumes {
vol.Status.State = volumeapi.VolumeStateBound
if _, err := volumeController.Update(ctx, &vol); err != nil {
errGroup = append(errGroup, err)
}
}
if opts.Detach {
// Output the name of the instance such that it can be piped
fmt.Fprintf(iostreams.G(ctx).Out, "%s\n", machine.Name)
continue
}
loggedMachines = append(loggedMachines, machine.Name)
}
if opts.Detach {
return nil
}
logOptions := logs.LogOptions{
Follow: true,
NoPrefix: opts.NoPrefix,
Platform: opts.Platform,
}
if err := logOptions.Run(ctx, loggedMachines); err != nil {
return err
}
for _, machine := range machines {
machine := machine // Go closures
log.G(ctx).
WithField("machine", machine.Name).
Trace("stopping")
if _, err := machineController.Stop(ctx, &machine); err != nil {
log.G(ctx).Errorf("could not stop: %v", err)
}
// Remove the instance on Ctrl+C if the --rm flag is passed
if opts.Remove {
log.G(ctx).
WithField("machine", machine.Name).
Trace("removing")
if _, err := machineController.Delete(ctx, &machine); err != nil {
log.G(ctx).Errorf("could not remove: %v", err)
}
}
}
var networkController networkapi.NetworkService
for _, machine := range machines {
if !opts.Remove || opts.Detach {
continue
}
// Set up a clean up method to remove the interface if the machine exits and
// we are requesting to remove the machine.
if len(machine.Spec.Networks) > 0 {
if networkController == nil {
networkController, err = network.NewNetworkV1alpha1ServiceIterator(ctx)
if err != nil {
return fmt.Errorf("instantiating network service controller iterator: %w", err)
}
}
for _, network := range machine.Spec.Networks {
// Get the latest version of the network.
found, err := networkController.Get(ctx, &networkapi.Network{
ObjectMeta: metav1.ObjectMeta{
Name: network.IfName,
},
})
if err != nil {
return fmt.Errorf("could not get network information for %s: %v", network.IfName, err)
}
// Remove the new network interface
for i, iface := range found.Spec.Interfaces {
if iface.UID == network.Interfaces[0].UID {
ret := make([]networkapi.NetworkInterfaceTemplateSpec, 0)
ret = append(ret, found.Spec.Interfaces[:i]...)
found.Spec.Interfaces = append(ret, found.Spec.Interfaces[i+1:]...)
break
}
}
if _, err = networkController.Update(ctx, found); err != nil {
return fmt.Errorf("could not update network %s: %v", network.IfName, err)
}
}
}
// Also update information about the volumes.
for _, vol := range machine.Spec.Volumes {
allMachines, err := machineController.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}
stillUsed := false
for _, m := range allMachines.Items {
if m.ObjectMeta.UID == machine.ObjectMeta.UID {
continue
}
for _, v := range m.Spec.Volumes {
if v.ObjectMeta.UID == vol.ObjectMeta.UID {
stillUsed = true
break
}
}
if stillUsed {
break
}
}
if !stillUsed {
vol.Status.State = volumeapi.VolumeStatePending
if _, err := volumeController.Update(ctx, &vol); err != nil {
errGroup = append(errGroup, err)
}
}
}
}
return errors.Join(errGroup...)
}