Skip to content

Commit

Permalink
amend! Rely on the 'component#GatherName' function instead of the Dev…
Browse files Browse the repository at this point in the history
…file metadata name

Compute and store the component name in the CLI context, and pass it as needed

As commented out in [1], the context should ideally be built
and passed down to the business clients structs.

[1] redhat-developer#6015 (comment)
  • Loading branch information
rm3l committed Aug 29, 2022
1 parent b8f9079 commit 718a6ad
Show file tree
Hide file tree
Showing 31 changed files with 929 additions and 895 deletions.
7 changes: 1 addition & 6 deletions pkg/binding/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ 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 @@ -101,6 +100,7 @@ func (o *BindingClient) AskNamingStrategy(flags map[string]string) (string, erro
}

func (o *BindingClient) AddBindingToDevfile(
componentName string,
bindingName string,
bindAsFiles bool,
serviceNs string,
Expand All @@ -113,11 +113,6 @@ func (o *BindingClient) AddBindingToDevfile(
return obj, err
}

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 {
Expand Down
10 changes: 9 additions & 1 deletion pkg/binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,15 @@ func TestBindingClient_AddBindingToDevfile(t *testing.T) {
o := &BindingClient{
kubernetesClient: tt.fields.kubernetesClient(ctrl),
}
got, err := o.AddBindingToDevfile(tt.args.bindingName, tt.args.bindAsFiles, tt.args.namespace, tt.args.namingStrategy, tt.args.unstructuredService, tt.args.obj)
got, err := o.AddBindingToDevfile(
tt.args.obj.GetMetadataName(),
tt.args.bindingName,
tt.args.bindAsFiles,
tt.args.namespace,
tt.args.namingStrategy,
tt.args.unstructuredService,
tt.args.obj,
)
if (err != nil) != tt.wantErr {
t.Errorf("AddBindingToDevfile() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
1 change: 1 addition & 0 deletions pkg/binding/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Client interface {
AskNamingStrategy(flags map[string]string) (string, error)
// AddBindingToDevfile adds the ServiceBinding manifest to the devfile
AddBindingToDevfile(
componentName string,
bindingName string,
bindAsFiles bool,
serviceNs string,
Expand Down
8 changes: 4 additions & 4 deletions pkg/binding/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions pkg/component/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,10 @@ func references(list []unstructured.Unstructured, ownerRef metav1.OwnerReference
}

// ListResourcesToDeleteFromDevfile parses all the devfile components and returns a list of resources that are present on the cluster and can be deleted
func (do DeleteComponentClient) ListResourcesToDeleteFromDevfile(devfileObj parser.DevfileObj, appName string, mode string) (isInnerLoopDeployed bool, resources []unstructured.Unstructured, err error) {
func (do DeleteComponentClient) ListResourcesToDeleteFromDevfile(devfileObj parser.DevfileObj, appName string, componentName string, mode string) (isInnerLoopDeployed bool, resources []unstructured.Unstructured, err error) {
var deployment *v1.Deployment
if mode == odolabels.ComponentDevMode || mode == odolabels.ComponentAnyMode {
// Inner Loop
// Fetch the deployment of the devfile component
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 @@ -156,16 +149,11 @@ func (do DeleteComponentClient) ListResourcesToDeleteFromDevfile(devfileObj pars
}

// ExecutePreStopEvents executes preStop events if any, as a precondition to deleting a devfile component deployment
func (do *DeleteComponentClient) ExecutePreStopEvents(devfileObj parser.DevfileObj, appName string) error {
func (do *DeleteComponentClient) ExecutePreStopEvents(devfileObj parser.DevfileObj, appName string, componentName string) error {
if !libdevfile.HasPreStopEvents(devfileObj) {
return nil
}

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
4 changes: 2 additions & 2 deletions pkg/component/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func TestDeleteComponentClient_ListResourcesToDeleteFromDevfile(t *testing.T) {
do := DeleteComponentClient{
kubeClient: tt.fields.kubeClient(ctrl),
}
gotIsInnerLoopDeployed, gotResources, err := do.ListResourcesToDeleteFromDevfile(tt.args.devfileObj, tt.args.appName, tt.args.mode)
gotIsInnerLoopDeployed, gotResources, err := do.ListResourcesToDeleteFromDevfile(tt.args.devfileObj, tt.args.appName, tt.args.devfileObj.GetMetadataName(), tt.args.mode)
if (err != nil) != tt.wantErr {
t.Errorf("ListResourcesToDeleteFromDevfile() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -702,7 +702,7 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
do := &DeleteComponentClient{
kubeClient: tt.fields.kubeClient(ctrl),
}
if err := do.ExecutePreStopEvents(tt.args.devfileObj, tt.args.appName); (err != nil) != tt.wantErr {
if err := do.ExecutePreStopEvents(tt.args.devfileObj, tt.args.appName, tt.args.devfileObj.GetMetadataName()); (err != nil) != tt.wantErr {
t.Errorf("DeleteComponent() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/component/delete/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type Client interface {
// set wait to true to wait for all the dependencies to be deleted
DeleteResources(resources []unstructured.Unstructured, wait bool) []unstructured.Unstructured
// ExecutePreStopEvents executes preStop events if any, as a precondition to deleting a devfile component deployment
ExecutePreStopEvents(devfileObj parser.DevfileObj, appName string) error
ExecutePreStopEvents(devfileObj parser.DevfileObj, appName string, componentName string) error
// ListResourcesToDeleteFromDevfile parses all the devfile components and returns a list of resources that are present on the cluster that can be deleted,
// and a bool that indicates if the devfile component has been pushed to the innerloop
// the mode indicates which component to list, either Dev, Deploy or Any (using constant labels.Component*Mode)
ListResourcesToDeleteFromDevfile(devfileObj parser.DevfileObj, appName string, mode string) (bool, []unstructured.Unstructured, error)
ListResourcesToDeleteFromDevfile(devfileObj parser.DevfileObj, appName string, componentName string, mode string) (bool, []unstructured.Unstructured, error)
}
16 changes: 8 additions & 8 deletions pkg/component/delete/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,30 @@ func NewDeployClient(kubeClient kclient.ClientInterface) *DeployClient {
}
}

func (o *DeployClient) Deploy(fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, appName string) error {
deployHandler := newDeployHandler(fs, devfileObj, path, o.kubeClient, appName)
func (o *DeployClient) Deploy(fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, appName string, componentName string) error {
deployHandler := newDeployHandler(fs, devfileObj, path, o.kubeClient, appName, componentName)
return libdevfile.Deploy(devfileObj, deployHandler)
}

type deployHandler struct {
fs filesystem.Filesystem
devfileObj parser.DevfileObj
path string
kubeClient kclient.ClientInterface
appName string
fs filesystem.Filesystem
devfileObj parser.DevfileObj
path string
kubeClient kclient.ClientInterface
appName string
componentName string
}

var _ libdevfile.Handler = (*deployHandler)(nil)

func newDeployHandler(fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, kubeClient kclient.ClientInterface, appName string) *deployHandler {
func newDeployHandler(fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, kubeClient kclient.ClientInterface, appName string, componentName string) *deployHandler {
return &deployHandler{
fs: fs,
devfileObj: devfileObj,
path: path,
kubeClient: kubeClient,
appName: appName,
fs: fs,
devfileObj: devfileObj,
path: path,
kubeClient: kubeClient,
appName: appName,
componentName: componentName,
}
}

Expand All @@ -58,7 +60,7 @@ func (o *deployHandler) ApplyImage(img v1alpha2.Component) error {

// ApplyKubernetes applies inline Kubernetes YAML from the devfile.yaml file
func (o *deployHandler) ApplyKubernetes(kubernetes v1alpha2.Component) error {
return component.ApplyKubernetes(odolabels.ComponentDeployMode, o.appName, o.devfileObj, kubernetes, o.kubeClient, o.path)
return component.ApplyKubernetes(odolabels.ComponentDeployMode, o.appName, o.componentName, o.devfileObj, kubernetes, o.kubeClient, o.path)
}

// Execute will deploy the listed information in the `exec` section of devfile.yaml
Expand Down
2 changes: 1 addition & 1 deletion pkg/deploy/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ type Client interface {
// Deploy resources from a devfile located in path, for the specified appName.
// The filesystem specified is used to download and store the Dockerfiles needed to build the necessary container images,
// in case such Dockerfiles are referenced as remote URLs in the Devfile.
Deploy(fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, appName string) error
Deploy(fs filesystem.Filesystem, devfileObj parser.DevfileObj, path string, appName string, componentName string) error
}
8 changes: 4 additions & 4 deletions pkg/deploy/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions pkg/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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"
k8sComponent "github.com/redhat-developer/odo/pkg/devfile/adapters/kubernetes/component"
"github.com/redhat-developer/odo/pkg/watch"
Expand Down Expand Up @@ -48,6 +47,7 @@ func NewDevClient(

func (o *DevClient) Start(
devfileObj parser.DevfileObj,
componentName string,
namespace string,
ignorePaths []string,
path string,
Expand All @@ -60,11 +60,6 @@ func (o *DevClient) Start(
) (watch.ComponentStatus, error) {
klog.V(4).Infoln("Creating new adapter")

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

adapter := k8sComponent.NewKubernetesAdapter(
o.kubernetesClient, o.prefClient, o.portForwardClient, o.bindingClient,
k8sComponent.AdapterContext{
Expand Down Expand Up @@ -106,6 +101,7 @@ func (o *DevClient) Start(
func (o *DevClient) Watch(
devfilePath string,
devfileObj parser.DevfileObj,
componentName string,
path string,
ignorePaths []string,
out io.Writer,
Expand All @@ -125,11 +121,6 @@ func (o *DevClient) Watch(
return err
}

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

watchParameters := watch.WatchParameters{
DevfilePath: devfilePath,
Path: path,
Expand Down
2 changes: 2 additions & 0 deletions pkg/dev/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Client interface {
// Returns the status of the started component
Start(
devfileObj parser.DevfileObj,
componentName string,
namespace string,
ignorePaths []string,
path string,
Expand All @@ -40,6 +41,7 @@ type Client interface {
Watch(
devfilePath string,
devfileObj parser.DevfileObj,
componentName string,
path string,
ignorePaths []string,
out io.Writer,
Expand Down
Loading

0 comments on commit 718a6ad

Please sign in to comment.