-
Notifications
You must be signed in to change notification settings - Fork 7
/
inbound_cache_swift.go
151 lines (130 loc) · 4.49 KB
/
inbound_cache_swift.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
/*******************************************************************************
*
* Copyright 2021 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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 openstack
import (
"bytes"
"database/sql"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"time"
"github.com/majewsky/schwift"
"github.com/sapcc/keppel/internal/keppel"
"github.com/sapcc/keppel/internal/models"
)
type inboundCacheDriverSwift struct {
Container *schwift.Container
HostInclusionRx *regexp.Regexp
HostExclusionRx *regexp.Regexp
}
func init() {
keppel.InboundCacheDriverRegistry.Add(func() keppel.InboundCacheDriver { return &inboundCacheDriverSwift{} })
}
// PluginTypeID implements the keppel.InboundCacheDriver interface.
func (d *inboundCacheDriverSwift) PluginTypeID() string { return "swift" }
// Init implements the keppel.InboundCacheDriver interface.
func (d *inboundCacheDriverSwift) Init(cfg keppel.Configuration) (err error) {
d.HostInclusionRx, err = compileOptionalImplicitlyBoundedRegex(os.Getenv("KEPPEL_INBOUND_CACHE_ONLY_HOSTS"))
if err != nil {
return err
}
d.HostExclusionRx, err = compileOptionalImplicitlyBoundedRegex(os.Getenv("KEPPEL_INBOUND_CACHE_EXCEPT_HOSTS"))
if err != nil {
return err
}
d.Container, err = initSwiftContainerConnection("KEPPEL_INBOUND_CACHE_")
return err
}
func compileOptionalImplicitlyBoundedRegex(pattern string) (*regexp.Regexp, error) {
if pattern == "" {
return nil, nil
}
rx, err := regexp.Compile(`^(?:` + pattern + `)$`)
if err != nil {
return nil, fmt.Errorf("%q is not a valid regex: %w", pattern, err)
}
return rx, nil
}
// LoadManifest implements the keppel.InboundCacheDriver interface.
func (d *inboundCacheDriverSwift) LoadManifest(location models.ImageReference, now time.Time) (contents []byte, mediaType string, returnedError error) {
if d.skip(location) {
return nil, "", sql.ErrNoRows
}
defer func() {
if returnedError != nil && !errors.Is(returnedError, sql.ErrNoRows) {
returnedError = fmt.Errorf("while performing a lookup in the inbound cache: %w", returnedError)
}
}()
obj := d.objectFor(location)
contents, err := obj.Download(nil).AsByteSlice()
if err != nil {
if schwift.Is(err, http.StatusNotFound) {
return nil, "", sql.ErrNoRows
}
return nil, "", err
}
hdr, err := obj.Headers() // NOTE: this does not actually make a HEAD request because we already did GET
if err != nil {
return nil, "", err
}
return contents, hdr.ContentType().Get(), nil
}
// StoreManifest implements the keppel.InboundCacheDriver interface.
func (d *inboundCacheDriverSwift) StoreManifest(location models.ImageReference, contents []byte, mediaType string, now time.Time) error {
if d.skip(location) {
return nil
}
hdr := schwift.NewObjectHeaders()
hdr.ContentType().Set(mediaType)
hdr.ExpiresAt().Set(d.expiryFor(location, now))
obj := d.objectFor(location)
err := obj.Upload(bytes.NewReader(contents), nil, hdr.ToOpts())
if err != nil {
return fmt.Errorf("while populating the inbound cache: %w", err)
}
return nil
}
func (d *inboundCacheDriverSwift) objectFor(imageRef models.ImageReference) *schwift.Object {
var name string
if imageRef.Reference.IsTag() {
name = fmt.Sprintf("%s/%s/_tags/%s",
imageRef.Host, imageRef.RepoName, imageRef.Reference.Tag)
} else {
name = fmt.Sprintf("%s/%s/_manifests/%s",
imageRef.Host, imageRef.RepoName, imageRef.Reference.Digest)
}
return d.Container.Object(name)
}
func (d *inboundCacheDriverSwift) expiryFor(imageRef models.ImageReference, now time.Time) time.Time {
if imageRef.Reference.IsTag() {
return now.Add(3 * time.Hour)
}
return now.Add(48 * time.Hour)
}
func (d *inboundCacheDriverSwift) skip(imageRef models.ImageReference) bool {
if d.HostInclusionRx != nil && !d.HostInclusionRx.MatchString(imageRef.Host) {
return true
}
if d.HostExclusionRx != nil && d.HostExclusionRx.MatchString(imageRef.Host) {
return true
}
return false
}