forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.go
133 lines (119 loc) · 5.36 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
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
package images
import (
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
kapiv1 "k8s.io/api/core/v1"
kextensionsv1beta1 "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
exutil "github.com/openshift/origin/test/extended/util"
)
var _ = g.Describe("[Feature:ImageLookup][registry] Image policy", func() {
defer g.GinkgoRecover()
var oc = exutil.NewCLI("resolve-local-names", exutil.KubeConfigPath())
one := int64(0)
g.It("should update standard Kube object image fields when local names are on", func() {
err := oc.Run("import-image").Args("busybox:latest", "--confirm").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("set", "image-lookup").Args("busybox").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
tag, err := oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("busybox:latest", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(tag.LookupPolicy.Local).To(o.BeTrue())
// pods should auto replace local references
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Create(&kapiv1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "resolve"},
Spec: kapiv1.PodSpec{
TerminationGracePeriodSeconds: &one,
RestartPolicy: kapiv1.RestartPolicyNever,
Containers: []kapiv1.Container{
{Name: "resolve", Image: "busybox:latest", Command: []string{"/bin/true"}},
{Name: "resolve2", Image: "busybox:unknown", Command: []string{"/bin/true"}},
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
if pod.Spec.Containers[0].Image != tag.Image.DockerImageReference {
g.Skip("default image resolution is not configured, can't verify pod resolution")
}
o.Expect(pod.Spec.Containers[0].Image).To(o.Equal(tag.Image.DockerImageReference))
o.Expect(pod.Spec.Containers[1].Image).To(o.HaveSuffix("/" + oc.Namespace() + "/busybox:unknown"))
defer func() { oc.KubeClient().CoreV1().Pods(oc.Namespace()).Delete(pod.Name, nil) }()
// replica sets should auto replace local references
rs, err := oc.KubeClient().ExtensionsV1beta1().ReplicaSets(oc.Namespace()).Create(&kextensionsv1beta1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{Name: "resolve"},
Spec: kextensionsv1beta1.ReplicaSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"resolve": "true"},
},
Template: kapiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"resolve": "true"}},
Spec: kapiv1.PodSpec{
TerminationGracePeriodSeconds: &one,
Containers: []kapiv1.Container{
{Name: "resolve", Image: "busybox:latest", Command: []string{"/bin/true"}},
},
},
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(rs.Spec.Template.Spec.Containers[0].Image).To(o.Equal(tag.Image.DockerImageReference))
defer func() { oc.KubeClient().ExtensionsV1beta1().ReplicaSets(oc.Namespace()).Delete(rs.Name, nil) }()
})
g.It("should perform lookup when the pod has the resolve-names annotation", func() {
err := oc.Run("import-image").Args("busybox:latest", "--confirm").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
tag, err := oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("busybox:latest", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
// pods should auto replace local references
pod, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).Create(&kapiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "resolve",
Annotations: map[string]string{
"alpha.image.policy.openshift.io/resolve-names": "*",
},
},
Spec: kapiv1.PodSpec{
TerminationGracePeriodSeconds: &one,
RestartPolicy: kapiv1.RestartPolicyNever,
Containers: []kapiv1.Container{
{Name: "resolve", Image: "busybox:latest", Command: []string{"/bin/true"}},
{Name: "resolve2", Image: "busybox:unknown", Command: []string{"/bin/true"}},
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
if pod.Spec.Containers[0].Image != tag.Image.DockerImageReference {
g.Skip("default image resolution is not configured, can't verify pod resolution")
}
o.Expect(pod.Spec.Containers[0].Image).To(o.Equal(tag.Image.DockerImageReference))
o.Expect(pod.Spec.Containers[1].Image).To(o.HaveSuffix("/" + oc.Namespace() + "/busybox:unknown"))
defer func() { oc.KubeClient().CoreV1().Pods(oc.Namespace()).Delete(pod.Name, nil) }()
// replica sets should auto replace local references
rs, err := oc.KubeClient().ExtensionsV1beta1().ReplicaSets(oc.Namespace()).Create(&kextensionsv1beta1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "resolve",
Annotations: map[string]string{
"alpha.image.policy.openshift.io/resolve-names": "*",
},
},
Spec: kextensionsv1beta1.ReplicaSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"resolve": "true"},
},
Template: kapiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"resolve": "true"}},
Spec: kapiv1.PodSpec{
TerminationGracePeriodSeconds: &one,
Containers: []kapiv1.Container{
{Name: "resolve", Image: "busybox:latest", Command: []string{"/bin/true"}},
},
},
},
},
})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(rs.Spec.Template.Spec.Containers[0].Image).To(o.Equal(tag.Image.DockerImageReference))
defer func() { oc.KubeClient().ExtensionsV1beta1().ReplicaSets(oc.Namespace()).Delete(rs.Name, nil) }()
})
})