Skip to content

Commit

Permalink
Replace component.execHandler by common.runHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed May 22, 2023
1 parent 4fcf81d commit 04018b4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
16 changes: 14 additions & 2 deletions pkg/component/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog"

"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/dev/common"
"github.com/redhat-developer/odo/pkg/exec"
"github.com/redhat-developer/odo/pkg/kclient"
odolabels "github.com/redhat-developer/odo/pkg/labels"
Expand Down Expand Up @@ -216,7 +216,19 @@ func (do *DeleteComponentClient) ExecutePreStopEvents(ctx context.Context, devfi

klog.V(4).Infof("Executing %q event commands for component %q", libdevfile.PreStop, componentName)
// ignore the failures if any; delete should not fail because preStop events failed to execute
err = libdevfile.ExecPreStopEvents(ctx, devfileObj, component.NewExecHandler(do.kubeClient, do.execClient, appName, componentName, pod.Name, "Executing pre-stop command in container", false, false))
handler := common.NewRunHandler(
do.kubeClient,
do.execClient,
appName,
componentName,
pod.Name,
false,
"Executing pre-stop command in container",

// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PreStop events
nil, nil, nil, parser.DevfileObj{}, "",
)
err = libdevfile.ExecPreStopEvents(ctx, devfileObj, handler)
if err != nil {
klog.V(4).Infof("Failed to execute %q event commands for component %q, cause: %v", libdevfile.PreStop, componentName, err.Error())
}
Expand Down
38 changes: 36 additions & 2 deletions pkg/dev/common/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,45 @@ type RunHandler struct {
PodName string

Ctx context.Context
Msg string
}

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

func NewRunHandler(
platformClient platform.Client,
execClient exec.Client,
appName string,
componentName string,
podName string,
componentExists bool,
msg string,

// For building images
fs filesystem.Filesystem,
imageBackend image.Backend,
ctx context.Context,

// For apply Kubernetes / Openshift
devfile parser.DevfileObj,
path string,

) *RunHandler {
return &RunHandler{
FS: fs,
ExecClient: execClient,
AppName: appName,
ComponentName: componentName,
Devfile: devfile,
PlatformClient: platformClient,
ImageBackend: imageBackend,
Path: path,
ComponentExists: componentExists,
PodName: podName,
Ctx: ctx,
}
}

func (a *RunHandler) ApplyImage(img devfilev1.Component) error {
return image.BuildPushSpecificImage(a.Ctx, a.ImageBackend, a.FS, img, envcontext.GetEnvConfig(a.Ctx).PushImages)
}
Expand Down Expand Up @@ -68,8 +103,7 @@ func (a *RunHandler) ExecuteNonTerminatingCommand(ctx context.Context, command d
}

func (a *RunHandler) ExecuteTerminatingCommand(ctx context.Context, command devfilev1.Command) error {
panic("handler: ExecuteRunCommand in ExecuteTerminatingCommand")
//return component.ExecuteRunCommand(ctx, a.ExecClient, a.PlatformClient, command, a.ComponentExists, a.PodName, a.AppName, a.ComponentName)
return component.ExecuteTerminatingCommand(ctx, a.ExecClient, a.PlatformClient, command, a.ComponentExists, a.PodName, a.AppName, a.ComponentName, a.Msg, false)
}

// IsRemoteProcessForCommandRunning returns true if the command is running
Expand Down
30 changes: 26 additions & 4 deletions pkg/dev/kubedev/innerloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"time"

devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"

"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/dev/common"
"github.com/redhat-developer/odo/pkg/devfile/image"
"github.com/redhat-developer/odo/pkg/libdevfile"
Expand Down Expand Up @@ -89,8 +89,20 @@ func (o *DevClient) innerloop(ctx context.Context, parameters common.PushParamet

// PostStart events from the devfile will only be executed when the component
// didn't previously exist
handler := common.NewRunHandler(
o.kubernetesClient,
o.execClient,
appName,
componentName,
pod.Name,
false,
"Executing post-start command in container",

// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PostStart commands
nil, nil, nil, parser.DevfileObj{}, "",
)
if !componentStatus.PostStartEventsDone && libdevfile.HasPostStartEvents(parameters.Devfile) {
err = libdevfile.ExecPostStartEvents(ctx, parameters.Devfile, component.NewExecHandler(o.kubernetesClient, o.execClient, appName, componentName, pod.Name, "Executing post-start command in container", parameters.Show, false))
err = libdevfile.ExecPostStartEvents(ctx, parameters.Devfile, handler)
if err != nil {
return err
}
Expand Down Expand Up @@ -145,8 +157,18 @@ func (o *DevClient) innerloop(ctx context.Context, parameters common.PushParamet
// Invoke the build command once (before calling libdevfile.ExecuteCommandByNameAndKind), as, if cmd is a composite command,
// the handler we pass will be called for each command in that composite command.
doExecuteBuildCommand := func() error {
execHandler := component.NewExecHandler(o.kubernetesClient, o.execClient, appName, componentName, pod.Name,
"Building your application in container", parameters.Show, running)
execHandler := common.NewRunHandler(
o.kubernetesClient,
o.execClient,
appName,
componentName,
pod.Name,
running,
"Building your application in container",

// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PostStart commands
nil, nil, nil, parser.DevfileObj{}, "",
)
return libdevfile.Build(ctx, parameters.Devfile, parameters.StartOptions.BuildCommand, execHandler)
}
if err = doExecuteBuildCommand(); err != nil {
Expand Down
17 changes: 10 additions & 7 deletions pkg/dev/podmandev/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/fatih/color"

"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/component"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/dev"
"github.com/redhat-developer/odo/pkg/dev/common"
Expand Down Expand Up @@ -65,15 +64,17 @@ func (o *DevClient) reconcile(
// PostStart events from the devfile will only be executed when the component
// didn't previously exist
if !componentStatus.PostStartEventsDone && libdevfile.HasPostStartEvents(devfileObj) {
execHandler := component.NewExecHandler(
execHandler := common.NewRunHandler(
o.podmanClient,
o.execClient,
appName,
componentName,
pod.Name,
"Executing post-start command in container",
false, /* TODO */
false,
"Executing post-start command in container",

// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PostStart commands
nil, nil, nil, parser.DevfileObj{}, "",
)
err = libdevfile.ExecPostStartEvents(ctx, devfileObj, execHandler)
if err != nil {
Expand All @@ -84,15 +85,17 @@ func (o *DevClient) reconcile(

if execRequired {
doExecuteBuildCommand := func() error {
execHandler := component.NewExecHandler(
execHandler := common.NewRunHandler(
o.podmanClient,
o.execClient,
appName,
componentName,
pod.Name,
false,
"Building your application in container",
false, /* TODO */
componentStatus.RunExecuted,

// TODO(feloy) set these values when we want to support Apply Image/Kubernetes/OpenShift commands for PreStop events
nil, nil, nil, parser.DevfileObj{}, "",
)
return libdevfile.Build(ctx, devfileObj, options.BuildCommand, execHandler)
}
Expand Down

0 comments on commit 04018b4

Please sign in to comment.