Skip to content

Commit

Permalink
add initial crd patch test
Browse files Browse the repository at this point in the history
  • Loading branch information
rschalo committed Sep 19, 2024
1 parent 6a350ab commit dad42cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/controllers/migration/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
10 changes: 10 additions & 0 deletions pkg/controllers/migration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"}))
}
})
})
})
10 changes: 10 additions & 0 deletions pkg/test/expectations/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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}})
Expand Down

0 comments on commit dad42cd

Please sign in to comment.