Skip to content

Commit ff9494c

Browse files
cbandybenjaminjb
andcommitted
Use the module cache to find snapshot CRDs
Without an explicit "-mod" flag, "golang.org/x/tools/go/packages" returns package information from the "vendor" directory, when it exists. The "vendor" directory only contains what Go needs to compile packages for the main module. It does *not* contain Kubernetes YAML files. The "-mod=readonly" build flag returns information about packages in the module cache. These directories *do* contain Kubernetes YAML files. Co-authored-by: Benjamin Blattberg <ben.blattberg@crunchydata.com> See: https://go.dev/ref/mod#build-commands
1 parent b86f239 commit ff9494c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

internal/testing/require/kubernetes.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,19 @@ func kubernetes3(t TestingT) (*envtest.Environment, client.Client) {
152152
base := Value(filepath.Rel(filepath.Dir(caller), root))
153153

154154
// Calculate the snapshotter module directory path relative to the project directory.
155+
// Ignore any "vendor" directory by explicitly passing "-mod=readonly" https://go.dev/ref/mod#build-commands
155156
var snapshotter string
156157
if pkgs, err := packages.Load(
157-
&packages.Config{Mode: packages.NeedModule},
158+
&packages.Config{BuildFlags: []string{"-mod=readonly"}, Mode: packages.NeedModule},
158159
"github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1",
159160
); assert.Check(t,
160-
err == nil && len(pkgs) > 0 && pkgs[0].Module != nil, "got %v\n%#v", err, pkgs,
161+
err == nil && len(pkgs) > 0 && pkgs[0].Module != nil, "unable to load package: %v\n%#v", err, pkgs,
161162
) {
162-
snapshotter = Value(filepath.Rel(root, pkgs[0].Module.Dir))
163+
mod := pkgs[0].Module
164+
assert.Assert(t, mod.Dir != "" && mod.Error == nil, "expected module in cache\n%#v", mod)
165+
166+
snapshotter, err = filepath.Rel(root, mod.Dir)
167+
assert.NilError(t, err, "module directory: %q", mod.Dir)
163168
}
164169

165170
env := EnvTest(t, envtest.CRDInstallOptions{

0 commit comments

Comments
 (0)