Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: spurious logging from migration controller #1728

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/controllers/migration/crd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand All @@ -52,6 +54,7 @@ func NewController(client client.Client, cloudProvider cloudprovider.CloudProvid
}
}

// nolint:gocyclo
func (c *Controller) Reconcile(ctx context.Context, crd *apiextensionsv1.CustomResourceDefinition) (reconcile.Result, error) {
ctx = injection.WithControllerName(ctx, "migration.crd")

Expand Down Expand Up @@ -98,8 +101,13 @@ func (c *Controller) Reconcile(ctx context.Context, crd *apiextensionsv1.CustomR
// if all custom resources have been updated, patch the CRD
stored := crd.DeepCopy()
crd.Status.StoredVersions = []string{"v1"}
if err := c.kubeClient.Status().Patch(ctx, crd, client.StrategicMergeFrom(stored, client.MergeFromWithOptimisticLock{})); err != nil {
return reconcile.Result{}, fmt.Errorf("patching crd status version %s, %w", crd.Name, err)
if !equality.Semantic.DeepEqual(stored, crd) {
if err := c.kubeClient.Status().Patch(ctx, crd, client.StrategicMergeFrom(stored, client.MergeFromWithOptimisticLock{})); client.IgnoreNotFound(err) != nil {
if errors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
}
return reconcile.Result{}, fmt.Errorf("patching crd status version %s, %w", crd.Name, err)
}
}
return reconcile.Result{}, nil
}
Expand Down
37 changes: 24 additions & 13 deletions pkg/controllers/migration/crd/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package crd_test

import (
"context"
"strings"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -44,11 +44,13 @@ import (
"sigs.k8s.io/karpenter/pkg/test/v1alpha1"
)

var ctx context.Context
var env *test.Environment
var resourceController *resource.Controller[*v1.NodeClaim]
var crdController *crd.Controller
var cloudProvider *fake.CloudProvider
var (
ctx context.Context
env *test.Environment
resourceController *resource.Controller[*v1.NodeClaim]
crdController *crd.Controller
cloudProvider *fake.CloudProvider
)

func TestAPIs(t *testing.T) {
ctx = TestContextWithLogger(t)
Expand All @@ -70,10 +72,8 @@ var _ = AfterSuite(func() {
var _ = Describe("Migration", func() {
var node *corev1.Node
var nodeClaim *v1.NodeClaim
var nodeClass *v1alpha1.TestNodeClass

BeforeEach(func() {
nodeClass = test.NodeClass()
nodeClaim, node = test.NodeClaimAndNode()
node.Labels[v1.NodePoolLabelKey] = test.NodePool().Name
})
Expand All @@ -84,15 +84,26 @@ var _ = Describe("Migration", func() {

Context("Stored Version", func() {
It("should update to v1 after custom resources have migrated", func() {
for _, item := range apis.CRDs {
crd := &apiextensionsv1.CustomResourceDefinition{}
Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(item), crd)).To(Succeed())
stored := crd.DeepCopy()
crd.Status.StoredVersions = append(crd.Status.StoredVersions, "v1beta1")
Eventually(func(g Gomega) {
g.Expect(env.Client.Status().Patch(ctx, crd, client.StrategicMergeFrom(stored, client.MergeFromWithOptimisticLock{}))).To(Succeed())
}).WithTimeout(time.Second * 10).Should(Succeed())
}
ExpectApplied(ctx, env.Client, node, nodeClaim)
ExpectReconcileSucceeded(ctx, resourceController, client.ObjectKeyFromObject(nodeClaim))
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim)
Expect(nodeClaim.Annotations).To(HaveKeyWithValue(v1.StoredVersionMigratedKey, "true"))
ExpectObjectReconciled(ctx, env.Client, crdController, v1alpha1.CRDs[0])
for _, crd := range env.CRDs {
if strings.Contains(crd.Name, strings.ToLower(nodeClass.Name)) {
Expect(crd.Status.StoredVersions).To(HaveExactElements("v1"))
}
for _, item := range apis.CRDs {
crd := &apiextensionsv1.CustomResourceDefinition{}
ExpectObjectReconciled(ctx, env.Client, crdController, item)
Eventually(func(g Gomega) {
g.Expect(env.Client.Get(ctx, client.ObjectKeyFromObject(item), crd)).To(Succeed())
g.Expect(crd.Status.StoredVersions).To(HaveExactElements("v1"))
}).WithTimeout(time.Second * 10).Should(Succeed())
}
})
It("shouldn't update the stored version to v1 if the storage version is v1beta1", func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/migration/resource/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Controller[T]) Reconcile(ctx context.Context, req reconcile.Request) (r
}))
// update annotation on CR
if !equality.Semantic.DeepEqual(stored, o) {
if err := c.kubeClient.Patch(ctx, o, client.MergeFromWithOptions(stored.(client.Object), client.MergeFromWithOptimisticLock{})); err != nil {
if err := c.kubeClient.Patch(ctx, o, client.MergeFrom(stored.(client.Object))); client.IgnoreNotFound(err) != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to do an optimistic lock here to avoid reserializing parts of the NodeClaim spec, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't, right? It's just an annotation update?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I got my wires crossed on the issues we were running into. Since it's still a patch we're good.

return reconcile.Result{}, fmt.Errorf("adding %s annotation to %s, %w", v1.StoredVersionMigratedKey, object.GVK(o), err)
}
}
Expand Down
Loading