/
remote.go
161 lines (130 loc) · 4.81 KB
/
remote.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
/*
Copyright 2019 The Skaffold Authors
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 docker
import (
"context"
"fmt"
"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/google/go-containerregistry/pkg/v1/tarball"
specs "github.com/opencontainers/image-spec/specs-go/v1"
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)
// for testing
var (
RemoteDigest = getRemoteDigest
remoteImage = remote.Image
remoteIndex = remote.Index
)
func AddRemoteTag(src, target string, cfg Config, platforms []specs.Platform) error {
log.Entry(context.TODO()).Debugf("attempting to add tag %s to src %s", target, src)
img, err := getRemoteImage(src, cfg, platforms)
if err != nil {
return fmt.Errorf("getting image: %w", err)
}
targetRef, err := parseReference(target, cfg, name.WeakValidation)
if err != nil {
return err
}
return remote.Write(targetRef, img, remote.WithAuthFromKeychain(primaryKeychain))
}
func getRemoteDigest(identifier string, cfg Config, platforms []specs.Platform) (string, error) {
idx, err := getRemoteIndex(identifier, cfg)
if err == nil {
return digest(idx)
}
img, err := getRemoteImage(identifier, cfg, platforms)
if err != nil {
return "", fmt.Errorf("getting image: %w", err)
}
return digest(img)
}
// RetrieveRemoteConfig retrieves the remote config file for an image
func RetrieveRemoteConfig(identifier string, cfg Config, platforms []specs.Platform) (*v1.ConfigFile, error) {
img, err := getRemoteImage(identifier, cfg, platforms)
if err != nil {
return nil, err
}
return img.ConfigFile()
}
// Push pushes the tarball image
func Push(tarPath, tag string, cfg Config, platforms []specs.Platform) (string, error) {
t, err := name.NewTag(tag, name.WeakValidation)
if err != nil {
return "", fmt.Errorf("parsing tag %q: %w", tag, err)
}
i, err := tarball.ImageFromPath(tarPath, nil)
if err != nil {
return "", fmt.Errorf("reading image %q: %w", tarPath, err)
}
if err := remote.Write(t, i, remote.WithAuthFromKeychain(primaryKeychain)); err != nil {
return "", fmt.Errorf("%s %q: %w", sErrors.PushImageErr, t, err)
}
return getRemoteDigest(tag, cfg, platforms)
}
func getRemoteImage(identifier string, cfg Config, platforms []specs.Platform) (v1.Image, error) {
// for multi-arch images, we can only get the image for one platform at a time.
// If the platform is not specified then it'll fetch the image for the host architecture.
// To avoid discrepencies, we fail image fetch for multi-platform images. These should instead be accessed with the `remoteIndex` method.
if len(platforms) > 1 {
return nil, fmt.Errorf("cannot fetch remote image %q for multiple platforms %q", identifier, platforms)
}
ref, err := parseReference(identifier, cfg)
if err != nil {
return nil, err
}
options := []remote.Option{
remote.WithAuthFromKeychain(primaryKeychain),
}
if len(platforms) == 1 {
options = append(options, remote.WithPlatform(util.ConvertToV1Platform(platforms[0])))
}
return remoteImage(ref, options...)
}
func getRemoteIndex(identifier string, cfg Config) (v1.ImageIndex, error) {
ref, err := parseReference(identifier, cfg)
if err != nil {
return nil, err
}
return remoteIndex(ref, remote.WithAuthFromKeychain(primaryKeychain))
}
// IsInsecure tests if an image is pulled from an insecure registry; default is false
func IsInsecure(ref name.Reference, insecureRegistries map[string]bool) bool {
return insecureRegistries[ref.Context().Registry.Name()]
}
func parseReference(s string, cfg Config, opts ...name.Option) (name.Reference, error) {
ref, err := name.ParseReference(s, opts...)
if err != nil {
return nil, fmt.Errorf("parsing reference %q: %w", s, err)
}
if IsInsecure(ref, cfg.GetInsecureRegistries()) {
ref, err = name.ParseReference(s, name.Insecure)
if err != nil {
log.Entry(context.TODO()).Warnf("error getting insecure registry: %s\nremote references may not be retrieved", err.Error())
}
}
return ref, nil
}
type digester interface {
Digest() (v1.Hash, error)
}
func digest(d digester) (string, error) {
h, err := d.Digest()
if err != nil {
return "", remoteDigestGetErr(err)
}
return h.String(), nil
}