Skip to content

Commit 99c06f6

Browse files
committed
feat(ko): Implement file watch dependencies
Add support for the `dependencies` section in the ko builder config. Default behavior is based on the Buildpacks builder. **Tracking:** #6041 **Related:** #6605
1 parent 509719e commit 99c06f6

File tree

4 files changed

+172
-6
lines changed

4 files changed

+172
-6
lines changed

docs/design_proposals/ko-builder.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,13 @@ Adding the ko builder requires making config changes to the Skaffold schema.
350350
```go
351351
// KoDependencies is used to specify dependencies for an artifact built by ko.
352352
type KoDependencies struct {
353-
// Paths should be set to the file dependencies for this artifact, so that the skaffold file watcher knows when to rebuild and perform file synchronization.
354-
// Defaults to {"go.mod", "**.go"}
355-
Paths []string `yaml:"paths,omitempty" yamltags:"oneOf=dependency"`
353+
// Paths should be set to the file dependencies for this artifact, so that the skaffold file watcher knows when to rebuild and perform file synchronization.
354+
// Defaults to ["."].
355+
Paths []string `yaml:"paths,omitempty" yamltags:"oneOf=dependency"`
356356

357-
// Ignore specifies the paths that should be ignored by skaffold's file watcher.
357+
// Ignore specifies the paths that should be ignored by skaffold's file watcher.
358358
// If a file exists in both `paths` and in `ignore`, it will be ignored, and will be excluded from both rebuilds and file synchronization.
359-
Ignore []string `yaml:"ignore,omitempty"`
359+
Ignore []string `yaml:"ignore,omitempty"`
360360
}
361361
```
362362
@@ -409,7 +409,7 @@ apiVersion: skaffold/v2beta19
409409
kind: Config
410410
build:
411411
artifacts:
412-
- image: ko://github.com/GoogleContainerTools/skaffold/examples/ko-complete
412+
- image: skaffold-example-ko-comprehensive
413413
ko:
414414
asmflags: []
415415
fromImage: gcr.io/distroless/base:nonroot

pkg/skaffold/build/ko/dependencies.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2021 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ko
18+
19+
// TODO(halvards)[09/14/2021]: Replace the latestV1 import path with the
20+
// real schema import path once the contents of ./schema has been added to
21+
// the real schema in pkg/skaffold/schema/latest/v1.
22+
import (
23+
"context"
24+
25+
// latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
26+
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko/schema"
27+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/list"
28+
)
29+
30+
// GetDependencies returns a list of files to watch for changes to rebuild.
31+
// TODO(halvards)[09/17/2021]: Call this function from sourceDependenciesForArtifact() in pkg/skaffold/graph/dependencies.go
32+
func GetDependencies(ctx context.Context, workspace string, a *latestV1.KoArtifact) ([]string, error) {
33+
if a.Dependencies == nil || (a.Dependencies.Paths == nil && a.Dependencies.Ignore == nil) {
34+
a.Dependencies = defaultKoDependencies()
35+
}
36+
return list.Files(workspace, a.Dependencies.Paths, a.Dependencies.Ignore)
37+
}
38+
39+
// defaultKoDependencies behavior is to watch all files in the context directory
40+
func defaultKoDependencies() *latestV1.KoDependencies {
41+
return &latestV1.KoDependencies{
42+
Paths: []string{"."},
43+
Ignore: []string{},
44+
}
45+
}
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright 2021 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ko
18+
19+
// TODO(halvards)[09/17/2021]: Replace the latestV1 import path with the
20+
// real schema import path once the contents of ./schema has been added to
21+
// the real schema in pkg/skaffold/schema/latest/v1.
22+
import (
23+
"context"
24+
"path/filepath"
25+
"testing"
26+
27+
"github.com/google/go-cmp/cmp/cmpopts"
28+
29+
// latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
30+
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/ko/schema"
31+
"github.com/GoogleContainerTools/skaffold/testutil"
32+
)
33+
34+
func TestGetDependencies(t *testing.T) {
35+
allFiles := []string{
36+
".ko.yaml",
37+
"cmd/foo/root.go",
38+
"go.mod",
39+
"main.go",
40+
"pkg/bar/bar.go",
41+
}
42+
tmpDir := testutil.NewTempDir(t).Touch(allFiles...)
43+
44+
tests := []struct {
45+
description string
46+
paths []string
47+
ignore []string
48+
expected []string
49+
shouldErr bool
50+
}{
51+
{
52+
description: "default is to watch everything",
53+
expected: allFiles,
54+
},
55+
{
56+
description: "ignore everything with nil paths",
57+
ignore: []string{"."},
58+
},
59+
{
60+
description: "ignore everything",
61+
paths: []string{"."},
62+
ignore: []string{"."},
63+
},
64+
{
65+
description: "watch everything with empty string path",
66+
paths: []string{""},
67+
expected: allFiles,
68+
},
69+
{
70+
description: "watch everything star",
71+
paths: []string{"*"},
72+
expected: allFiles,
73+
},
74+
{
75+
description: "watch everything globstar",
76+
paths: []string{"**"},
77+
expected: allFiles,
78+
},
79+
{
80+
description: "ignore a directory",
81+
paths: []string{"."},
82+
ignore: []string{"cmd"},
83+
expected: []string{
84+
".ko.yaml",
85+
"go.mod",
86+
"main.go",
87+
"pkg/bar/bar.go",
88+
},
89+
},
90+
// {
91+
// // TODO(halvards)[09/17/2021]: Uncomment this test case after #6605 has been merged.
92+
// description: "globstar with file extension matching",
93+
// paths: []string{"**/*.go"},
94+
// expected: []string{
95+
// "main.go",
96+
// "cmd/foo/root.go",
97+
// "pkg/bar/bar.go",
98+
// },
99+
// },
100+
{
101+
description: "error",
102+
paths: []string{"unknown"},
103+
shouldErr: true,
104+
},
105+
}
106+
for _, test := range tests {
107+
testutil.Run(t, test.description, func(t *testutil.T) {
108+
deps, err := GetDependencies(context.Background(), tmpDir.Root(), &latestV1.KoArtifact{
109+
Dependencies: &latestV1.KoDependencies{
110+
Paths: test.paths,
111+
Ignore: test.ignore,
112+
},
113+
})
114+
t.CheckError(test.shouldErr, err)
115+
t.CheckDeepEqual(test.expected, deps,
116+
cmpopts.AcyclicTransformer("separator", filepath.FromSlash),
117+
)
118+
})
119+
}
120+
}

pkg/skaffold/build/ko/ko.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Builder struct {
4040
}
4141

4242
// NewArtifactBuilder returns a new ko artifact builder
43+
// TODO(halvards)[09/17/2021]: Call this function from newPerArtifactBuilder() in pkg/skaffold/build/local/types.go
4344
func NewArtifactBuilder(localDocker docker.LocalDaemon, pushImages bool, runMode config.RunMode, insecureRegistries map[string]bool) *Builder {
4445
return &Builder{
4546
localDocker: localDocker,

0 commit comments

Comments
 (0)