Skip to content

Commit 77841ae

Browse files
authored
Merge 2ada482 into b6c9c88
2 parents b6c9c88 + 2ada482 commit 77841ae

File tree

22 files changed

+574
-34
lines changed

22 files changed

+574
-34
lines changed

cmd/odo/help_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Examples:
3434
init Init bootstraps a new project
3535
logs Show logs of all containers of the component
3636
registry List all components from the Devfile registry
37+
run Run a specific command in the Dev mode
3738
3839
`
3940

pkg/component/component.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ func Log(platformClient platform.Client, componentName string, appName string, f
9494

9595
pod, err := platformClient.GetRunningPodFromSelector(odolabels.GetSelector(componentName, appName, odolabels.ComponentDevMode, false))
9696
if err != nil {
97-
return nil, fmt.Errorf("the component %s doesn't exist on the cluster", componentName)
98-
}
99-
100-
if pod.Status.Phase != corev1.PodRunning {
101-
return nil, fmt.Errorf("unable to show logs, component is not in running state. current status=%v", pod.Status.Phase)
97+
return nil, fmt.Errorf("a running component %s doesn't exist on the cluster: %w", componentName, err)
10298
}
10399

104100
containerName := command.Exec.Component

pkg/component/delete/delete.go

-6
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,6 @@ func (do *DeleteComponentClient) ExecutePreStopEvents(ctx context.Context, devfi
212212
return fmt.Errorf("unable to determine if component %s exists; cause: %v", componentName, err.Error())
213213
}
214214

215-
// do not fail Delete operation if if the pod is not running or if the event execution fails
216-
if pod.Status.Phase != corev1.PodRunning {
217-
klog.V(4).Infof("unable to execute preStop events, pod for component %q is not running", componentName)
218-
return nil
219-
}
220-
221215
klog.V(4).Infof("Executing %q event commands for component %q", libdevfile.PreStop, componentName)
222216
// ignore the failures if any; delete should not fail because preStop events failed to execute
223217
handler := component.NewRunHandler(

pkg/component/delete/delete_test.go

-19
Original file line numberDiff line numberDiff line change
@@ -700,25 +700,6 @@ func TestDeleteComponentClient_ExecutePreStopEvents(t *testing.T) {
700700
},
701701
wantErr: false,
702702
},
703-
{
704-
name: "did not execute PreStopEvents because the pod is not in the running state",
705-
fields: fields{
706-
kubeClient: func(ctrl *gomock.Controller) kclient.ClientInterface {
707-
client := kclient.NewMockClientInterface(ctrl)
708-
709-
selector := odolabels.GetSelector(componentName, "app", odolabels.ComponentDevMode, false)
710-
pod := odoTestingUtil.CreateFakePod(componentName, "mypod", "runtime")
711-
pod.Status.Phase = corev1.PodFailed
712-
client.EXPECT().GetRunningPodFromSelector(selector).Return(pod, nil)
713-
return client
714-
},
715-
},
716-
args: args{
717-
devfileObj: devfileObjWithPreStopEvents,
718-
appName: appName,
719-
},
720-
wantErr: false,
721-
},
722703
{
723704
name: "failed to execute PreStopEvents because it failed to execute the command inside the container, but no error returned",
724705
fields: fields{

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. Please check the command 'odo dev' is running", 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/interface.go

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ type Client interface {
4848
options StartOptions,
4949
) error
5050

51+
Run(
52+
ctx context.Context,
53+
commandName string,
54+
) error
55+
5156
// CleanupResources deletes the component created using the context's devfile and writes any outputs to out
5257
CleanupResources(ctx context.Context, out io.Writer) error
5358
}

pkg/dev/kubedev/run.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package kubedev
2+
3+
import (
4+
"context"
5+
6+
"github.com/redhat-developer/odo/pkg/dev/common"
7+
"k8s.io/klog"
8+
)
9+
10+
func (o *DevClient) Run(
11+
ctx context.Context,
12+
commandName string,
13+
) error {
14+
klog.V(4).Infof("running command %q on cluster", commandName)
15+
return common.Run(
16+
ctx,
17+
commandName,
18+
o.kubernetesClient,
19+
o.execClient,
20+
o.configAutomountClient,
21+
o.filesystem,
22+
)
23+
}

pkg/dev/mock.go

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

pkg/dev/podmandev/run.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package podmandev
2+
3+
import (
4+
"context"
5+
6+
"github.com/redhat-developer/odo/pkg/dev/common"
7+
"k8s.io/klog"
8+
)
9+
10+
func (o *DevClient) Run(
11+
ctx context.Context,
12+
commandName string,
13+
) error {
14+
klog.V(4).Infof("running command %q on podman", commandName)
15+
return common.Run(
16+
ctx,
17+
commandName,
18+
o.podmanClient,
19+
o.execClient,
20+
nil, // TODO(feloy) set when running on new container is supported on podman
21+
o.fs,
22+
)
23+
}

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/libdevfile/errors.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func (e NoCommandFoundError) Error() string {
2222
if e.name == "" {
2323
return fmt.Sprintf("no %s command found in devfile", e.kind)
2424
}
25+
if e.kind == "" {
26+
return fmt.Sprintf("no command named %q found in devfile", e.name)
27+
}
2528
return fmt.Sprintf("no %s command with name %q found in Devfile", e.kind, e.name)
2629
}
2730

pkg/libdevfile/libdevfile.go

+19
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ func ExecuteCommandByNameAndKind(ctx context.Context, devfileObj parser.DevfileO
6666
return executeCommand(ctx, devfileObj, cmd, handler)
6767
}
6868

69+
// ExecuteCommandByName executes the specified command cmdName in the Devfile.
70+
// If ignoreCommandNotFound is true, nothing is executed if the command is not found and no error is returned.
71+
func ExecuteCommandByName(ctx context.Context, devfileObj parser.DevfileObj, cmdName string, handler Handler, ignoreCommandNotFound bool) error {
72+
commands, err := devfileObj.Data.GetCommands(
73+
common.DevfileOptions{
74+
FilterByName: cmdName,
75+
},
76+
)
77+
if err != nil {
78+
return err
79+
}
80+
if len(commands) != 1 {
81+
return NewNoCommandFoundError("", cmdName)
82+
}
83+
84+
cmd := commands[0]
85+
return executeCommand(ctx, devfileObj, cmd, handler)
86+
}
87+
6988
// executeCommand executes a specific command of a devfile using handler as backend
7089
func executeCommand(ctx context.Context, devfileObj parser.DevfileObj, command v1alpha2.Command, handler Handler) error {
7190
cmd, err := newCommand(devfileObj, command)

pkg/odo/cli/cli.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"unicode"
1111

1212
"github.com/redhat-developer/odo/pkg/odo/cli/logs"
13+
"github.com/redhat-developer/odo/pkg/odo/cli/run"
1314
"github.com/redhat-developer/odo/pkg/odo/commonflags"
1415

1516
"github.com/redhat-developer/odo/pkg/log"
@@ -195,6 +196,7 @@ func odoRootCmd(ctx context.Context, name, fullName string) *cobra.Command {
195196
set.NewCmdSet(set.RecommendedCommandName, util.GetFullName(fullName, set.RecommendedCommandName)),
196197
logs.NewCmdLogs(logs.RecommendedCommandName, util.GetFullName(fullName, logs.RecommendedCommandName)),
197198
completion.NewCmdCompletion(completion.RecommendedCommandName, util.GetFullName(fullName, completion.RecommendedCommandName)),
199+
run.NewCmdRun(run.RecommendedCommandName, util.GetFullName(fullName, run.RecommendedCommandName)),
198200
)
199201

200202
// Add all subcommands to base commands

pkg/odo/cli/errors/errors.go

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ func (o NoCommandInDevfileError) Error() string {
1919
return fmt.Sprintf("no command of kind %q found in the devfile", o.command)
2020
}
2121

22+
type NoCommandNameInDevfileError struct {
23+
name string
24+
}
25+
26+
func NewNoCommandNameInDevfileError(name string) NoCommandNameInDevfileError {
27+
return NoCommandNameInDevfileError{
28+
name: name,
29+
}
30+
}
31+
32+
func (o NoCommandNameInDevfileError) Error() string {
33+
return fmt.Sprintf("no command named %q found in the devfile", o.name)
34+
}
35+
2236
type Warning struct {
2337
msg string
2438
err error

0 commit comments

Comments
 (0)