From dad42cd07e98d2b91986ff33f632d59e2f874ae4 Mon Sep 17 00:00:00 2001 From: Reed Schalo Date: Thu, 19 Sep 2024 00:22:51 -0700 Subject: [PATCH] add initial crd patch test --- pkg/controllers/migration/controller.go | 1 + pkg/controllers/migration/suite_test.go | 10 ++++++++++ pkg/test/expectations/expectations.go | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/pkg/controllers/migration/controller.go b/pkg/controllers/migration/controller.go index ef5acab932..e1a4838d4e 100644 --- a/pkg/controllers/migration/controller.go +++ b/pkg/controllers/migration/controller.go @@ -77,6 +77,7 @@ func (c *Controller[T]) Reconcile(ctx context.Context) (reconcile.Result, error) if err := c.kubeClient.List(ctx, crdList); err != nil { return reconcile.Result{}, fmt.Errorf("getting crds, %w", err) } + // should filter tighter than this crds := lo.Filter(crdList.Items, func(crd apiextensionsv1.CustomResourceDefinition, _ int) bool { return strings.Contains(crd.ObjectMeta.Name, strings.ToLower(o.GetObjectKind().GroupVersionKind().Kind)) }) diff --git a/pkg/controllers/migration/suite_test.go b/pkg/controllers/migration/suite_test.go index 471a406676..3f7be6312a 100644 --- a/pkg/controllers/migration/suite_test.go +++ b/pkg/controllers/migration/suite_test.go @@ -30,6 +30,7 @@ import ( "sigs.k8s.io/karpenter/pkg/test" corev1 "k8s.io/api/core/v1" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" . "sigs.k8s.io/karpenter/pkg/utils/testing" @@ -76,5 +77,14 @@ var _ = Describe("Migration", func() { nodeClaim = ExpectExists(ctx, env.Client, nodeClaim) Expect(nodeClaim.Annotations).To(HaveKeyWithValue("stored-version", "v1")) }) + It("should patch CRD status", func() { + ExpectApplied(ctx, env.Client, node, nodeClaim) + operatorexpectations.ExpectSingletonReconciled(ctx, migrationController) + nodeClaim = ExpectExists(ctx, env.Client, nodeClaim) + crds := ExpectListExists(ctx, env.Client, &apiextensionsv1.CustomResourceDefinition{}) + for _, crd := range crds { + Expect(crd.Object["status"]).To(HaveKeyWithValue("storedVersions", []interface{}{"v1"})) + } + }) }) }) diff --git a/pkg/test/expectations/expectations.go b/pkg/test/expectations/expectations.go index 9bb3d6958d..2617747037 100644 --- a/pkg/test/expectations/expectations.go +++ b/pkg/test/expectations/expectations.go @@ -28,6 +28,7 @@ import ( "sigs.k8s.io/karpenter/pkg/test/v1alpha1" + "github.com/awslabs/operatorpkg/object" "github.com/awslabs/operatorpkg/singleton" "github.com/awslabs/operatorpkg/status" . "github.com/onsi/ginkgo/v2" //nolint:revive,stylecheck @@ -43,6 +44,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" @@ -94,6 +96,14 @@ func ExpectExists[T client.Object](ctx context.Context, c client.Client, obj T) return resp } +func ExpectListExists[T client.Object](ctx context.Context, c client.Client, obj T) []unstructured.Unstructured { + GinkgoHelper() + list := &unstructured.UnstructuredList{} + list.SetGroupVersionKind(object.GVK(obj)) + Expect(c.List(ctx, list)).To(Succeed()) + return list.Items +} + func ExpectPodExists(ctx context.Context, c client.Client, name string, namespace string) *corev1.Pod { GinkgoHelper() return ExpectExists(ctx, c, &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}})