-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
48 lines (40 loc) · 1.41 KB
/
image.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
package neco
import (
"context"
"fmt"
"io"
"net/http"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
)
// ImageFetcher retrieves Docker image from registries.
type ImageFetcher struct {
transport http.RoundTripper
env string
}
// NewImageFetcher creates a new ImageFetcher.
// `transport` must not be nil. `env` must not be empty string.
func NewImageFetcher(transport http.RoundTripper, env string) (ImageFetcher, error) {
if env == "" {
return ImageFetcher{}, fmt.Errorf("env has no value set")
}
return ImageFetcher{
transport: transport,
env: env,
}, nil
}
// GetTarball fetches an image from the registry and write it as a tarball.
// The tarball can be loaded into Docker with `docker load`.
func (f ImageFetcher) GetTarball(ctx context.Context, img ContainerImage, w io.Writer) error {
ref, err := name.ParseReference(img.FullName(f.env == DevEnv || f.env == StagingEnv || f.env == ProdEnv))
if err != nil {
return err
}
rimg, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithContext(ctx), remote.WithJobs(1), remote.WithTransport(f.transport))
if err != nil {
return fmt.Errorf("failed to create remote image for %s: %w", img.Name, err)
}
return tarball.Write(ref, rimg, w)
}