Skip to content

Commit 7c69167

Browse files
rsrchboyarttor
authored andcommitted
Handle images with digests
Previously, we were splitting image addresses at the _last_ instance of a colon; this had unfortunate side-effects when said address included a digest. This change causes us to look for the _first_ instance of a colon, allowing both tag and digest to properly co-exist; we don't try to do anything fancier than that.
1 parent 0b132ed commit 7c69167

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

pkg/processor/pod/pod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpe
166166
}
167167

168168
func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Container, values *helmify.Values) (corev1.Container, error) {
169-
index := strings.LastIndex(c.Image, ":")
169+
index := strings.Index(c.Image, ":")
170170
if index < 0 {
171171
return c, fmt.Errorf("wrong image format: %q", c.Image)
172172
}

pkg/processor/pod/pod_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@ spec:
4040
- containerPort: 80
4141
`
4242

43+
strDeploymentWithTagAndDigest = `
44+
apiVersion: apps/v1
45+
kind: Deployment
46+
metadata:
47+
name: nginx-deployment
48+
labels:
49+
app: nginx
50+
spec:
51+
replicas: 3
52+
selector:
53+
matchLabels:
54+
app: nginx
55+
template:
56+
metadata:
57+
labels:
58+
app: nginx
59+
spec:
60+
containers:
61+
- name: nginx
62+
image: nginx:1.14.2@sha256:cb5c1bddd1b5665e1867a7fa1b5fa843a47ee433bbb75d4293888b71def53229
63+
ports:
64+
- containerPort: 80
65+
`
66+
4367
strDeploymentWithNoArgs = `
4468
apiVersion: apps/v1
4569
kind: Deployment
@@ -149,4 +173,43 @@ func Test_pod_Process(t *testing.T) {
149173
}, tmpl)
150174
})
151175

176+
t.Run("deployment with image tag and digest", func(t *testing.T) {
177+
var deploy appsv1.Deployment
178+
obj := internal.GenerateObj(strDeploymentWithTagAndDigest)
179+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy)
180+
specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec)
181+
assert.NoError(t, err)
182+
183+
assert.Equal(t, map[string]interface{}{
184+
"containers": []interface{}{
185+
map[string]interface{}{
186+
"env": []interface{}{
187+
map[string]interface{}{
188+
"name": "KUBERNETES_CLUSTER_DOMAIN",
189+
"value": "{{ quote .Values.kubernetesClusterDomain }}",
190+
},
191+
},
192+
"image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}",
193+
"name": "nginx", "ports": []interface{}{
194+
map[string]interface{}{
195+
"containerPort": int64(80),
196+
},
197+
},
198+
"resources": map[string]interface{}{},
199+
},
200+
},
201+
}, specMap)
202+
203+
assert.Equal(t, helmify.Values{
204+
"nginx": map[string]interface{}{
205+
"nginx": map[string]interface{}{
206+
"image": map[string]interface{}{
207+
"repository": "nginx",
208+
"tag": "1.14.2@sha256:cb5c1bddd1b5665e1867a7fa1b5fa843a47ee433bbb75d4293888b71def53229",
209+
},
210+
},
211+
},
212+
}, tmpl)
213+
})
214+
152215
}

0 commit comments

Comments
 (0)