Skip to content

Commit 95e8d4b

Browse files
🐛 Fix TestServerSideApplyWithDefaulting flakes (kubernetes-sigs#10905)
* Fix TestServerSideApplyWithDefaulting flakes * Address feedback
1 parent de39402 commit 95e8d4b

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

internal/controllers/topology/cluster/structuredmerge/serversidepathhelper_test.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -679,22 +679,30 @@ func TestServerSideApplyWithDefaulting(t *testing.T) {
679679
g.Expect(env.CleanupAndWait(ctx, mutatingWebhookConfiguration)).To(Succeed())
680680
}()
681681

682+
// Test if web hook is working
683+
// NOTE: we are doing this no matter of defaultOriginal to make sure that the web hook is up and running
684+
// before calling NewServerSidePatchHelper down below.
685+
g.Eventually(ctx, func(g Gomega) {
686+
kct2 := kct.DeepCopy()
687+
kct2.Name = fmt.Sprintf("%s-2", kct.Name)
688+
g.Expect(env.Create(ctx, kct2, client.DryRunAll)).To(Succeed())
689+
g.Expect(kct2.Spec.Template.Spec.Users).To(BeComparableTo([]bootstrapv1.User{{Name: "default-user"}}))
690+
}, 5*time.Second).Should(Succeed())
691+
682692
// Run defaulting on the KubeadmConfigTemplate (triggered by an "external controller")
683-
// Note: We have to retry this with eventually as it seems to take a bit of time until
684-
// the webhook is active.
685693
if tt.defaultOriginal {
686-
g.Eventually(ctx, func(g Gomega) {
687-
patchKCT := &bootstrapv1.KubeadmConfigTemplate{}
688-
g.Expect(env.Get(ctx, client.ObjectKeyFromObject(kct), patchKCT)).To(Succeed())
694+
patchKCT := &bootstrapv1.KubeadmConfigTemplate{}
695+
g.Expect(env.Get(ctx, client.ObjectKeyFromObject(kct), patchKCT)).To(Succeed())
689696

690-
if patchKCT.Labels == nil {
691-
patchKCT.Labels = map[string]string{}
692-
}
693-
patchKCT.Labels["trigger"] = "update"
697+
if patchKCT.Labels == nil {
698+
patchKCT.Labels = map[string]string{}
699+
}
700+
patchKCT.Labels["trigger"] = "update"
694701

695-
g.Expect(env.Patch(ctx, patchKCT, client.MergeFrom(kct))).To(Succeed())
702+
g.Expect(env.Patch(ctx, patchKCT, client.MergeFrom(kct))).To(Succeed())
696703

697-
// Ensure patchKCT was defaulted.
704+
// Wait for cache to be updated with the defaulted object
705+
g.Eventually(ctx, func(g Gomega) {
698706
g.Expect(env.Get(ctx, client.ObjectKeyFromObject(kct), patchKCT)).To(Succeed())
699707
g.Expect(patchKCT.Spec.Template.Spec.Users).To(BeComparableTo([]bootstrapv1.User{{Name: "default-user"}}))
700708
}, 5*time.Second).Should(Succeed())
@@ -716,7 +724,7 @@ func TestServerSideApplyWithDefaulting(t *testing.T) {
716724
// Apply modified.
717725
p0, err = NewServerSidePatchHelper(ctx, original, modified, env.GetClient(), ssaCache)
718726
g.Expect(err).ToNot(HaveOccurred())
719-
g.Expect(p0.HasChanges()).To(Equal(tt.expectChanges))
727+
g.Expect(p0.HasChanges()).To(Equal(tt.expectChanges), fmt.Sprintf("changes: %s", string(p0.Changes())))
720728
g.Expect(p0.HasSpecChanges()).To(Equal(tt.expectSpecChanges))
721729
g.Expect(p0.Patch(ctx)).To(Succeed())
722730

0 commit comments

Comments
 (0)