Skip to content

feat: use endpoint names as container port names if they shorter than 16 characters #736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/common/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"regexp"
"strings"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/constants"
)

Expand All @@ -36,6 +37,13 @@ func EndpointName(endpointName string) string {
return name
}

func PortName(endpoint dw.Endpoint) string {
if len(endpoint.Name) <= 15 {
return endpoint.Name
}
return fmt.Sprintf("%d-%s", endpoint.TargetPort, endpoint.Protocol)
}

func ServiceName(workspaceId string) string {
return fmt.Sprintf("%s-%s", workspaceId, "service")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/library/container/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/common"

"github.com/devfile/devworkspace-operator/pkg/config"
"github.com/devfile/devworkspace-operator/pkg/constants"
Expand Down Expand Up @@ -62,7 +63,7 @@ func devfileEndpointsToContainerPorts(endpoints []dw.Endpoint) []v1.ContainerPor
}
containerPorts = append(containerPorts, v1.ContainerPort{
// Use meaningless name for port since endpoint.Name does not match requirements for ContainerPort name
Name: fmt.Sprintf("%d-%s", endpoint.TargetPort, endpoint.Protocol),
Name: common.PortName(endpoint),
ContainerPort: int32(endpoint.TargetPort),
Protocol: v1.ProtocolTCP,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ output:
- "test"
- "args"
ports:
- name: "3100-wss"
- name: "test-endpoint-1"
containerPort: 3100
protocol: TCP
- name: "8080-http"
- name: "test-endpoint-2"
containerPort: 8080
protocol: TCP
volumeMounts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ output:
- name: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-1"
ports:
- name: "3100-http"
- name: "test-endpoint-1"
containerPort: 3100
protocol: TCP
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "Uses endpoint name as port name if it it fits pod spec"

input:
components:
- name: testing-container-1
container:
image: testing-image-1
memoryRequest: "-1" # isolate test to not include this field
memoryLimit: "-1" # isolate test to not include this field
cpuRequest: "-1" # isolate test to not include this field
cpuLimit: "-1" # isolate test to not include this field
mountSources: false
endpoints:
- name: "short-name" # Should use endpoint name if <=15 chars long (as supported by pod spec)
targetPort: 8080
protocol: http
- name: "longer-endpoint-name" # Should fallback to "<port>-<protocol>" for names too long for pod spec
targetPort: 8081
protocol: http

output:
podAdditions:
containers:
- name: testing-container-1
image: testing-image-1
imagePullPolicy: Always
resources:
requests:
memory: "-1"
cpu: "-1"
limits:
memory: "-1"
cpu: "-1"
env:
- name: "DEVWORKSPACE_COMPONENT_NAME"
value: "testing-container-1"
ports:
- name: "short-name"
containerPort: 8080
protocol: TCP
- name: "8081-http"
containerPort: 8081
protocol: TCP