Skip to content

Commit

Permalink
[suite_test] Adapt to V2 enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Sep 21, 2022
1 parent 871a5b4 commit 6dde7da
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
36 changes: 36 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -84,6 +85,9 @@ var _ = BeforeSuite(func() {
node2 := testutils.NewNode("node2", nil)
Expect(k8sClient.Create(context.Background(), node2)).Should(Succeed())

err = patchCRDsForV1()
Expect(err).ToNot(HaveOccurred())

// Start controllers
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Expand Down Expand Up @@ -116,3 +120,35 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})

// Applies the patch defined in config/test-v1/storagev1_in_datadogagents.yaml
func patchCRDsForV1() error {
crdKey := client.ObjectKey{
Namespace: "default",
Name: "datadogagents.datadoghq.com",
}

crd := v1.CustomResourceDefinition{}
if err := k8sClient.Get(context.TODO(), crdKey, &crd); err != nil {
return err
}

// Versions[0] is v1alpha1 and [1] is v2alpha1
crd.Spec.Versions[0].Storage = true
crd.Spec.Versions[0].Served = true
crd.Spec.Versions[1].Storage = false
crd.Spec.Versions[1].Served = false

if err := k8sClient.Update(context.TODO(), &crd); err != nil {
return err
}

// Wait until the CRD is accessible. Otherwise, we might get errors when
// trying to create objects of this type.
Eventually(func() bool {
err := k8sClient.Get(context.Background(), crdKey, &crd)
return err == nil
}, timeout, interval).Should(BeTrue())

return nil
}
35 changes: 0 additions & 35 deletions controllers/suite_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
Expand Down Expand Up @@ -91,9 +90,6 @@ var _ = BeforeSuite(func() {
node2 := testutils.NewNode("node2", nil)
Expect(k8sClient.Create(context.Background(), node2)).Should(Succeed())

err = patchCRDsForV2()
Expect(err).ToNot(HaveOccurred())

// Start controllers
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Expand Down Expand Up @@ -126,34 +122,3 @@ var _ = AfterSuite(func() {
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})

// Applies the patch defined in config/test-v2/storagev2_in_datadogagents.yaml
func patchCRDsForV2() error {
crdKey := client.ObjectKey{
Namespace: "default",
Name: "datadogagents.datadoghq.com",
}

crd := v1.CustomResourceDefinition{}
if err := k8sClient.Get(context.TODO(), crdKey, &crd); err != nil {
return err
}

// Versions[0] is v1alpha1 and [1] is v2alpha1
crd.Spec.Versions[0].Storage = false
crd.Spec.Versions[1].Storage = true
crd.Spec.Versions[1].Served = true

if err := k8sClient.Update(context.TODO(), &crd); err != nil {
return err
}

// Wait until the CRD is accessible. Otherwise, we might get errors when
// trying to create objects of this type.
Eventually(func() bool {
err := k8sClient.Get(context.Background(), crdKey, &crd)
return err == nil
}, timeout, interval).Should(BeTrue())

return nil
}

0 comments on commit 6dde7da

Please sign in to comment.