...

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

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

     1  package component
     2  
     3  import (
     4  	"fmt"
     5  
     6  	appsv1 "k8s.io/api/apps/v1"
     7  	v1 "k8s.io/api/core/v1"
     8  
     9  	odolabels "github.com/redhat-developer/odo/pkg/labels"
    10  	"github.com/redhat-developer/odo/pkg/storage"
    11  )
    12  
    13  type provider interface {
    14  	GetLabels() map[string]string
    15  	GetAnnotations() map[string]string
    16  	GetName() string
    17  	GetEnvVars() []v1.EnvVar
    18  	GetLinkedSecrets() []SecretMount
    19  }
    20  
    21  // SecretMount describes a Secret mount (either as environment variables with envFrom or as a volume)
    22  type SecretMount struct {
    23  	ServiceName string
    24  	SecretName  string
    25  	MountVolume bool
    26  	MountPath   string
    27  }
    28  
    29  // PushedComponent is an abstraction over the cluster representation of the component
    30  type PushedComponent interface {
    31  	provider
    32  	GetApplication() string
    33  	GetType() (string, error)
    34  	GetStorage() ([]storage.Storage, error)
    35  }
    36  
    37  type defaultPushedComponent struct {
    38  	application   string
    39  	storage       []storage.Storage
    40  	provider      provider
    41  	storageClient storage.Client
    42  }
    43  
    44  var _ provider = (*defaultPushedComponent)(nil)
    45  var _ PushedComponent = (*defaultPushedComponent)(nil)
    46  
    47  func (d defaultPushedComponent) GetLabels() map[string]string {
    48  	return d.provider.GetLabels()
    49  }
    50  
    51  func (d defaultPushedComponent) GetAnnotations() map[string]string {
    52  	return d.provider.GetAnnotations()
    53  }
    54  
    55  func (d defaultPushedComponent) GetName() string {
    56  	return d.provider.GetName()
    57  }
    58  
    59  func (d defaultPushedComponent) GetType() (string, error) {
    60  	return getType(d.provider)
    61  }
    62  
    63  func (d defaultPushedComponent) GetEnvVars() []v1.EnvVar {
    64  	return d.provider.GetEnvVars()
    65  }
    66  
    67  func (d defaultPushedComponent) GetLinkedSecrets() []SecretMount {
    68  	return d.provider.GetLinkedSecrets()
    69  }
    70  
    71  // GetStorage gets the storage using the storage client of the given pushed component
    72  func (d defaultPushedComponent) GetStorage() ([]storage.Storage, error) {
    73  	if d.storage == nil {
    74  		if _, ok := d.provider.(*devfileComponent); ok {
    75  			storageList, err := d.storageClient.List()
    76  			if err != nil {
    77  				return nil, err
    78  			}
    79  			d.storage = storageList.Items
    80  		}
    81  	}
    82  	return d.storage, nil
    83  }
    84  
    85  func (d defaultPushedComponent) GetApplication() string {
    86  	return d.application
    87  }
    88  
    89  type devfileComponent struct {
    90  	d appsv1.Deployment
    91  }
    92  
    93  var _ provider = (*devfileComponent)(nil)
    94  
    95  func (d devfileComponent) GetLinkedSecrets() (secretMounts []SecretMount) {
    96  	for _, container := range d.d.Spec.Template.Spec.Containers {
    97  		for _, env := range container.EnvFrom {
    98  			if env.SecretRef != nil {
    99  				secretMounts = append(secretMounts, SecretMount{
   100  					SecretName:  env.SecretRef.Name,
   101  					MountVolume: false,
   102  				})
   103  			}
   104  		}
   105  	}
   106  
   107  	for _, volume := range d.d.Spec.Template.Spec.Volumes {
   108  		if volume.Secret != nil {
   109  			mountPath := ""
   110  			for _, container := range d.d.Spec.Template.Spec.Containers {
   111  				for _, mount := range container.VolumeMounts {
   112  					if mount.Name == volume.Name {
   113  						mountPath = mount.MountPath
   114  						break
   115  					}
   116  				}
   117  			}
   118  			secretMounts = append(secretMounts, SecretMount{
   119  				SecretName:  volume.Secret.SecretName,
   120  				MountVolume: true,
   121  				MountPath:   mountPath,
   122  			})
   123  		}
   124  	}
   125  
   126  	return secretMounts
   127  }
   128  
   129  func (d devfileComponent) GetEnvVars() []v1.EnvVar {
   130  	var envs []v1.EnvVar
   131  	for _, container := range d.d.Spec.Template.Spec.Containers {
   132  		envs = append(envs, container.Env...)
   133  	}
   134  	return envs
   135  }
   136  
   137  func (d devfileComponent) GetLabels() map[string]string {
   138  	return d.d.Labels
   139  }
   140  func (d devfileComponent) GetAnnotations() map[string]string {
   141  	return d.d.Annotations
   142  }
   143  
   144  func (d devfileComponent) GetName() string {
   145  	return odolabels.GetComponentName(d.d.Labels)
   146  }
   147  
   148  func getType(component provider) (string, error) {
   149  	res, err := odolabels.GetProjectType(component.GetLabels(), component.GetAnnotations())
   150  	if err != nil {
   151  		return "", fmt.Errorf("%s component doesn't provide a type annotation; consider pushing the component again", component.GetName())
   152  	}
   153  	return res, nil
   154  }
   155  

View as plain text