Skip to content

Commit a069261

Browse files
committed
Refactor common code for podman/cluster
1 parent cf33c09 commit a069261

File tree

7 files changed

+80
-58
lines changed

7 files changed

+80
-58
lines changed

pkg/dev/common/run.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/redhat-developer/odo/pkg/component"
8+
"github.com/redhat-developer/odo/pkg/configAutomount"
9+
"github.com/redhat-developer/odo/pkg/devfile/image"
10+
"github.com/redhat-developer/odo/pkg/exec"
11+
"github.com/redhat-developer/odo/pkg/libdevfile"
12+
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
13+
"github.com/redhat-developer/odo/pkg/platform"
14+
"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
15+
)
16+
17+
func Run(
18+
ctx context.Context,
19+
commandName string,
20+
platformClient platform.Client,
21+
execClient exec.Client,
22+
configAutomountClient configAutomount.Client,
23+
filesystem filesystem.Filesystem,
24+
) error {
25+
var (
26+
componentName = odocontext.GetComponentName(ctx)
27+
devfileObj = odocontext.GetEffectiveDevfileObj(ctx)
28+
devfilePath = odocontext.GetDevfilePath(ctx)
29+
)
30+
31+
pod, err := platformClient.GetPodUsingComponentName(componentName)
32+
if err != nil {
33+
return fmt.Errorf("unable to get pod for component %s: %w", componentName, err)
34+
}
35+
36+
handler := component.NewRunHandler(
37+
ctx,
38+
platformClient,
39+
execClient,
40+
configAutomountClient,
41+
pod.Name,
42+
false,
43+
component.GetContainersNames(pod),
44+
"Executing command in container",
45+
46+
filesystem,
47+
image.SelectBackend(ctx),
48+
*devfileObj,
49+
devfilePath,
50+
)
51+
52+
return libdevfile.ExecuteCommandByName(ctx, *devfileObj, commandName, handler, false)
53+
}

pkg/dev/kubedev/run.go

+3-28
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,22 @@ package kubedev
22

33
import (
44
"context"
5-
"fmt"
65

7-
"github.com/redhat-developer/odo/pkg/component"
8-
"github.com/redhat-developer/odo/pkg/devfile/image"
9-
"github.com/redhat-developer/odo/pkg/libdevfile"
10-
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
6+
"github.com/redhat-developer/odo/pkg/dev/common"
117
"k8s.io/klog"
128
)
139

1410
func (o *DevClient) Run(
1511
ctx context.Context,
1612
commandName string,
1713
) error {
18-
var (
19-
componentName = odocontext.GetComponentName(ctx)
20-
devfileObj = odocontext.GetEffectiveDevfileObj(ctx)
21-
devfilePath = odocontext.GetDevfilePath(ctx)
22-
)
23-
2414
klog.V(4).Infof("running command %q on cluster", commandName)
25-
26-
pod, err := o.kubernetesClient.GetPodUsingComponentName(componentName)
27-
if err != nil {
28-
return fmt.Errorf("unable to get pod for component %s: %w", componentName, err)
29-
}
30-
31-
handler := component.NewRunHandler(
15+
return common.Run(
3216
ctx,
17+
commandName,
3318
o.kubernetesClient,
3419
o.execClient,
3520
o.configAutomountClient,
36-
pod.Name,
37-
false,
38-
component.GetContainersNames(pod),
39-
"Executing command in container",
40-
4121
o.filesystem,
42-
image.SelectBackend(ctx),
43-
*devfileObj,
44-
devfilePath,
4522
)
46-
47-
return libdevfile.ExecuteCommandByName(ctx, *devfileObj, commandName, handler, false)
4823
}

pkg/dev/podmandev/run.go

+3-28
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,22 @@ package podmandev
22

33
import (
44
"context"
5-
"fmt"
65

7-
"github.com/redhat-developer/odo/pkg/component"
8-
"github.com/redhat-developer/odo/pkg/devfile/image"
9-
"github.com/redhat-developer/odo/pkg/libdevfile"
10-
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
6+
"github.com/redhat-developer/odo/pkg/dev/common"
117
"k8s.io/klog"
128
)
139

1410
func (o *DevClient) Run(
1511
ctx context.Context,
1612
commandName string,
1713
) error {
18-
var (
19-
componentName = odocontext.GetComponentName(ctx)
20-
devfileObj = odocontext.GetEffectiveDevfileObj(ctx)
21-
devfilePath = odocontext.GetDevfilePath(ctx)
22-
)
23-
2414
klog.V(4).Infof("running command %q on cluster", commandName)
25-
26-
pod, err := o.podmanClient.GetPodUsingComponentName(componentName)
27-
if err != nil {
28-
return fmt.Errorf("unable to get pod for component %s: %w", componentName, err)
29-
}
30-
31-
handler := component.NewRunHandler(
15+
return common.Run(
3216
ctx,
17+
commandName,
3318
o.podmanClient,
3419
o.execClient,
3520
nil, // TODO(feloy) set when running on new container is supported on podman
36-
pod.Name,
37-
false,
38-
component.GetContainersNames(pod),
39-
"Executing command in container",
40-
4121
o.fs,
42-
image.SelectBackend(ctx),
43-
*devfileObj,
44-
devfilePath,
4522
)
46-
47-
return libdevfile.ExecuteCommandByName(ctx, *devfileObj, commandName, handler, false)
4823
}

pkg/exec/exec_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func (o fakePlatform) GetRunningPodFromSelector(selector string) (*corev1.Pod, e
4444
panic("not implemented yet")
4545
}
4646

47+
func (o fakePlatform) GetPodUsingComponentName(componentName string) (*corev1.Pod, error) {
48+
panic("not implemented yet")
49+
}
50+
4751
func TestExecuteCommand(t *testing.T) {
4852
for _, tt := range []struct {
4953
name string

pkg/platform/interface.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ type Client interface {
3131
// GetRunningPodFromSelector returns any pod matching the given label selector.
3232
// If multiple pods are found, implementations might have different behavior, by either returning an error or returning any element.
3333
GetRunningPodFromSelector(selector string) (*corev1.Pod, error)
34+
35+
GetPodUsingComponentName(componentName string) (*corev1.Pod, error)
3436
}

pkg/platform/mock.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/podman/interface.go

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,5 @@ type Client interface {
3838

3939
ListAllComponents() ([]api.ComponentAbstract, error)
4040

41-
GetPodUsingComponentName(componentName string) (*corev1.Pod, error)
42-
4341
Version(ctx context.Context) (SystemVersionReport, error)
4442
}

0 commit comments

Comments
 (0)