-
Notifications
You must be signed in to change notification settings - Fork 64
/
rm.go
181 lines (152 loc) · 4.66 KB
/
rm.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, 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 rm
import (
"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"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/internal/set"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
"kraftkit.sh/machine/network"
mplatform "kraftkit.sh/machine/platform"
)
type Rm struct {
All bool `long:"all" usage:"Remove all machines"`
platform string
}
func New() *cobra.Command {
cmd, err := cmdfactory.New(&Rm{}, cobra.Command{
Short: "Remove one or more running unikernels",
Use: "rm [FLAGS] MACHINE [MACHINE [...]]",
Args: cobra.MinimumNArgs(0),
Aliases: []string{"remove"},
Long: heredoc.Doc(`
Remove one or more running unikernels`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "run",
},
})
if err != nil {
panic(err)
}
cmd.Flags().VarP(
cmdfactory.NewEnumFlag(set.NewStringSet(mplatform.DriverNames()...).Add("auto").ToSlice(), "auto"),
"plat",
"p",
"Set the platform virtual machine monitor driver.",
)
return cmd
}
func (opts *Rm) Pre(cmd *cobra.Command, _ []string) error {
opts.platform = cmd.Flag("plat").Value.String()
return nil
}
func (opts *Rm) Run(cmd *cobra.Command, args []string) error {
var err error
ctx := cmd.Context()
platform := mplatform.PlatformUnknown
if opts.platform == "" || opts.platform == "auto" {
var mode mplatform.SystemMode
platform, mode, err = mplatform.Detect(ctx)
if mode == mplatform.SystemGuest {
return fmt.Errorf("nested virtualization not supported")
} else if err != nil {
return err
}
} else {
var ok bool
platform, ok = mplatform.Platforms()[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())
}
controller, err := strategy.NewMachineV1alpha1(ctx)
if err != nil {
return err
}
machines, err := controller.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}
var remove []machineapi.Machine
for _, machine := range machines.Items {
if len(args) == 0 && opts.All {
remove = append(remove, machine)
continue
}
if args[0] == machine.Name || args[0] == string(machine.UID) {
remove = append(remove, machine)
}
}
if len(remove) == 0 {
return fmt.Errorf("machine(s) not found")
}
netcontrollers := make(map[string]networkapi.NetworkService, 0)
for _, machine := range remove {
// First remove all the associated network interfaces.
for _, net := range machine.Spec.Networks {
netcontroller, ok := netcontrollers[net.Driver]
// Store the instantiation of the network controller strategy.
if !ok {
strategy, ok := network.Strategies()[net.Driver]
if !ok {
return fmt.Errorf("unknown machine network driver: %s", net.Driver)
}
netcontroller, err = strategy.NewNetworkV1alpha1(ctx)
if err != nil {
return err
}
netcontrollers[net.Driver] = netcontroller
}
// Get the latest version of the network.
found, err := netcontroller.Get(ctx, &networkapi.Network{
ObjectMeta: metav1.ObjectMeta{
Name: net.IfName,
},
})
if err != nil {
log.G(ctx).Warnf("could not get network information for %s: %v", net.IfName, err)
continue
}
for _, machineIface := range net.Interfaces {
// Remove the associated network interfaces
for i, netIface := range found.Spec.Interfaces {
if machineIface.UID == netIface.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 = netcontroller.Update(ctx, found); err != nil {
log.G(ctx).Warnf("could not update network %s: %v", net.IfName, err)
continue
}
}
}
// Stop the machine before deleting it.
if machine.Status.State == machineapi.MachineStateRunning {
if _, err := controller.Stop(ctx, &machine); err != nil {
log.G(ctx).Errorf("could not stop machine %s: %v", machine.Name, err)
}
}
// Now delete the machine.
if _, err := controller.Delete(ctx, &machine); err != nil {
log.G(ctx).Errorf("could not delete machine %s: %v", machine.Name, err)
} else {
fmt.Fprintln(iostreams.G(ctx).Out, machine.Name)
}
}
return nil
}