Skip to content
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

use endpoint name as container port name and service port name #128

Merged
merged 1 commit into from
Jan 21, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down Expand Up @@ -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),
},
Expand Down
8 changes: 6 additions & 2 deletions pkg/devfile/generator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are changing a user provided name underneath the covers if length exceeds 15 characters. It'll be good to warn them of these changes ahead of time (via cmd line, log, and/or doc)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. This change is because we want to unify the service port name & the container port name. since service port does not accept starting with a number (has to be alphabet), thus we use the convention port-<number>
@valaparthvi FYI.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the warning need to happen on the odo side?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

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{
Expand Down Expand Up @@ -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)
}
}
Expand Down
60 changes: 37 additions & 23 deletions pkg/devfile/generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
},
Expand All @@ -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",
},
Expand Down Expand Up @@ -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
Expand All @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should cover most of our scenarios since we have validation on unique ports but should we handle the exception case as well where dedicatedPod=true? /~https://github.com/devfile/api/blob/main/pkg/validation/validation-rule.md#endpoints

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do not specifially do tests for deficatedPod in library, since it is currently not supported in library. see devfile/api#670

containerComponents: []v1.Component{
{
Name: "testcontainer1",
Expand All @@ -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,
},
},
},
Expand All @@ -722,11 +741,6 @@ func TestGetServiceSpec(t *testing.T) {
Port: 8080,
TargetPort: intstr.FromInt(8080),
},
{
Name: "port-9090",
Port: 9090,
TargetPort: intstr.FromInt(9090),
},
},
},
{
Expand Down Expand Up @@ -755,7 +769,7 @@ func TestGetServiceSpec(t *testing.T) {
Container: &v1.ContainerComponent{
Endpoints: []v1.Endpoint{
{
Name: endpointNames[2],
Name: endpointNames[1],
TargetPort: 9090,
},
},
Expand All @@ -768,7 +782,7 @@ func TestGetServiceSpec(t *testing.T) {
},
wantPorts: []corev1.ServicePort{
{
Name: "port-9090",
Name: endpointNames[1],
Port: 9090,
TargetPort: intstr.FromInt(9090),
},
Expand All @@ -784,7 +798,7 @@ func TestGetServiceSpec(t *testing.T) {
Container: &v1.ContainerComponent{
Endpoints: []v1.Endpoint{
{
Name: endpointNames[2],
Name: endpointNames[1],
TargetPort: 9090,
},
},
Expand Down
18 changes: 12 additions & 6 deletions pkg/devfile/parser/data/v2/2.2.0/devfileJsonSchema220.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ const JsonSchema220 = `{
},
"name": {
"type": "string",
"maxLength": 63,
"maxLength": 15,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have corresponding updates in our API docs to show the new max length?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the spec is the api doc

"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
},
"path": {
Expand All @@ -372,6 +372,7 @@ const JsonSchema220 = `{
"type": "boolean"
},
"targetPort": {
"description": "The port number should be unique.",
"type": "integer"
}
},
Expand Down Expand Up @@ -618,7 +619,7 @@ const JsonSchema220 = `{
},
"name": {
"type": "string",
"maxLength": 63,
"maxLength": 15,
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
},
"path": {
Expand All @@ -643,6 +644,7 @@ const JsonSchema220 = `{
"type": "boolean"
},
"targetPort": {
"description": "The port number should be unique.",
"type": "integer"
}
},
Expand Down Expand Up @@ -719,7 +721,7 @@ const JsonSchema220 = `{
},
"name": {
"type": "string",
"maxLength": 63,
"maxLength": 15,
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
},
"path": {
Expand All @@ -744,6 +746,7 @@ const JsonSchema220 = `{
"type": "boolean"
},
"targetPort": {
"description": "The port number should be unique.",
"type": "integer"
}
},
Expand Down Expand Up @@ -1231,7 +1234,7 @@ const JsonSchema220 = `{
},
"name": {
"type": "string",
"maxLength": 63,
"maxLength": 15,
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
},
"path": {
Expand All @@ -1255,6 +1258,7 @@ const JsonSchema220 = `{
"type": "boolean"
},
"targetPort": {
"description": "The port number should be unique.",
"type": "integer"
}
},
Expand Down Expand Up @@ -1493,7 +1497,7 @@ const JsonSchema220 = `{
},
"name": {
"type": "string",
"maxLength": 63,
"maxLength": 15,
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
},
"path": {
Expand All @@ -1517,6 +1521,7 @@ const JsonSchema220 = `{
"type": "boolean"
},
"targetPort": {
"description": "The port number should be unique.",
"type": "integer"
}
},
Expand Down Expand Up @@ -1591,7 +1596,7 @@ const JsonSchema220 = `{
},
"name": {
"type": "string",
"maxLength": 63,
"maxLength": 15,
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
},
"path": {
Expand All @@ -1615,6 +1620,7 @@ const JsonSchema220 = `{
"type": "boolean"
},
"targetPort": {
"description": "The port number should be unique.",
"type": "integer"
}
},
Expand Down