Skip to content

Commit

Permalink
Make application ports detected appear first in the endpoint list
Browse files Browse the repository at this point in the history
This allows such ports to be port-forwarded first, before the Debug ones.
  • Loading branch information
rm3l committed Dec 5, 2022
1 parent 3f87579 commit e82b0a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
31 changes: 16 additions & 15 deletions pkg/init/backend/applicationports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"fmt"
"io"
"strconv"
"strings"

"github.com/devfile/library/pkg/devfile/parser"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"k8s.io/klog"

"github.com/redhat-developer/odo/pkg/libdevfile"
)

// handleApplicationPorts updates the ports in the Devfile as needed.
Expand All @@ -33,33 +34,33 @@ func handleApplicationPorts(w io.Writer, devfileobj parser.DevfileObj, ports []i
klog.V(3).Infof("found more than 1 container components in Devfile at path %q => cannot find out which component needs to be updated."+
"This case will be handled in https://github.com/redhat-developer/odo/issues/6264", devfileobj.Ctx.GetAbsPath())
fmt.Fprintln(w, "\nApplication ports detected but the current Devfile contains multiple container components. Could not determine which component to update. "+
"Please feel free to customize the Devfile configuration below.")
"Please feel free to customize the Devfile configuration.")
return devfileobj, nil
}

component := components[0]

//Remove all but Debug endpoints
var portsToRemove []string
for _, ep := range component.Container.Endpoints {
if ep.Name == "debug" || strings.HasPrefix(ep.Name, "debug-") {
continue
}
portsToRemove = append(portsToRemove, strconv.Itoa(ep.TargetPort))
// Add the new ports at the beginning of the list (that is before any Debug endpoints).
// This way, application ports will be port-forwarded first.
portsToSet := make([]string, 0, len(ports))
for _, p := range ports {
portsToSet = append(portsToSet, strconv.Itoa(p))
}
err = devfileobj.Data.RemovePorts(map[string][]string{component.Name: portsToRemove})
debugEndpoints, err := libdevfile.GetDebugEndpointsForComponent(component)
if err != nil {
return parser.DevfileObj{}, err
}
// Clear the existing endpoint list
component.Container.Endpoints = nil

portsToSet := make([]string, 0, len(ports))
for _, p := range ports {
portsToSet = append(portsToSet, strconv.Itoa(p))
}
// Add the new application ports first
err = devfileobj.Data.SetPorts(map[string][]string{component.Name: portsToSet})
if err != nil {
return parser.DevfileObj{}, err
}

return devfileobj, err
// Append debug endpoints to the end of the list
component.Container.Endpoints = append(component.Container.Endpoints, debugEndpoints...)

return devfileobj, nil
}
20 changes: 13 additions & 7 deletions pkg/init/backend/applicationports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ func Test_handleApplicationPorts(t *testing.T) {
devfileObjProvider: func() parser.DevfileObj {
return buildDevfileObjWithComponents(
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082))
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
)
},
},
wantProvider: func() parser.DevfileObj {
return buildDevfileObjWithComponents(
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082))
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
)
},
},
{
Expand All @@ -82,15 +84,17 @@ func Test_handleApplicationPorts(t *testing.T) {
return buildDevfileObjWithComponents(
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
testingutil.GetFakeVolumeComponent("vol1", "1Gi"),
)
},
ports: []int{8888, 8889, 8890},
},
wantProvider: func() parser.DevfileObj {
return buildDevfileObjWithComponents(
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082),
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082),
testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
testingutil.GetFakeVolumeComponent("vol1", "1Gi"),
)
},
},
{
Expand All @@ -100,7 +104,8 @@ func Test_handleApplicationPorts(t *testing.T) {
contWithDebug := testingutil.GetFakeContainerComponent("cont1", 18080, 18081, 18082)
contWithDebug.ComponentUnion.Container.Endpoints = append(contWithDebug.ComponentUnion.Container.Endpoints,
v1.Endpoint{Name: "debug", TargetPort: 5005},
v1.Endpoint{Name: "debug-another", TargetPort: 5858})
v1.Endpoint{Name: "debug-another", TargetPort: 5858},
)
return buildDevfileObjWithComponents(
contWithDebug,
testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
Expand All @@ -110,10 +115,11 @@ func Test_handleApplicationPorts(t *testing.T) {
wantProvider: func() parser.DevfileObj {
newCont := testingutil.GetFakeContainerComponent("cont1")
newCont.ComponentUnion.Container.Endpoints = append(newCont.ComponentUnion.Container.Endpoints,
v1.Endpoint{Name: "port-3000-tcp", TargetPort: 3000, Protocol: v1.TCPEndpointProtocol},
v1.Endpoint{Name: "port-9000-tcp", TargetPort: 9000, Protocol: v1.TCPEndpointProtocol},
v1.Endpoint{Name: "debug", TargetPort: 5005},
v1.Endpoint{Name: "debug-another", TargetPort: 5858},
v1.Endpoint{Name: "port-3000-tcp", TargetPort: 3000, Protocol: v1.TCPEndpointProtocol},
v1.Endpoint{Name: "port-9000-tcp", TargetPort: 9000, Protocol: v1.TCPEndpointProtocol})
)
return buildDevfileObjWithComponents(
newCont,
testingutil.GetFakeVolumeComponent("vol1", "1Gi"))
Expand Down

0 comments on commit e82b0a0

Please sign in to comment.