Skip to content

Commit

Permalink
Handle images with digests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rsrchboy authored and arttor committed Nov 21, 2023
1 parent 0b132ed commit 7c69167
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/processor/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpe
}

func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Container, values *helmify.Values) (corev1.Container, error) {
index := strings.LastIndex(c.Image, ":")
index := strings.Index(c.Image, ":")
if index < 0 {
return c, fmt.Errorf("wrong image format: %q", c.Image)
}
Expand Down
63 changes: 63 additions & 0 deletions pkg/processor/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ spec:
- containerPort: 80
`

strDeploymentWithTagAndDigest = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2@sha256:cb5c1bddd1b5665e1867a7fa1b5fa843a47ee433bbb75d4293888b71def53229
ports:
- containerPort: 80
`

strDeploymentWithNoArgs = `
apiVersion: apps/v1
kind: Deployment
Expand Down Expand Up @@ -149,4 +173,43 @@ func Test_pod_Process(t *testing.T) {
}, tmpl)
})

t.Run("deployment with image tag and digest", func(t *testing.T) {
var deploy appsv1.Deployment
obj := internal.GenerateObj(strDeploymentWithTagAndDigest)
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy)
specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec)
assert.NoError(t, err)

assert.Equal(t, map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": []interface{}{
map[string]interface{}{
"name": "KUBERNETES_CLUSTER_DOMAIN",
"value": "{{ quote .Values.kubernetesClusterDomain }}",
},
},
"image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}",
"name": "nginx", "ports": []interface{}{
map[string]interface{}{
"containerPort": int64(80),
},
},
"resources": map[string]interface{}{},
},
},
}, specMap)

assert.Equal(t, helmify.Values{
"nginx": map[string]interface{}{
"nginx": map[string]interface{}{
"image": map[string]interface{}{
"repository": "nginx",
"tag": "1.14.2@sha256:cb5c1bddd1b5665e1867a7fa1b5fa843a47ee433bbb75d4293888b71def53229",
},
},
},
}, tmpl)
})

}

0 comments on commit 7c69167

Please sign in to comment.