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 the observedGeneration update #4920

Merged
merged 1 commit into from
Jul 12, 2021
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
1 change: 1 addition & 0 deletions controlplane/kubeadm/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func patchKubeadmControlPlane(ctx context.Context, patchHelper *patch.Helper, kc
controlplanev1.AvailableCondition,
controlplanev1.CertificatesAvailableCondition,
}},
patch.WithStatusObservedGeneration{},
)
}

Expand Down
55 changes: 55 additions & 0 deletions controlplane/kubeadm/controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,61 @@ func TestClusterToKubeadmControlPlaneOtherControlPlane(t *testing.T) {
g.Expect(got).To(BeNil())
}

func TestReconcileUpdateObservedGeneration(t *testing.T) {
g := NewWithT(t)
r := &KubeadmControlPlaneReconciler{
Client: testEnv,
recorder: record.NewFakeRecorder(32),
managementCluster: &internal.Management{Client: testEnv.Client},
Log: log.NullLogger{},
}
ctx := context.TODO()

cluster, kcp, _ := createClusterWithControlPlane()
g.Expect(testEnv.CreateObj(ctx, cluster)).To(Succeed())
g.Expect(testEnv.CreateObj(ctx, kcp)).To(Succeed())

// read kcp.Generation after create
errGettingObject := testEnv.Get(ctx, util.ObjectKey(kcp), kcp)
g.Expect(errGettingObject).NotTo(HaveOccurred())
generation := kcp.Generation

// Set cluster.status.InfrastructureReady so we actually enter in the reconcile loop
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"status\":{\"infrastructureReady\":%t}}", true)))
g.Expect(testEnv.Status().Patch(ctx, cluster, patch)).To(Succeed())

// call reconcile the first time, so we can check if observedGeneration is set when adding a finalizer
result, err := r.Reconcile(ctrl.Request{NamespacedName: util.ObjectKey(kcp)})
g.Expect(err).NotTo(HaveOccurred())
g.Expect(result).To(Equal(ctrl.Result{}))

g.Eventually(func() int64 {
errGettingObject = testEnv.Get(ctx, util.ObjectKey(kcp), kcp)
g.Expect(errGettingObject).NotTo(HaveOccurred())
return kcp.Status.ObservedGeneration
}, 10*time.Second).Should(Equal(generation))

// triggers a generation change by changing the spec
kcp.Spec.Replicas = pointer.Int32Ptr(*kcp.Spec.Replicas + 2)
g.Expect(testEnv.Update(ctx, kcp)).To(Succeed())

// read kcp.Generation after the update
errGettingObject = testEnv.Get(ctx, util.ObjectKey(kcp), kcp)
g.Expect(errGettingObject).NotTo(HaveOccurred())
generation = kcp.Generation

// call reconcile the second time, so we can check if observedGeneration is set when calling defer patch
// NB. The call to reconcile fails because KCP is not properly setup (e.g. missing InfrastructureTemplate)
// but this is not important because what we want is KCP to be patched
_, _ = r.Reconcile(ctrl.Request{NamespacedName: util.ObjectKey(kcp)})

g.Eventually(func() int64 {
errGettingObject = testEnv.Get(ctx, util.ObjectKey(kcp), kcp)
g.Expect(errGettingObject).NotTo(HaveOccurred())
return kcp.Status.ObservedGeneration
}, 10*time.Second).Should(Equal(generation))
}

func TestReconcileNoClusterOwnerRef(t *testing.T) {
g := NewWithT(t)

Expand Down