Skip to content

Commit

Permalink
Try making crds ourselves
Browse files Browse the repository at this point in the history
  • Loading branch information
jveski committed Nov 20, 2023
1 parent 3cbea85 commit c33cb2c
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -66,10 +67,11 @@ func NewManager(t *testing.T) *Manager {
_, b, _, _ := goruntime.Caller(0)
root := filepath.Join(filepath.Dir(b), "..", "..")

testCrdDir := filepath.Join(root, "internal", "controllers", "reconciliation", "fixtures", "v1", "config", "crd")
env := &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join(root, "api", "v1", "config", "crd"),
filepath.Join(root, "internal", "controllers", "reconciliation", "fixtures", "v1", "config", "crd"),
testCrdDir,
},
ErrorIfCRDPathMissing: true,

Expand Down Expand Up @@ -107,10 +109,6 @@ func NewManager(t *testing.T) *Manager {

downstreamEnv := &envtest.Environment{
BinaryAssetsDirectory: dir,
CRDDirectoryPaths: []string{
filepath.Join(root, "internal", "controllers", "reconciliation", "fixtures", "v1", "config", "crd"),
},
ErrorIfCRDPathMissing: true,
}

// k8s <1.13 will not start if these flags are set
Expand All @@ -130,6 +128,7 @@ func NewManager(t *testing.T) *Manager {
m.DownstreamRestConfig, err = downstreamEnv.Start()
require.NoError(t, err)

// Log apiserver version
disc, err := discovery.NewDiscoveryClientForConfig(m.DownstreamRestConfig)
if err == nil {
version, err := disc.ServerVersion()
Expand All @@ -138,6 +137,23 @@ func NewManager(t *testing.T) *Manager {
}
}

// Install CRDs
// This is required because older k8s versions don't have apiextensions v1, which envtest uses
crds, err := os.ReadDir(testCrdDir)
require.NoError(t, err)

for _, crdFile := range crds {
raw, err := os.ReadFile(filepath.Join(testCrdDir, crdFile.Name()))
require.NoError(t, err)

res := &unstructured.Unstructured{}
require.NoError(t, res.UnmarshalJSON(raw))

cli, err := client.New(m.DownstreamRestConfig, client.Options{})
require.NoError(t, err)
require.NoError(t, cli.Create(context.Background(), res))
}

return m
}

Expand Down

0 comments on commit c33cb2c

Please sign in to comment.