Skip to content

Commit

Permalink
Load VolumeSnapshot CRDs from the client Go module during tests
Browse files Browse the repository at this point in the history
We only use these CRD files in Go tests, and Go has already downloaded
them as part of the module we import for serializing API objects.
  • Loading branch information
cbandy committed Nov 15, 2024
1 parent 5545bd7 commit 851f55c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
13 changes: 2 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ PGMONITOR_DIR ?= hack/tools/pgmonitor
PGMONITOR_VERSION ?= v5.1.1
QUERIES_CONFIG_DIR ?= hack/tools/queries

EXTERNAL_SNAPSHOTTER_DIR ?= hack/tools/external-snapshotter
EXTERNAL_SNAPSHOTTER_VERSION ?= v8.0.1

# Buildah's "build" used to be "bud". Use the alias to be compatible for a while.
BUILDAH_BUILD ?= buildah bud

Expand Down Expand Up @@ -55,12 +52,6 @@ get-pgmonitor:
cp -r '$(PGMONITOR_DIR)/postgres_exporter/common/.' '${QUERIES_CONFIG_DIR}'
cp '$(PGMONITOR_DIR)/postgres_exporter/linux/queries_backrest.yml' '${QUERIES_CONFIG_DIR}'

.PHONY: get-external-snapshotter
get-external-snapshotter:
git -C '$(dir $(EXTERNAL_SNAPSHOTTER_DIR))' clone https://github.com/kubernetes-csi/external-snapshotter.git || git -C '$(EXTERNAL_SNAPSHOTTER_DIR)' fetch origin
@git -C '$(EXTERNAL_SNAPSHOTTER_DIR)' checkout '$(EXTERNAL_SNAPSHOTTER_VERSION)'
@git -C '$(EXTERNAL_SNAPSHOTTER_DIR)' config pull.ff only

