4
4
"bytes"
5
5
"errors"
6
6
"fmt"
7
- "net"
8
7
"net/url"
9
8
"regexp"
10
9
"sort"
@@ -18,6 +17,7 @@ import (
18
17
"github.com/golang/glog"
19
18
20
19
"github.com/openshift/origin/pkg/image/apis/image/internal/digest"
20
+ "github.com/openshift/origin/pkg/image/apis/image/reference"
21
21
)
22
22
23
23
const (
@@ -145,191 +145,16 @@ func ParseImageStreamTagName(istag string) (name string, tag string, err error)
145
145
return
146
146
}
147
147
148
- // IsRegistryDockerHub returns true if the given registry name belongs to
149
- // Docker hub.
150
- func IsRegistryDockerHub (registry string ) bool {
151
- switch registry {
152
- case DockerDefaultRegistry , DockerDefaultV1Registry , DockerDefaultV2Registry :
153
- return true
154
- default :
155
- return false
156
- }
157
- }
158
-
159
148
// ParseDockerImageReference parses a Docker pull spec string into a
160
149
// DockerImageReference.
161
- func ParseDockerImageReference (spec string ) (DockerImageReference , error ) {
162
- var ref DockerImageReference
163
-
164
- namedRef , err := parseNamedDockerImageReference (spec )
150
+ func ParseDockerImageReference (spec string ) (reference.DockerImageReference , error ) {
151
+ ref , err := reference .Parse (spec )
165
152
if err != nil {
166
153
return ref , err
167
154
}
168
-
169
- ref .Registry = namedRef .Registry
170
- ref .Namespace = namedRef .Namespace
171
- ref .Name = namedRef .Name
172
- ref .Tag = namedRef .Tag
173
- ref .ID = namedRef .ID
174
-
175
155
return ref , nil
176
156
}
177
157
178
- // Equal returns true if the other DockerImageReference is equivalent to the
179
- // reference r. The comparison applies defaults to the Docker image reference,
180
- // so that e.g., "foobar" equals "docker.io/library/foobar:latest".
181
- func (r DockerImageReference ) Equal (other DockerImageReference ) bool {
182
- defaultedRef := r .DockerClientDefaults ()
183
- otherDefaultedRef := other .DockerClientDefaults ()
184
- return defaultedRef == otherDefaultedRef
185
- }
186
-
187
- // DockerClientDefaults sets the default values used by the Docker client.
188
- func (r DockerImageReference ) DockerClientDefaults () DockerImageReference {
189
- if len (r .Registry ) == 0 {
190
- r .Registry = DockerDefaultRegistry
191
- }
192
- if len (r .Namespace ) == 0 && IsRegistryDockerHub (r .Registry ) {
193
- r .Namespace = DockerDefaultNamespace
194
- }
195
- if len (r .Tag ) == 0 {
196
- r .Tag = DefaultImageTag
197
- }
198
- return r
199
- }
200
-
201
- // Minimal reduces a DockerImageReference to its minimalist form.
202
- func (r DockerImageReference ) Minimal () DockerImageReference {
203
- if r .Tag == DefaultImageTag {
204
- r .Tag = ""
205
- }
206
- return r
207
- }
208
-
209
- // AsRepository returns the reference without tags or IDs.
210
- func (r DockerImageReference ) AsRepository () DockerImageReference {
211
- r .Tag = ""
212
- r .ID = ""
213
- return r
214
- }
215
-
216
- // RepositoryName returns the registry relative name
217
- func (r DockerImageReference ) RepositoryName () string {
218
- r .Tag = ""
219
- r .ID = ""
220
- r .Registry = ""
221
- return r .Exact ()
222
- }
223
-
224
- // RegistryHostPort returns the registry hostname and the port.
225
- // If the port is not specified in the registry hostname we default to 443.
226
- // This will also default to Docker client defaults if the registry hostname is empty.
227
- func (r DockerImageReference ) RegistryHostPort (insecure bool ) (string , string ) {
228
- registryHost := r .AsV2 ().DockerClientDefaults ().Registry
229
- if strings .Contains (registryHost , ":" ) {
230
- hostname , port , _ := net .SplitHostPort (registryHost )
231
- return hostname , port
232
- }
233
- if insecure {
234
- return registryHost , "80"
235
- }
236
- return registryHost , "443"
237
- }
238
-
239
- // RepositoryName returns the registry relative name
240
- func (r DockerImageReference ) RegistryURL () * url.URL {
241
- return & url.URL {
242
- Scheme : "https" ,
243
- Host : r .AsV2 ().Registry ,
244
- }
245
- }
246
-
247
- // DaemonMinimal clears defaults that Docker assumes.
248
- func (r DockerImageReference ) DaemonMinimal () DockerImageReference {
249
- switch r .Registry {
250
- case DockerDefaultV1Registry , DockerDefaultV2Registry :
251
- r .Registry = DockerDefaultRegistry
252
- }
253
- if IsRegistryDockerHub (r .Registry ) && r .Namespace == DockerDefaultNamespace {
254
- r .Namespace = ""
255
- }
256
- return r .Minimal ()
257
- }
258
-
259
- func (r DockerImageReference ) AsV2 () DockerImageReference {
260
- switch r .Registry {
261
- case DockerDefaultV1Registry , DockerDefaultRegistry :
262
- r .Registry = DockerDefaultV2Registry
263
- }
264
- return r
265
- }
266
-
267
- // MostSpecific returns the most specific image reference that can be constructed from the
268
- // current ref, preferring an ID over a Tag. Allows client code dealing with both tags and IDs
269
- // to get the most specific reference easily.
270
- func (r DockerImageReference ) MostSpecific () DockerImageReference {
271
- if len (r .ID ) == 0 {
272
- return r
273
- }
274
- if _ , err := digest .ParseDigest (r .ID ); err == nil {
275
- r .Tag = ""
276
- return r
277
- }
278
- if len (r .Tag ) == 0 {
279
- r .Tag , r .ID = r .ID , ""
280
- return r
281
- }
282
- return r
283
- }
284
-
285
- // NameString returns the name of the reference with its tag or ID.
286
- func (r DockerImageReference ) NameString () string {
287
- switch {
288
- case len (r .Name ) == 0 :
289
- return ""
290
- case len (r .Tag ) > 0 :
291
- return r .Name + ":" + r .Tag
292
- case len (r .ID ) > 0 :
293
- var ref string
294
- if _ , err := digest .ParseDigest (r .ID ); err == nil {
295
- // if it parses as a digest, its v2 pull by id
296
- ref = "@" + r .ID
297
- } else {
298
- // if it doesn't parse as a digest, it's presumably a v1 registry by-id tag
299
- ref = ":" + r .ID
300
- }
301
- return r .Name + ref
302
- default :
303
- return r .Name
304
- }
305
- }
306
-
307
- // Exact returns a string representation of the set fields on the DockerImageReference
308
- func (r DockerImageReference ) Exact () string {
309
- name := r .NameString ()
310
- if len (name ) == 0 {
311
- return name
312
- }
313
- s := r .Registry
314
- if len (s ) > 0 {
315
- s += "/"
316
- }
317
-
318
- if len (r .Namespace ) != 0 {
319
- s += r .Namespace + "/"
320
- }
321
- return s + name
322
- }
323
-
324
- // String converts a DockerImageReference to a Docker pull spec (which implies a default namespace
325
- // according to V1 Docker registry rules). Use Exact() if you want no defaulting.
326
- func (r DockerImageReference ) String () string {
327
- if len (r .Namespace ) == 0 && IsRegistryDockerHub (r .Registry ) {
328
- r .Namespace = DockerDefaultNamespace
329
- }
330
- return r .Exact ()
331
- }
332
-
333
158
// SplitImageStreamTag turns the name of an ImageStreamTag into Name and Tag.
334
159
// It returns false if the tag was not properly specified in the name.
335
160
func SplitImageStreamTag (nameAndTag string ) (name string , tag string , ok bool ) {
@@ -790,7 +615,7 @@ func ResolveImageID(stream *ImageStream, imageID string) (*TagEvent, error) {
790
615
// MostAccuratePullSpec returns a docker image reference that uses the current ID if possible, the current tag otherwise, and
791
616
// returns false if the reference if the spec could not be parsed. The returned spec has all client defaults applied.
792
617
func MostAccuratePullSpec (pullSpec string , id , tag string ) (string , bool ) {
793
- ref , err := ParseDockerImageReference (pullSpec )
618
+ ref , err := reference . Parse (pullSpec )
794
619
if err != nil {
795
620
return pullSpec , false
796
621
}
0 commit comments