forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resolve.go
76 lines (63 loc) · 2.2 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
package clientcmd
import (
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion"
)
// resolveImagePullSpec resolves the provided source which can be "docker", "istag" or
// "isimage" and returns the full Docker pull spec.
func resolveImagePullSpec(imageClient imageclient.ImageInterface, source, name, defaultNamespace string) (string, error) {
// for Docker source, just passtrough the image name
if isDockerImageSource(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) {
if resolved, err := imageClient.ImageStreamTags(namespace).Get(image, metav1.GetOptions{}); err != nil {
return "", fmt.Errorf("failed to get image stream tag %q: %v", image, err)
} else {
dockerImageReference = resolved.Image.DockerImageReference
}
}
if isImageStreamImage(source) {
if resolved, err := imageClient.ImageStreamImages(namespace).Get(image, metav1.GetOptions{}); err != nil {
return "", fmt.Errorf("failed to get image stream image %q: %v", image, 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 isDockerImageSource(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
}