Skip to content

Commit

Permalink
Add duplicate port validation, update devfile libraries, and add test…
Browse files Browse the repository at this point in the history
…s to check port name matches endpoint name (#5407)

* Add duplicate port validation, update devfile libraries, and add tests to check port name matches endpoint name

* Fix k8s test
  • Loading branch information
valaparthvi authored Feb 9, 2022
1 parent 905b4d7 commit 752c928
Show file tree
Hide file tree
Showing 28 changed files with 364 additions and 179 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/Netflix/go-expect v0.0.0-20201125194554-85d881c3777e
github.com/Xuanwo/go-locale v1.0.0
github.com/blang/semver v3.5.1+incompatible
github.com/devfile/api/v2 v2.0.0-20211118170330-959f3c8007c3
github.com/devfile/library v1.2.1-0.20211207205254-de570f015d84
github.com/devfile/api/v2 v2.0.0-20220126144139-f6d7cd85a481
github.com/devfile/library v1.2.1-0.20220201022328-58ef0b78c0fe
github.com/devfile/registry-support/index/generator v0.0.0-20211012185733-0a73f866043f
github.com/devfile/registry-support/registry-library v0.0.0-20211125162259-d7edf148d3e2
github.com/fatih/color v1.10.0
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,12 @@ github.com/deislabs/oras v0.8.1/go.mod h1:Mx0rMSbBNaNfY9hjpccEnxkOqJL6KGjtxNHPLC
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/devfile/api/v2 v2.0.0-20210910153124-da620cd1a7a1/go.mod h1:kLX/nW93gigOHXK3NLeJL2fSS/sgEe+OHu8bo3aoOi4=
github.com/devfile/api/v2 v2.0.0-20211118170330-959f3c8007c3 h1:G7TIrgpGx5lcZ3X7bpQeVCeeeO7tv6SAzPPvB9ThHXE=
github.com/devfile/api/v2 v2.0.0-20211118170330-959f3c8007c3/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
github.com/devfile/api/v2 v2.0.0-20220117162434-6e6e6a8bc14c/go.mod h1:d99eTN6QxgzihOOFyOZA+VpUyD4Q1pYRYHZ/ci9J96Q=
github.com/devfile/api/v2 v2.0.0-20220126144139-f6d7cd85a481 h1:G4trvnLOua7AsSetNa8k4u9VUNavPIwXa3CNfBwQwx4=
github.com/devfile/api/v2 v2.0.0-20220126144139-f6d7cd85a481/go.mod h1:kLX/nW93gigOHXK3NLeJL2fSS/sgEe+OHu8bo3aoOi4=
github.com/devfile/library v1.1.1-0.20210910214722-7c5ff63711ec/go.mod h1:svPWwWb+BP15SXCHl0dyOeE4Sohrjl5a2BaOzc/riLc=
github.com/devfile/library v1.2.1-0.20211207205254-de570f015d84 h1:9Tce6BIw9PCdA1AopHxVo0gheU+dP3jfl2MgBKLnMRQ=
github.com/devfile/library v1.2.1-0.20211207205254-de570f015d84/go.mod h1:qSS0cLIKTyysdkvGr4cMlTOHO0zkoobFcfvAVPtoq5Y=
github.com/devfile/library v1.2.1-0.20220201022328-58ef0b78c0fe h1:IzpRD2XS7m7k+6lFLYjaFAPYy0CSv0861teFvzNFkdM=
github.com/devfile/library v1.2.1-0.20220201022328-58ef0b78c0fe/go.mod h1:Fp8wxWPVA5pz8geGIp0DioGVgnZAdwyqsgohvRaR5rk=
github.com/devfile/registry-support/index/generator v0.0.0-20211012185733-0a73f866043f h1:fKNUmoOPh7yAs69uMRZWHvev+m3e7T4jBL/hOXZB9ys=
github.com/devfile/registry-support/index/generator v0.0.0-20211012185733-0a73f866043f/go.mod h1:bLGagbW2SFn7jo5+kUPlCMehIGqWkRtLKc5O0OyJMJM=
github.com/devfile/registry-support/registry-library v0.0.0-20211125162259-d7edf148d3e2 h1:Rwuc0bdx8xSZWdIwXjAxaKYnZIWHneJmDAAZ6a5jXzY=
Expand Down
14 changes: 9 additions & 5 deletions pkg/envinfo/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/redhat-developer/odo/pkg/util"
)

