Skip to content

Commit 741af7b

Browse files
authored
Merge pull request #51 from yangcao77/287-formatPodContainerPortName
287-format pod container port name
2 parents b2b6e1a + 811bcb2 commit 741af7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+228
-112
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/devfile/library
33
go 1.13
44

55
require (
6-
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e
6+
github.com/devfile/api/v2 v2.0.0-20210111205815-273464363288
77
github.com/fatih/color v1.7.0
88
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
99
github.com/gobwas/glob v0.2.3

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
4444
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4545
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4646
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
47-
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e h1:Jy3C3ul05YvL4bJpAVhFwPZD8neOJUBZy7GuCcjc8nc=
48-
github.com/devfile/api v0.0.0-20201211221100-a68230324c7e/go.mod h1:/aDiwWjDEW/fY1/Ig8umVtmneAXKZImnLvWqzMlcfrY=
47+
github.com/devfile/api/v2 v2.0.0-20210111205815-273464363288 h1:3Ea0dVgtCEyWSjobneehLnqIGoK2pS6bWnGnJ1ek24Q=
48+
github.com/devfile/api/v2 v2.0.0-20210111205815-273464363288/go.mod h1:ujP0i3nip2g/aSOXGEy3A7vTC6EIe1kZf9Cteja6OJg=
4949
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5050
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
5151
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=

pkg/devfile/generator/generators_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"reflect"
55
"testing"
66

7-
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
8-
"github.com/devfile/api/pkg/attributes"
7+
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
8+
"github.com/devfile/api/v2/pkg/attributes"
99
"github.com/devfile/library/pkg/devfile/parser"
1010
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1111
"github.com/devfile/library/pkg/testingutil"

pkg/devfile/generator/utils.go

+18-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"path/filepath"
66
"strings"
77

8-
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
8+
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
99
"github.com/devfile/library/pkg/devfile/parser"
1010
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
11-
"github.com/devfile/library/pkg/util"
1211
buildv1 "github.com/openshift/api/build/v1"
1312
routev1 "github.com/openshift/api/route/v1"
1413
appsv1 "k8s.io/api/apps/v1"
@@ -34,14 +33,25 @@ func convertEnvs(vars []v1.EnvVar) []corev1.EnvVar {
3433
// convertPorts converts endpoint variables from the devfile structure to kubernetes ContainerPort
3534
func convertPorts(endpoints []v1.Endpoint) []corev1.ContainerPort {
3635
containerPorts := []corev1.ContainerPort{}
36+
portMap := make(map[string]bool)
3737
for _, endpoint := range endpoints {
38-
name := strings.TrimSpace(util.GetDNS1123Name(strings.ToLower(endpoint.Name)))
39-
name = util.TruncateString(name, 15)
38+
var portProtocol corev1.Protocol
39+
portNumber := int32(endpoint.TargetPort)
4040

41-
containerPorts = append(containerPorts, corev1.ContainerPort{
42-
Name: name,
43-
ContainerPort: int32(endpoint.TargetPort),
44-
})
41+
if endpoint.Protocol == v1.UDPEndpointProtocol {
42+
portProtocol = corev1.ProtocolUDP
43+
} else {
44+
portProtocol = corev1.ProtocolTCP
45+
}
46+
name := fmt.Sprintf("%d-%s", portNumber, strings.ToLower(string(portProtocol)))
47+
if _, exist := portMap[name]; !exist {
48+
portMap[name] = true
49+
containerPorts = append(containerPorts, corev1.ContainerPort{
50+
Name: name,
51+
ContainerPort: portNumber,
52+
Protocol: portProtocol,
53+
})
54+
}
4555
}
4656
return containerPorts
4757
}

pkg/devfile/generator/utils_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"strings"
77
"testing"
88

9-
"github.com/devfile/api/pkg/attributes"
9+
"github.com/devfile/api/v2/pkg/attributes"
1010
"github.com/devfile/library/pkg/devfile/parser"
1111
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1212
"github.com/devfile/library/pkg/testingutil"
1313
buildv1 "github.com/openshift/api/build/v1"
1414

15-
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
15+
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
1616

1717
corev1 "k8s.io/api/core/v1"
1818
"k8s.io/apimachinery/pkg/api/resource"
@@ -108,13 +108,14 @@ func TestConvertPorts(t *testing.T) {
108108
},
109109
want: []corev1.ContainerPort{
110110
{
111-
Name: endpointsNames[0],
111+
Name: "8080-tcp",
112112
ContainerPort: int32(endpointsPorts[0]),
113+
Protocol: "TCP",
113114
},
114115
},
115116
},
116117
{
117-
name: "Case 2: Multiple env vars",
118+
name: "Case 2: Multiple endpoints",
118119
endpoints: []v1.Endpoint{
119120
{
120121
Name: endpointsNames[0],
@@ -127,12 +128,14 @@ func TestConvertPorts(t *testing.T) {
127128
},
128129
want: []corev1.ContainerPort{
129130
{
130-
Name: endpointsNames[0],
131+
Name: "8080-tcp",
131132
ContainerPort: int32(endpointsPorts[0]),
133+
Protocol: "TCP",
132134
},
133135
{
134-
Name: endpointsNames[1],
136+
Name: "9090-tcp",
135137
ContainerPort: int32(endpointsPorts[1]),
138+
Protocol: "TCP",
136139
},
137140
},
138141
},

pkg/devfile/parser/configurables.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strconv"
66
"strings"
77

8-
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
8+
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
99
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1010
corev1 "k8s.io/api/core/v1"
1111
)

pkg/devfile/parser/configurables_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55
"testing"
66

7-
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
7+
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
88
devfileCtx "github.com/devfile/library/pkg/devfile/parser/context"
99
v2 "github.com/devfile/library/pkg/devfile/parser/data/v2"
1010
"github.com/devfile/library/pkg/testingutil/filesystem"

pkg/devfile/parser/context/schema_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const (
6767
],
6868
"commands": [
6969
{
70-
"id": "download dependencies",
70+
"id": "download-dependencies",
7171
"exec": {
7272
"component": "nodejs",
7373
"commandLine": "npm install",
@@ -78,7 +78,7 @@ const (
7878
}
7979
},
8080
{
81-
"id": "run the app",
81+
"id": "run-the-app",
8282
"exec": {
8383
"component": "nodejs",
8484
"commandLine": "nodemon app.js",
@@ -90,7 +90,7 @@ const (
9090
}
9191
},
9292
{
93-
"id": "run the app (debugging enabled)",
93+
"id": "run-the-app-debugging-enabled",
9494
"exec": {
9595
"component": "nodejs",
9696
"commandLine": "nodemon --inspect app.js",
@@ -101,14 +101,14 @@ const (
101101
}
102102
},
103103
{
104-
"id": "stop the app",
104+
"id": "stop-the-app",
105105
"exec": {
106106
"component": "nodejs",
107107
"commandLine": "node_server_pids=$(pgrep -fx '.*nodemon (--inspect )?app.js' | tr \"\\\\n\" \" \") && echo \"Stopping node server with PIDs: ${node_server_pids}\" && kill -15 ${node_server_pids} &>/dev/null && echo 'Done.'"
108108
}
109109
},
110110
{
111-
"id": "Attach remote debugger",
111+
"id": "attach-remote-debugger",
112112
"vscodeLaunch": {
113113
"inlined": "{\n \"version\": \"0.2.0\",\n \"configurations\": [\n {\n \"type\": \"node\",\n \"request\": \"attach\",\n \"name\": \"Attach to Remote\",\n \"address\": \"localhost\",\n \"port\": 9229,\n \"localRoot\": \"${workspaceFolder}\",\n \"remoteRoot\": \"${workspaceFolder}\"\n }\n ]\n}\n"
114114
}

pkg/devfile/parser/data/interface.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package data
22

33
import (
4-
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
5-
devfilepkg "github.com/devfile/api/pkg/devfile"
4+
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
5+
devfilepkg "github.com/devfile/api/v2/pkg/devfile"
66
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
77
)
88

0 commit comments

Comments
 (0)