Skip to content

Commit 909d091

Browse files
committed
fix image processing for repo with port
1 parent 7c69167 commit 909d091

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

pkg/processor/pod/pod.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ 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.Index(c.Image, ":")
169+
index := strings.LastIndex(c.Image, ":")
170+
if strings.Contains(c.Image, "@") && strings.Count(c.Image, ":") >= 2 {
171+
last := strings.LastIndex(c.Image, ":")
172+
index = strings.LastIndex(c.Image[:last], ":")
173+
}
170174
if index < 0 {
171175
return c, fmt.Errorf("wrong image format: %q", c.Image)
172176
}

pkg/processor/pod/pod_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ spec:
8787
ports:
8888
- containerPort: 80
8989
`
90+
91+
strDeploymentWithPort = `
92+
apiVersion: apps/v1
93+
kind: Deployment
94+
metadata:
95+
name: nginx-deployment
96+
labels:
97+
app: nginx
98+
spec:
99+
replicas: 3
100+
selector:
101+
matchLabels:
102+
app: nginx
103+
template:
104+
metadata:
105+
labels:
106+
app: nginx
107+
spec:
108+
containers:
109+
- name: nginx
110+
image: localhost:6001/my_project:latest
111+
ports:
112+
- containerPort: 80
113+
`
90114
)
91115

92116
func Test_pod_Process(t *testing.T) {
@@ -212,4 +236,43 @@ func Test_pod_Process(t *testing.T) {
212236
}, tmpl)
213237
})
214238

239+
t.Run("deployment with image tag and port", func(t *testing.T) {
240+
var deploy appsv1.Deployment
241+
obj := internal.GenerateObj(strDeploymentWithPort)
242+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy)
243+
specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec)
244+
assert.NoError(t, err)
245+
246+
assert.Equal(t, map[string]interface{}{
247+
"containers": []interface{}{
248+
map[string]interface{}{
249+
"env": []interface{}{
250+
map[string]interface{}{
251+
"name": "KUBERNETES_CLUSTER_DOMAIN",
252+
"value": "{{ quote .Values.kubernetesClusterDomain }}",
253+
},
254+
},
255+
"image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}",
256+
"name": "nginx", "ports": []interface{}{
257+
map[string]interface{}{
258+
"containerPort": int64(80),
259+
},
260+
},
261+
"resources": map[string]interface{}{},
262+
},
263+
},
264+
}, specMap)
265+
266+
assert.Equal(t, helmify.Values{
267+
"nginx": map[string]interface{}{
268+
"nginx": map[string]interface{}{
269+
"image": map[string]interface{}{
270+
"repository": "localhost:6001/my_project",
271+
"tag": "latest",
272+
},
273+
},
274+
},
275+
}, tmpl)
276+
})
277+
215278
}

0 commit comments

Comments
 (0)