Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 envtest: Dynamically load CRDs using golang resolvers #1931

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
30 changes: 11 additions & 19 deletions test/helpers/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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"),
randomvariable marked this conversation as resolved.
Show resolved Hide resolved
}
return defaultValue
}