This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathapplicationports.go
More file actions
79 lines (66 loc) · 2.83 KB
/
applicationports.go
File metadata and controls
79 lines (66 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package backend
import (
"fmt"
"io"
"strconv"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
"k8s.io/klog/v2"
"github.com/redhat-developer/odo/pkg/libdevfile"
)
// handleApplicationPorts updates the ports in the Devfile as needed.
// If there are multiple container components in the Devfile, nothing is done. This will be handled in https://github.com/redhat-developer/odo/issues/6264.
// Otherwise, all the container component endpoints/ports (other than Debug) are updated with the specified ports.
func handleApplicationPorts(w io.Writer, devfileobj parser.DevfileObj, ports []int) (parser.DevfileObj, error) {
if len(ports) == 0 {
return devfileobj, nil
}
components, err := devfileobj.Data.GetDevfileContainerComponents(parsercommon.DevfileOptions{})
if err != nil {
return parser.DevfileObj{}, err
}
nbContainerComponents := len(components)
klog.V(3).Infof("Found %d container components in Devfile at path %q", nbContainerComponents, devfileobj.Ctx.GetAbsPath())
if nbContainerComponents == 0 {
// no container components => nothing to do
return devfileobj, nil
}
if nbContainerComponents > 1 {
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.")
return devfileobj, nil
}
component := components[0]
err = setPortsInContainerComponent(&devfileobj, &component, ports, true)
if err != nil {
return parser.DevfileObj{}, err
}
return devfileobj, nil
}
func setPortsInContainerComponent(devfileobj *parser.DevfileObj, component *v1alpha2.Component, ports []int, withDebug bool) error {
// 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))
}
debugEndpoints, err := libdevfile.GetDebugEndpointsForComponent(*component)
if err != nil {
return err
}
// Clear the existing endpoint list
component.Container.Endpoints = nil
// Add the new application ports first
err = devfileobj.Data.SetPorts(map[string][]string{component.Name: portsToSet})
if err != nil {
return err
}
// Append debug endpoints to the end of the list
if withDebug {
component.Container.Endpoints = append(component.Container.Endpoints, debugEndpoints...)
}
return nil
}