Skip to content

Commit

Permalink
Merge 0d17bc5 into 3b7b6b1
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy authored Nov 30, 2022
2 parents 3b7b6b1 + 0d17bc5 commit d3dcc04
Show file tree
Hide file tree
Showing 8 changed files with 415 additions and 275 deletions.
9 changes: 9 additions & 0 deletions pkg/dev/kubedev/kubedev.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
"github.com/redhat-developer/odo/pkg/watch"
)

const (
promptMessage = `
[Ctrl+c] - Exit and delete resources from the cluster
[p] - Manually apply local changes to the application on the cluster
`
)

type DevClient struct {
kubernetesClient kclient.ClientInterface
prefClient preference.Client
Expand Down Expand Up @@ -121,7 +128,9 @@ func (o *DevClient) Start(
Variables: options.Variables,
RandomPorts: options.RandomPorts,
WatchFiles: options.WatchFiles,
WatchCluster: true,
ErrOut: errOut,
PromptMessage: promptMessage,
}

return o.watchClient.WatchAndPush(out, watchParameters, ctx, componentStatus)
Expand Down
29 changes: 29 additions & 0 deletions pkg/dev/podmandev/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,38 @@ package podmandev

import (
"context"
"fmt"
"io"

"k8s.io/klog"
)

func (o *DevClient) CleanupResources(ctx context.Context, out io.Writer) error {
fmt.Printf("Cleaning up resources\n")

if o.deployedPod == nil {
return nil
}

err := o.podmanClient.PodStop(o.deployedPod.GetName())
if err != nil {
return err
}
err = o.podmanClient.PodRm(o.deployedPod.GetName())
if err != nil {
return err
}

for _, volume := range o.deployedPod.Spec.Volumes {
if volume.PersistentVolumeClaim == nil {
continue
}
volumeName := volume.PersistentVolumeClaim.ClaimName
klog.V(3).Infof("deleting podman volume %q", volumeName)
err = o.podmanClient.VolumeRm(volumeName)
if err != nil {
return err
}
}
return nil
}
195 changes: 47 additions & 148 deletions pkg/dev/podmandev/podmandev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@ import (
"path/filepath"
"strings"

devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/fatih/color"

"github.com/redhat-developer/odo/pkg/api"
"github.com/redhat-developer/odo/pkg/component"
"github.com/redhat-developer/odo/pkg/dev"
"github.com/redhat-developer/odo/pkg/dev/common"
"github.com/redhat-developer/odo/pkg/devfile/adapters"
"github.com/redhat-developer/odo/pkg/exec"
"github.com/redhat-developer/odo/pkg/libdevfile"
"github.com/redhat-developer/odo/pkg/log"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/podman"
"github.com/redhat-developer/odo/pkg/state"
"github.com/redhat-developer/odo/pkg/sync"
"github.com/redhat-developer/odo/pkg/watch"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog"
)

const (
PromptMessage = `
promptMessage = `
[Ctrl+c] - Exit and delete resources from podman
[p] - Manually apply local changes to the application on podman
`
)

Expand All @@ -37,6 +32,9 @@ type DevClient struct {
syncClient sync.Client
execClient exec.Client
stateClient state.Client
watchClient watch.Client

deployedPod *corev1.Pod
}

var _ dev.Client = (*DevClient)(nil)
Expand All @@ -46,12 +44,14 @@ func NewDevClient(
syncClient sync.Client,
execClient exec.Client,
stateClient state.Client,
watchClient watch.Client,
) *DevClient {
return &DevClient{
podmanClient: podmanClient,
syncClient: syncClient,
execClient: execClient,
stateClient: stateClient,
watchClient: watchClient,
}
}

Expand All @@ -67,152 +67,38 @@ func (o *DevClient) Start(
devfileObj = odocontext.GetDevfileObj(ctx)
devfilePath = odocontext.GetDevfilePath(ctx)
path = filepath.Dir(devfilePath)
)

pod, fwPorts, err := o.deployPod(ctx, options)
if err != nil {
return err
}

for _, fwPort := range fwPorts {
s := fmt.Sprintf("Forwarding from %s:%d -> %d", fwPort.LocalAddress, fwPort.LocalPort, fwPort.ContainerPort)
fmt.Fprintf(out, " - %s", log.SboldColor(color.FgGreen, s))
}
err = o.stateClient.SetForwardedPorts(fwPorts)
if err != nil {
return err
}

execRequired, err := o.syncFiles(ctx, options, pod, path)
if err != nil {
return err
}

// PostStart events from the devfile will only be executed when the component
// didn't previously exist
if libdevfile.HasPostStartEvents(*devfileObj) {
execHandler := component.NewExecHandler(
o.podmanClient,
o.execClient,
appName,
componentName,
pod.Name,
"",
false, /* TODO */
)
err = libdevfile.ExecPostStartEvents(*devfileObj, execHandler)
if err != nil {
return err
}
}

if execRequired {
doExecuteBuildCommand := func() error {
execHandler := component.NewExecHandler(
o.podmanClient,
o.execClient,
appName,
componentName,
pod.Name,
"Building your application in container",
false, /* TODO */
)
return libdevfile.Build(*devfileObj, options.BuildCommand, execHandler)
}
err = doExecuteBuildCommand()
if err != nil {
return err
}

cmdKind := devfilev1.RunCommandGroupKind
cmdName := options.RunCommand
if options.Debug {
cmdKind = devfilev1.DebugCommandGroupKind
cmdName = options.DebugCommand
}
cmdHandler := commandHandler{
execClient: o.execClient,
platformClient: o.podmanClient,
componentExists: false, // TODO
podName: pod.Name,
appName: appName,
componentName: componentName,
}
err = libdevfile.ExecuteCommandByNameAndKind(*devfileObj, cmdName, cmdKind, &cmdHandler, false)
if err != nil {
return err
}
}

fmt.Fprintf(
out,
" %s%s",
log.Sbold("Keyboard Commands:"),
PromptMessage,
componentStatus = watch.ComponentStatus{}
)

<-ctx.Done()

fmt.Printf("Cleaning up resources\n")
err = o.podmanClient.PodStop(pod.GetName())
if err != nil {
return err
}
err = o.podmanClient.PodRm(pod.GetName())
err := o.reconcile(ctx, out, errOut, options, &componentStatus)
if err != nil {
return err
}

for _, volume := range pod.Spec.Volumes {
if volume.PersistentVolumeClaim == nil {
continue
}
volumeName := volume.PersistentVolumeClaim.ClaimName
klog.V(3).Infof("deleting podman volume %q", volumeName)
err = o.podmanClient.VolumeRm(volumeName)
if err != nil {
return err
}
}

return nil
}

// deployPod deploys the component as a Pod in podman
func (o *DevClient) deployPod(ctx context.Context, options dev.StartOptions) (*corev1.Pod, []api.ForwardedPort, error) {
var (
appName = odocontext.GetApplication(ctx)
componentName = odocontext.GetComponentName(ctx)
devfileObj = odocontext.GetDevfileObj(ctx)
)

spinner := log.Spinner("Deploying pod")
defer spinner.End(false)

pod, fwPorts, err := createPodFromComponent(
*devfileObj,
componentName,
appName,
options.BuildCommand,
options.RunCommand,
"",
)
if err != nil {
return nil, nil, err
}

err = o.checkVolumesFree(pod)
if err != nil {
return nil, nil, err
}

err = o.podmanClient.PlayKube(pod)
if err != nil {
return nil, nil, err
}

spinner.End(true)
return pod, fwPorts, nil
watch.PrintInfoMessage(out, path, options.WatchFiles, promptMessage)

watchParameters := watch.WatchParameters{
DevfilePath: devfilePath,
Path: path,
ComponentName: componentName,
ApplicationName: appName,
InitialDevfileObj: *devfileObj,
DevfileWatchHandler: o.watchHandler,
FileIgnores: options.IgnorePaths,
Debug: options.Debug,
DevfileBuildCmd: options.BuildCommand,
DevfileRunCmd: options.RunCommand,
Variables: options.Variables,
RandomPorts: options.RandomPorts,
WatchFiles: options.WatchFiles,
WatchCluster: false,
Out: out,
ErrOut: errOut,
PromptMessage: promptMessage,
}

return o.watchClient.WatchAndPush(out, watchParameters, ctx, componentStatus)
}

// syncFiles syncs the local source files in path into the pod's source volume
Expand Down Expand Up @@ -269,3 +155,16 @@ func (o *DevClient) checkVolumesFree(pod *corev1.Pod) error {
}
return nil
}

func (o *DevClient) watchHandler(ctx context.Context, pushParams adapters.PushParameters, watchParams watch.WatchParameters, componentStatus *watch.ComponentStatus) error {
startOptions := dev.StartOptions{
IgnorePaths: watchParams.FileIgnores,
Debug: watchParams.Debug,
BuildCommand: watchParams.DevfileBuildCmd,
RunCommand: watchParams.DevfileRunCmd,
RandomPorts: watchParams.RandomPorts,
WatchFiles: watchParams.WatchFiles,
Variables: watchParams.Variables,
}
return o.reconcile(ctx, watchParams.Out, watchParams.ErrOut, startOptions, componentStatus)
}
Loading

0 comments on commit d3dcc04

Please sign in to comment.