diff --git a/go.mod b/go.mod index 1d105c37..827ce877 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/devfile/library go 1.15 require ( - github.com/devfile/api/v2 v2.0.0-20220105201057-dd1d65d4d91f + github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c github.com/fatih/color v1.7.0 github.com/gobwas/glob v0.2.3 github.com/golang/mock v1.5.0 diff --git a/go.sum b/go.sum index ec6159ef..53d6ff84 100644 --- a/go.sum +++ b/go.sum @@ -83,8 +83,8 @@ github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devfile/api/v2 v2.0.0-20220105201057-dd1d65d4d91f h1:Mdj4fXcQ0hEd7iMsqwydqrW0JfxepoPjzwxrshtvqYY= -github.com/devfile/api/v2 v2.0.0-20220105201057-dd1d65d4d91f/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q= +github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c h1:sjghKUov/WT71dBreHYQcTgQctxHT/p4uAhUFdGQfSU= +github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index 2bcbbab2..733064e7 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -911,7 +911,7 @@ func TestGetService(t *testing.T) { Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ { - Name: "port-8080", + Name: "http-8080", Port: 8080, TargetPort: intstr.FromInt(8080), }, @@ -949,7 +949,7 @@ func TestGetService(t *testing.T) { Spec: corev1.ServiceSpec{ Ports: []corev1.ServicePort{ { - Name: "port-8080", + Name: "http-8080", Port: 8080, TargetPort: intstr.FromInt(8080), }, diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index f6816462..0b1567ae 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -46,7 +46,12 @@ func convertPorts(endpoints []v1.Endpoint) []corev1.ContainerPort { } else { portProtocol = corev1.ProtocolTCP } - name := fmt.Sprintf("%d-%s", portNumber, strings.ToLower(string(portProtocol))) + name := endpoint.Name + if len(name) > 15 { + // to be compatible with endpoint longer than 15 chars + name = fmt.Sprintf("port-%v", portNumber) + } + if _, exist := portMap[name]; !exist { portMap[name] = true containerPorts = append(containerPorts, corev1.ContainerPort{ @@ -268,7 +273,6 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri } // if Exposure == none, should not create a service for that port if !portExist && portExposureMap[int(port.ContainerPort)] != v1.NoneEndpointExposure { - port.Name = fmt.Sprintf("port-%v", port.ContainerPort) containerPorts = append(containerPorts, port) } } diff --git a/pkg/devfile/generator/utils_test.go b/pkg/devfile/generator/utils_test.go index f095d2ce..575ecc4a 100644 --- a/pkg/devfile/generator/utils_test.go +++ b/pkg/devfile/generator/utils_test.go @@ -97,7 +97,7 @@ func TestConvertEnvs(t *testing.T) { } func TestConvertPorts(t *testing.T) { - endpointsNames := []string{"endpoint1", "endpoint2"} + endpointsNames := []string{"endpoint1", "endpoint2", "a-very-long-port-name-before-endpoint-length-limit-8080"} endpointsPorts := []int{8080, 9090} tests := []struct { name string @@ -114,7 +114,23 @@ func TestConvertPorts(t *testing.T) { }, want: []corev1.ContainerPort{ { - Name: "8080-tcp", + Name: endpointsNames[0], + ContainerPort: int32(endpointsPorts[0]), + Protocol: "TCP", + }, + }, + }, + { + name: "One Endpoint with >15 chars length", + endpoints: []v1.Endpoint{ + { + Name: endpointsNames[2], + TargetPort: endpointsPorts[0], + }, + }, + want: []corev1.ContainerPort{ + { + Name: "port-8080", ContainerPort: int32(endpointsPorts[0]), Protocol: "TCP", }, @@ -134,12 +150,12 @@ func TestConvertPorts(t *testing.T) { }, want: []corev1.ContainerPort{ { - Name: "8080-tcp", + Name: endpointsNames[0], ContainerPort: int32(endpointsPorts[0]), Protocol: "TCP", }, { - Name: "9090-tcp", + Name: endpointsNames[1], ContainerPort: int32(endpointsPorts[1]), Protocol: "TCP", }, @@ -652,7 +668,7 @@ func TestGetPodTemplateSpec(t *testing.T) { func TestGetServiceSpec(t *testing.T) { - endpointNames := []string{"port-8080-1", "port-8080-2", "port-9090"} + endpointNames := []string{"port-8080-url", "port-9090-url", "a-very-long-port-name-before-endpoint-length-limit-8080"} tests := []struct { name string @@ -663,7 +679,7 @@ func TestGetServiceSpec(t *testing.T) { wantPorts []corev1.ServicePort }{ { - name: "multiple endpoints share the same port", + name: "multiple endpoints have different ports", containerComponents: []v1.Component{ { Name: "testcontainer1", @@ -676,37 +692,40 @@ func TestGetServiceSpec(t *testing.T) { }, { Name: endpointNames[1], - TargetPort: 8080, + TargetPort: 9090, }, }, }, }, }, }, - labels: map[string]string{}, + labels: map[string]string{ + "component": "testcomponent", + }, wantPorts: []corev1.ServicePort{ { - Name: "port-8080", + Name: endpointNames[0], Port: 8080, TargetPort: intstr.FromInt(8080), }, + { + Name: endpointNames[1], + Port: 9090, + TargetPort: intstr.FromInt(9090), + }, }, }, { - name: "multiple endpoints have different ports", + name: "long port name before endpoint length limit to <=15", containerComponents: []v1.Component{ { Name: "testcontainer1", ComponentUnion: v1.ComponentUnion{ Container: &v1.ContainerComponent{ Endpoints: []v1.Endpoint{ - { - Name: endpointNames[0], - TargetPort: 8080, - }, { Name: endpointNames[2], - TargetPort: 9090, + TargetPort: 8080, }, }, }, @@ -722,11 +741,6 @@ func TestGetServiceSpec(t *testing.T) { Port: 8080, TargetPort: intstr.FromInt(8080), }, - { - Name: "port-9090", - Port: 9090, - TargetPort: intstr.FromInt(9090), - }, }, }, { @@ -755,7 +769,7 @@ func TestGetServiceSpec(t *testing.T) { Container: &v1.ContainerComponent{ Endpoints: []v1.Endpoint{ { - Name: endpointNames[2], + Name: endpointNames[1], TargetPort: 9090, }, }, @@ -768,7 +782,7 @@ func TestGetServiceSpec(t *testing.T) { }, wantPorts: []corev1.ServicePort{ { - Name: "port-9090", + Name: endpointNames[1], Port: 9090, TargetPort: intstr.FromInt(9090), }, @@ -784,7 +798,7 @@ func TestGetServiceSpec(t *testing.T) { Container: &v1.ContainerComponent{ Endpoints: []v1.Endpoint{ { - Name: endpointNames[2], + Name: endpointNames[1], TargetPort: 9090, }, }, diff --git a/pkg/devfile/parser/data/v2/2.2.0/devfileJsonSchema220.go b/pkg/devfile/parser/data/v2/2.2.0/devfileJsonSchema220.go index ee8b553c..54e36df5 100644 --- a/pkg/devfile/parser/data/v2/2.2.0/devfileJsonSchema220.go +++ b/pkg/devfile/parser/data/v2/2.2.0/devfileJsonSchema220.go @@ -347,7 +347,7 @@ const JsonSchema220 = `{ }, "name": { "type": "string", - "maxLength": 63, + "maxLength": 15, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }, "path": { @@ -372,6 +372,7 @@ const JsonSchema220 = `{ "type": "boolean" }, "targetPort": { + "description": "The port number should be unique.", "type": "integer" } }, @@ -618,7 +619,7 @@ const JsonSchema220 = `{ }, "name": { "type": "string", - "maxLength": 63, + "maxLength": 15, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }, "path": { @@ -643,6 +644,7 @@ const JsonSchema220 = `{ "type": "boolean" }, "targetPort": { + "description": "The port number should be unique.", "type": "integer" } }, @@ -719,7 +721,7 @@ const JsonSchema220 = `{ }, "name": { "type": "string", - "maxLength": 63, + "maxLength": 15, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }, "path": { @@ -744,6 +746,7 @@ const JsonSchema220 = `{ "type": "boolean" }, "targetPort": { + "description": "The port number should be unique.", "type": "integer" } }, @@ -1231,7 +1234,7 @@ const JsonSchema220 = `{ }, "name": { "type": "string", - "maxLength": 63, + "maxLength": 15, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }, "path": { @@ -1255,6 +1258,7 @@ const JsonSchema220 = `{ "type": "boolean" }, "targetPort": { + "description": "The port number should be unique.", "type": "integer" } }, @@ -1493,7 +1497,7 @@ const JsonSchema220 = `{ }, "name": { "type": "string", - "maxLength": 63, + "maxLength": 15, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }, "path": { @@ -1517,6 +1521,7 @@ const JsonSchema220 = `{ "type": "boolean" }, "targetPort": { + "description": "The port number should be unique.", "type": "integer" } }, @@ -1591,7 +1596,7 @@ const JsonSchema220 = `{ }, "name": { "type": "string", - "maxLength": 63, + "maxLength": 15, "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" }, "path": { @@ -1615,6 +1620,7 @@ const JsonSchema220 = `{ "type": "boolean" }, "targetPort": { + "description": "The port number should be unique.", "type": "integer" } },