Skip to content

Commit

Permalink
fix image processing for repo with port
Browse files Browse the repository at this point in the history
  • Loading branch information
arttor committed Nov 22, 2023
1 parent 7c69167 commit 909d091
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/processor/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ 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.Index(c.Image, ":")
index := strings.LastIndex(c.Image, ":")
if strings.Contains(c.Image, "@") && strings.Count(c.Image, ":") >= 2 {
last := strings.LastIndex(c.Image, ":")
index = strings.LastIndex(c.Image[:last], ":")
}
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 @@ -87,6 +87,30 @@ spec:
ports:
- containerPort: 80
`

strDeploymentWithPort = `
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: localhost:6001/my_project:latest
ports:
- containerPort: 80
`
)

func Test_pod_Process(t *testing.T) {
Expand Down Expand Up @@ -212,4 +236,43 @@ func Test_pod_Process(t *testing.T) {
}, tmpl)
})

t.Run("deployment with image tag and port", func(t *testing.T) {
var deploy appsv1.Deployment
obj := internal.GenerateObj(strDeploymentWithPort)
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": "localhost:6001/my_project",
"tag": "latest",
},
},
},
}, tmpl)
})

}

0 comments on commit 909d091

Please sign in to comment.