Skip to content

Commit

Permalink
Adds odo dev --no-watch (redhat-developer#5675)
Browse files Browse the repository at this point in the history
* Adds `odo dev --no-watch`

* Remove select for a single case

More details on - dominikh/go-tools#503 (comment)

* Update mocks

* Update tests/integration/devfile/cmd_dev_test.go

Co-authored-by: Armel Soro <[email protected]>

* Rename CleanupFunc to Cleanup

* Add usage doc for --no-watch

* Remove infinite for loop

Co-authored-by: Armel Soro <[email protected]>
  • Loading branch information
2 people authored and cdrage committed Aug 31, 2022
1 parent 935fdd8 commit ccaee03
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
19 changes: 15 additions & 4 deletions pkg/odo/cli/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/devfile/library/pkg/devfile/parser"
parsercommon "github.com/devfile/library/pkg/devfile/parser/data/v2/common"
"github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/templates"
ktemplates "k8s.io/kubectl/pkg/util/templates"

"github.com/redhat-developer/odo/pkg/component"
ododevfile "github.com/redhat-developer/odo/pkg/devfile"
Expand Down Expand Up @@ -62,6 +62,7 @@ type DevOptions struct {

// Flags
randomPorts bool
noWatchFlag bool
}

type Handler struct{}
Expand All @@ -73,9 +74,12 @@ func NewDevOptions() *DevOptions {
}
}

var devExample = templates.Examples(`
var devExample = ktemplates.Examples(`
# Deploy component to the development cluster
%[1]s
# Deploy component to the development cluster without automatically syncing the code upon any file changes
%[1]s --no-watch
`)

func (o *DevOptions) SetClientset(clientset *clientset.Clientset) {
Expand Down Expand Up @@ -230,8 +234,14 @@ func (o *DevOptions) Run(ctx context.Context) error {
scontext.SetProjectType(ctx, devFileObj.Data.GetMetadata().ProjectType)
scontext.SetDevfileName(ctx, devFileObj.GetMetadataName())

d := Handler{}
err = o.clientset.DevClient.Watch(devFileObj, path, o.ignorePaths, o.out, &d, o.ctx)
if o.noWatchFlag {
log.Finfof(log.GetStdout(), "\n"+watch.CtrlCMessage)
<-o.ctx.Done()
err = o.clientset.WatchClient.Cleanup(devFileObj, log.GetStdout())
} else {
d := Handler{}
err = o.clientset.DevClient.Watch(devFileObj, path, o.ignorePaths, o.out, &d, o.ctx)
}

return err
}
Expand Down Expand Up @@ -293,6 +303,7 @@ It forwards endpoints with exposure values 'public' or 'internal' to a port on l
},
}
devCmd.Flags().BoolVarP(&o.randomPorts, "random-ports", "f", false, "Assign random ports to redirected ports")
devCmd.Flags().BoolVar(&o.noWatchFlag, "no-watch", false, "Do not watch for file changes")

clientset.Add(devCmd, clientset.DEV, clientset.INIT, clientset.KUBERNETES)
// Add a defined annotation in order to appear in the help menu
Expand Down
4 changes: 4 additions & 0 deletions pkg/watch/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package watch
import (
"context"
"io"

"github.com/devfile/library/pkg/devfile/parser"
)

type Client interface {
// WatchAndPush watches the component under the context directory and triggers Push if there are any changes
// It also listens on ctx's Done channel to trigger cleanup when indicated to do so
WatchAndPush(out io.Writer, parameters WatchParameters, ctx context.Context) error
// Cleanup deletes the component created using the devfileObj and writes any outputs to out
Cleanup(devfileObj parser.DevfileObj, out io.Writer) error
}
15 changes: 15 additions & 0 deletions pkg/watch/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
const (
// PushErrorString is the string that is printed when an error occurs during watch's Push operation
PushErrorString = "Error occurred on Push"
CtrlCMessage = "Press Ctrl+c to exit `odo dev` and delete resources from the cluster"
)

type WatchClient struct {
Expand Down Expand Up @@ -187,7 +188,7 @@ func (o *WatchClient) WatchAndPush(out io.Writer, parameters WatchParameters, ct

printInfoMessage(out, parameters.Path)

return eventWatcher(ctx, watcher, parameters, out, evaluateFileChanges, processEvents, o.cleanupFunc)
return eventWatcher(ctx, watcher, parameters, out, evaluateFileChanges, processEvents, o.Cleanup)
}

// eventWatcher loops till the context's Done channel indicates it to stop looping, at which point it performs cleanup.
Expand Down Expand Up @@ -330,7 +331,7 @@ func processEvents(changedFiles, deletedPaths []string, parameters WatchParamete
}
}

func (o *WatchClient) cleanupFunc(devfileObj parser.DevfileObj, out io.Writer) error {
func (o *WatchClient) Cleanup(devfileObj parser.DevfileObj, out io.Writer) error {
isInnerLoopDeployed, resources, err := o.deleteClient.ListResourcesToDeleteFromDevfile(devfileObj, "app")
if err != nil {
fmt.Fprintf(out, "failed to delete inner loop resources: %v", err)
Expand Down Expand Up @@ -393,6 +394,5 @@ func removeDuplicates(input []string) []string {
}

func printInfoMessage(out io.Writer, path string) {
log.Finfof(out, "\nWatching for changes in the current directory %s\n"+
"Press Ctrl+c to exit `odo dev` and delete resources from the cluster\n", path)
log.Finfof(out, "\nWatching for changes in the current directory %s\n"+CtrlCMessage+"\n", path)
}
2 changes: 1 addition & 1 deletion tests/helper/helper_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func StartDevMode(opts ...string) (DevSession, []byte, []byte, []string, error)
args := []string{"dev", "--random-ports"}
args = append(args, opts...)
session := CmdRunner("odo", args...)
WaitForOutputToContain("Watching for changes in the current directory", 360, 10, session)
WaitForOutputToContain("Press Ctrl+c to exit `odo dev` and delete resources from the cluster", 360, 10, session)
result := DevSession{
session: session,
}
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/devfile/cmd_dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,31 @@ var _ = Describe("odo dev command tests", func() {
})
})
})

When("odo is executed with --no-watch flag", func() {
BeforeEach(func() {
devSession, _, _, _, err := helper.StartDevMode("--no-watch")
Expect(err).ToNot(HaveOccurred())
defer func() {
devSession.Kill()
devSession.WaitEnd()
}()
// An ENV file should have been created indicating current namespace
Expect(helper.VerifyFileExists(".odo/env/env.yaml")).To(BeTrue())
helper.FileShouldContainSubstring(".odo/env/env.yaml", "Project: "+commonVar.Project)
})

When("a file in component directory is modified", func() {
It("should not trigger a push", func() {
helper.ReplaceString(filepath.Join(commonVar.Context, "server.js"), "App started", "App is super started")
podName := commonVar.CliRunner.GetRunningPodNameByComponent(cmpName, commonVar.Project)
execResult := commonVar.CliRunner.Exec(podName, commonVar.Project, "cat", "/projects/server.js")
Expect(execResult).To(ContainSubstring("App started"))
Expect(execResult).ToNot(ContainSubstring("App is super started"))

})
})
})
})

Context("port-forwarding for the component", func() {
Expand Down

0 comments on commit ccaee03

Please sign in to comment.