diff --git a/pkg/common/naming.go b/pkg/common/naming.go index b47915031..bf702af0d 100644 --- a/pkg/common/naming.go +++ b/pkg/common/naming.go @@ -20,6 +20,7 @@ import ( "regexp" "strings" + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/devworkspace-operator/pkg/constants" ) @@ -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") } diff --git a/pkg/library/container/conversion.go b/pkg/library/container/conversion.go index d21ebd177..bd9efa14c 100644 --- a/pkg/library/container/conversion.go +++ b/pkg/library/container/conversion.go @@ -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" @@ -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, }) diff --git a/pkg/library/container/testdata/component/converts-all-fields.yaml b/pkg/library/container/testdata/component/converts-all-fields.yaml index c494ca4fb..2260202b2 100644 --- a/pkg/library/container/testdata/component/converts-all-fields.yaml +++ b/pkg/library/container/testdata/component/converts-all-fields.yaml @@ -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: diff --git a/pkg/library/container/testdata/container/endpoints-common-port.yaml b/pkg/library/container/testdata/container/endpoints-common-port.yaml index af6b75180..9375c4fb6 100644 --- a/pkg/library/container/testdata/container/endpoints-common-port.yaml +++ b/pkg/library/container/testdata/container/endpoints-common-port.yaml @@ -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 diff --git a/pkg/library/container/testdata/container/endpoints-uses-name-if-shorter-than-15-chars.yaml b/pkg/library/container/testdata/container/endpoints-uses-name-if-shorter-than-15-chars.yaml new file mode 100644 index 000000000..4f840cfd2 --- /dev/null +++ b/pkg/library/container/testdata/container/endpoints-uses-name-if-shorter-than-15-chars.yaml @@ -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 "-" 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