Skip to content

Commit 2e92729

Browse files
authored
Merge pull request #128 from yangcao77/700-useEndpointAsPortName
use endpoint name as container port name and service port name
2 parents fe639c2 + d259e1e commit 2e92729

File tree

6 files changed

+60
-36
lines changed

6 files changed

+60
-36
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/devfile/library
33
go 1.15
44

55
require (
6-
github.com/devfile/api/v2 v2.0.0-20220105201057-dd1d65d4d91f
6+
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c
77
github.com/fatih/color v1.7.0
88
github.com/gobwas/glob v0.2.3
99
github.com/golang/mock v1.5.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ
8383
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8484
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8585
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
86-
github.com/devfile/api/v2 v2.0.0-20220105201057-dd1d65d4d91f h1:Mdj4fXcQ0hEd7iMsqwydqrW0JfxepoPjzwxrshtvqYY=
87-
github.com/devfile/api/v2 v2.0.0-20220105201057-dd1d65d4d91f/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
86+
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c h1:sjghKUov/WT71dBreHYQcTgQctxHT/p4uAhUFdGQfSU=
87+
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
8888
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
8989
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
9090
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=

pkg/devfile/generator/generators_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ func TestGetService(t *testing.T) {
911911
Spec: corev1.ServiceSpec{
912912
Ports: []corev1.ServicePort{
913913
{
914-
Name: "port-8080",
914+
Name: "http-8080",
915915
Port: 8080,
916916
TargetPort: intstr.FromInt(8080),
917917
},
@@ -949,7 +949,7 @@ func TestGetService(t *testing.T) {
949949
Spec: corev1.ServiceSpec{
950950
Ports: []corev1.ServicePort{
951951
{
952-
Name: "port-8080",
952+
Name: "http-8080",
953953
Port: 8080,
954954
TargetPort: intstr.FromInt(8080),
955955
},

pkg/devfile/generator/utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ func convertPorts(endpoints []v1.Endpoint) []corev1.ContainerPort {
4646
} else {
4747
portProtocol = corev1.ProtocolTCP
4848
}
49-
name := fmt.Sprintf("%d-%s", portNumber, strings.ToLower(string(portProtocol)))
49+
name := endpoint.Name
50+
if len(name) > 15 {
51+
// to be compatible with endpoint longer than 15 chars
52+
name = fmt.Sprintf("port-%v", portNumber)
53+
}
54+
5055
if _, exist := portMap[name]; !exist {
5156
portMap[name] = true
5257
containerPorts = append(containerPorts, corev1.ContainerPort{
@@ -268,7 +273,6 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
268273
}
269274
// if Exposure == none, should not create a service for that port
270275
if !portExist && portExposureMap[int(port.ContainerPort)] != v1.NoneEndpointExposure {
271-
port.Name = fmt.Sprintf("port-%v", port.ContainerPort)
272276
containerPorts = append(containerPorts, port)
273277
}
274278
}

pkg/devfile/generator/utils_test.go

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestConvertEnvs(t *testing.T) {
9797
}
9898

9999
func TestConvertPorts(t *testing.T) {
100-
endpointsNames := []string{"endpoint1", "endpoint2"}
100+
endpointsNames := []string{"endpoint1", "endpoint2", "a-very-long-port-name-before-endpoint-length-limit-8080"}
101101
endpointsPorts := []int{8080, 9090}
102102
tests := []struct {
103103
name string
@@ -114,7 +114,23 @@ func TestConvertPorts(t *testing.T) {
114114
},
115115
want: []corev1.ContainerPort{
116116
{
117-
Name: "8080-tcp",
117+
Name: endpointsNames[0],
118+
ContainerPort: int32(endpointsPorts[0]),
119+
Protocol: "TCP",
120+
},
121+
},
122+
},
123+
{
124+
name: "One Endpoint with >15 chars length",
125+
endpoints: []v1.Endpoint{
126+
{
127+
Name: endpointsNames[2],
128+
TargetPort: endpointsPorts[0],
129+
},
130+
},
131+
want: []corev1.ContainerPort{
132+
{
133+
Name: "port-8080",
118134
ContainerPort: int32(endpointsPorts[0]),
119135
Protocol: "TCP",
120136
},
@@ -134,12 +150,12 @@ func TestConvertPorts(t *testing.T) {
134150
},
135151
want: []corev1.ContainerPort{
136152
{
137-
Name: "8080-tcp",
153+
Name: endpointsNames[0],
138154
ContainerPort: int32(endpointsPorts[0]),
139155
Protocol: "TCP",
140156
},
141157
{
142-
Name: "9090-tcp",
158+
Name: endpointsNames[1],
143159
ContainerPort: int32(endpointsPorts[1]),
144160
Protocol: "TCP",
145161
},
@@ -652,7 +668,7 @@ func TestGetPodTemplateSpec(t *testing.T) {
652668

653669
func TestGetServiceSpec(t *testing.T) {
654670

655-
endpointNames := []string{"port-8080-1", "port-8080-2", "port-9090"}
671+
endpointNames := []string{"port-8080-url", "port-9090-url", "a-very-long-port-name-before-endpoint-length-limit-8080"}
656672

657673
tests := []struct {
658674
name string
@@ -663,7 +679,7 @@ func TestGetServiceSpec(t *testing.T) {
663679
wantPorts []corev1.ServicePort
664680
}{
665681
{
666-
name: "multiple endpoints share the same port",
682+
name: "multiple endpoints have different ports",
667683
containerComponents: []v1.Component{
668684
{
669685
Name: "testcontainer1",
@@ -676,37 +692,40 @@ func TestGetServiceSpec(t *testing.T) {
676692
},
677693
{
678694
Name: endpointNames[1],
679-
TargetPort: 8080,
695+
TargetPort: 9090,
680696
},
681697
},
682698
},
683699
},
684700
},
685701
},
686-
labels: map[string]string{},
702+
labels: map[string]string{
703+
"component": "testcomponent",
704+
},
687705
wantPorts: []corev1.ServicePort{
688706
{
689-
Name: "port-8080",
707+
Name: endpointNames[0],
690708
Port: 8080,
691709
TargetPort: intstr.FromInt(8080),
692710
},
711+
{
712+
Name: endpointNames[1],
713+
Port: 9090,
714+
TargetPort: intstr.FromInt(9090),
715+
},
693716
},
694717
},
695718
{
696-
name: "multiple endpoints have different ports",
719+
name: "long port name before endpoint length limit to <=15",
697720
containerComponents: []v1.Component{
698721
{
699722
Name: "testcontainer1",
700723
ComponentUnion: v1.ComponentUnion{
701724
Container: &v1.ContainerComponent{
702725
Endpoints: []v1.Endpoint{
703-
{
704-
Name: endpointNames[0],
705-
TargetPort: 8080,
706-
},
707726
{
708727
Name: endpointNames[2],
709-
TargetPort: 9090,
728+
TargetPort: 8080,
710729
},
711730
},
712731
},
@@ -722,11 +741,6 @@ func TestGetServiceSpec(t *testing.T) {
722741
Port: 8080,
723742
TargetPort: intstr.FromInt(8080),
724743
},
725-
{
726-
Name: "port-9090",
727-
Port: 9090,
728-
TargetPort: intstr.FromInt(9090),
729-
},
730744
},
731745
},
732746
{
@@ -755,7 +769,7 @@ func TestGetServiceSpec(t *testing.T) {
755769
Container: &v1.ContainerComponent{
756770
Endpoints: []v1.Endpoint{
757771
{
758-
Name: endpointNames[2],
772+
Name: endpointNames[1],
759773
TargetPort: 9090,
760774
},
761775
},
@@ -768,7 +782,7 @@ func TestGetServiceSpec(t *testing.T) {
768782
},
769783
wantPorts: []corev1.ServicePort{
770784
{
771-
Name: "port-9090",
785+
Name: endpointNames[1],
772786
Port: 9090,
773787
TargetPort: intstr.FromInt(9090),
774788
},
@@ -784,7 +798,7 @@ func TestGetServiceSpec(t *testing.T) {
784798
Container: &v1.ContainerComponent{
785799
Endpoints: []v1.Endpoint{
786800
{
787-
Name: endpointNames[2],
801+
Name: endpointNames[1],
788802
TargetPort: 9090,
789803
},
790804
},

pkg/devfile/parser/data/v2/2.2.0/devfileJsonSchema220.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ const JsonSchema220 = `{
347347
},
348348
"name": {
349349
"type": "string",
350-
"maxLength": 63,
350+
"maxLength": 15,
351351
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
352352
},
353353
"path": {
@@ -372,6 +372,7 @@ const JsonSchema220 = `{
372372
"type": "boolean"
373373
},
374374
"targetPort": {
375+
"description": "The port number should be unique.",
375376
"type": "integer"
376377
}
377378
},
@@ -618,7 +619,7 @@ const JsonSchema220 = `{
618619
},
619620
"name": {
620621
"type": "string",
621-
"maxLength": 63,
622+
"maxLength": 15,
622623
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
623624
},
624625
"path": {
@@ -643,6 +644,7 @@ const JsonSchema220 = `{
643644
"type": "boolean"
644645
},
645646
"targetPort": {
647+
"description": "The port number should be unique.",
646648
"type": "integer"
647649
}
648650
},
@@ -719,7 +721,7 @@ const JsonSchema220 = `{
719721
},
720722
"name": {
721723
"type": "string",
722-
"maxLength": 63,
724+
"maxLength": 15,
723725
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
724726
},
725727
"path": {
@@ -744,6 +746,7 @@ const JsonSchema220 = `{
744746
"type": "boolean"
745747
},
746748
"targetPort": {
749+
"description": "The port number should be unique.",
747750
"type": "integer"
748751
}
749752
},
@@ -1231,7 +1234,7 @@ const JsonSchema220 = `{
12311234
},
12321235
"name": {
12331236
"type": "string",
1234-
"maxLength": 63,
1237+
"maxLength": 15,
12351238
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
12361239
},
12371240
"path": {
@@ -1255,6 +1258,7 @@ const JsonSchema220 = `{
12551258
"type": "boolean"
12561259
},
12571260
"targetPort": {
1261+
"description": "The port number should be unique.",
12581262
"type": "integer"
12591263
}
12601264
},
@@ -1493,7 +1497,7 @@ const JsonSchema220 = `{
14931497
},
14941498
"name": {
14951499
"type": "string",
1496-
"maxLength": 63,
1500+
"maxLength": 15,
14971501
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
14981502
},
14991503
"path": {
@@ -1517,6 +1521,7 @@ const JsonSchema220 = `{
15171521
"type": "boolean"
15181522
},
15191523
"targetPort": {
1524+
"description": "The port number should be unique.",
15201525
"type": "integer"
15211526
}
15221527
},
@@ -1591,7 +1596,7 @@ const JsonSchema220 = `{
15911596
},
15921597
"name": {
15931598
"type": "string",
1594-
"maxLength": 63,
1599+
"maxLength": 15,
15951600
"pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
15961601
},
15971602
"path": {
@@ -1615,6 +1620,7 @@ const JsonSchema220 = `{
16151620
"type": "boolean"
16161621
},
16171622
"targetPort": {
1623+
"description": "The port number should be unique.",
16181624
"type": "integer"
16191625
}
16201626
},

0 commit comments

Comments
 (0)