Skip to content

Commit e68d7be

Browse files
authored
Merge pull request kubernetes#85457 from BenTheElder/typecheck-fun
Typecheck support for verifying providerless build
2 parents 864596f + b24dbac commit e68d7be

File tree

4 files changed

+80
-25
lines changed

4 files changed

+80
-25
lines changed

hack/make-rules/verify.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ EXCLUDED_PATTERNS=(
3939
# Exclude typecheck in certain cases, if they're running in a separate job.
4040
if [[ ${EXCLUDE_TYPECHECK:-} =~ ^[yY]$ ]]; then
4141
EXCLUDED_PATTERNS+=(
42-
"verify-typecheck.sh" # runs in separate typecheck job
42+
"verify-typecheck.sh" # runs in separate typecheck job
43+
"verify-typecheck-providerless.sh" # runs in separate typecheck job
4344
)
4445
fi
4546

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
22+
23+
cd "${KUBE_ROOT}"
24+
# verify the providerless build
25+
# https://github.com/kubernetes/enhancements/blob/master/keps/sig-cloud-provider/20190729-building-without-in-tree-providers.md
26+
hack/verify-typecheck.sh --skip-test --tags=providerless --ignore-dirs=test

test/typecheck/main.go

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,15 @@ import (
4141
)
4242

4343
var (
44-
verbose = flag.Bool("verbose", false, "print more information")
45-
cross = flag.Bool("cross", true, "build for all platforms")
46-
platforms = flag.String("platform", "", "comma-separated list of platforms to typecheck")
47-
timings = flag.Bool("time", false, "output times taken for each phase")
48-
defuses = flag.Bool("defuse", false, "output defs/uses")
49-
serial = flag.Bool("serial", false, "don't type check platforms in parallel")
44+
verbose = flag.Bool("verbose", false, "print more information")
45+
cross = flag.Bool("cross", true, "build for all platforms")
46+
platforms = flag.String("platform", "", "comma-separated list of platforms to typecheck")
47+
timings = flag.Bool("time", false, "output times taken for each phase")
48+
defuses = flag.Bool("defuse", false, "output defs/uses")
49+
serial = flag.Bool("serial", false, "don't type check platforms in parallel")
50+
skipTest = flag.Bool("skip-test", false, "don't type check test code")
51+
tags = flag.String("tags", "", "comma-separated list of build tags to apply in addition to go's defaults")
52+
ignoreDirs = flag.String("ignore-dirs", "", "comma-separated list of directories to ignore in addition to the default hardcoded list including staging, vendor, and hidden dirs")
5053

5154
isTerminal = terminal.IsTerminal(int(os.Stdout.Fd()))
5255
logPrefix = ""
@@ -62,6 +65,24 @@ var (
6265
}
6366
darwinPlatString = "darwin/386,darwin/amd64"
6467
windowsPlatString = "windows/386,windows/amd64"
68+
69+
// directories we always ignore
70+
standardIgnoreDirs = []string{
71+
// Staging code is symlinked from vendor/k8s.io, and uses import
72+
// paths as if it were inside of vendor/. It fails typechecking
73+
// inside of staging/, but works when typechecked as part of vendor/.
74+
"staging",
75+
// OS-specific vendor code tends to be imported by OS-specific
76+
// packages. We recursively typecheck imported vendored packages for
77+
// each OS, but don't typecheck everything for every OS.
78+
"vendor",
79+
"_output",
80+
// This is a weird one. /testdata/ is *mostly* ignored by Go,
81+
// and this translates to kubernetes/vendor not working.
82+
// edit/record.go doesn't compile without gopkg.in/yaml.v2
83+
// in $GOSRC/$GOROOT (both typecheck and the shell script).
84+
"pkg/kubectl/cmd/testdata/edit",
85+
}
6586
)
6687

6788
type analyzer struct {
@@ -79,6 +100,10 @@ func newAnalyzer(platform string) *analyzer {
79100
platSplit := strings.Split(platform, "/")
80101
ctx.GOOS, ctx.GOARCH = platSplit[0], platSplit[1]
81102
ctx.CgoEnabled = true
103+
if *tags != "" {
104+
tagsSplit := strings.Split(*tags, ",")
105+
ctx.BuildTags = append(ctx.BuildTags, tagsSplit...)
106+
}
82107

83108
a := &analyzer{
84109
platform: platform,
@@ -158,6 +183,9 @@ func (a *analyzer) filterFiles(fs map[string]*ast.File) []*ast.File {
158183
files := []*ast.File{}
159184
for _, f := range fs {
160185
fpath := a.fset.File(f.Pos()).Name()
186+
if *skipTest && strings.HasSuffix(fpath, "_test.go") {
187+
continue
188+
}
161189
dir, name := filepath.Split(fpath)
162190
matches, err := a.ctx.MatchFile(dir, name)
163191
if err != nil {
@@ -219,7 +247,8 @@ func (a *analyzer) typeCheck(dir string, files []*ast.File) error {
219247
}
220248

221249
type collector struct {
222-
dirs []string
250+
dirs []string
251+
ignoreDirs []string
223252
}
224253

225254
// handlePath walks the filesystem recursively, collecting directories,
@@ -231,23 +260,14 @@ func (c *collector) handlePath(path string, info os.FileInfo, err error) error {
231260
}
232261
if info.IsDir() {
233262
// Ignore hidden directories (.git, .cache, etc)
234-
if len(path) > 1 && path[0] == '.' ||
235-
// Staging code is symlinked from vendor/k8s.io, and uses import
236-
// paths as if it were inside of vendor/. It fails typechecking
237-
// inside of staging/, but works when typechecked as part of vendor/.
238-
path == "staging" ||
239-
// OS-specific vendor code tends to be imported by OS-specific
240-
// packages. We recursively typecheck imported vendored packages for
241-
// each OS, but don't typecheck everything for every OS.
242-
path == "vendor" ||
243-
path == "_output" ||
244-
// This is a weird one. /testdata/ is *mostly* ignored by Go,
245-
// and this translates to kubernetes/vendor not working.
246-
// edit/record.go doesn't compile without gopkg.in/yaml.v2
247-
// in $GOSRC/$GOROOT (both typecheck and the shell script).
248-
path == "pkg/kubectl/cmd/testdata/edit" {
263+
if len(path) > 1 && path[0] == '.' {
249264
return filepath.SkipDir
250265
}
266+
for _, dir := range c.ignoreDirs {
267+
if path == dir {
268+
return filepath.SkipDir
269+
}
270+
}
251271
c.dirs = append(c.dirs, path)
252272
}
253273
return nil
@@ -310,7 +330,13 @@ func main() {
310330
args = append(args, ".")
311331
}
312332

313-
c := collector{}
333+
c := collector{
334+
ignoreDirs: append([]string(nil), standardIgnoreDirs...),
335+
}
336+
if *ignoreDirs != "" {
337+
c.ignoreDirs = append(c.ignoreDirs, strings.Split(*ignoreDirs, ",")...)
338+
}
339+
314340
for _, arg := range args {
315341
err := filepath.Walk(arg, c.handlePath)
316342
if err != nil {

test/typecheck/main_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ func TestHandlePackage(t *testing.T) {
146146
}
147147

148148
func TestHandlePath(t *testing.T) {
149-
c := collector{}
149+
c := collector{
150+
ignoreDirs: standardIgnoreDirs,
151+
}
150152
e := errors.New("ex")
151153
i, _ := os.Stat(".") // i.IsDir() == true
152154
if c.handlePath("foo", nil, e) != e {

0 commit comments

Comments
 (0)