...

Source file src/github.com/redhat-developer/odo/pkg/storage/list.go

Documentation: github.com/redhat-developer/odo/pkg/storage

     1  package storage
     2  
     3  import (
     4  	devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     5  	"github.com/devfile/library/v2/pkg/devfile/generator"
     6  	"github.com/devfile/library/v2/pkg/devfile/parser"
     7  	"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
     8  )
     9  
    10  const (
    11  	// DefaultVolumeSize Default volume size for volumes defined in a devfile
    12  	DefaultVolumeSize = "1Gi"
    13  )
    14  
    15  // LocalStorage holds storage related information
    16  type LocalStorage struct {
    17  	// Name of the storage
    18  	Name string `yaml:"Name,omitempty"`
    19  	// Size of the storage
    20  	Size string `yaml:"Size,omitempty"`
    21  	// Boolean indicating if the volume should be ephemeral. A nil pointer indicates to use the default behaviour
    22  	Ephemeral *bool `yaml:"Ephemeral,omitempty"`
    23  	// Path of the storage to which it will be mounted on the container
    24  	Path string `yaml:"Path,omitempty"`
    25  	// Container is the container name on which this storage is mounted
    26  	Container string `yaml:"-" json:"-"`
    27  }
    28  
    29  // ListStorage gets all the storage from the devfile.yaml
    30  func ListStorage(devfileObj parser.DevfileObj) ([]LocalStorage, error) {
    31  	var storageList []LocalStorage
    32  
    33  	volumeMap := make(map[string]devfilev1.Volume)
    34  	components, err := devfileObj.Data.GetComponents(common.DevfileOptions{})
    35  	if err != nil {
    36  		return storageList, err
    37  	}
    38  
    39  	for _, component := range components {
    40  		if component.Volume == nil {
    41  			continue
    42  		}
    43  		if component.Volume.Size == "" {
    44  			component.Volume.Size = DefaultVolumeSize
    45  		}
    46  		volumeMap[component.Name] = component.Volume.Volume
    47  	}
    48  
    49  	for _, component := range components {
    50  		if component.Container == nil {
    51  			continue
    52  		}
    53  		for _, volumeMount := range component.Container.VolumeMounts {
    54  			vol, ok := volumeMap[volumeMount.Name]
    55  			if ok {
    56  				storageList = append(storageList, LocalStorage{
    57  					Name:      volumeMount.Name,
    58  					Size:      vol.Size,
    59  					Ephemeral: vol.Ephemeral,
    60  					Path:      generator.GetVolumeMountPath(volumeMount),
    61  					Container: component.Name,
    62  				})
    63  			}
    64  		}
    65  	}
    66  
    67  	return storageList, nil
    68  }
    69  

View as plain text