Skip to content

Commit

Permalink
Merge pull request #1562 from dgageot/fsnotifier
Browse files Browse the repository at this point in the history
Reintroduce the fsNotify trigger
  • Loading branch information
nkubala authored Jan 31, 2019
2 parents b62bfbb + c58ccb9 commit f0f28e3
Show file tree
Hide file tree
Showing 41 changed files with 4,718 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@
[[constraint]]
name = "github.com/bmatcuk/doublestar"
revision = "v1.1.1"

[[constraint]]
name = "github.com/rjeczalik/notify"
version = "0.9.2"
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ GO_LDFLAGS += -X $(VERSION_PACKAGE).gitTreeState=$(if $(shell git status --porce
GO_LDFLAGS +="

GO_FILES := $(shell find . -type f -name '*.go' -not -path "./vendor/*")
GO_BUILD_TAGS := "kqueue"

DOCSY_COMMIT:=a7141a2eac26cb598b707cab87d224f9105c315d

$(BUILD_DIR)/$(PROJECT): $(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH)
cp $(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH) $@

$(BUILD_DIR)/$(PROJECT)-%-$(GOARCH): $(GO_FILES) $(BUILD_DIR)
GOOS=$* GOARCH=$(GOARCH) CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -gcflags $(GO_GCFLAGS) -asmflags $(GO_ASMFLAGS) -o $@ $(BUILD_PACKAGE)
GOOS=$* GOARCH=$(GOARCH) CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -gcflags $(GO_GCFLAGS) -asmflags $(GO_ASMFLAGS) -tags $(GO_BUILD_TAGS) -o $@ $(BUILD_PACKAGE)

%.sha256: %
shasum -a 256 $< > $@
Expand All @@ -81,7 +82,7 @@ test:

.PHONY: install
install: $(GO_FILES) $(BUILD_DIR)
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go install -ldflags $(GO_LDFLAGS) -gcflags $(GO_GCFLAGS) -asmflags $(GO_ASMFLAGS) $(BUILD_PACKAGE)
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go install -ldflags $(GO_LDFLAGS) -gcflags $(GO_GCFLAGS) -asmflags $(GO_ASMFLAGS) -tags $(GO_BUILD_TAGS) $(BUILD_PACKAGE)

.PHONY: integration
integration: install $(BUILD_DIR)/$(PROJECT)
Expand Down
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewCmdDev(out io.Writer) *cobra.Command {
}
AddRunDevFlags(cmd)
cmd.Flags().BoolVar(&opts.TailDev, "tail", true, "Stream logs from deployed objects")
cmd.Flags().StringVar(&opts.Trigger, "trigger", "polling", "How are changes detected? (polling or manual)")
cmd.Flags().StringVar(&opts.Trigger, "trigger", "polling", "How are changes detected? (polling, manual or notify)")
cmd.Flags().BoolVar(&opts.Cleanup, "cleanup", true, "Delete deployments after dev mode is interrupted")
cmd.Flags().StringArrayVarP(&opts.Watch, "watch-image", "w", nil, "Choose which artifacts to watch. Artifacts with image names that contain the expression will be watched only. Default is to watch sources for all artifacts")
cmd.Flags().IntVarP(&opts.WatchPollInterval, "watch-poll-interval", "i", 1000, "Interval (in ms) between two checks for file changes")
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/docs/references/cli/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ Flags:
--skip-tests Whether to skip the tests after building
--tail Stream logs from deployed objects (default true)
--toot Emit a terminal beep after the deploy is complete
--trigger string How are changes detected? (polling or manual) (default "polling")
--trigger string How are changes detected? (polling, manual or notify) (default "polling")
-w, --watch-image stringArray Choose which artifacts to watch. Artifacts with image names that contain the expression will be watched only. Default is to watch sources for all artifacts
-i, --watch-poll-interval int Interval (in ms) between two checks for file changes (default 1000)
Expand Down
53 changes: 53 additions & 0 deletions pkg/skaffold/watch/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/rjeczalik/notify"
"github.com/sirupsen/logrus"
)

Expand All @@ -45,6 +46,10 @@ func NewTrigger(opts *config.SkaffoldOptions) (Trigger, error) {
return &pollTrigger{
Interval: time.Duration(opts.WatchPollInterval) * time.Millisecond,
}, nil
case "notify":
return &fsNotifyTrigger{
Interval: time.Duration(opts.WatchPollInterval) * time.Millisecond,
}, nil
case "manual":
return &manualTrigger{}, nil
default:
Expand Down Expand Up @@ -125,3 +130,51 @@ func (t *manualTrigger) Start(ctx context.Context) (<-chan bool, error) {

return trigger, nil
}

// notifyTrigger watches for changes with fsnotify
type fsNotifyTrigger struct {
Interval time.Duration
}

// Debounce tells the watcher to not debounce rapid sequence of changes.
func (t *fsNotifyTrigger) Debounce() bool {
// This trigger has built-in debouncing.
return false
}

func (t *fsNotifyTrigger) WatchForChanges(out io.Writer) {
color.Yellow.Fprintln(out, "Watching for changes...")
}

// Start Listening for file system changes
func (t *fsNotifyTrigger) Start(ctx context.Context) (<-chan bool, error) {
// TODO(@dgageot): If file changes happen too quickly, events might be lost
c := make(chan notify.EventInfo, 100)

// Watch current directory recursively
if err := notify.Watch("./...", c, notify.All); err != nil {
return nil, err
}

trigger := make(chan bool)
go func() {
timer := time.NewTimer(1<<63 - 1) // Forever

for {
select {
case e := <-c:
logrus.Debugln("Change detected", e)

// Wait t.interval before triggering.
// This way, rapid stream of events will be grouped.
timer.Reset(t.Interval)
case <-timer.C:
trigger <- true
case <-ctx.Done():
timer.Stop()
}
}
}()

return trigger, nil
}
23 changes: 23 additions & 0 deletions pkg/skaffold/watch/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func TestNewTrigger(t *testing.T) {
Interval: time.Duration(1) * time.Millisecond,
},
},
{
description: "notify trigger",
opts: &config.SkaffoldOptions{Trigger: "notify", WatchPollInterval: 1},
expected: &fsNotifyTrigger{
Interval: time.Duration(1) * time.Millisecond,
},
},
{
description: "manual trigger",
opts: &config.SkaffoldOptions{Trigger: "manual"},
Expand Down Expand Up @@ -75,6 +82,22 @@ func TestPollTrigger_WatchForChanges(t *testing.T) {
testutil.CheckDeepEqual(t, want, got)
}

func TestNotifyTrigger_Debounce(t *testing.T) {
trigger := &fsNotifyTrigger{}
got, want := trigger.Debounce(), false
testutil.CheckDeepEqual(t, want, got)
}

func TestNotifyTrigger_WatchForChanges(t *testing.T) {
out := new(bytes.Buffer)

trigger := &fsNotifyTrigger{Interval: 10}
trigger.WatchForChanges(out)

got, want := out.String(), "Watching for changes...\n"
testutil.CheckDeepEqual(t, want, got)
}

func TestManualTrigger_Debounce(t *testing.T) {
trigger := &manualTrigger{}
got, want := trigger.Debounce(), false
Expand Down
10 changes: 10 additions & 0 deletions vendor/github.com/rjeczalik/notify/AUTHORS

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

21 changes: 21 additions & 0 deletions vendor/github.com/rjeczalik/notify/LICENSE

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

53 changes: 53 additions & 0 deletions vendor/github.com/rjeczalik/notify/debug.go

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

9 changes: 9 additions & 0 deletions vendor/github.com/rjeczalik/notify/debug_debug.go

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

9 changes: 9 additions & 0 deletions vendor/github.com/rjeczalik/notify/debug_nodebug.go

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

43 changes: 43 additions & 0 deletions vendor/github.com/rjeczalik/notify/doc.go

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

Loading

0 comments on commit f0f28e3

Please sign in to comment.