/
mount.go
147 lines (125 loc) · 3.97 KB
/
mount.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
// Copyright © 2022 Alibaba Group Holding Ltd.
//
// 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 imagedistributor
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
v1 "github.com/sealerio/sealer/types/api/v1"
"github.com/sealerio/sealer/utils/os/fs"
)
type buildAhMounter struct {
imageEngine imageengine.Interface
}
func (b buildAhMounter) Mount(imageName string, platform v1.Platform, dest string) (string, string, string, error) {
mountDir := filepath.Join(dest,
strings.ReplaceAll(imageName, "/", "_"),
strings.Join([]string{platform.OS, platform.Architecture, platform.Variant}, "_"))
imageID, err := b.imageEngine.Pull(&options.PullOptions{
Quiet: false,
PullPolicy: "missing",
Image: imageName,
Platform: platform.ToString(),
})
if err != nil {
return "", "", "", err
}
if err := fs.FS.MkdirAll(filepath.Dir(mountDir)); err != nil {
return "", "", "", err
}
id, err := b.imageEngine.CreateWorkingContainer(&options.BuildRootfsOptions{
DestDir: mountDir,
ImageNameOrID: imageID,
})
if err != nil {
return "", "", "", err
}
return mountDir, id, imageID, nil
}
func (b buildAhMounter) Umount(mountDir, cid string) error {
if err := fs.FS.RemoveAll(mountDir); err != nil {
return fmt.Errorf("failed to remove mount dir %s: %v", mountDir, err)
}
if err := b.imageEngine.RemoveContainer(&options.RemoveContainerOptions{
ContainerNamesOrIDs: []string{cid},
}); err != nil {
return fmt.Errorf("failed to remove working container: %v", err)
}
return nil
}
func NewBuildAhMounter(imageEngine imageengine.Interface) Mounter {
return buildAhMounter{
imageEngine: imageEngine,
}
}
type ImagerMounter struct {
Mounter
rootDir string
hostsPlatform map[v1.Platform][]net.IP
}
type ClusterImageMountInfo struct {
// target hosts ip list, not all cluster ips.
Hosts []net.IP
Platform v1.Platform
MountDir string
ContainerID string
ImageID string
}
func (c ImagerMounter) Mount(imageName string) ([]ClusterImageMountInfo, error) {
var imageMountInfos []ClusterImageMountInfo
for platform, hosts := range c.hostsPlatform {
mountDir, cid, imageID, err := c.Mounter.Mount(imageName, platform, c.rootDir)
if err != nil {
return nil, fmt.Errorf("failed to mount image %s with platform %s:%v", imageName, platform.ToString(), err)
}
imageMountInfos = append(imageMountInfos, ClusterImageMountInfo{
Hosts: hosts,
Platform: platform,
MountDir: mountDir,
ContainerID: cid,
ImageID: imageID,
})
}
return imageMountInfos, nil
}
func (c ImagerMounter) Umount(imageName string, imageMountInfo []ClusterImageMountInfo) error {
for _, info := range imageMountInfo {
err := c.Mounter.Umount(info.MountDir, info.ContainerID)
if err != nil {
return fmt.Errorf("failed to umount %s:%v", info.MountDir, err)
}
}
// delete all mounted images
if err := fs.FS.RemoveAll(c.rootDir); err != nil {
return err
}
return nil
}
func NewImageMounter(imageEngine imageengine.Interface, hostsPlatform map[v1.Platform][]net.IP) (*ImagerMounter, error) {
tempDir, err := os.MkdirTemp("", "sealer-mount-tmp")
if err != nil {
return nil, fmt.Errorf("failed to create tmp mount dir, err: %v", err)
}
c := &ImagerMounter{
// todo : user could set this value by env or sealer config
rootDir: tempDir,
hostsPlatform: hostsPlatform,
}
c.Mounter = NewBuildAhMounter(imageEngine)
return c, nil
}