-
Notifications
You must be signed in to change notification settings - Fork 458
/
pull.go
120 lines (92 loc) · 3.13 KB
/
pull.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
// 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 extensions
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/containerd/containerd"
"github.com/containerd/containerd/mount"
"github.com/opencontainers/image-spec/identity"
"github.com/siderolabs/talos/internal/pkg/containers/image"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/constants"
)
// Puller pulls, unpacks and mounts extensions images.
type Puller struct {
client *containerd.Client
snapshots []string
mounts []string
}
// NewPuller creates a new instance of system extensions puller helper.
func NewPuller(client *containerd.Client) (*Puller, error) {
// prepare by ensuring empty extension directory
if _, err := os.Stat(constants.SystemExtensionsPath); err == nil {
if err = os.RemoveAll(constants.SystemExtensionsPath); err != nil {
return nil, err
}
}
if err := os.MkdirAll(constants.SystemExtensionsPath, 0o700); err != nil {
return nil, err
}
return &Puller{
client: client,
}, nil
}
// PullAndMount pulls the system extension images, unpacks them and mounts under well known path (constants.SystemExtensionsPath).
func (puller *Puller) PullAndMount(ctx context.Context, registryConfig config.Registries, extensions []config.Extension) error {
snapshotService := puller.client.SnapshotService(containerd.DefaultSnapshotter)
for i, ext := range extensions {
extensionImage := ext.Image()
// use numeric prefix to keep extensions sorted in a proper way
path := fmt.Sprintf("%03d.%s", i, strings.ReplaceAll(strings.ReplaceAll(extensionImage, ":", "-"), "/", "-"))
log.Printf("pulling extension %q", extensionImage)
var extImg containerd.Image
extImg, err := image.Pull(ctx, registryConfig, puller.client, extensionImage, image.WithSkipIfAlreadyPulled())
if err != nil {
return err
}
diffs, err := extImg.RootFS(ctx)
if err != nil {
return err
}
chainID := identity.ChainID(diffs)
_, err = snapshotService.Stat(ctx, chainID.String())
if err != nil {
return err
}
mounts, err := snapshotService.Prepare(ctx, path, chainID.String())
if err != nil {
return err
}
puller.snapshots = append(puller.snapshots, path)
mountTarget := filepath.Join(constants.SystemExtensionsPath, path)
if err = os.Mkdir(mountTarget, 0o700); err != nil {
return err
}
if err = mount.All(mounts, mountTarget); err != nil {
return err
}
puller.mounts = append(puller.mounts, mountTarget)
}
return nil
}
// Cleanup the temporary stuff created by the puller process.
func (puller *Puller) Cleanup(ctx context.Context) error {
for _, target := range puller.mounts {
if err := mount.UnmountAll(target, 0); err != nil {
return err
}
}
snapshotService := puller.client.SnapshotService(containerd.DefaultSnapshotter)
for _, key := range puller.snapshots {
if err := snapshotService.Remove(ctx, key); err != nil {
return err
}
}
return os.RemoveAll(constants.SystemExtensionsPath)
}