//getPorts gets the ports from devfile
// getPorts gets the ports from devfile
func (ei *EnvInfo) getPorts(container string) ([]string, error) {
var portList []string
containerComponents, err := ei.devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
Expand All @@ -42,20 +42,20 @@ func (ei *EnvInfo) getPorts(container string) ([]string, error) {
return portList, nil
}

//GetContainerPorts returns list of the ports of specified container, if it exists
// GetContainerPorts returns list of the ports of specified container, if it exists
func (ei *EnvInfo) GetContainerPorts(container string) ([]string, error) {
if container == "" {
return nil, fmt.Errorf("please provide a container")
}
return ei.getPorts(container)
}

//GetComponentPorts returns all unique ports declared in all the containers
// GetComponentPorts returns all unique ports declared in all the containers
func (ei *EnvInfo) GetComponentPorts() ([]string, error) {
return ei.getPorts("")
}

//checkValidPort checks and retrieves valid port from devfile when no port is specified
// checkValidPort checks and retrieves valid port from devfile when no port is specified
func (ei *EnvInfo) checkValidPort(url *localConfigProvider.LocalURL, portsOf string, ports []string) (err error) {
if url.Port == -1 {
if len(ports) > 1 {
Expand Down Expand Up @@ -191,7 +191,11 @@ func (ei *EnvInfo) ValidateURL(url localConfigProvider.LocalURL) error {
}
for _, endpoint := range component.Container.Endpoints {
if endpoint.Name == url.Name && !ei.updateURL {
return fmt.Errorf("url %v already exist in devfile endpoint entry under container %v", url.Name, component.Name)
return fmt.Errorf("url %v already exists in devfile endpoint entry under container %q", url.Name, component.Name)
}
// Check for a duplicate port entry
if endpoint.TargetPort == url.Port && !ei.updateURL {
return fmt.Errorf("port %v already exists in devfile endpoint entry under container %q", url.Port, component.Name)
}
containerPortMap[endpoint.TargetPort] = component.Name
}
Expand Down
39 changes: 34 additions & 5 deletions pkg/envinfo/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,7 @@ func TestEnvInfo_ValidateURL(t *testing.T) {
fields: fields{
devfileObj: odoTestingUtil.GetTestDevfileObj(fs),
componentSettings: ComponentSettings{
URL: &[]localConfigProvider.LocalURL{
{
Name: "port-3030",
},
},
URL: &[]localConfigProvider.LocalURL{},
},
},
args: args{
Expand Down Expand Up @@ -616,6 +612,39 @@ func TestEnvInfo_ValidateURL(t *testing.T) {
updateURL: true,
wantErr: false,
},
{
name: "case 14: create a new url with a port that already exists in a devfile endpoint",
fields: fields{
devfileObj: odoTestingUtil.GetTestDevfileObj(fs),
componentSettings: ComponentSettings{
URL: &[]localConfigProvider.LocalURL{},
},
},
args: args{
url: localConfigProvider.LocalURL{
Name: "myport",
Port: 3000,
},
},
wantErr: true,
},
{
name: "case 15: update url with a port that already exists in a devfile endpoint",
fields: fields{
devfileObj: odoTestingUtil.GetTestDevfileObj(fs),
componentSettings: ComponentSettings{
URL: &[]localConfigProvider.LocalURL{},
},
},
args: args{
url: localConfigProvider.LocalURL{
Port: 3000,
Host: "something.com",
},
},
wantErr: false,
updateURL: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/devfile/cmd_devfile_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ var _ = Describe("odo devfile delete command tests", func() {
resourceTypes := []string{helper.ResourceTypeDeployment, helper.ResourceTypePod, helper.ResourceTypeService, helper.ResourceTypeIngress, helper.ResourceTypePVC}

BeforeEach(func() {
helper.Cmd("odo", "url", "create", "example", "--host", "1.2.3.4.nip.io", "--port", "3000", "--ingress").ShouldPass()
helper.Cmd("odo", "url", "create", "example", "--host", "1.2.3.4.nip.io", "--port", "3030", "--ingress").ShouldPass()

if os.Getenv("KUBERNETES") != "true" {
helper.Cmd("odo", "url", "create", "example-1", "--port", "3000").ShouldPass()
helper.Cmd("odo", "url", "create", "example-1", "--port", "4000").ShouldPass()
resourceTypes = append(resourceTypes, helper.ResourceTypeRoute)
}
})
Expand Down
Loading

0 comments on commit 752c928

Please sign in to comment.