.PHONY: clean
clean: ## Clean resources
clean: clean-deprecated
Expand Down Expand Up @@ -203,7 +194,7 @@ check: get-pgmonitor
check-envtest: ## Run check using envtest and a mock kube api
check-envtest: ENVTEST_USE = $(ENVTEST) --bin-dir=$(CURDIR)/hack/tools/envtest use $(ENVTEST_K8S_VERSION)
check-envtest: SHELL = bash
check-envtest: get-pgmonitor tools/setup-envtest get-external-snapshotter
check-envtest: get-pgmonitor tools/setup-envtest
@$(ENVTEST_USE) --print=overview && echo
source <($(ENVTEST_USE) --print=env) && PGO_NAMESPACE="postgres-operator" QUERIES_CONFIG_DIR="$(CURDIR)/${QUERIES_CONFIG_DIR}" \
$(GO_TEST) -count=1 -cover ./...
Expand All @@ -214,7 +205,7 @@ check-envtest: get-pgmonitor tools/setup-envtest get-external-snapshotter
# make check-envtest-existing PGO_TEST_TIMEOUT_SCALE=1.2
.PHONY: check-envtest-existing
check-envtest-existing: ## Run check using envtest and an existing kube api
check-envtest-existing: get-pgmonitor get-external-snapshotter
check-envtest-existing: get-pgmonitor
check-envtest-existing: createnamespaces
kubectl apply --server-side -k ./config/dev
USE_EXISTING_CLUSTER=true PGO_NAMESPACE="postgres-operator" QUERIES_CONFIG_DIR="$(CURDIR)/${QUERIES_CONFIG_DIR}" \
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
go.opentelemetry.io/otel/sdk v1.27.0
go.opentelemetry.io/otel/trace v1.27.0
golang.org/x/crypto v0.27.0
golang.org/x/tools v0.22.0
gotest.tools/v3 v3.1.0
k8s.io/api v0.30.2
k8s.io/apimachinery v0.30.2
Expand Down Expand Up @@ -72,13 +73,14 @@ require (
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuh
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -172,6 +174,8 @@ golang.org/x/oauth2 v0.21.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbht
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
21 changes: 2 additions & 19 deletions internal/controller/postgrescluster/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package postgrescluster
import (
"context"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -20,19 +19,17 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/crunchydata/postgres-operator/internal/controller/runtime"
"github.com/crunchydata/postgres-operator/internal/logging"
"github.com/crunchydata/postgres-operator/internal/testing/require"
)

var suite struct {
Client client.Client
Config *rest.Config

Environment *envtest.Environment
ServerVersion *version.Version

Manager manager.Manager
Expand All @@ -53,21 +50,7 @@ var _ = BeforeSuite(func() {
log.SetLogger(logging.FromContext(context.Background()))

By("bootstrapping test environment")
suite.Environment = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "..", "config", "crd", "bases"),
filepath.Join("..", "..", "..", "hack", "tools", "external-snapshotter", "client", "config", "crd"),
},
}

_, err := suite.Environment.Start()
Expect(err).ToNot(HaveOccurred())

DeferCleanup(suite.Environment.Stop)

suite.Config = suite.Environment.Config
suite.Client, err = client.New(suite.Config, client.Options{Scheme: runtime.Scheme})
Expect(err).ToNot(HaveOccurred())
suite.Config, suite.Client = require.Kubernetes2(GinkgoT())

dc, err := discovery.NewDiscoveryClientForConfig(suite.Config)
Expect(err).ToNot(HaveOccurred())
Expand Down
34 changes: 27 additions & 7 deletions internal/testing/require/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
goruntime "runtime"
"strings"
"sync"
"testing"

"golang.org/x/tools/go/packages"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
Expand All @@ -22,14 +22,22 @@ import (
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
)

type TestingT interface {
assert.TestingT
Cleanup(func())
Helper()
Name() string
SkipNow()
}

// https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest#pkg-constants
var envtestVarsSet = os.Getenv("KUBEBUILDER_ASSETS") != "" ||
strings.EqualFold(os.Getenv("USE_EXISTING_CLUSTER"), "true")

// EnvTest returns an unstarted Environment with crds. It calls t.Skip when
// the "KUBEBUILDER_ASSETS" and "USE_EXISTING_CLUSTER" environment variables
// are unset.
func EnvTest(t testing.TB, crds envtest.CRDInstallOptions) *envtest.Environment {
func EnvTest(t TestingT, crds envtest.CRDInstallOptions) *envtest.Environment {
t.Helper()

if !envtestVarsSet {
Expand Down Expand Up @@ -59,21 +67,21 @@ var kubernetes struct {
//
// Tests that call t.Parallel might share the same local API. Call t.Parallel after this
// function to ensure they share.
func Kubernetes(t testing.TB) client.Client {
func Kubernetes(t TestingT) client.Client {
t.Helper()
_, cc := kubernetes3(t)
return cc
}

// Kubernetes2 is the same as [Kubernetes] but also returns a copy of the client
// configuration.
func Kubernetes2(t testing.TB) (*rest.Config, client.Client) {
func Kubernetes2(t TestingT) (*rest.Config, client.Client) {
t.Helper()
env, cc := kubernetes3(t)
return rest.CopyConfig(env.Config), cc
}

func kubernetes3(t testing.TB) (*envtest.Environment, client.Client) {
func kubernetes3(t TestingT) (*envtest.Environment, client.Client) {
t.Helper()

if !envtestVarsSet {
Expand Down Expand Up @@ -102,6 +110,18 @@ func kubernetes3(t testing.TB) (*envtest.Environment, client.Client) {
base, err := filepath.Rel(filepath.Dir(caller), root)
assert.NilError(t, err)

// Calculate the snapshotter module directory path relative to the project directory.
var snapshotter string
if pkgs, err := packages.Load(
&packages.Config{Mode: packages.NeedModule},
"github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1",
); assert.Check(t,
err == nil && len(pkgs) > 0 && pkgs[0].Module != nil, "got %v\n%#v", err, pkgs,
) {
snapshotter, err = filepath.Rel(root, pkgs[0].Module.Dir)
assert.NilError(t, err)
}

kubernetes.Lock()
defer kubernetes.Unlock()

Expand All @@ -110,7 +130,7 @@ func kubernetes3(t testing.TB) (*envtest.Environment, client.Client) {
ErrorIfPathMissing: true,
Paths: []string{
filepath.Join(base, "config", "crd", "bases"),
filepath.Join(base, "hack", "tools", "external-snapshotter", "client", "config", "crd"),
filepath.Join(base, snapshotter, "config", "crd"),
},
Scheme: runtime.Scheme,
})
Expand Down Expand Up @@ -145,7 +165,7 @@ func kubernetes3(t testing.TB) (*envtest.Environment, client.Client) {

// Namespace creates a random namespace that is deleted by t.Cleanup. It calls
// t.Fatal when creation fails. The caller may delete the namespace at any time.
func Namespace(t testing.TB, cc client.Client) *corev1.Namespace {
func Namespace(t TestingT, cc client.Client) *corev1.Namespace {
t.Helper()

// Remove / that shows up when running a sub-test
Expand Down

0 comments on commit 851f55c

Please sign in to comment.