-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
321 lines (292 loc) · 7.4 KB
/
main.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
package main
import (
"bytes"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
"os"
"path"
"sort"
"strings"
"time"
"github.com/cego/docker-registry-pruner/registry"
"github.com/fsouza/go-dockerclient"
"github.com/hashicorp/consul/api"
"github.com/myENA/consul-decoder"
"github.com/myENA/drprune/models"
"github.com/rs/zerolog"
)
var log = zerolog.New(os.Stderr).With().Str("app", "drprune").Timestamp().Logger()
func main() {
var registryURL string
var user string
var pass string
var skipVerify bool
var consulPrefix string
var consulDefaultPath string
var skipDeletes bool
var runGC bool
var drContainerName string
var drConfigFilePath string
fs := flag.NewFlagSet("drprune", flag.ExitOnError)
fs.StringVar(
®istryURL,
"docker-registry-url",
os.Getenv("DOCKER_REGISTRY_URL"),
"pass this or DOCKER_REGISTRY_URL env - required",
)
fs.StringVar(
&user,
"docker-registry-user",
os.Getenv("DOCKER_REGISTRY_USER"),
"pass this or DOCKER_REGISTRY_USER env - optional",
)
fs.StringVar(
&pass,
"docker-registry-pass",
os.Getenv("DOCKER_REGISTRY_PASS"),
"pass this or DOCKER_REGISTRY_PASS env - optional",
)
fs.BoolVar(
&skipVerify,
"https-skip-verify",
false,
"ignore https errors - good for self-signed keys",
)
fs.StringVar(
&consulDefaultPath,
"consul-default-path",
"apps/drprune/default",
"path to default config",
)
fs.StringVar(
&consulPrefix,
"consul-prefix",
"apps/drprune/overrides",
"prefix for image based config overrides",
)
fs.BoolVar(
&runGC,
"run-gc",
false,
"run garage collector",
)
fs.StringVar(
&drContainerName,
"docker-registry-container-name-prefix",
"docker-registry",
"docker registry container name prefix",
)
fs.BoolVar(
&skipDeletes,
"skip-deletes",
false,
"this skips finding containers. this can be useful if you just want to trigger gc",
)
fs.StringVar(
&drConfigFilePath,
"docker-registry-config-file-path",
"/etc/docker/registry/config.yml",
"docker registry config file path inside container",
)
_ = fs.Parse(os.Args[1:])
if registryURL == "" {
_, _ = fmt.Fprintln(os.Stderr, "must specify docker registry url")
fs.Usage()
os.Exit(1)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 60 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 1,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
var (
reg *registry.API
)
if skipVerify {
transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
reg = registry.NewAPI(registryURL)
reg.SetHTTPClient(&http.Client{Transport: transport})
reg.SetPageSize(100000)
if !skipDeletes {
cnl, err := api.NewClient(api.DefaultConfig())
if err != nil {
log.Fatal().Err(err).Msg("could not connect to consul")
}
cfg := models.DefaultConfig()
d := &decoder.Decoder{
CaseSensitive: false,
}
kvps, _, err := cnl.KV().List(consulDefaultPath, nil)
fmt.Printf("kvps: %#v\n", kvps)
if err != nil {
log.Error().Err(err).Msgf("default config could not be read from config")
} else {
err = d.Unmarshal(consulDefaultPath, kvps, cfg)
if err != nil {
log.Error().Err(err).Msgf("could not unmarshal into config")
}
}
fmt.Printf("config: %#v\n", cfg)
repos, err := reg.GetRepositories()
if err != nil {
log.Fatal().Err(err).Msg("Failed to list repositories from registry")
}
var tagsToDelete tagsMetaList
for _, repo := range repos {
p := path.Join(consulPrefix, repo)
kvs, _, err := cnl.KV().List(p, nil)
c := cfg
if err != nil {
log.Error().Err(err).Str("path", p).Msg("error fetching consul path config")
} else if kvs != nil {
// only allocate new config if present
c = cfg.Clone()
err = d.Unmarshal(p, kvps, c)
if err != nil {
log.Error().Err(err).Str("path", p).Msg("failed to unmarshal config")
}
fmt.Printf("found config: %#v\n", c)
}
tags, err := reg.GetTags(repo)
if err != nil {
log.Error().Err(err).Str("repo", repo).Msg("error processing repository")
continue
}
var releaseTags tagsMetaList
for _, tag := range tags {
if tag == "latest" {
continue
}
created, err := reg.GetManifestCreated(repo, tag)
if err != nil {
log.Error().Err(err).Str("repo", repo).Str("tag", tag).Msg("could not get manifest created")
continue
}
digest, err := reg.GetDigest(repo, tag)
if err != nil {
log.Error().Err(err).Str("repo", repo).Str("tag", tag).Msg("could not get digest")
continue
}
var isRelease bool
for _, rt := range c.ReleaseTags {
if strings.HasPrefix(tag, rt) {
isRelease = true
break
}
}
if isRelease {
releaseTags = append(releaseTags, tagsMeta{
created: created,
repo: repo,
tag: tag,
digest: digest,
})
} else {
// not special
d := time.Since(created)
if d > time.Duration(int(time.Hour)*24*c.MinFeatureEvictionDays) {
tagsToDelete = append(tagsToDelete, tagsMeta{
created: created,
repo: repo,
tag: tag,
digest: digest,
})
}
}
}
if len(releaseTags) > c.MinReleaseImages {
sort.Sort(sort.Reverse(releaseTags))
for _, rt := range releaseTags[c.MinReleaseImages:] {
d := time.Since(rt.created)
if d > time.Duration(int(time.Hour)*24*c.MinReleaseEvictionDays) {
tagsToDelete = append(tagsToDelete, rt)
}
}
}
}
sort.Sort(tagsToDelete)
l := len(tagsToDelete)
for i, t := range tagsToDelete {
log.Info().
Str("progress", fmt.Sprintf("%d of %d", i+1, l)).
Str("repo", t.repo).Str("tag", t.tag).
Time("created", t.created).
Msg("deleting")
err = reg.DeleteManifest(t.repo, t.digest)
if err != nil {
log.Error().Err(err).Str("repo", t.repo).Str("tag", t.tag).Msg("error deleting manifest")
}
}
}
if runGC {
dc, err := docker.NewClientFromEnv()
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize docker client")
}
containers, err := dc.ListContainers(docker.ListContainersOptions{})
if err != nil {
log.Fatal().Err(err).Msg("could not list containers")
}
for _, c := range containers {
var found bool
for _, n := range c.Names {
if strings.HasPrefix(path.Base(n), drContainerName) {
found = true
break
}
}
if found {
var stdOutBuf bytes.Buffer
var stdErrBuf bytes.Buffer
exec, err := dc.CreateExec(docker.CreateExecOptions{
Cmd: []string{"/bin/registry", "garbage-collect", drConfigFilePath},
Container: c.ID,
AttachStdout: true,
AttachStderr: true,
})
if err != nil {
log.Fatal().Err(err).Msg("failed to CreateExec")
}
err = dc.StartExec(exec.ID, docker.StartExecOptions{
OutputStream: &stdOutBuf,
ErrorStream: &stdErrBuf,
})
if err != nil {
log.Error().Err(err).Msg("error running command")
}
fmt.Printf("stdout: \n%s\n", stdOutBuf.String())
fmt.Printf("stderr: \n%s\n", stdErrBuf.String())
break
}
}
}
}
type tagsMeta struct {
created time.Time
repo string
tag string
digest string
}
type tagsMetaList []tagsMeta
func (t tagsMetaList) Len() int {
return len(t)
}
func (t tagsMetaList) Less(i, j int) bool {
return t[i].created.Before(t[j].created)
}
func (t tagsMetaList) Swap(i, j int) {
t[j], t[i] = t[i], t[j]
}