Skip to content

Commit

Permalink
Merge pull request #4920 from ykakarap/kcp-patch-fix
Browse files Browse the repository at this point in the history
🐛 Fix the observedGeneration update
  • Loading branch information
k8s-ci-robot authored Jul 12, 2021
2 parents 9162904 + 74bfd71 commit 4e2b971
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
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

0 comments on commit 4e2b971

Please sign in to comment.