...

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

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

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	v1 "k8s.io/api/apps/v1"
     7  
     8  	"github.com/devfile/library/v2/pkg/devfile/parser"
     9  	"github.com/redhat-developer/odo/pkg/kclient"
    10  	"github.com/redhat-developer/odo/pkg/log"
    11  )
    12  
    13  const (
    14  	// OdoSourceVolume is the constant containing the name of the emptyDir volume containing the project source
    15  	OdoSourceVolume = "odo-projects"
    16  
    17  	// SharedDataVolumeName is the constant containing the name of the emptyDir volume containing shared data for odo
    18  	SharedDataVolumeName = "odo-shared-data"
    19  
    20  	// SharedDataMountPath The Mount Path for the container mounting the odo volume
    21  	SharedDataMountPath = "/opt/odo/"
    22  
    23  	// OdoSourceVolumeSize specifies size for odo source volume.
    24  	OdoSourceVolumeSize = "2Gi"
    25  )
    26  
    27  // generic contains information required for all the Storage clients
    28  type generic struct {
    29  	appName       string
    30  	componentName string
    31  	runtime       string
    32  }
    33  
    34  type ClientOptions struct {
    35  	Client     kclient.ClientInterface
    36  	Deployment *v1.Deployment
    37  	Runtime    string
    38  }
    39  
    40  type Client interface {
    41  	Create(Storage) error
    42  	Delete(string) error
    43  	List() (StorageList, error)
    44  }
    45  
    46  // NewClient gets the appropriate Storage client based on the parameters
    47  func NewClient(componentName string, appName string, options ClientOptions) Client {
    48  	var genericInfo generic
    49  
    50  	genericInfo.componentName = componentName
    51  	genericInfo.appName = appName
    52  	genericInfo.runtime = options.Runtime
    53  
    54  	return kubernetesClient{
    55  		generic:    genericInfo,
    56  		client:     options.Client,
    57  		deployment: options.Deployment,
    58  	}
    59  }
    60  
    61  // Push creates and deletes the required persistent storages and returns the list of ephemeral storages
    62  // it compares the local storage against the storage on the cluster
    63  func Push(client Client, devfileObj parser.DevfileObj) (ephemerals map[string]Storage, _ error) {
    64  	// list all the storage in the cluster
    65  	storageClusterList, err := client.List()
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	storageClusterNames := make(map[string]Storage)
    70  	for _, storage := range storageClusterList.Items {
    71  		storageClusterNames[storage.Name] = storage
    72  	}
    73  
    74  	// list the persistent storages in the config
    75  	persistentConfigNames := make(map[string]Storage)
    76  	// list the ephemeral storages
    77  	ephemeralConfigNames := make(map[string]Storage)
    78  
    79  	localStorage, err := ListStorage(devfileObj)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	for _, storage := range ConvertListLocalToMachine(localStorage).Items {
    84  		if storage.Spec.Ephemeral == nil || storage.Spec.Ephemeral != nil && !*storage.Spec.Ephemeral {
    85  			persistentConfigNames[storage.Name] = storage
    86  		} else {
    87  			ephemeralConfigNames[storage.Name] = storage
    88  		}
    89  	}
    90  
    91  	// find storage to delete
    92  	for storageName, storage := range storageClusterNames {
    93  		val, ok := persistentConfigNames[storageName]
    94  		if !ok {
    95  			// delete the pvc
    96  			err = client.Delete(storage.Name)
    97  			if err != nil {
    98  				return nil, err
    99  			}
   100  			log.Successf("Deleted storage %v from component", storage.Name)
   101  			continue
   102  		} else if storage.Name == val.Name {
   103  			if val.Spec.Size != storage.Spec.Size {
   104  				return nil, fmt.Errorf("config mismatch for storage with the same name %s", storage.Name)
   105  			}
   106  		}
   107  	}
   108  
   109  	// find storage to create
   110  	for storageName, storage := range persistentConfigNames {
   111  		_, ok := storageClusterNames[storageName]
   112  		if ok {
   113  			continue
   114  		}
   115  		if e := client.Create(storage); e != nil {
   116  			return nil, e
   117  		}
   118  		log.Successf("Added storage %v to component", storage.Name)
   119  	}
   120  
   121  	return ephemeralConfigNames, nil
   122  }
   123  

View as plain text