Skip to content

Commit

Permalink
Handle hotReloadCapable in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed May 3, 2023
1 parent b5ac956 commit 21e8770
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pkg/component/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ 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))
err = libdevfile.ExecPreStopEvents(ctx, devfileObj, component.NewExecHandler(do.kubeClient, do.execClient, appName, componentName, pod.Name, "Executing pre-stop command in container", false, false))
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
37 changes: 22 additions & 15 deletions pkg/component/exec_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"k8s.io/utils/pointer"

"github.com/redhat-developer/odo/pkg/exec"
"github.com/redhat-developer/odo/pkg/libdevfile"
Expand All @@ -16,28 +17,30 @@ import (
)

type execHandler struct {
platformClient platform.Client
execClient exec.Client
appName string
componentName string
podName string
msg string
show bool
platformClient platform.Client
execClient exec.Client
appName string
componentName string
podName string
msg string
show bool
componentExists bool
}

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

const ShellExecutable string = "/bin/sh"

func NewExecHandler(platformClient platform.Client, execClient exec.Client, appName, cmpName, podName, msg string, show bool) *execHandler {
func NewExecHandler(platformClient platform.Client, execClient exec.Client, appName, cmpName, podName, msg string, show bool, componentExists bool) *execHandler {
return &execHandler{
platformClient: platformClient,
execClient: execClient,
appName: appName,
componentName: cmpName,
podName: podName,
msg: msg,
show: show,
platformClient: platformClient,
execClient: execClient,
appName: appName,
componentName: cmpName,
podName: podName,
msg: msg,
show: show,
componentExists: componentExists,
}
}

Expand All @@ -54,6 +57,10 @@ func (o *execHandler) ApplyOpenShift(openshift v1alpha2.Component) error {
}

func (o *execHandler) Execute(ctx context.Context, command v1alpha2.Command) error {
if o.componentExists && command.Exec != nil && pointer.BoolDeref(command.Exec.HotReloadCapable, false) {
return nil
}

msg := o.msg
if msg == "" {
msg = fmt.Sprintf("Executing %s command on container %q", command.Id, command.Exec.Component)
Expand Down
26 changes: 6 additions & 20 deletions pkg/dev/kubedev/innerloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/port"
"github.com/redhat-developer/odo/pkg/sync"
"github.com/redhat-developer/odo/pkg/util"
"github.com/redhat-developer/odo/pkg/watch"

"k8s.io/klog"
Expand Down Expand Up @@ -90,7 +89,7 @@ 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
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))
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))
if err != nil {
return err
}
Expand All @@ -102,11 +101,6 @@ func (o *DevClient) innerloop(ctx context.Context, parameters common.PushParamet
return err
}

buildCmd, err := libdevfile.ValidateAndGetCommand(parameters.Devfile, parameters.StartOptions.BuildCommand, devfilev1.BuildCommandGroupKind)
if err != nil {
return err
}

commandType, err := parsercommon.GetCommandType(cmd)
if err != nil {
return err
Expand Down Expand Up @@ -149,22 +143,14 @@ func (o *DevClient) innerloop(ctx context.Context, parameters common.PushParamet
// 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)
"Building your application in container", parameters.Show, running)
return libdevfile.Build(ctx, parameters.Devfile, parameters.StartOptions.BuildCommand, execHandler)
}
if running {
if buildCmd.Exec == nil || !util.SafeGetBool(buildCmd.Exec.HotReloadCapable) {
if err = doExecuteBuildCommand(); err != nil {
componentStatus.SetState(watch.StateReady)
return err
}
}
} else {
if err = doExecuteBuildCommand(); err != nil {
componentStatus.SetState(watch.StateReady)
return err
}
if err = doExecuteBuildCommand(); err != nil {
componentStatus.SetState(watch.StateReady)
return err
}

err = libdevfile.ExecuteCommandByNameAndKind(ctx, parameters.Devfile, cmdName, cmdKind, &cmdHandler, false)
if err != nil {
return err
Expand Down
13 changes: 3 additions & 10 deletions pkg/dev/podmandev/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/redhat-developer/odo/pkg/log"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/port"
"github.com/redhat-developer/odo/pkg/util"
"github.com/redhat-developer/odo/pkg/watch"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -74,6 +73,7 @@ func (o *DevClient) reconcile(
pod.Name,
"Executing post-start command in container",
false, /* TODO */
false,
)
err = libdevfile.ExecPostStartEvents(ctx, devfileObj, execHandler)
if err != nil {
Expand All @@ -92,23 +92,16 @@ func (o *DevClient) reconcile(
pod.Name,
"Building your application in container",
false, /* TODO */
componentStatus.RunExecuted,
)
return libdevfile.Build(ctx, devfileObj, options.BuildCommand, execHandler)
}

var buildCmd devfilev1.Command
buildCmd, err = libdevfile.ValidateAndGetCommand(parameters.Devfile, parameters.StartOptions.BuildCommand, devfilev1.BuildCommandGroupKind)
err = doExecuteBuildCommand()
if err != nil {
return err
}

if !componentStatus.RunExecuted || !util.SafeGetBool(buildCmd.Exec.HotReloadCapable) {
err = doExecuteBuildCommand()
if err != nil {
return err
}
}

cmdKind := devfilev1.RunCommandGroupKind
cmdName := options.RunCommand
if options.Debug {
Expand Down

0 comments on commit 21e8770

Please sign in to comment.