Skip to content

Commit

Permalink
Merge pull request #925 from dgageot/skaffold-dev-filter
Browse files Browse the repository at this point in the history
Allow `skaffold dev —watch image`
  • Loading branch information
dgageot authored Sep 5, 2018
2 parents 1354cb4 + 1ac9d35 commit e78bf17
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func setFlagsFromEnvVariables(commands []*cobra.Command) {

func AddDevFlags(cmd *cobra.Command) {
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.")
}

func AddRunDeployFlags(cmd *cobra.Command) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/gcb/cloud_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ func TestBuildDescription(t *testing.T) {
Timeout: "10m",
}

testutil.CheckErrorAndDeepEqual(t, false, nil, expected, *desc)
testutil.CheckDeepEqual(t, expected, *desc)
}
1 change: 1 addition & 0 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type SkaffoldOptions struct {
Profiles []string
CustomTag string
Namespace string
Watch []string
}

// Labels returns a map of labels to be applied to all deployed
Expand Down
19 changes: 19 additions & 0 deletions pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/bazel"
Expand Down Expand Up @@ -234,6 +235,10 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*v1
for i := range artifacts {
artifact := artifacts[i]

if !r.shouldWatch(artifact) {
continue
}

if err := watcher.Register(
func() ([]string, error) { return dependenciesForArtifact(artifact) },
func() { changed.Add(artifact) },
Expand Down Expand Up @@ -271,6 +276,20 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*v1
return nil, watcher.Run(ctx, PollInterval, onChange)
}

func (r *SkaffoldRunner) shouldWatch(artifact *v1alpha2.Artifact) bool {
if len(r.opts.Watch) == 0 {
return true
}

for _, watchExpression := range r.opts.Watch {
if strings.Contains(artifact.ImageName, watchExpression) {
return true
}
}

return false
}

// buildAndDeploy builds a subset of the artifacts and deploys everything.
func (r *SkaffoldRunner) buildAndDeploy(ctx context.Context, out io.Writer, artifacts []*v1alpha2.Artifact, images *kubernetes.ImageList) error {
firstRun := r.builds == nil
Expand Down
51 changes: 51 additions & 0 deletions pkg/skaffold/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ func TestBuildAndDeployAllArtifacts(t *testing.T) {
runner := &SkaffoldRunner{
Builder: builder,
Deployer: deployer,
opts: &config.SkaffoldOptions{},
}

ctx := context.Background()
Expand Down Expand Up @@ -390,3 +391,53 @@ func TestBuildAndDeployAllArtifacts(t *testing.T) {
t.Errorf("Expected 2 artifacts to be deployed. Got %d", len(deployer.deployed))
}
}

func TestShouldWatch(t *testing.T) {
var tests = []struct {
description string
watch []string
expectedMatch bool
}{
{
description: "match all",
watch: nil,
expectedMatch: true,
},
{
description: "match full name",
watch: []string{"domain/image"},
expectedMatch: true,
},
{
description: "match partial name",
watch: []string{"image"},
expectedMatch: true,
},
{
description: "match any",
watch: []string{"other", "image"},
expectedMatch: true,
},
{
description: "no match",
watch: []string{"other"},
expectedMatch: false,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
runner := &SkaffoldRunner{
opts: &config.SkaffoldOptions{
Watch: test.watch,
},
}

match := runner.shouldWatch(&v1alpha2.Artifact{
ImageName: "domain/image",
})

testutil.CheckDeepEqual(t, test.expectedMatch, match)
})
}
}
2 changes: 1 addition & 1 deletion pkg/skaffold/watch/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestHasChanged(t *testing.T) {

changed := hasChanged(prev, curr)

testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedChanged, changed)
testutil.CheckDeepEqual(t, test.expectedChanged, changed)
})
}
}
8 changes: 8 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ type FakeReaderCloser struct {
func (f FakeReaderCloser) Close() error { return nil }
func (f FakeReaderCloser) Read([]byte) (int, error) { return 0, f.Err }

func CheckDeepEqual(t *testing.T, expected, actual interface{}) {
t.Helper()
if diff := cmp.Diff(actual, expected); diff != "" {
t.Errorf("%T differ (-got, +want): %s", expected, diff)
return
}
}

func CheckErrorAndDeepEqual(t *testing.T, shouldErr bool, err error, expected, actual interface{}) {
t.Helper()
if err := checkErr(shouldErr, err); err != nil {
Expand Down

0 comments on commit e78bf17

Please sign in to comment.