-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
445 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package backend | ||
|
||
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" | ||
) | ||
|
||
// 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 below.") | ||
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)) | ||
} | ||
err = devfileobj.Data.RemovePorts(map[string][]string{component.Name: portsToRemove}) | ||
if err != nil { | ||
return parser.DevfileObj{}, err | ||
} | ||
|
||
portsToSet := make([]string, 0, len(ports)) | ||
for _, p := range ports { | ||
portsToSet = append(portsToSet, strconv.Itoa(p)) | ||
} | ||
err = devfileobj.Data.SetPorts(map[string][]string{component.Name: portsToSet}) | ||
if err != nil { | ||
return parser.DevfileObj{}, err | ||
} | ||
|
||
return devfileobj, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package backend | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" | ||
devfilepkg "github.com/devfile/api/v2/pkg/devfile" | ||
"github.com/devfile/library/pkg/devfile/parser" | ||
devfileCtx "github.com/devfile/library/pkg/devfile/parser/context" | ||
"github.com/devfile/library/pkg/devfile/parser/data" | ||
devfilefs "github.com/devfile/library/pkg/testingutil/filesystem" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"github.com/redhat-developer/odo/pkg/testingutil" | ||
) | ||
|
||
var fs = devfilefs.NewFakeFs() | ||
|
||
func buildDevfileObjWithComponents(components ...v1.Component) parser.DevfileObj { | ||
devfileData, _ := data.NewDevfileData(string(data.APISchemaVersion220)) | ||
devfileData.SetMetadata(devfilepkg.DevfileMetadata{Name: "my-nodejs-app"}) | ||
_ = devfileData.AddComponents(components) | ||
return parser.DevfileObj{ | ||
Ctx: devfileCtx.FakeContext(fs, parser.OutputDevfileYamlPath), | ||
Data: devfileData, | ||
} | ||
} | ||
|
||
func Test_handleApplicationPorts(t *testing.T) { | ||
type devfileProvider func() parser.DevfileObj | ||
type args struct { | ||
devfileObjProvider devfileProvider | ||
ports []int | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
wantProvider devfileProvider | ||
}{ | ||
{ | ||
name: "no component, no ports to set", | ||
args: args{ | ||
devfileObjProvider: func() parser.DevfileObj { return buildDevfileObjWithComponents() }, | ||
}, | ||
wantProvider: func() parser.DevfileObj { return buildDevfileObjWithComponents() }, | ||
}, | ||
{ | ||
name: "multiple container components, no ports to set", | ||
args: args{ | ||
devfileObjProvider: func() parser.DevfileObj { | ||
return buildDevfileObjWithComponents( | ||
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082), | ||
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082)) | ||
}, | ||
}, | ||
wantProvider: func() parser.DevfileObj { | ||
return buildDevfileObjWithComponents( | ||
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082), | ||
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082)) | ||
}, | ||
}, | ||
{ | ||
name: "no container components", | ||
args: args{ | ||
devfileObjProvider: func() parser.DevfileObj { | ||
return buildDevfileObjWithComponents(testingutil.GetFakeVolumeComponent("vol1", "1Gi")) | ||
}, | ||
ports: []int{8888, 8889, 8890}, | ||
}, | ||
wantProvider: func() parser.DevfileObj { | ||
return buildDevfileObjWithComponents(testingutil.GetFakeVolumeComponent("vol1", "1Gi")) | ||
}, | ||
}, | ||
{ | ||
name: "more than one container components", | ||
args: args{ | ||
devfileObjProvider: func() parser.DevfileObj { | ||
return buildDevfileObjWithComponents( | ||
testingutil.GetFakeContainerComponent("cont1", 8080, 8081, 8082), | ||
testingutil.GetFakeContainerComponent("cont2", 9080, 9081, 9082), | ||
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")) | ||
}, | ||
}, | ||
{ | ||
name: "single container component with both application and debug ports", | ||
args: args{ | ||
devfileObjProvider: func() parser.DevfileObj { | ||
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}) | ||
return buildDevfileObjWithComponents( | ||
contWithDebug, | ||
testingutil.GetFakeVolumeComponent("vol1", "1Gi")) | ||
}, | ||
ports: []int{3000, 9000}, | ||
}, | ||
wantProvider: func() parser.DevfileObj { | ||
newCont := testingutil.GetFakeContainerComponent("cont1") | ||
newCont.ComponentUnion.Container.Endpoints = append(newCont.ComponentUnion.Container.Endpoints, | ||
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")) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var output bytes.Buffer | ||
got, err := handleApplicationPorts(&output, tt.args.devfileObjProvider(), tt.args.ports) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("handleApplicationPorts() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if diff := cmp.Diff(tt.wantProvider(), got, | ||
cmp.AllowUnexported(devfileCtx.DevfileCtx{}), | ||
cmpopts.IgnoreInterfaces(struct{ devfilefs.Filesystem }{})); diff != "" { | ||
t.Errorf("handleApplicationPorts() mismatch (-want +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.