Skip to content

Commit

Permalink
Refactors storage code for localConfigProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-dass committed Jan 7, 2021
1 parent e6be258 commit 301ae1d
Show file tree
Hide file tree
Showing 20 changed files with 928 additions and 306 deletions.
4 changes: 2 additions & 2 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ func GetComponentFromConfig(localConfig *config.LocalConfigInfo) (Component, err
component.Spec.Env = append(component.Spec.Env, corev1.EnvVar{Name: localEnv.Name, Value: localEnv.Value})
}

for _, localStorage := range localConfig.GetStorage() {
for _, localStorage := range localConfig.ListStorage() {
component.Spec.Storage = append(component.Spec.Storage, localStorage.Name)
}
return component, nil
Expand Down Expand Up @@ -1653,7 +1653,7 @@ func GetMachineReadableFormatForCombinedCompList(s2iComps []Component, devfileCo
// returns a list of storage in storage struct format
func getStorageFromConfig(localConfig *config.LocalConfigInfo) storage.StorageList {
storageList := storage.StorageList{}
for _, storageVar := range localConfig.GetStorage() {
for _, storageVar := range localConfig.ListStorage() {
storageList.Items = append(storageList.Items, storage.GetMachineReadableFormat(storageVar.Name, storageVar.Size, storageVar.Path))
}
return storageList
Expand Down
5 changes: 1 addition & 4 deletions pkg/component/component_full_description.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ func (cfd *ComponentFullDescription) loadStoragesFromClientAndLocalConfig(client
storages = storage.GetLocalDevfileStorage(devfile.Data)
storages = storage.GetMachineReadableFormatForList(storages.Items)
} else {
storageLocal, err := localConfigInfo.StorageList()
if err != nil {
return err
}
storageLocal := localConfigInfo.ListStorage()
// convert to machine readable format
storages = storage.ConvertListLocalToMachine(storageLocal)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ func TestGetComponentFromConfig(t *testing.T) {
localExistingConfigInfoValue.LocalConfig.ListURLs()[0].Name, localExistingConfigInfoValue.LocalConfig.ListURLs()[1].Name,
},
Storage: []string{
localExistingConfigInfoValue.LocalConfig.GetStorage()[0].Name, localExistingConfigInfoValue.LocalConfig.GetStorage()[1].Name,
localExistingConfigInfoValue.ListStorage()[0].Name, localExistingConfigInfoValue.ListStorage()[1].Name,
},
Env: []corev1.EnvVar{
{
Expand Down Expand Up @@ -906,7 +906,7 @@ func TestGetComponentFromConfig(t *testing.T) {
gitExistingConfigInfoValue.LocalConfig.ListURLs()[0].Name, gitExistingConfigInfoValue.LocalConfig.ListURLs()[1].Name,
},
Storage: []string{
gitExistingConfigInfoValue.LocalConfig.GetStorage()[0].Name, localExistingConfigInfoValue.LocalConfig.GetStorage()[1].Name,
gitExistingConfigInfoValue.ListStorage()[0].Name, localExistingConfigInfoValue.ListStorage()[1].Name,
},
Env: []corev1.EnvVar{
{
Expand Down
21 changes: 4 additions & 17 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ const (
DefaultDebugPort = 5858
)

type ComponentStorageSettings struct {
Name string `yaml:"Name,omitempty"`
Size string `yaml:"Size,omitempty"`
Path string `yaml:"Path,omitempty"`
}

// ComponentSettings holds all component related information
type ComponentSettings struct {
// The builder image to use
Expand Down Expand Up @@ -68,7 +62,7 @@ type ComponentSettings struct {
// DebugPort controls the port used by the pod to run the debugging agent on
DebugPort *int `yaml:"DebugPort,omitempty"`

Storage *[]ComponentStorageSettings `yaml:"Storage,omitempty"`
Storage *[]localConfigProvider.LocalStorage `yaml:"Storage,omitempty"`

// Ignore if set to true then odoignore file should be considered
Ignore *bool `yaml:"Ignore,omitempty"`
Expand Down Expand Up @@ -239,11 +233,11 @@ func (lci *LocalConfigInfo) SetConfiguration(parameter string, value interface{}
case "maxcpu":
lci.componentSettings.MaxCPU = &strValue
case "storage":
storageSetting, _ := value.(ComponentStorageSettings)
storageSetting, _ := value.(localConfigProvider.LocalStorage)
if lci.componentSettings.Storage != nil {
*lci.componentSettings.Storage = append(*lci.componentSettings.Storage, storageSetting)
} else {
lci.componentSettings.Storage = &[]ComponentStorageSettings{storageSetting}
lci.componentSettings.Storage = &[]localConfigProvider.LocalStorage{storageSetting}
}
case "cpu":
lci.componentSettings.MinCPU = &strValue
Expand Down Expand Up @@ -345,7 +339,7 @@ func (lci *LocalConfigInfo) DeleteFromConfigurationList(parameter string, value
if parameter, ok := AsLocallySupportedParameter(parameter); ok {
switch parameter {
case "storage":
for i, storage := range lci.GetStorage() {
for i, storage := range lci.ListStorage() {
if storage.Name == value {
*lci.componentSettings.Storage = append((*lci.componentSettings.Storage)[:i], (*lci.componentSettings.Storage)[i+1:]...)
}
Expand Down Expand Up @@ -459,13 +453,6 @@ func (lc *LocalConfig) GetMaxCPU() string {
return util.GetStringOrEmpty(lc.componentSettings.MaxCPU)
}

// GetStorage returns the Storage, returns empty if nil
func (lc *LocalConfig) GetStorage() []ComponentStorageSettings {
if lc.componentSettings.Storage == nil {
return []ComponentStorageSettings{}
}
return *lc.componentSettings.Storage
}

// GetEnvs returns the Envs, returns empty if nil
func (lc *LocalConfig) GetEnvs() EnvVarList {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/fakeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func GetOneExistingConfigInfo(componentName, applicationName, projectName string
componentType := "nodejs"
sourceLocation := "./"

storageValue := []ComponentStorageSettings{
storageValue := []localConfigProvider.LocalStorage{
{
Name: "example-storage-0",
},
Expand Down Expand Up @@ -64,7 +64,7 @@ func GetOneExistingConfigInfoStorage(componentName, applicationName, projectName
componentType := "nodejs"
sourceLocation := "./"

storageValue := []ComponentStorageSettings{
storageValue := []localConfigProvider.LocalStorage{
{
Name: storeName,
Size: storeSize,
Expand Down
90 changes: 51 additions & 39 deletions pkg/config/storage.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,84 @@
package config

import (
"fmt"

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

// StorageCreate sets the storage related information in the local configuration
func (lci *LocalConfigInfo) StorageCreate(name, size, path string) (ComponentStorageSettings, error) {
storage := ComponentStorageSettings{
Name: name,
Size: size,
Path: path,
// GetStorage gets the storage with the given name
func (lci *LocalConfigInfo) GetStorage(storageName string) *localConfigProvider.LocalStorage {
for _, storage := range lci.ListStorage() {
if storageName == storage.Name {
return &storage
}
}
return nil
}

// CreateStorage sets the storage related information in the local configuration
func (lci *LocalConfigInfo) CreateStorage(storage localConfigProvider.LocalStorage) error {
err := lci.SetConfiguration("storage", storage)
if err != nil {
return ComponentStorageSettings{}, err
return err
}
return storage, err
return err
}

func (lci *LocalConfigInfo) StorageExists(storageName string) bool {
for _, storage := range lci.GetStorage() {
if storageName == storage.Name {
return true
}
// ListStorage gets all the storage from the config
func (lci *LocalConfigInfo) ListStorage() []localConfigProvider.LocalStorage {
if lci.componentSettings.Storage == nil {
return []localConfigProvider.LocalStorage{}
}
return false
}

func (lci *LocalConfigInfo) StorageList() ([]ComponentStorageSettings, error) {
storageConfigList := lci.GetStorage()
var storageList []ComponentStorageSettings
for _, storage := range storageConfigList {
storageList = append(storageList, ComponentStorageSettings{
var storageList []localConfigProvider.LocalStorage
for _, storage := range *lci.componentSettings.Storage {
storageList = append(storageList, localConfigProvider.LocalStorage{
Name: storage.Name,
Path: storage.Path,
Size: storage.Size,
})
}
return storageList, nil
return storageList
}

func (lci *LocalConfigInfo) ValidateStorage(storageName, storagePath string) error {
for _, storage := range lci.GetStorage() {
if storage.Name == storageName {
return errors.Errorf("there already is a storage with the name %s", storageName)
}
if storage.Path == storagePath {
return errors.Errorf("there already is a storage mounted at %s", storagePath)
}
// DeleteStorage deletes the storage with the given name
func (lci *LocalConfigInfo) DeleteStorage(name string) error {
storage := lci.GetStorage(name)
if storage == nil {
return errors.Errorf("storage named %s doesn't exists", name)
}
return nil
return lci.DeleteFromConfigurationList("storage", name)
}

func (lci *LocalConfigInfo) StorageDelete(name string) error {
exists := lci.StorageExists(name)
if !exists {
return errors.Errorf("storage named %s doesn't exists", name)
// CompleteStorage completes the given storage
func (lci *LocalConfigInfo) CompleteStorage(storage *localConfigProvider.LocalStorage) {}

// ValidateStorage validates the given storage
func (lci *LocalConfigInfo) ValidateStorage(storage localConfigProvider.LocalStorage) error {
if storage.Size == "" || storage.Path == "" {
return fmt.Errorf("\"size\" and \"path\" flags are required for s2i components")
}
return lci.DeleteFromConfigurationList("storage", name)

for _, store := range lci.ListStorage() {
if store.Name == storage.Name {
return errors.Errorf("there already is a storage with the name %s", storage.Name)
}
if store.Path == storage.Path {
return errors.Errorf("there already is a storage mounted at %s", storage.Path)
}
}
return nil
}

func (lci *LocalConfigInfo) GetMountPath(storageName string) string {
// GetStorageMountPath gets the mount path of the storage with the given storage name
func (lci *LocalConfigInfo) GetStorageMountPath(storageName string) (string, error) {
var mPath string
storageList, _ := lci.StorageList()
for _, storage := range storageList {
for _, storage := range lci.ListStorage() {
if storage.Name == storageName {
mPath = storage.Path
}
}
return mPath
return mPath, nil
}
Loading

0 comments on commit 301ae1d

Please sign in to comment.