/
server.go
363 lines (308 loc) · 9.71 KB
/
server.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2019 The GCR Cleaner Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcrcleaner
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strings"
"time"
)
const (
contentTypeHeader = "Content-Type"
contentTypeJSON = "application/json"
)
// Server is a cleaning server.
type Server struct {
cleaner *Cleaner
logger *Logger
}
// NewServer creates a new server for handler functions.
func NewServer(cleaner *Cleaner) (*Server, error) {
if cleaner == nil {
return nil, fmt.Errorf("missing cleaner")
}
return &Server{
cleaner: cleaner,
logger: cleaner.logger,
}, nil
}
// PubSubHandler is an http handler that invokes the cleaner from a pubsub
// request. Unlike an HTTP request, the pubsub endpoint always returns a success
// unless the pubsub message is malformed.
func (s *Server) PubSubHandler(cache Cache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var m pubsubMessage
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
err = fmt.Errorf("failed to decode pubsub message: %w", err)
s.handleError(w, err, 400)
return
}
// PubSub is "at least once" delivery. The cleaner is idempotent, but
// let's try to prevent unnecessary work by not processing messages we've
// already received.
msgID := m.Subscription + "/" + m.Message.ID
if exists := cache.Insert(msgID); exists {
s.logger.Info("already processed message", "id", msgID)
w.WriteHeader(204)
return
}
if len(m.Message.Data) == 0 {
err := fmt.Errorf("missing data in pubsub payload")
s.handleError(w, err, 400)
return
}
// Start a goroutine to delete the images
body := ioutil.NopCloser(bytes.NewReader(m.Message.Data))
go func() {
// Intentionally don't use the request context, since it terminates but
// the background job should still be processing.
ctx := context.Background()
if _, _, err := s.clean(ctx, body); err != nil {
s.logger.Error("failed to clean", "error", err)
}
}()
w.WriteHeader(204)
}
}
// HTTPHandler is an http handler that invokes the cleaner with the given
// parameters.
func (s *Server) HTTPHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
deleted, status, err := s.clean(ctx, r.Body)
if err != nil {
s.handleError(w, err, status)
return
}
refs := make([]string, 0, 16)
for _, v := range deleted {
refs = append(refs, v...)
}
sort.Strings(refs)
b, err := json.Marshal(&cleanResp{
Count: len(deleted),
Refs: refs,
RefsByRepo: deleted,
})
if err != nil {
err = fmt.Errorf("failed to marshal JSON errors: %w", err)
s.handleError(w, err, 500)
return
}
w.WriteHeader(200)
w.Header().Set(contentTypeHeader, contentTypeJSON)
fmt.Fprint(w, string(b))
}
}
// clean reads the given body as JSON and starts a cleaner instance.
func (s *Server) clean(ctx context.Context, r io.ReadCloser) (map[string][]string, int, error) {
var p Payload
if err := json.NewDecoder(r).Decode(&p); err != nil {
return nil, 500, fmt.Errorf("failed to decode payload as JSON: %w", err)
}
// Convert duration to a negative value, since we're about to "add" it to the
// since time.
sub := time.Duration(p.Grace)
if p.Grace > 0 {
sub = sub * -1
}
since := time.Now().UTC().Add(sub)
tagFilter, err := BuildTagFilter(p.TagFilterFirst, p.TagFilterAny, p.TagFilterAll)
if err != nil {
return nil, http.StatusBadRequest, fmt.Errorf("failed to build tag filter: %w", err)
}
// Gather all the repositories.
repos := make([]string, 0, len(p.Repos))
for _, v := range p.Repos {
if t := strings.TrimSpace(v); t != "" {
repos = append(repos, t)
}
}
if t := strings.TrimSpace(p.Repo); t != "" {
s.logger.Warn("specifying \"repo\" is deprecated - switch to \"repos\"")
repos = append(repos, t)
}
if p.Recursive {
s.logger.Debug("gathering child repositories recursively")
allRepos, err := s.cleaner.ListChildRepositories(ctx, repos)
if err != nil {
return nil, http.StatusBadRequest, fmt.Errorf("failed to list child repositories for %q: %w", p.Repo, err)
}
s.logger.Debug("recursively listed child repositories",
"in", repos,
"out", allRepos)
// This is safe because ListChildRepositories is guaranteed to include at
// least the list repos givenh to it.
repos = allRepos
}
s.logger.Info("deleting refs",
"since", since,
"repos", repos)
// Do the deletion.
deleted := make(map[string][]string, len(repos))
for _, repo := range repos {
s.logger.Info("deleting refs for repo", "repo", repo)
childrenDeleted, err := s.cleaner.Clean(ctx, repo, since, p.Keep, tagFilter, p.DryRun)
if err != nil {
return nil, http.StatusBadRequest, fmt.Errorf("failed to clean repo %q: %w", repo, err)
}
if len(childrenDeleted) > 0 {
s.logger.Info("deleted refs", "repo", repo, "refs", childrenDeleted)
deleted[repo] = append(deleted[repo], childrenDeleted...)
}
}
s.logger.Info("deleted refs", "refs", deleted)
return deleted, http.StatusOK, nil
}
// handleError returns a JSON-formatted error message
func (s *Server) handleError(w http.ResponseWriter, err error, status int) {
s.logger.Error(err.Error(), "error", err)
b, err := json.Marshal(&errorResp{Error: err.Error()})
if err != nil {
err = fmt.Errorf("failed to marshal JSON errors: %w", err)
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(status)
w.Header().Set(contentTypeHeader, contentTypeJSON)
fmt.Fprint(w, string(b))
}
// Payload is the expected incoming payload format.
type Payload struct {
// Repo is the name of the repo to clean.
//
// Deprecated: Use Repos instead.
Repo string `json:"repo"`
// Repos is the list of repositories to clean.
Repos sortedStringSlice `json:"repos"`
// Grace is a time.Duration value indicating how much grade period should be
// given to new, untagged layers. The default is no grace.
Grace duration `json:"grace"`
// Keep is the minimum number of images to keep.
Keep int `json:"keep"`
// TagFilterAny is the tags pattern to be allowed removing. If given, any
// image with at least one tag that matches this given regular expression will
// be deleted. The image will be deleted even if it has other tags that do not
// match the given regular expression.
TagFilterAny string `json:"tag_filter_any"`
// TagFilterAll is the tags pattern to be allowed removing. If given, any
// image where all tags match this given regular expression will be deleted.
// The image will not be delete if it has other tags that do not match the
// given regular expression.
TagFilterAll string `json:"tag_filter_all"`
// DryRun instructs the server to not perform actual cleaning. The response
// will include repositories that would have been deleted.
DryRun bool `json:"dry_run"`
// Recursive enables cleaning all child repositories.
Recursive bool `json:"recursive"`
// TagFilterFirst is the tags pattern to be allowed removing. If specified, any
// images where the first tag matches the given regular expression will be
// deleted.
//
// Deprecated: Use tag_filter_all or tag_filter_any instead.
TagFilterFirst string `json:"tag_filter"`
// AllowTagged is a Boolean value determine if tagged images are allowed to be
// deleted.
//
// Deprecated: Use tag_filter_all or tag_filter_any instead. Setting either of
// these values enables deleting tagged images.
AllowTagged bool `json:"allow_tagged"`
}
type pubsubMessage struct {
Message struct {
Data []byte `json:"data"`
ID string `json:"message_id"`
} `json:"message"`
Subscription string `json:"subscription"`
}
type cleanResp struct {
Count int `json:"count"`
Refs []string `json:"refs"`
RefsByRepo map[string][]string `json:"refs_by_repo"`
}
type errorResp struct {
Error string `json:"error"`
}
type sortedStringSlice []string
func (s sortedStringSlice) MarshalJSON() ([]byte, error) {
return json.Marshal([]string(s))
}
func (s *sortedStringSlice) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
m := make(map[string]struct{}, 4)
switch val := v.(type) {
case string:
if t := strings.TrimSpace(val); t != "" {
m[t] = struct{}{}
}
case []interface{}:
for i, v := range val {
s, ok := v.(string)
if !ok {
return fmt.Errorf("list must contain only strings (got %T at index %d)", v, i)
}
if t := strings.TrimSpace(s); t != "" {
m[t] = struct{}{}
}
}
case []string:
for _, v := range val {
if t := strings.TrimSpace(v); t != "" {
m[t] = struct{}{}
}
}
default:
return fmt.Errorf("invalid list type %T", val)
}
list := make([]string, 0, len(m))
for v := range m {
list = append(list, v)
}
sort.Strings(list)
*s = list
return nil
}
type duration time.Duration
func (d duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch val := v.(type) {
case float64:
*d = duration(time.Duration(val))
return nil
case string:
s, err := time.ParseDuration(val)
if err != nil {
return err
}
*d = duration(s)
return nil
default:
return fmt.Errorf("invalid duration type %T", val)
}
}