Skip to content

Commit

Permalink
Refactors URL for localConfigProvider. The CLI layer code for url cre…
Browse files Browse the repository at this point in the history
…ate and delete are refactored to use the new changes.

It also moves the localConfigProvider to a new package.

Signed-off-by: mik-dass <mrinald7@gmail.com>
  • Loading branch information
mik-dass committed Dec 16, 2020
1 parent 1cb28b2 commit c6be268
Show file tree
Hide file tree
Showing 30 changed files with 2,117 additions and 1,317 deletions.
9 changes: 5 additions & 4 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/devfile/library/pkg/devfile/parser"
"github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/localConfigProvider"

"github.com/openshift/odo/pkg/envinfo"

Expand Down Expand Up @@ -638,8 +639,8 @@ func ApplyConfig(client *occlient.Client, kClient *kclient.Client, componentConf
return urlpkg.Push(client, kClient, urlpkg.PushParameters{
ComponentName: componentName,
ApplicationName: applicationName,
ConfigURLs: componentConfig.GetURL(),
EnvURLS: envSpecificInfo.GetURL(),
ConfigURLs: componentConfig.ListURLs(),
EnvURLS: envSpecificInfo.ListURLs(),
IsRouteSupported: isRouteSupported,
ContainerComponents: containerComponents,
IsS2I: isS2I,
Expand Down Expand Up @@ -1038,7 +1039,7 @@ func GetComponentFromDevfile(info *envinfo.EnvSpecificInfo) (Component, parser.D
return Component{}, parser.DevfileObj{}, nil
}

func getComponentFrom(info envinfo.LocalConfigProvider, componentType string) Component {
func getComponentFrom(info localConfigProvider.LocalConfigProvider, componentType string) Component {
if info.Exists() {
component := getMachineReadableFormat(info.GetName(), componentType)

Expand All @@ -1050,7 +1051,7 @@ func getComponentFrom(info envinfo.LocalConfigProvider, componentType string) Co
Ports: []string{fmt.Sprintf("%d", info.GetDebugPort())},
}

urls := info.GetURL()
urls := info.ListURLs()
if len(urls) > 0 {
for _, url := range urls {
component.Spec.URL = append(component.Spec.URL, url.Name)
Expand Down
3 changes: 2 additions & 1 deletion pkg/component/component_full_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
"encoding/json"
"fmt"
"github.com/openshift/odo/pkg/localConfigProvider"
"strings"

devfilev1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
Expand Down Expand Up @@ -156,7 +157,7 @@ func NewComponentFullDescriptionFromClientAndLocalConfig(client *occlient.Client
return cfd, e
}
var components []devfilev1.Component
var configProvider envinfo.LocalConfigProvider = localConfigInfo
var configProvider localConfigProvider.LocalConfigProvider = localConfigInfo
if envInfo != nil {
configProvider = envInfo
components = devfile.Data.GetDevfileContainerComponents()
Expand Down
16 changes: 8 additions & 8 deletions pkg/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
applabels "github.com/openshift/odo/pkg/application/labels"
componentlabels "github.com/openshift/odo/pkg/component/labels"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/envinfo"
"github.com/openshift/odo/pkg/localConfigProvider"
"github.com/openshift/odo/pkg/occlient"
"github.com/openshift/odo/pkg/testingutil"

Expand All @@ -40,15 +40,15 @@ func TestGetComponentFrom(t *testing.T) {
name string
isEnvInfo bool
componentType string
envURL []envinfo.EnvInfoURL
envURL []localConfigProvider.LocalURL
cmpSetting cmpSetting
want Component
}{
{
name: "Case 1: Get component when env info file exists",
isEnvInfo: true,
componentType: "nodejs",
envURL: []envinfo.EnvInfoURL{
envURL: []localConfigProvider.LocalURL{
{
Name: "url1",
},
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestGetComponentFrom(t *testing.T) {
name: "Case 2: Get component when env info file does not exists",
isEnvInfo: false,
componentType: "nodejs",
envURL: []envinfo.EnvInfoURL{
envURL: []localConfigProvider.LocalURL{
{
Name: "url2",
},
Expand All @@ -99,7 +99,7 @@ func TestGetComponentFrom(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLocalConfigProvider := envinfo.NewMockLocalConfigProvider(ctrl)
mockLocalConfigProvider := localConfigProvider.NewMockLocalConfigProvider(ctrl)

mockLocalConfigProvider.EXPECT().Exists().Return(tt.isEnvInfo)

Expand All @@ -120,7 +120,7 @@ func TestGetComponentFrom(t *testing.T) {
Ports: []string{fmt.Sprintf("%d", tt.cmpSetting.debugPort)},
}

mockLocalConfigProvider.EXPECT().GetURL().Return(tt.envURL)
mockLocalConfigProvider.EXPECT().ListURLs().Return(tt.envURL)

if len(tt.envURL) > 0 {
for _, url := range tt.envURL {
Expand Down Expand Up @@ -867,7 +867,7 @@ func TestGetComponentFromConfig(t *testing.T) {
Type: localExistingConfigInfoValue.GetType(),
Source: localExistingConfigInfoValue.GetSourceLocation(),
URL: []string{
localExistingConfigInfoValue.LocalConfig.GetURL()[0].Name, localExistingConfigInfoValue.LocalConfig.GetURL()[1].Name,
localExistingConfigInfoValue.LocalConfig.ListURLs()[0].Name, localExistingConfigInfoValue.LocalConfig.ListURLs()[1].Name,
},
Storage: []string{
localExistingConfigInfoValue.LocalConfig.GetStorage()[0].Name, localExistingConfigInfoValue.LocalConfig.GetStorage()[1].Name,
Expand Down Expand Up @@ -903,7 +903,7 @@ func TestGetComponentFromConfig(t *testing.T) {
Type: gitExistingConfigInfoValue.GetType(),
Source: gitExistingConfigInfoValue.GetSourceLocation(),
URL: []string{
gitExistingConfigInfoValue.LocalConfig.GetURL()[0].Name, gitExistingConfigInfoValue.LocalConfig.GetURL()[1].Name,
gitExistingConfigInfoValue.LocalConfig.ListURLs()[0].Name, gitExistingConfigInfoValue.LocalConfig.ListURLs()[1].Name,
},
Storage: []string{
gitExistingConfigInfoValue.LocalConfig.GetStorage()[0].Name, localExistingConfigInfoValue.LocalConfig.GetStorage()[1].Name,
Expand Down
37 changes: 4 additions & 33 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"strconv"
"strings"

"github.com/openshift/odo/pkg/envinfo"

"github.com/devfile/library/pkg/devfile/parser"
"github.com/openshift/odo/pkg/localConfigProvider"
"github.com/openshift/odo/pkg/testingutil/filesystem"

"github.com/pkg/errors"
Expand Down Expand Up @@ -80,7 +79,7 @@ type ComponentSettings struct {

Envs EnvVarList `yaml:"Envs,omitempty"`

URL *[]envinfo.EnvInfoURL `yaml:"Url,omitempty"`
URL *[]localConfigProvider.LocalURL `yaml:"Url,omitempty"`
}

// LocalConfig holds all the config relavent to a specific Component.
Expand Down Expand Up @@ -250,11 +249,11 @@ func (lci *LocalConfigInfo) SetConfiguration(parameter string, value interface{}
lci.componentSettings.MinCPU = &strValue
lci.componentSettings.MaxCPU = &strValue
case "url":
urlValue := value.(envinfo.EnvInfoURL)
urlValue := value.(localConfigProvider.LocalURL)
if lci.componentSettings.URL != nil {
*lci.componentSettings.URL = append(*lci.componentSettings.URL, urlValue)
} else {
lci.componentSettings.URL = &[]envinfo.EnvInfoURL{urlValue}
lci.componentSettings.URL = &[]localConfigProvider.LocalURL{urlValue}
}
}

Expand Down Expand Up @@ -339,18 +338,6 @@ func (lci *LocalConfigInfo) DeleteConfiguration(parameter string) error {

}

// DeleteURL is used to delete config from local odo config
func (lci *LocalConfigInfo) DeleteURL(parameter string) error {
for i, url := range *lci.componentSettings.URL {
if url.Name == parameter {
s := *lci.componentSettings.URL
s = append(s[:i], s[i+1:]...)
lci.componentSettings.URL = &s
}
}
return lci.writeToFile()
}

// DeleteFromConfigurationList is used to delete a value from a list from the local odo config
// parameter is the name of the config parameter
// value is the value to be deleted
Expand Down Expand Up @@ -422,14 +409,6 @@ func (lc *LocalConfig) GetSourceType() SrcType {
return *lc.componentSettings.SourceType
}

// GetPorts returns the ports, returns default if nil
func (lc *LocalConfig) GetPorts() []string {
if lc.componentSettings.Ports == nil {
return nil
}
return *lc.componentSettings.Ports
}

// GetApplication returns the app, returns default if nil
func (lc *LocalConfig) GetApplication() string {
return util.GetStringOrEmpty(lc.componentSettings.Application)
Expand Down Expand Up @@ -480,14 +459,6 @@ func (lc *LocalConfig) GetMaxCPU() string {
return util.GetStringOrEmpty(lc.componentSettings.MaxCPU)
}

// GetURL returns the ConfigURL, returns default if nil
func (lc *LocalConfig) GetURL() []envinfo.EnvInfoURL {
if lc.componentSettings.URL == nil {
return []envinfo.EnvInfoURL{}
}
return *lc.componentSettings.URL
}

// GetStorage returns the Storage, returns empty if nil
func (lc *LocalConfig) GetStorage() []ComponentStorageSettings {
if lc.componentSettings.Storage == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/fakeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"path/filepath"

"github.com/openshift/odo/pkg/envinfo"
"github.com/openshift/odo/pkg/localConfigProvider"
"github.com/openshift/odo/pkg/testingutil/filesystem"
)

Expand All @@ -23,7 +23,7 @@ func GetOneExistingConfigInfo(componentName, applicationName, projectName string

portsValue := []string{"8080/TCP", "45/UDP"}

urlValue := []envinfo.EnvInfoURL{
urlValue := []localConfigProvider.LocalURL{
{
Name: "example-url-0",
Port: 8080,
Expand Down
85 changes: 85 additions & 0 deletions pkg/config/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package config

import (
"fmt"
"strings"

"github.com/openshift/odo/pkg/localConfigProvider"
"github.com/openshift/odo/pkg/util"
)

// GetPorts returns the ports, returns default if nil
func (lc *LocalConfig) GetPorts() []string {
if lc.componentSettings.Ports == nil {
return nil
}
return *lc.componentSettings.Ports
}

// CompleteURL completes the given URL with default values
func (lc *LocalConfig) CompleteURL(url *localConfigProvider.LocalURL) error {
var err error
url.Port, err = util.GetValidPortNumber(lc.GetName(), url.Port, lc.GetPorts())
if err != nil {
return err
}

// get the name
if len(url.Name) == 0 {
url.Name = util.GetURLName(lc.GetName(), url.Port)
}

return nil
}

// ValidateURL validates the given URL
func (lc *LocalConfig) ValidateURL(url localConfigProvider.LocalURL) error {
errorList := make([]string, 0)

for _, localURL := range lc.ListURLs() {
if url.Name == localURL.Name {
errorList = append(errorList, fmt.Sprintf("URL %s already exists in application: %s", url.Name, lc.GetApplication()))
}
}

if len(errorList) > 0 {
return fmt.Errorf(strings.Join(errorList, "\n"))
}

return nil
}

// GetURL gets the given url localConfig
func (lc *LocalConfig) GetURL(name string) *localConfigProvider.LocalURL {
for _, url := range lc.ListURLs() {
if name == url.Name {
return &url
}
}
return nil
}

// CreateURL writes the given url to the localConfig
func (lci *LocalConfigInfo) CreateURL(url localConfigProvider.LocalURL) error {
return lci.SetConfiguration("url", localConfigProvider.LocalURL{Name: url.Name, Port: url.Port, Secure: url.Secure})
}

// ListURLs returns the ConfigURL, returns default if nil
func (lc *LocalConfig) ListURLs() []localConfigProvider.LocalURL {
if lc.componentSettings.URL == nil {
return []localConfigProvider.LocalURL{}
}
return *lc.componentSettings.URL
}

// DeleteURL is used to delete config from local odo config
func (lci *LocalConfigInfo) DeleteURL(parameter string) error {
for i, url := range *lci.componentSettings.URL {
if url.Name == parameter {
s := *lci.componentSettings.URL
s = append(s[:i], s[i+1:]...)
lci.componentSettings.URL = &s
}
}
return lci.writeToFile()
}
2 changes: 1 addition & 1 deletion pkg/devfile/adapters/docker/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func getPortMap(context string, endpoints []devfilev1.Endpoint, show bool) (nat.
return nil, nil, err
}

urlArr := envInfo.GetURL()
urlArr := envInfo.ListURLs()

for _, url := range urlArr {
if url.ExposedPort > 0 && common.IsPortPresent(endpoints, url.Port) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/devfile/adapters/docker/component/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package component

import (
"github.com/openshift/odo/pkg/localConfigProvider"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -335,21 +336,21 @@ func TestGenerateAndGetHostConfig(t *testing.T) {

tests := []struct {
name string
urlValue []envinfo.EnvInfoURL
urlValue []localConfigProvider.LocalURL
expectResult nat.PortMap
client *lclient.Client
endpoints []devfilev1.Endpoint
}{
{
name: "Case 1: no port mappings",
urlValue: []envinfo.EnvInfoURL{},
urlValue: []localConfigProvider.LocalURL{},
expectResult: nil,
client: fakeClient,
endpoints: []devfilev1.Endpoint{},
},
{
name: "Case 2: only one port mapping",
urlValue: []envinfo.EnvInfoURL{
urlValue: []localConfigProvider.LocalURL{
{Name: "url1", Port: 8080, ExposedPort: 65432},
},
expectResult: nat.PortMap{
Expand All @@ -370,7 +371,7 @@ func TestGenerateAndGetHostConfig(t *testing.T) {
},
{
name: "Case 3: multiple port mappings",
urlValue: []envinfo.EnvInfoURL{
urlValue: []localConfigProvider.LocalURL{
{Name: "url1", Port: 8080, ExposedPort: 65432},
{Name: "url2", Port: 9090, ExposedPort: 54321},
{Name: "url3", Port: 9080, ExposedPort: 45678},
Expand Down
Loading

0 comments on commit c6be268

Please sign in to comment.