-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
volumes.go
179 lines (152 loc) · 3.75 KB
/
volumes.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
package inotify
import (
"bytes"
"context"
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/abiosoft/colima/environment/container/containerd"
"github.com/abiosoft/colima/environment/container/docker"
)
func (f *inotifyProcess) monitorContainerVolumes(ctx context.Context, c chan<- []string) error {
log := f.log
if f.runtime == "" {
return fmt.Errorf("empty runtime")
}
fetch := func() ([]string, error) {
var vols []string
// docker
if f.runtime != containerd.Name {
vols, err := f.fetchVolumes(docker.Name)
if err != nil {
return nil, fmt.Errorf("error fetching docker volumes: %w", err)
}
return vols, nil
}
// containerd
var namespaces []string
out, err := f.guest.RunOutput("sudo", "nerdctl", "namespace", "list", "-q")
if err != nil {
return nil, fmt.Errorf("error retrieving containerd namespaces: %w", err)
}
if out != "" {
namespaces = strings.Fields(out)
}
for _, ns := range namespaces {
v, err := f.fetchVolumes("sudo", "nerdctl", "--namespace", ns)
if err != nil {
return nil, fmt.Errorf("error retrieving containerd volumes: %w", err)
}
if len(v) > 0 {
vols = append(vols, v...)
}
}
return vols, nil
}
go func() {
for {
select {
case <-ctx.Done():
log.Trace("stop signal received")
err := ctx.Err()
if err != nil {
log.Trace(fmt.Errorf("error during stop: %w", err))
}
case <-time.After(volumesInterval):
if vols, err := fetch(); err != nil {
log.Error(err)
} else {
c <- vols
}
}
}
}()
return nil
}
func (f *inotifyProcess) fetchVolumes(cmdArgs ...string) ([]string, error) {
log := f.log
// fetch all containers
var containers []string
{
args := append([]string{}, cmdArgs...)
args = append(args, "ps", "-q")
out, err := f.guest.RunOutput(args...)
if err != nil {
return nil, fmt.Errorf("error listing containers: %w", err)
}
containers = strings.Fields(out)
if len(containers) == 0 {
return nil, nil
}
}
log.Tracef("found containers %+v", containers)
// fetch volumes
var resp []struct {
Mounts []struct {
Source string `json:"Source"`
} `json:"Mounts"`
}
{
args := append([]string{}, cmdArgs...)
args = append(args, "inspect")
args = append(args, containers...)
var buf bytes.Buffer
if err := f.guest.RunWith(nil, &buf, args...); err != nil {
return nil, fmt.Errorf("error inspecting containers: %w", err)
}
if err := json.NewDecoder(&buf).Decode(&resp); err != nil {
return nil, fmt.Errorf("error decoding docker response")
}
}
// process and discard redundant volumes
vols := []string{}
{
shouldMount := func(child string) bool {
// ignore all invalid directories.
// i.e. directories not within the mounted VM directories
for _, parent := range f.vmVols {
if strings.HasPrefix(child, parent) {
return true
}
}
return false
}
for _, r := range resp {
for _, mount := range r.Mounts {
if shouldMount(mount.Source) {
vols = append(vols, mount.Source)
}
}
}
vols = omitChildrenDirectories(vols)
log.Tracef("found volumes %+v", vols)
}
return vols, nil
}
func omitChildrenDirectories(dirs []string) []string {
sort.Strings(dirs) // sort to put the parent directories first
// keep track for uniqueness
set := map[string]struct{}{}
var newVols []string
omitted := map[int]struct{}{}
for i := 0; i < len(dirs); i++ {
// if the index is omitted, skip
if _, ok := omitted[i]; ok {
continue
}
parent := dirs[i]
if _, ok := set[parent]; !ok {
newVols = append(newVols, parent)
set[parent] = struct{}{}
}
for j := i + 1; j < len(dirs); j++ {
child := dirs[j]
if strings.HasPrefix(child, strings.TrimSuffix(parent, "/")+"/") {
omitted[j] = struct{}{}
}
}
}
return newVols
}