Skip to content

Commit e2ed92e

Browse files
committed
Fix issue with duplicate containerPort due to webviews endpoint
Need to make sure we only add each exposed port once on a container Signed-off-by: Angel Misevski <[email protected]>
1 parent c5c0fd9 commit e2ed92e

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

pkg/library/container/container_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func TestGetKubeContainersFromDevfile(t *testing.T) {
6767
loadTestCaseOrPanic(t, "detects-init-containers.yaml"),
6868
loadTestCaseOrPanic(t, "handles-mountSources.yaml"),
6969
loadTestCaseOrPanic(t, "handles-resources.yaml"),
70+
loadTestCaseOrPanic(t, "handles-endpoints-with-common-port.yaml"),
7071
loadTestCaseOrPanic(t, "ignores-non-container-components.yaml"),
7172
loadTestCaseOrPanic(t, "converts-all-fields.yaml"),
7273
loadTestCaseOrPanic(t, "error-has-parent.yaml"),

pkg/library/container/conversion.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,18 @@ func convertContainerToK8s(devfileComponent v1alpha2.Component) (*v1.Container,
4949

5050
func devfileEndpointsToContainerPorts(endpoints []v1alpha2.Endpoint) []v1.ContainerPort {
5151
var containerPorts []v1.ContainerPort
52+
exposedPorts := map[int]bool{}
5253
for _, endpoint := range endpoints {
54+
if exposedPorts[endpoint.TargetPort] {
55+
continue
56+
}
5357
containerPorts = append(containerPorts, v1.ContainerPort{
5458
// Use meaningless name for port since endpoint.Name does not match requirements for ContainerPort name
5559
Name: fmt.Sprintf("%d-%s", endpoint.TargetPort, endpoint.Protocol),
5660
ContainerPort: int32(endpoint.TargetPort),
5761
Protocol: v1.ProtocolTCP,
5862
})
63+
exposedPorts[endpoint.TargetPort] = true
5964
}
6065
return containerPorts
6166
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: "Handles container with multiple endpoints with same targetPort"
2+
3+
input:
4+
components:
5+
- name: testing-container-1
6+
container:
7+
image: testing-image-1
8+
memoryLimit: 999Mi # isolate test to not include memoryLimit
9+
mountSources: false
10+
endpoints:
11+
- name: "test-endpoint-1"
12+
targetPort: 3100
13+
protocol: http
14+
- name: "test-endpoint-2"
15+
targetPort: 3100
16+
protocol: http
17+
18+
output:
19+
podAdditions:
20+
containers:
21+
- name: testing-container-1
22+
image: testing-image-1
23+
imagePullPolicy: Always
24+
resources:
25+
limits:
26+
memory: "999Mi"
27+
ports:
28+
- name: "3100-http"
29+
containerPort: 3100
30+
protocol: TCP

pkg/library/storage/commonStorage_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,14 @@ func TestRewriteContainerVolumeMounts(t *testing.T) {
8383
t.Run(tt.Name, func(t *testing.T) {
8484
// sanity check that file is read correctly.
8585
assert.NotNil(t, tt.Input.Workspace, "Input does not define workspace")
86-
podAdditions := v1alpha1.PodAdditions(tt.Input.PodAdditions)
87-
err := RewriteContainerVolumeMounts(tt.Input.WorkspaceID, &podAdditions, tt.Input.Workspace)
86+
err := RewriteContainerVolumeMounts(tt.Input.WorkspaceID, &tt.Input.PodAdditions, tt.Input.Workspace)
8887
if tt.Output.ErrRegexp != nil && assert.Error(t, err) {
8988
assert.Regexp(t, *tt.Output.ErrRegexp, err.Error(), "Error message should match")
9089
} else {
9190
if !assert.NoError(t, err, "Should not return error") {
9291
return
9392
}
94-
assert.Equal(t, tt.Output.PodAdditions, podAdditions, "PodAdditions should match expected output")
93+
assert.Equal(t, tt.Output.PodAdditions, tt.Input.PodAdditions, "PodAdditions should match expected output")
9594
}
9695
})
9796
}

0 commit comments

Comments
 (0)