-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.go
255 lines (213 loc) · 6.03 KB
/
handler.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
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, CIQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0
package repository
import (
"context"
"errors"
"io"
"net"
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"time"
"go.ciq.dev/beskar/internal/pkg/config"
"go.ciq.dev/beskar/internal/pkg/gossip"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
eventv1 "go.ciq.dev/beskar/pkg/api/event/v1"
"gocloud.dev/blob"
)
type HandlerParams struct {
Dir string
Bucket *blob.Bucket
RemoteOptions []remote.Option
NameOptions []name.Option
remove func(string)
BeskarMeta *gossip.BeskarMeta
Sync config.SyncConfig
}
func (hp HandlerParams) Remove(repository string) {
hp.remove(repository)
}
func (hp HandlerParams) GetBeskarServiceHostPort() string {
return net.JoinHostPort(hp.BeskarMeta.Hostname, strconv.Itoa(int(hp.BeskarMeta.ServicePort)))
}
func (hp HandlerParams) GetBeskarRegistryHostPort() string {
return net.JoinHostPort(hp.BeskarMeta.Hostname, strconv.Itoa(int(hp.BeskarMeta.RegistryPort)))
}
// Handler - Interface for handling events for a repository.
type Handler interface {
// QueueEvent - Called when a new event is received. If store is true, the event should be stored in the database.
// Note: Avoid performing any long-running operations in this function.
QueueEvent(event *eventv1.EventPayload, store bool) error
// Started - Returns true if the handler has started.
Started() bool
// Start - Called when the handler should start processing events.
// This is your chance to set up any resources, e.g., database connections, run loops, etc.
// This will only be called once.
Start(context.Context)
// Stop - Called when the handler should stop processing events and clean up resources.
Stop()
}
// RepoHandler - A partial default implementation of the Handler interface that provides some common functionality.
// You can embed this in your own handler to get some default functionality, e.g., an event queue.
type RepoHandler struct {
Repository string
Params *HandlerParams
queue []*eventv1.EventPayload
queueMutex sync.RWMutex
Queued chan struct{}
Stopped atomic.Bool
startedCh chan struct{}
cancel context.CancelFunc
syncArtifactsMutex sync.RWMutex
syncArtifacts map[string]chan error
}
func NewRepoHandler(repository string, params *HandlerParams, cancel context.CancelFunc) *RepoHandler {
return &RepoHandler{
Repository: repository,
Params: params,
Queued: make(chan struct{}, 1),
startedCh: make(chan struct{}),
cancel: cancel,
syncArtifacts: make(map[string]chan error),
}
}
func (rh *RepoHandler) EnqueueEvent(event *eventv1.EventPayload) {
rh.queueMutex.Lock()
rh.queue = append(rh.queue, event)
rh.queueMutex.Unlock()
rh.EventQueueUpdate()
}
func (rh *RepoHandler) DequeueEvents() []*eventv1.EventPayload {
rh.queueMutex.Lock()
events := rh.queue
rh.queue = nil
rh.queueMutex.Unlock()
return events
}
func (rh *RepoHandler) EventQueueLength() int {
rh.queueMutex.RLock()
defer rh.queueMutex.RUnlock()
return len(rh.queue)
}
func (rh *RepoHandler) EventQueueUpdate() {
select {
case rh.Queued <- struct{}{}:
default:
}
}
func (rh *RepoHandler) Started() bool {
// closed when started
<-rh.startedCh
if rh.Stopped.Load() {
<-rh.Queued
return false
}
return true
}
func (rh *RepoHandler) Stop() {
rh.Stopped.Store(true)
rh.cancel()
<-rh.Queued
}
func (rh *RepoHandler) DownloadBlob(ref string, destinationPath string) (errFn error) {
downloadDir := filepath.Dir(destinationPath)
if err := os.MkdirAll(downloadDir, 0o700); err != nil {
return err
} else if _, err := os.Stat(destinationPath); err == nil {
return nil
}
dst, err := os.Create(destinationPath)
if err != nil {
return err
}
defer func() {
err = dst.Close()
if errFn == nil {
errFn = err
}
}()
digest, err := name.NewDigest(ref, rh.Params.NameOptions...)
if err != nil {
return err
}
layer, err := remote.Layer(digest, rh.Params.RemoteOptions...)
if err != nil {
return err
}
rc, err := layer.Compressed()
if err != nil {
return err
}
defer rc.Close()
_, err = io.Copy(dst, rc)
return err
}
func (rh *RepoHandler) GetManifestDigest(ref string) (string, error) {
namedRef, err := name.ParseReference(ref, rh.Params.NameOptions...)
if err != nil {
return "", err
}
desc, err := remote.Head(namedRef, rh.Params.RemoteOptions...)
if err != nil {
return "", err
}
return desc.Digest.String(), nil
}
func (rh *RepoHandler) DeleteManifest(ref string) (errFn error) {
namedRef, err := name.ParseReference(ref, rh.Params.NameOptions...)
if err != nil {
return err
}
return remote.Delete(namedRef, rh.Params.RemoteOptions...)
}
func (rh *RepoHandler) PullManifest(ref string) (errFn error) {
namedRef, err := name.ParseReference(ref, rh.Params.NameOptions...)
if err != nil {
return err
}
return remote.Delete(namedRef, rh.Params.RemoteOptions...)
}
func (rh *RepoHandler) SyncArtifact(ctx context.Context, name string, timeout time.Duration) (chan error, func() error) {
errCh := make(chan error, 1)
rh.syncArtifactsMutex.Lock()
rh.syncArtifacts[name] = errCh
rh.syncArtifactsMutex.Unlock()
return errCh, func() error {
var err error
pkgCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
select {
case <-pkgCtx.Done():
if errors.Is(pkgCtx.Err(), context.Canceled) {
err = errors.New("waiting timeout")
} else {
err = errors.New("waiting interruption")
}
case err = <-errCh:
}
rh.syncArtifactsMutex.Lock()
close(errCh)
delete(rh.syncArtifacts, name)
rh.syncArtifactsMutex.Unlock()
return err
}
}
func (rh *RepoHandler) SyncArtifactResult(name string, err error) {
rh.syncArtifactsMutex.RLock()
if errCh, ok := rh.syncArtifacts[name]; ok {
errCh <- err
}
rh.syncArtifactsMutex.RUnlock()
}
func (rh *RepoHandler) SyncArtifactReset() {
rh.syncArtifactsMutex.Lock()
for k, v := range rh.syncArtifacts {
delete(rh.syncArtifacts, k)
close(v)
}
rh.syncArtifactsMutex.Unlock()
}