Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rm3l committed Aug 11, 2022
1 parent 9e0be19 commit 1d64567
Show file tree
Hide file tree
Showing 20 changed files with 3,571 additions and 2,947 deletions.
8 changes: 7 additions & 1 deletion pkg/binding/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/redhat-developer/odo/pkg/component"
sboApi "github.com/redhat-developer/service-binding-operator/apis/binding/v1alpha1"

"github.com/redhat-developer/odo/pkg/binding/asker"
Expand Down Expand Up @@ -112,7 +113,12 @@ func (o *BindingClient) AddBindingToDevfile(
return obj, err
}

deploymentName := fmt.Sprintf("%s-app", obj.GetMetadataName())
componentName, err := component.GatherName(obj)
if err != nil {
return obj, err
}

deploymentName := fmt.Sprintf("%s-app", componentName)
deploymentGVK, err := o.kubernetesClient.GetDeploymentAPIVersion()
if err != nil {
return obj, err
Expand Down
36 changes: 10 additions & 26 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package component
import (
"fmt"
"io"
"path/filepath"
"sort"
"strings"

Expand All @@ -13,14 +12,14 @@ import (
"github.com/devfile/library/pkg/devfile/parser/data"
dfutil "github.com/devfile/library/pkg/util"

"github.com/redhat-developer/odo/pkg/alizer"
"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/labels"
odolabels "github.com/redhat-developer/odo/pkg/labels"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog"
)

const (
Expand All @@ -42,32 +41,17 @@ func GetComponentTypeFromDevfileMetadata(metadata devfile.DevfileMetadata) strin
return componentType
}

// GatherName parses the Devfile and retrieves an appropriate name in two ways.
// 1. If metadata.name exists, we use it
// 2. If metadata.name does NOT exist, we use the folder name where the devfile.yaml is located
func GatherName(devObj parser.DevfileObj, devfilePath string) (string, error) {

metadata := devObj.Data.GetMetadata()

klog.V(4).Infof("metadata.Name: %s", metadata.Name)

// 1. Use metadata.name if it exists
if metadata.Name != "" {

// Remove any suffix's that end with `-`. This is because many Devfile's use the original v1 Devfile pattern of
// having names such as "foo-bar-" in order to prepend container names such as "foo-bar-container1"
return strings.TrimSuffix(metadata.Name, "-"), nil
}

// 2. Use the folder name as a last resort if nothing else exists
sourcePath, err := dfutil.GetAbsPath(devfilePath)
if err != nil {
return "", fmt.Errorf("unable to get source path: %w", err)
// GatherName returns the name of the component.
//// If a name is set in the Devfile metadata (which is optional) and is not blank, it returns that.
//// Otherwise, it uses Alizer to detect the name of the component, from the project build tools (pom.xml, package.json, ...),
//// or from the component directory name.
func GatherName(devfileObj parser.DevfileObj) (string, error) {
m := devfileObj.GetMetadataName()
if m != "" && strings.TrimSpace(m) != "" {
return m, nil
}
klog.V(4).Infof("Source path: %s", sourcePath)
klog.V(4).Infof("devfile dir: %s", filepath.Dir(sourcePath))

return filepath.Base(filepath.Dir(sourcePath)), nil
return alizer.DetectName(devfileObj.Ctx.GetAbsPath())
}

// Exists checks whether a component with the given name exists in the current application or not
Expand Down
14 changes: 12 additions & 2 deletions pkg/component/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ func (do DeleteComponentClient) ListResourcesToDeleteFromDevfile(devfileObj pars
if mode == odolabels.ComponentDevMode || mode == odolabels.ComponentAnyMode {
// Inner Loop
// Fetch the deployment of the devfile component
componentName := devfileObj.GetMetadataName()
var componentName string
componentName, err = component.GatherName(devfileObj)
if err != nil {
return isInnerLoopDeployed, resources, err
}

var deploymentName string
deploymentName, err = util.NamespaceKubernetesObject(componentName, appName)
if err != nil {
Expand Down Expand Up @@ -155,7 +160,12 @@ func (do *DeleteComponentClient) ExecutePreStopEvents(devfileObj parser.DevfileO
if !libdevfile.HasPreStopEvents(devfileObj) {
return nil
}
componentName := devfileObj.GetMetadataName()

componentName, err := component.GatherName(devfileObj)
if err != nil {
return err
}

klog.V(4).Infof("Gathering information for component: %q", componentName)

klog.V(3).Infof("Checking component status for %q", componentName)
Expand Down
22 changes: 17 additions & 5 deletions pkg/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"github.com/devfile/library/pkg/devfile/parser"
"k8s.io/klog/v2"

"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/devfile/adapters"
"github.com/redhat-developer/odo/pkg/devfile/adapters/kubernetes/component"
k8sComponent "github.com/redhat-developer/odo/pkg/devfile/adapters/kubernetes/component"
"github.com/redhat-developer/odo/pkg/watch"
)

Expand Down Expand Up @@ -58,10 +59,16 @@ func (o *DevClient) Start(
fs filesystem.Filesystem,
) (watch.ComponentStatus, error) {
klog.V(4).Infoln("Creating new adapter")
adapter := component.NewKubernetesAdapter(

componentName, err := component.GatherName(devfileObj)
if err != nil {
return watch.ComponentStatus{}, err
}

adapter := k8sComponent.NewKubernetesAdapter(
o.kubernetesClient, o.prefClient, o.portForwardClient, o.bindingClient,
component.AdapterContext{
ComponentName: devfileObj.GetMetadataName(),
k8sComponent.AdapterContext{
ComponentName: componentName,
Context: path,
AppName: "app",
Devfile: devfileObj,
Expand Down Expand Up @@ -118,10 +125,15 @@ func (o *DevClient) Watch(
return err
}

componentName, err := component.GatherName(devfileObj)
if err != nil {
return err
}

watchParameters := watch.WatchParameters{
DevfilePath: devfilePath,
Path: path,
ComponentName: devfileObj.GetMetadataName(),
ComponentName: componentName,
ApplicationName: "app",
DevfileWatchHandler: h.RegenerateAdapterAndPush,
EnvSpecificInfo: envSpecificInfo,
Expand Down
7 changes: 6 additions & 1 deletion pkg/devfile/adapters/kubernetes/component/commandhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ func ApplyKubernetes(mode, appName string, devfile parser.DevfileObj, kubernetes
return err
}

componentName, err := component.GatherName(devfile)
if err != nil {
return err
}

// Get the most common labels that's applicable to all resources being deployed.
// Set the mode. Regardless of what Kubernetes resource we are deploying.
labels := odolabels.GetLabels(devfile.Data.GetMetadata().Name, appName, mode, false)
labels := odolabels.GetLabels(componentName, appName, mode, false)

klog.V(4).Infof("Injecting labels: %+v into k8s artifact", labels)

Expand Down
6 changes: 5 additions & 1 deletion pkg/odo/cli/add/binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/redhat-developer/odo/pkg/binding/asker"
"github.com/redhat-developer/odo/pkg/binding/backend"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
Expand Down Expand Up @@ -123,7 +124,10 @@ func (o *AddBindingOptions) Run(_ context.Context) error {
}
componentName = workloadName
} else {
componentName = o.EnvSpecificInfo.GetDevfileObj().GetMetadataName()
componentName, err = component.GatherName(o.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return err
}
}

bindingName, err := o.clientset.BindingClient.AskBindingName(serviceName, componentName, o.flags)
Expand Down
8 changes: 7 additions & 1 deletion pkg/odo/cli/delete/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
ktemplates "k8s.io/kubectl/pkg/util/templates"

"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/labels"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
Expand Down Expand Up @@ -122,7 +123,12 @@ func (o *ComponentOptions) deleteNamedComponent() error {
// deleteDevfileComponent deletes all the components defined by the devfile in the current directory
func (o *ComponentOptions) deleteDevfileComponent() error {
devfileObj := o.EnvSpecificInfo.GetDevfileObj()
componentName := devfileObj.GetMetadataName()

componentName, err := component.GatherName(devfileObj)
if err != nil {
return err
}

namespace := o.GetProject()
appName := "app"

Expand Down
9 changes: 7 additions & 2 deletions pkg/odo/cli/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func (o *DeployOptions) Validate() error {
// Run contains the logic for the odo command
func (o *DeployOptions) Run(ctx context.Context) error {
devfileObj := o.EnvSpecificInfo.GetDevfileObj()
devfileName := devfileObj.GetMetadataName()

devfileName, err := component.GatherName(devfileObj)
if err != nil {
return err
}

path := filepath.Dir(o.EnvSpecificInfo.GetDevfilePath())
appName := o.GetApplication()
namespace := o.GetProject()
Expand All @@ -160,7 +165,7 @@ func (o *DeployOptions) Run(ctx context.Context) error {
"odo version: "+version.VERSION)

// Run actual deploy command to be used
err := o.clientset.DeployClient.Deploy(o.clientset.FS, devfileObj, path, appName)
err = o.clientset.DeployClient.Deploy(o.clientset.FS, devfileObj, path, appName)

if err == nil {
log.Info("\nYour Devfile has been successfully deployed")
Expand Down
14 changes: 12 additions & 2 deletions pkg/odo/cli/describe/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ func (o *ComponentOptions) describeDevfileComponent() (result api.Component, dev
if err != nil {
return api.Component{}, nil, err
}
runningIn, err := component.GetRunningModes(o.clientset.KubernetesClient, devfileObj.GetMetadataName())

componentName, err := component.GatherName(devfileObj)
if err != nil {
return api.Component{}, nil, err
}

runningIn, err := component.GetRunningModes(o.clientset.KubernetesClient, componentName)
if err != nil {
if !errors.As(err, &component.NoComponentFoundError{}) {
return api.Component{}, nil, err
Expand All @@ -159,7 +165,11 @@ func (o *ComponentOptions) describeDevfileComponent() (result api.Component, dev

func printHumanReadableOutput(cmp api.Component, devfileObj *parser.DevfileObj) error {
if cmp.DevfileData != nil {
log.Describef("Name: ", cmp.DevfileData.Devfile.GetMetadata().Name)
componentName, err := component.GatherName(*devfileObj)
if err != nil {
return err
}
log.Describef("Name: ", componentName)
log.Describef("Display Name: ", cmp.DevfileData.Devfile.GetMetadata().DisplayName)
log.Describef("Project Type: ", cmp.DevfileData.Devfile.GetMetadata().ProjectType)
log.Describef("Language: ", cmp.DevfileData.Devfile.GetMetadata().Language)
Expand Down
14 changes: 9 additions & 5 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,16 @@ func (o *DevOptions) Validate() error {

func (o *DevOptions) Run(ctx context.Context) (err error) {
var (
devFileObj = o.Context.EnvSpecificInfo.GetDevfileObj()
path = filepath.Dir(o.Context.EnvSpecificInfo.GetDevfilePath())
devfileName = devFileObj.GetMetadataName()
namespace = o.GetProject()
devFileObj = o.Context.EnvSpecificInfo.GetDevfileObj()
path = filepath.Dir(o.Context.EnvSpecificInfo.GetDevfilePath())
namespace = o.GetProject()
)

devfileName, err := component.GatherName(devFileObj)
if err != nil {
return err
}

defer func() {
if err != nil {
_ = o.clientset.WatchClient.CleanupDevResources(devFileObj, log.GetStdout())
Expand Down Expand Up @@ -248,7 +252,7 @@ func (o *DevOptions) Run(ctx context.Context) (err error) {
scontext.SetComponentType(ctx, component.GetComponentTypeFromDevfileMetadata(devFileObj.Data.GetMetadata()))
scontext.SetLanguage(ctx, devFileObj.Data.GetMetadata().Language)
scontext.SetProjectType(ctx, devFileObj.Data.GetMetadata().ProjectType)
scontext.SetDevfileName(ctx, devFileObj.GetMetadataName())
scontext.SetDevfileName(ctx, devfileName)

d := Handler{
clientset: *o.clientset,
Expand Down
6 changes: 5 additions & 1 deletion pkg/odo/cli/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ func (lo *ListOptions) Complete(cmdline cmdline.Cmdline, args []string) (err err
}

// Create a local component from the parse devfile
componentName, err := component.GatherName(devObj)
if err != nil {
return err
}
localComponent := api.ComponentAbstract{
Name: devObj.Data.GetMetadata().Name,
Name: componentName,
ManagedBy: "",
RunningIn: []api.RunningMode{},
Type: component.GetComponentTypeFromDevfileMetadata(devObj.Data.GetMetadata()),
Expand Down
17 changes: 12 additions & 5 deletions pkg/odo/cli/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ import (
"context"
"errors"
"fmt"
"github.com/fatih/color"
odolabels "github.com/redhat-developer/odo/pkg/labels"
"io"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/fatih/color"

"github.com/redhat-developer/odo/pkg/component"
odolabels "github.com/redhat-developer/odo/pkg/labels"

"github.com/redhat-developer/odo/pkg/log"

"github.com/redhat-developer/odo/pkg/devfile/location"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"

"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"

"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
)

const RecommendedCommandName = "logs"
Expand Down Expand Up @@ -88,7 +92,10 @@ func (o *LogsOptions) Complete(cmdline cmdline.Cmdline, _ []string) error {
return fmt.Errorf("unable to create context: %v", err)
}

o.componentName = o.Context.EnvSpecificInfo.GetDevfileObj().GetMetadataName()
o.componentName, err = component.GatherName(o.Context.EnvSpecificInfo.GetDevfileObj())
if err != nil {
return err
}

o.clientset.KubernetesClient.SetNamespace(o.Context.GetProject())

Expand Down
3 changes: 1 addition & 2 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/devfile/registry-support/registry-library/library"

"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/devfile"
"github.com/redhat-developer/odo/pkg/devfile/location"
"github.com/redhat-developer/odo/pkg/log"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (o RegistryClient) DownloadFileInMemory(params dfutil.HTTPRequestParams) ([
// DownloadStarterProject downloads a starter project referenced in devfile
// This will first remove the content of the contextDir
func (o RegistryClient) DownloadStarterProject(starterProject *devfilev1.StarterProject, decryptedToken string, contextDir string, verbose bool) error {
return component.DownloadStarterProject(starterProject, decryptedToken, contextDir, verbose)
return DownloadStarterProject(starterProject, decryptedToken, contextDir, verbose)
}

// GetDevfileRegistries gets devfile registries from preference file,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package component
package registry

import (
"errors"
Expand Down
Loading

0 comments on commit 1d64567

Please sign in to comment.