Files
odo/pkg/component/pushed_component.go
Charlie Drage 83e994582a Dont save name of component to env.yaml (#5852)
<!--
Thank you for opening a PR! Here are some things you need to know before submitting:

1. Please read our developer guideline: https://github.com/redhat-developer/odo/wiki/Dev:-odo-Dev-Guidelines
2. Label this PR accordingly with the '/kind' line
3. Ensure you have written and ran the appropriate tests: https://github.com/redhat-developer/odo/wiki/Dev:-Writing-and-running-tests
4. Read how we approve and LGTM each PR: https://github.com/redhat-developer/odo/wiki/Pull-Requests:-Review-guideline

Documentation:

If you are pushing a change to documentation, please read: https://github.com/redhat-developer/odo/wiki/Documentation:-Contributing
-->

**What type of PR is this:**

<!--
Add one of the following kinds:
/kind feature
/kind cleanup
/kind tests
/kind documentation

Feel free to use other [labels](https://github.com/redhat-developer/odo/labels) as needed. However one of the above labels must be present or the PR will not be reviewed. This instruction is for reviewers as well.
-->
/kind bug

**What does this PR do / why we need it:**

By default we should be saving the namespace / project name, not the app name /
component name.

**Which issue(s) this PR fixes:**
<!--
Specifying the issue will automatically close it when this PR is merged
-->

Fixes #5780
Fixes #5886

**PR acceptance criteria:**

- [X] Unit test

- [X] Integration test

- [X] Documentation

**How to test changes / Special notes to the reviewer:**

Signed-off-by: Charlie Drage <charlie@charliedrage.com>
2022-07-28 19:31:45 +02:00

155 lines
3.7 KiB
Go

package component
import (
"fmt"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
odolabels "github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/storage"
)
type provider interface {
GetLabels() map[string]string
GetAnnotations() map[string]string
GetName() string
GetEnvVars() []v1.EnvVar
GetLinkedSecrets() []SecretMount
}
// SecretMount describes a Secret mount (either as environment variables with envFrom or as a volume)
type SecretMount struct {
ServiceName string
SecretName string
MountVolume bool
MountPath string
}
// PushedComponent is an abstraction over the cluster representation of the component
type PushedComponent interface {
provider
GetApplication() string
GetType() (string, error)
GetStorage() ([]storage.Storage, error)
}
type defaultPushedComponent struct {
application string
storage []storage.Storage
provider provider
storageClient storage.Client
}
var _ provider = (*defaultPushedComponent)(nil)
var _ PushedComponent = (*defaultPushedComponent)(nil)
func (d defaultPushedComponent) GetLabels() map[string]string {
return d.provider.GetLabels()
}
func (d defaultPushedComponent) GetAnnotations() map[string]string {
return d.provider.GetAnnotations()
}
func (d defaultPushedComponent) GetName() string {
return d.provider.GetName()
}
func (d defaultPushedComponent) GetType() (string, error) {
return getType(d.provider)
}
func (d defaultPushedComponent) GetEnvVars() []v1.EnvVar {
return d.provider.GetEnvVars()
}
func (d defaultPushedComponent) GetLinkedSecrets() []SecretMount {
return d.provider.GetLinkedSecrets()
}
// GetStorage gets the storage using the storage client of the given pushed component
func (d defaultPushedComponent) GetStorage() ([]storage.Storage, error) {
if d.storage == nil {
if _, ok := d.provider.(*devfileComponent); ok {
storageList, err := d.storageClient.List()
if err != nil {
return nil, err
}
d.storage = storageList.Items
}
}
return d.storage, nil
}
func (d defaultPushedComponent) GetApplication() string {
return d.application
}
type devfileComponent struct {
d appsv1.Deployment
}
var _ provider = (*devfileComponent)(nil)
func (d devfileComponent) GetLinkedSecrets() (secretMounts []SecretMount) {
for _, container := range d.d.Spec.Template.Spec.Containers {
for _, env := range container.EnvFrom {
if env.SecretRef != nil {
secretMounts = append(secretMounts, SecretMount{
SecretName: env.SecretRef.Name,
MountVolume: false,
})
}
}
}
for _, volume := range d.d.Spec.Template.Spec.Volumes {
if volume.Secret != nil {
mountPath := ""
for _, container := range d.d.Spec.Template.Spec.Containers {
for _, mount := range container.VolumeMounts {
if mount.Name == volume.Name {
mountPath = mount.MountPath
break
}
}
}
secretMounts = append(secretMounts, SecretMount{
SecretName: volume.Secret.SecretName,
MountVolume: true,
MountPath: mountPath,
})
}
}
return secretMounts
}
func (d devfileComponent) GetEnvVars() []v1.EnvVar {
var envs []v1.EnvVar
for _, container := range d.d.Spec.Template.Spec.Containers {
envs = append(envs, container.Env...)
}
return envs
}
func (d devfileComponent) GetLabels() map[string]string {
return d.d.Labels
}
func (d devfileComponent) GetAnnotations() map[string]string {
return d.d.Annotations
}
func (d devfileComponent) GetName() string {
return odolabels.GetComponentName(d.d.Labels)
}
func getType(component provider) (string, error) {
res, err := odolabels.GetProjectType(component.GetLabels(), component.GetAnnotations())
if err != nil {
return "", fmt.Errorf("%s component doesn't provide a type annotation; consider pushing the component again", component.GetName())
}
return res, nil
}