...
1 package backend
2
3 import (
4 "fmt"
5 "io"
6 "strconv"
7
8 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
9 "github.com/devfile/library/v2/pkg/devfile/parser"
10 parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
11 "k8s.io/klog"
12
13 "github.com/redhat-developer/odo/pkg/libdevfile"
14 )
15
16
17
18
19 func handleApplicationPorts(w io.Writer, devfileobj parser.DevfileObj, ports []int) (parser.DevfileObj, error) {
20 if len(ports) == 0 {
21 return devfileobj, nil
22 }
23
24 components, err := devfileobj.Data.GetDevfileContainerComponents(parsercommon.DevfileOptions{})
25 if err != nil {
26 return parser.DevfileObj{}, err
27 }
28 nbContainerComponents := len(components)
29 klog.V(3).Infof("Found %d container components in Devfile at path %q", nbContainerComponents, devfileobj.Ctx.GetAbsPath())
30 if nbContainerComponents == 0 {
31
32 return devfileobj, nil
33 }
34 if nbContainerComponents > 1 {
35 klog.V(3).Infof("found more than 1 container components in Devfile at path %q => cannot find out which component needs to be updated."+
36 "This case will be handled in https://github.com/redhat-developer/odo/issues/6264", devfileobj.Ctx.GetAbsPath())
37 fmt.Fprintln(w, "\nApplication ports detected but the current Devfile contains multiple container components. Could not determine which component to update. "+
38 "Please feel free to customize the Devfile configuration.")
39 return devfileobj, nil
40 }
41
42 component := components[0]
43
44 err = setPortsInContainerComponent(&devfileobj, &component, ports, true)
45 if err != nil {
46 return parser.DevfileObj{}, err
47 }
48 return devfileobj, nil
49 }
50
51 func setPortsInContainerComponent(devfileobj *parser.DevfileObj, component *v1alpha2.Component, ports []int, withDebug bool) error {
52
53
54 portsToSet := make([]string, 0, len(ports))
55 for _, p := range ports {
56 portsToSet = append(portsToSet, strconv.Itoa(p))
57 }
58
59 debugEndpoints, err := libdevfile.GetDebugEndpointsForComponent(*component)
60 if err != nil {
61 return err
62 }
63
64
65 component.Container.Endpoints = nil
66
67
68 err = devfileobj.Data.SetPorts(map[string][]string{component.Name: portsToSet})
69 if err != nil {
70 return err
71 }
72
73
74 if withDebug {
75 component.Container.Endpoints = append(component.Container.Endpoints, debugEndpoints...)
76 }
77
78 return nil
79 }
80
View as plain text