-
Notifications
You must be signed in to change notification settings - Fork 14
/
manager.go
351 lines (285 loc) · 8.66 KB
/
manager.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package artifacts
import (
"context"
"fmt"
"os"
"path/filepath"
"slices"
"sync"
"time"
"github.com/blang/semver/v4"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/siderolabs/gen/xerrors"
"go.uber.org/zap"
"golang.org/x/sync/singleflight"
)
// Manager supports loading, caching and serving Talos release artifacts.
type Manager struct { //nolint:govet
options Options
storagePath string
schematicsPath string
logger *zap.Logger
imageRegistry name.Registry
pullers map[Arch]*remote.Puller
sf singleflight.Group
officialExtensionsMu sync.Mutex
officialExtensions map[string][]ExtensionRef
officialOverlaysMu sync.Mutex
officialOverlays map[string][]OverlayRef
talosVersionsMu sync.Mutex
talosVersions []semver.Version
talosVersionsTimestamp time.Time
}
// NewManager creates a new artifacts manager.
func NewManager(logger *zap.Logger, options Options) (*Manager, error) {
tmpDir, err := os.MkdirTemp("", "image-factory")
if err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
}
schematicsPath := filepath.Join(tmpDir, "schematics")
if err = os.Mkdir(schematicsPath, 0o700); err != nil {
return nil, fmt.Errorf("failed to create schematics directory: %w", err)
}
opts := []name.Option{}
if options.InsecureImageRegistry {
opts = append(opts, name.Insecure)
}
imageRegistry, err := name.NewRegistry(options.ImageRegistry, opts...)
if err != nil {
return nil, fmt.Errorf("failed to parse image registry: %w", err)
}
pullers := make(map[Arch]*remote.Puller, 2)
for _, arch := range []Arch{ArchAmd64, ArchArm64} {
pullers[arch], err = remote.NewPuller(
append(
[]remote.Option{
remote.WithPlatform(v1.Platform{
Architecture: string(arch),
OS: "linux",
}),
},
options.RemoteOptions...,
)...,
)
if err != nil {
return nil, fmt.Errorf("failed to create puller: %w", err)
}
}
return &Manager{
options: options,
storagePath: tmpDir,
schematicsPath: schematicsPath,
logger: logger,
imageRegistry: imageRegistry,
pullers: pullers,
}, nil
}
// Close the manager.
func (m *Manager) Close() error {
return os.RemoveAll(m.storagePath)
}
func (m *Manager) validateTalosVersion(ctx context.Context, version semver.Version) error {
availableVersion, err := m.GetTalosVersions(ctx)
if err != nil {
return fmt.Errorf("failed to get available Talos versions: %w", err)
}
if !slices.ContainsFunc(availableVersion, version.Equals) {
return xerrors.NewTaggedf[ErrNotFoundTag]("version %s is not available", version)
}
return nil
}
// Get returns the artifact path for the given version, arch and kind.
func (m *Manager) Get(ctx context.Context, versionString string, arch Arch, kind Kind) (string, error) {
version, err := semver.Parse(versionString)
if err != nil {
return "", fmt.Errorf("failed to parse version: %w", err)
}
if err = m.validateTalosVersion(ctx, version); err != nil {
return "", err
}
tag := "v" + version.String()
// check if already extracted
if _, err = os.Stat(filepath.Join(m.storagePath, tag)); err != nil {
resultCh := m.sf.DoChan(tag, func() (any, error) { //nolint:contextcheck
return nil, m.fetchImager(tag)
})
// wait for the fetch to finish
select {
case result := <-resultCh:
if result.Err != nil {
return "", err
}
case <-ctx.Done():
return "", ctx.Err()
}
}
// build the path
path := filepath.Join(m.storagePath, tag, string(arch), string(kind))
_, err = os.Stat(path)
if err != nil {
return "", fmt.Errorf("failed to find artifact: %w", err)
}
return path, nil
}
// GetTalosVersions returns a list of Talos versions available.
func (m *Manager) GetTalosVersions(ctx context.Context) ([]semver.Version, error) {
m.talosVersionsMu.Lock()
versions, timestamp := m.talosVersions, m.talosVersionsTimestamp
m.talosVersionsMu.Unlock()
if time.Since(timestamp) < m.options.TalosVersionRecheckInterval {
return versions, nil
}
resultCh := m.sf.DoChan("talos-versions", m.fetchTalosVersions)
select {
case <-ctx.Done():
return nil, ctx.Err()
case result := <-resultCh:
if result.Err != nil {
return nil, result.Err
}
}
m.talosVersionsMu.Lock()
versions = m.talosVersions
m.talosVersionsMu.Unlock()
return versions, nil
}
// GetOfficialExtensions returns a list of Talos extensions per Talos version available.
//
//nolint:dupl
func (m *Manager) GetOfficialExtensions(ctx context.Context, versionString string) ([]ExtensionRef, error) {
tag, err := m.parseTag(ctx, versionString)
if err != nil {
return nil, err
}
m.officialExtensionsMu.Lock()
extensions, ok := m.officialExtensions[tag]
m.officialExtensionsMu.Unlock()
if ok {
return extensions, nil
}
resultCh := m.sf.DoChan("extensions-"+tag, func() (any, error) { //nolint:contextcheck
return nil, m.fetchOfficialExtensions(tag)
})
select {
case <-ctx.Done():
return nil, ctx.Err()
case result := <-resultCh:
if result.Err != nil {
return nil, result.Err
}
}
m.officialExtensionsMu.Lock()
extensions = m.officialExtensions[tag]
m.officialExtensionsMu.Unlock()
return extensions, nil
}
// GetOfficialOverlays returns a list of overlays per Talos version available.
//
//nolint:dupl
func (m *Manager) GetOfficialOverlays(ctx context.Context, versionString string) ([]OverlayRef, error) {
tag, err := m.parseTag(ctx, versionString)
if err != nil {
return nil, err
}
m.officialOverlaysMu.Lock()
overlays, ok := m.officialOverlays[tag]
m.officialOverlaysMu.Unlock()
if ok {
return overlays, nil
}
resultCh := m.sf.DoChan("overlays-"+tag, func() (any, error) { //nolint:contextcheck
return nil, m.fetchOfficialOverlays(tag)
})
select {
case <-ctx.Done():
return nil, ctx.Err()
case result := <-resultCh:
if result.Err != nil {
return nil, result.Err
}
}
m.officialOverlaysMu.Lock()
overlays = m.officialOverlays[tag]
m.officialOverlaysMu.Unlock()
return overlays, nil
}
// GetInstallerImage pulls and stoers in OCI layout installer image.
func (m *Manager) GetInstallerImage(ctx context.Context, arch Arch, versionString string) (string, error) {
version, err := semver.ParseTolerant(versionString)
if err != nil {
return "", fmt.Errorf("failed to parse version: %w", err)
}
if err = m.validateTalosVersion(ctx, version); err != nil {
return "", err
}
tag := "v" + version.String()
ociPath := filepath.Join(m.storagePath, string(arch)+"-installer-"+tag)
// check if already fetched
if _, err := os.Stat(ociPath); err != nil {
resultCh := m.sf.DoChan(ociPath, func() (any, error) { //nolint:contextcheck
return nil, m.fetchInstallerImage(arch, tag, ociPath)
})
select {
case <-ctx.Done():
return "", ctx.Err()
case result := <-resultCh:
if result.Err != nil {
return "", result.Err
}
}
}
return ociPath, nil
}
// GetExtensionImage pulls and stores in OCI layout an extension image.
func (m *Manager) GetExtensionImage(ctx context.Context, arch Arch, ref ExtensionRef) (string, error) {
ociPath := filepath.Join(m.storagePath, string(arch)+"-"+ref.Digest)
// check if already fetched
if _, err := os.Stat(ociPath); err != nil {
resultCh := m.sf.DoChan(ociPath, func() (any, error) { //nolint:contextcheck
return nil, m.fetchExtensionImage(arch, ref, ociPath)
})
select {
case <-ctx.Done():
return "", ctx.Err()
case result := <-resultCh:
if result.Err != nil {
return "", result.Err
}
}
}
return ociPath, nil
}
// GetOverlayImage pulls and stores in OCI layout an overlay image.
func (m *Manager) GetOverlayImage(ctx context.Context, arch Arch, ref OverlayRef) (string, error) {
ociPath := filepath.Join(m.storagePath, string(arch)+"-"+ref.Digest)
// check if already fetched
if _, err := os.Stat(ociPath); err != nil {
resultCh := m.sf.DoChan(ociPath, func() (any, error) { //nolint:contextcheck
return nil, m.fetchOverlayImage(arch, ref, ociPath)
})
select {
case <-ctx.Done():
return "", ctx.Err()
case result := <-resultCh:
if result.Err != nil {
return "", result.Err
}
}
}
return ociPath, nil
}
func (m *Manager) parseTag(ctx context.Context, versionString string) (string, error) {
version, err := semver.ParseTolerant(versionString)
if err != nil {
return "", fmt.Errorf("failed to parse version: %w", err)
}
if err = m.validateTalosVersion(ctx, version); err != nil {
return "", err
}
return "v" + version.String(), nil
}