-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_container.go
156 lines (123 loc) · 4.08 KB
/
cache_container.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
package volumes
import (
"context"
"fmt"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
docker_helpers "gitlab.com/gitlab-org/gitlab-runner/helpers/docker"
)
type containerClient interface {
docker_helpers.Client
LabelContainer(container *container.Config, containerType string, otherLabels ...string)
WaitForContainer(id string) error
RemoveContainer(ctx context.Context, id string) error
}
type CacheContainersManager interface {
FindOrCleanExisting(containerName string, containerPath string) string
Create(containerName string, containerPath string) (string, error)
Cleanup(ctx context.Context, ids []string) chan bool
}
type cacheContainerManager struct {
ctx context.Context
logger debugLogger
containerClient containerClient
helperImage *types.ImageInspect
failedContainerIDs []string
}
func NewCacheContainerManager(ctx context.Context, logger debugLogger, cClient containerClient, helperImage *types.ImageInspect) CacheContainersManager {
return &cacheContainerManager{
ctx: ctx,
logger: logger,
containerClient: cClient,
helperImage: helperImage,
}
}
func (m *cacheContainerManager) FindOrCleanExisting(containerName string, containerPath string) string {
inspected, err := m.containerClient.ContainerInspect(m.ctx, containerName)
if err != nil {
m.logger.Debugln(fmt.Sprintf("Error while inspecting %q container: %v", containerName, err))
return ""
}
// check if we have valid cache, if not remove the broken container
_, ok := inspected.Config.Volumes[containerPath]
if !ok {
m.logger.Debugln(fmt.Sprintf("Removing broken cache container for %q path", containerPath))
err = m.containerClient.RemoveContainer(m.ctx, inspected.ID)
m.logger.Debugln(fmt.Sprintf("Cache container for %q path removed with %v", containerPath, err))
return ""
}
return inspected.ID
}
func (m *cacheContainerManager) Create(containerName string, containerPath string) (string, error) {
containerID, err := m.createCacheContainer(containerName, containerPath)
if err != nil {
return "", err
}
err = m.startCacheContainer(containerID)
if err != nil {
return "", err
}
return containerID, nil
}
func (m *cacheContainerManager) createCacheContainer(containerName string, containerPath string) (string, error) {
config := &container.Config{
Image: m.helperImage.ID,
Cmd: []string{"gitlab-runner-helper", "cache-init", containerPath},
Volumes: map[string]struct{}{
containerPath: {},
},
}
m.containerClient.LabelContainer(config, "cache", "cache.dir="+containerPath)
hostConfig := &container.HostConfig{
LogConfig: container.LogConfig{
Type: "json-file",
},
}
resp, err := m.containerClient.ContainerCreate(m.ctx, config, hostConfig, nil, containerName)
if err != nil {
if resp.ID != "" {
m.failedContainerIDs = append(m.failedContainerIDs, resp.ID)
}
return "", err
}
return resp.ID, nil
}
func (m *cacheContainerManager) startCacheContainer(containerID string) error {
m.logger.Debugln(fmt.Sprintf("Starting cache container %q...", containerID))
err := m.containerClient.ContainerStart(m.ctx, containerID, types.ContainerStartOptions{})
if err != nil {
m.failedContainerIDs = append(m.failedContainerIDs, containerID)
return err
}
m.logger.Debugln(fmt.Sprintf("Waiting for cache container %q...", containerID))
err = m.containerClient.WaitForContainer(containerID)
if err != nil {
m.failedContainerIDs = append(m.failedContainerIDs, containerID)
return err
}
return nil
}
func (m *cacheContainerManager) Cleanup(ctx context.Context, ids []string) chan bool {
done := make(chan bool, 1)
ids = append(m.failedContainerIDs, ids...)
go func() {
wg := new(sync.WaitGroup)
wg.Add(len(ids))
for _, id := range ids {
m.remove(ctx, wg, id)
}
wg.Wait()
done <- true
}()
return done
}
func (m *cacheContainerManager) remove(ctx context.Context, wg *sync.WaitGroup, id string) {
go func() {
err := m.containerClient.RemoveContainer(ctx, id)
if err != nil {
m.logger.Debugln(fmt.Sprintf("Error while removing the container: %v", err))
}
wg.Done()
}()
}