Skip to content

Commit

Permalink
Compute and store the component name in the CLI context, and pass it …
Browse files Browse the repository at this point in the history
…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 e5fd847 commit 35a2605
Show file tree
Hide file tree
Showing 28 changed files with 311 additions and 161 deletions.
3 changes: 2 additions & 1 deletion pkg/binding/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,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 @@ -112,7 +113,7 @@ func (o *BindingClient) AddBindingToDevfile(
return obj, err
}

deploymentName := fmt.Sprintf("%s-app", obj.GetMetadataName())
deploymentName := fmt.Sprintf("%s-app", componentName)
deploymentGVK, err := o.kubernetesClient.GetDeploymentAPIVersion()
if err != nil {
return obj, err
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.

8 changes: 3 additions & 5 deletions pkg/component/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +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
componentName := devfileObj.GetMetadataName()
var deploymentName string
deploymentName, err = util.NamespaceKubernetesObject(componentName, appName)
if err != nil {
Expand Down Expand Up @@ -151,11 +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 := devfileObj.GetMetadataName()

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: 8 additions & 5 deletions pkg/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"k8s.io/klog/v2"

"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 @@ -47,6 +47,7 @@ func NewDevClient(

func (o *DevClient) Start(
devfileObj parser.DevfileObj,
componentName string,
namespace string,
ignorePaths []string,
path string,
Expand All @@ -58,10 +59,11 @@ func (o *DevClient) Start(
fs filesystem.Filesystem,
) (watch.ComponentStatus, error) {
klog.V(4).Infoln("Creating new adapter")
adapter := component.NewKubernetesAdapter(

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 @@ -98,6 +100,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 @@ -120,7 +123,7 @@ func (o *DevClient) Watch(
watchParameters := watch.WatchParameters{
DevfilePath: devfilePath,
Path: path,
ComponentName: devfileObj.GetMetadataName(),
ComponentName: componentName,
ApplicationName: "app",
DevfileWatchHandler: h.RegenerateAdapterAndPush,
EnvSpecificInfo: envSpecificInfo,
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 35a2605

Please sign in to comment.