diff --git a/go.mod b/go.mod index 8ee3cc317e..3bf5ce3883 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( golang.org/x/mod v0.12.0 golang.org/x/oauth2 v0.10.0 golang.org/x/text v0.11.0 + golang.org/x/tools v0.9.3 gopkg.in/gcfg.v1 v1.2.3 gopkg.in/yaml.v2 v2.4.0 k8s.io/api v0.27.2 @@ -60,7 +61,6 @@ require ( golang.org/x/net v0.12.0 // indirect golang.org/x/sys v0.10.0 // indirect golang.org/x/term v0.10.0 // indirect - golang.org/x/tools v0.9.3 // indirect ) require ( diff --git a/go.sum b/go.sum index 09e7f19a97..8ee5f171bd 100644 --- a/go.sum +++ b/go.sum @@ -795,6 +795,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/test/helpers/envtest.go b/test/helpers/envtest.go index 10cba13f9d..74dc145b2e 100644 --- a/test/helpers/envtest.go +++ b/test/helpers/envtest.go @@ -20,14 +20,13 @@ package helpers import ( goctx "context" "fmt" - "go/build" - "os" "path" "path/filepath" goruntime "runtime" "github.com/onsi/ginkgo/v2" "github.com/vmware/govmomi/simulator" + "golang.org/x/tools/go/packages" admissionv1 "k8s.io/api/admissionregistration/v1" corev1 "k8s.io/api/core/v1" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -85,7 +84,7 @@ func init() { } // append CAPI CRDs path - if capiPaths := getFilePathToCAPICRDs(root); capiPaths != nil { + if capiPaths := getFilePathToCAPICRDs(); capiPaths != nil { crdPaths = append(crdPaths, capiPaths...) } @@ -218,28 +217,21 @@ func (t *TestEnvironment) CreateKubeconfigSecret(ctx goctx.Context, cluster *clu return t.Create(ctx, kubeconfig.GenerateSecret(cluster, kubeconfig.FromEnvTestConfig(t.Config, cluster))) } -func getFilePathToCAPICRDs(root string) []string { - mod, err := NewMod(filepath.Join(root, "go.mod")) - if err != nil { - return nil +func getFilePathToCAPICRDs() []string { + packageName := "sigs.k8s.io/cluster-api" + packageConfig := &packages.Config{ + Mode: packages.NeedModule, } - packageName := "sigs.k8s.io/cluster-api" - clusterAPIVersion, err := mod.FindDependencyVersion(packageName) + pkgs, err := packages.Load(packageConfig, packageName) if err != nil { return nil } - gopath := envOr("GOPATH", build.Default.GOPATH) - return []string{ - filepath.Join(gopath, "pkg", "mod", "sigs.k8s.io", fmt.Sprintf("cluster-api@%s", clusterAPIVersion), "config", "crd", "bases"), - filepath.Join(gopath, "pkg", "mod", "sigs.k8s.io", fmt.Sprintf("cluster-api@%s", clusterAPIVersion), "controlplane", "kubeadm", "config", "crd", "bases"), - } -} + pkg := pkgs[0] -func envOr(envKey, defaultValue string) string { - if value, ok := os.LookupEnv(envKey); ok { - return value + return []string{ + filepath.Join(pkg.Module.Dir, "config", "crd", "bases"), + filepath.Join(pkg.Module.Dir, "controlplane", "kubeadm", "config", "crd", "bases"), } - return defaultValue }