-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
196 lines (169 loc) · 5.55 KB
/
client.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
package stereoscope
import (
"context"
"fmt"
"runtime"
"github.com/wagoodman/go-partybus"
"github.com/anchore/go-logger"
"github.com/anchore/stereoscope/internal/bus"
dockerClient "github.com/anchore/stereoscope/internal/docker"
"github.com/anchore/stereoscope/internal/log"
"github.com/anchore/stereoscope/internal/podman"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/image"
"github.com/anchore/stereoscope/pkg/image/docker"
"github.com/anchore/stereoscope/pkg/image/oci"
"github.com/anchore/stereoscope/pkg/image/sif"
)
var rootTempDirGenerator = file.NewTempDirGenerator("stereoscope")
func WithRegistryOptions(options image.RegistryOptions) Option {
return func(c *config) error {
c.Registry = options
return nil
}
}
func WithInsecureSkipTLSVerify() Option {
return func(c *config) error {
c.Registry.InsecureSkipTLSVerify = true
return nil
}
}
func WithInsecureAllowHTTP() Option {
return func(c *config) error {
c.Registry.InsecureUseHTTP = true
return nil
}
}
func WithCredentials(credentials ...image.RegistryCredentials) Option {
return func(c *config) error {
c.Registry.Credentials = append(c.Registry.Credentials, credentials...)
return nil
}
}
func WithAdditionalMetadata(metadata ...image.AdditionalMetadata) Option {
return func(c *config) error {
c.AdditionalMetadata = append(c.AdditionalMetadata, metadata...)
return nil
}
}
func WithPlatform(platform string) Option {
return func(c *config) error {
p, err := image.NewPlatform(platform)
if err != nil {
return err
}
c.Platform = p
return nil
}
}
// GetImageFromSource returns an image from the explicitly provided source.
func GetImageFromSource(ctx context.Context, imgStr string, source image.Source, options ...Option) (*image.Image, error) {
log.Debugf("image: source=%+v location=%+v", source, imgStr)
var cfg config
for _, option := range options {
if option == nil {
continue
}
if err := option(&cfg); err != nil {
return nil, fmt.Errorf("unable to parse option: %w", err)
}
}
provider, err := selectImageProvider(imgStr, source, cfg)
if err != nil {
return nil, err
}
img, err := provider.Provide(ctx, cfg.AdditionalMetadata...)
if err != nil {
return nil, fmt.Errorf("unable to use %s source: %w", source, err)
}
err = img.Read()
if err != nil {
return nil, fmt.Errorf("could not read image: %+v", err)
}
return img, nil
}
func selectImageProvider(imgStr string, source image.Source, cfg config) (image.Provider, error) {
var provider image.Provider
tempDirGenerator := rootTempDirGenerator.NewGenerator()
if err := setPlatform(source, &cfg, runtime.GOARCH); err != nil {
return nil, err
}
switch source {
case image.DockerTarballSource:
// note: the imgStr is the path on disk to the tar file
provider = docker.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.DockerDaemonSource:
c, err := dockerClient.GetClient()
if err != nil {
return nil, err
}
provider, err = docker.NewProviderFromDaemon(imgStr, tempDirGenerator, c, cfg.Platform)
if err != nil {
return nil, err
}
case image.PodmanDaemonSource:
c, err := podman.GetClient()
if err != nil {
return nil, err
}
provider, err = docker.NewProviderFromDaemon(imgStr, tempDirGenerator, c, cfg.Platform)
if err != nil {
return nil, err
}
case image.OciDirectorySource:
provider = oci.NewProviderFromPath(imgStr, tempDirGenerator)
case image.OciTarballSource:
provider = oci.NewProviderFromTarball(imgStr, tempDirGenerator)
case image.OciRegistrySource:
provider = oci.NewProviderFromRegistry(imgStr, tempDirGenerator, cfg.Registry, cfg.Platform)
case image.SingularitySource:
provider = sif.NewProviderFromPath(imgStr, tempDirGenerator)
default:
return nil, fmt.Errorf("unable to determine image source")
}
return provider, nil
}
func setPlatform(source image.Source, cfg *config, defaultArch string) error {
// we should override the platform based on the host architecture if the user did not specify a platform
// see https://github.com/anchore/stereoscope/issues/149 for more details
defaultPlatform, err := image.NewPlatform(defaultArch)
if err != nil {
log.WithFields("error", err).Warnf("unable to set default platform to %q", runtime.GOARCH)
defaultPlatform = nil
}
switch source {
case image.DockerTarballSource, image.OciDirectorySource, image.OciTarballSource, image.SingularitySource:
if cfg.Platform != nil {
return fmt.Errorf("specified platform=%q however image source=%q does not support selecting platform", cfg.Platform.String(), source.String())
}
case image.DockerDaemonSource, image.PodmanDaemonSource, image.OciRegistrySource:
if cfg.Platform == nil {
cfg.Platform = defaultPlatform
}
default:
return fmt.Errorf("unable to determine image source to select platform")
}
return nil
}
// GetImage parses the user provided image string and provides an image object;
// note: the source where the image should be referenced from is automatically inferred.
func GetImage(ctx context.Context, userStr string, options ...Option) (*image.Image, error) {
source, imgStr, err := image.DetectSource(userStr)
if err != nil {
return nil, err
}
return GetImageFromSource(ctx, imgStr, source, options...)
}
func SetLogger(logger logger.Logger) {
log.Log = logger
}
func SetBus(b *partybus.Bus) {
bus.SetPublisher(b)
}
// Cleanup deletes all directories created by stereoscope calls.
// Deprecated: please use image.Image.Cleanup() over this.
func Cleanup() {
if err := rootTempDirGenerator.Cleanup(); err != nil {
log.Errorf("failed to cleanup tempdir root: %w", err)
}
}