forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolve.go
82 lines (70 loc) · 2.4 KB
/
resolve.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
package util
import (
"fmt"
"strings"
"github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// ResolveImagePullSpec resolves the provided source which can be "docker", "istag" or
// "isimage" and returns the full Docker pull spec.
func ResolveImagePullSpec(images client.ImageStreamImagesNamespacer, tags client.ImageStreamTagsNamespacer, source, name, defaultNamespace string) (string, error) {
// for Docker source, just passtrough the image name
if IsDocker(source) {
return name, nil
}
// parse the namespace from the provided image
namespace, image := splitNamespaceAndImage(name)
if len(namespace) == 0 {
namespace = defaultNamespace
}
dockerImageReference := ""
if IsImageStreamTag(source) {
name, tag, ok := imageapi.SplitImageStreamTag(image)
if !ok {
return "", fmt.Errorf("invalid image stream tag %q, must be of the form [NAMESPACE/]NAME:TAG", name)
}
if resolved, err := tags.ImageStreamTags(namespace).Get(name, tag); err != nil {
return "", fmt.Errorf("failed to get image stream tag %q: %v", name, err)
} else {
dockerImageReference = resolved.Image.DockerImageReference
}
}
if IsImageStreamImage(source) {
name, digest, ok := imageapi.SplitImageStreamImage(image)
if !ok {
return "", fmt.Errorf("invalid image stream image %q, must be of the form [NAMESPACE/]NAME@DIGEST", name)
}
if resolved, err := images.ImageStreamImages(namespace).Get(name, digest); err != nil {
return "", fmt.Errorf("failed to get image stream image %q: %v", name, err)
} else {
dockerImageReference = resolved.Image.DockerImageReference
}
}
if len(dockerImageReference) == 0 {
return "", fmt.Errorf("unable to resolve %s %q", source, name)
}
reference, err := imageapi.ParseDockerImageReference(dockerImageReference)
if err != nil {
return "", err
}
return reference.String(), nil
}
func IsDocker(source string) bool {
return source == "docker"
}
func IsImageStreamTag(source string) bool {
return source == "istag" || source == "imagestreamtag"
}
func IsImageStreamImage(source string) bool {
return source == "isimage" || source == "imagestreamimage"
}
func splitNamespaceAndImage(name string) (string, string) {
namespace := ""
imageName := ""
if parts := strings.Split(name, "/"); len(parts) == 2 {
namespace, imageName = parts[0], parts[1]
} else if len(parts) == 1 {
imageName = parts[0]
}
return namespace, imageName
}