Skip to content

Commit dc7c51e

Browse files
authored
Merge pull request #12776 from sbueringer/pr-simplify-cleanupConfigFields
🌱 Simplify cleanupConfigFields in KCP
2 parents 0e860e7 + 231500e commit dc7c51e

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed

controlplane/kubeadm/internal/filters.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -288,42 +288,28 @@ func getAdjustedKcpConfig(kcp *controlplanev1.KubeadmControlPlane, machineConfig
288288
}
289289

290290
// cleanupConfigFields cleanups all the fields that are not relevant for the comparison.
291+
// Note: This function assumes that old defaults have been applied to kcpConfig and machineConfig
292+
// as a consequence we can assume JoinConfiguration and JoinConfiguration.NodeRegistration are always defined.
291293
func cleanupConfigFields(kcpConfig *bootstrapv1.KubeadmConfigSpec, machineConfig *bootstrapv1.KubeadmConfig) {
292-
// KCP ClusterConfiguration will only be compared with a machine's ClusterConfiguration annotation, so
293-
// we are cleaning up from the reflect.DeepEqual comparison.
294+
// KCP ClusterConfiguration will be compared in `matchClusterConfiguration` so we are cleaning it up here
295+
// so it doesn't lead to a duplicate diff in `matchInitOrJoinConfiguration`.
294296
kcpConfig.ClusterConfiguration = bootstrapv1.ClusterConfiguration{}
295297
machineConfig.Spec.ClusterConfiguration = bootstrapv1.ClusterConfiguration{}
296298

297-
// If KCP JoinConfiguration is not present, set machine JoinConfiguration to nil (nothing can trigger rollout here).
298-
// NOTE: this is required because CABPK applies an empty joinConfiguration in case no one is provided.
299-
if !kcpConfig.JoinConfiguration.IsDefined() {
300-
machineConfig.Spec.JoinConfiguration = bootstrapv1.JoinConfiguration{}
301-
}
302-
303299
// Cleanup JoinConfiguration.Discovery from kcpConfig and machineConfig, because those info are relevant only for
304300
// the join process and not for comparing the configuration of the machine.
305-
emptyDiscovery := bootstrapv1.Discovery{}
306-
if kcpConfig.JoinConfiguration.IsDefined() {
307-
kcpConfig.JoinConfiguration.Discovery = emptyDiscovery
308-
}
309-
if machineConfig.Spec.JoinConfiguration.IsDefined() {
310-
machineConfig.Spec.JoinConfiguration.Discovery = emptyDiscovery
311-
}
312-
313-
// If KCP JoinConfiguration.ControlPlane is not present, set machine join configuration to nil (nothing can trigger rollout here).
314-
// NOTE: this is required because CABPK applies an empty joinConfiguration.ControlPlane in case no one is provided.
315-
if kcpConfig.JoinConfiguration.IsDefined() && kcpConfig.JoinConfiguration.ControlPlane == nil &&
316-
machineConfig.Spec.JoinConfiguration.ControlPlane != nil {
301+
// Note: Changes to Discovery will apply for the next join, but they will not lead to a rollout.
302+
kcpConfig.JoinConfiguration.Discovery = bootstrapv1.Discovery{}
303+
machineConfig.Spec.JoinConfiguration.Discovery = bootstrapv1.Discovery{}
304+
305+
// If KCP JoinConfiguration.ControlPlane is nil and the Machine JoinConfiguration.ControlPlane is empty,
306+
// set Machine JoinConfiguration.ControlPlane to nil.
307+
// NOTE: This is required because CABPK applies an empty JoinConfiguration.ControlPlane in case it is nil.
308+
if kcpConfig.JoinConfiguration.ControlPlane == nil &&
309+
reflect.DeepEqual(machineConfig.Spec.JoinConfiguration.ControlPlane, &bootstrapv1.JoinControlPlane{}) {
317310
machineConfig.Spec.JoinConfiguration.ControlPlane = nil
318311
}
319312

320-
// If KCP's join NodeRegistration is empty, set machine's node registration to empty as no changes should trigger rollout.
321-
emptyNodeRegistration := bootstrapv1.NodeRegistrationOptions{}
322-
if kcpConfig.JoinConfiguration.IsDefined() && reflect.DeepEqual(kcpConfig.JoinConfiguration.NodeRegistration, emptyNodeRegistration) &&
323-
!reflect.DeepEqual(machineConfig.Spec.JoinConfiguration.NodeRegistration, emptyNodeRegistration) {
324-
machineConfig.Spec.JoinConfiguration.NodeRegistration = emptyNodeRegistration
325-
}
326-
327313
// Drop differences that do not lead to changes to Machines, but that might exist due
328314
// to changes in how we serialize objects or how webhooks work.
329315
dropOmittableFields(kcpConfig)

controlplane/kubeadm/internal/filters_test.go

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,22 +333,6 @@ func TestCleanupConfigFields(t *testing.T) {
333333
g.Expect(kcpConfig.ClusterConfiguration.IsDefined()).To(BeFalse())
334334
g.Expect(machineConfig.Spec.ClusterConfiguration.IsDefined()).To(BeFalse())
335335
})
336-
t.Run("JoinConfiguration gets removed from MachineConfig if it was not derived by KCPConfig", func(t *testing.T) {
337-
g := NewWithT(t)
338-
kcpConfig := &bootstrapv1.KubeadmConfigSpec{
339-
JoinConfiguration: bootstrapv1.JoinConfiguration{}, // KCP not providing a JoinConfiguration
340-
}
341-
machineConfig := &bootstrapv1.KubeadmConfig{
342-
Spec: bootstrapv1.KubeadmConfigSpec{
343-
JoinConfiguration: bootstrapv1.JoinConfiguration{
344-
ControlPlane: &bootstrapv1.JoinControlPlane{},
345-
}, // Machine gets a default JoinConfiguration from CABPK
346-
},
347-
}
348-
cleanupConfigFields(kcpConfig, machineConfig)
349-
g.Expect(kcpConfig.JoinConfiguration.IsDefined()).To(BeFalse())
350-
g.Expect(machineConfig.Spec.JoinConfiguration.IsDefined()).To(BeFalse())
351-
})
352336
t.Run("JoinConfiguration.Discovery gets removed because it is not relevant for compare", func(t *testing.T) {
353337
g := NewWithT(t)
354338
kcpConfig := &bootstrapv1.KubeadmConfigSpec{
@@ -367,7 +351,7 @@ func TestCleanupConfigFields(t *testing.T) {
367351
g.Expect(kcpConfig.JoinConfiguration.Discovery).To(BeComparableTo(bootstrapv1.Discovery{}))
368352
g.Expect(machineConfig.Spec.JoinConfiguration.Discovery).To(BeComparableTo(bootstrapv1.Discovery{}))
369353
})
370-
t.Run("JoinConfiguration.ControlPlane gets removed from MachineConfig if it was not derived by KCPConfig", func(t *testing.T) {
354+
t.Run("JoinConfiguration.ControlPlane gets removed from MachineConfig if it was added by CABPK", func(t *testing.T) {
371355
g := NewWithT(t)
372356
kcpConfig := &bootstrapv1.KubeadmConfigSpec{
373357
JoinConfiguration: bootstrapv1.JoinConfiguration{
@@ -385,23 +369,28 @@ func TestCleanupConfigFields(t *testing.T) {
385369
g.Expect(kcpConfig.JoinConfiguration).ToNot(BeNil())
386370
g.Expect(machineConfig.Spec.JoinConfiguration.ControlPlane).To(BeNil())
387371
})
388-
t.Run("JoinConfiguration.NodeRegistrationOptions gets removed from MachineConfig if it was not derived by KCPConfig", func(t *testing.T) {
372+
t.Run("JoinConfiguration.ControlPlane gets not removed from MachineConfig if it is not empty", func(t *testing.T) {
389373
g := NewWithT(t)
390374
kcpConfig := &bootstrapv1.KubeadmConfigSpec{
391375
JoinConfiguration: bootstrapv1.JoinConfiguration{
392-
NodeRegistration: bootstrapv1.NodeRegistrationOptions{}, // NodeRegistrationOptions configuration missing in KCP
376+
ControlPlane: nil, // Control plane configuration missing in KCP
393377
},
394378
}
395379
machineConfig := &bootstrapv1.KubeadmConfig{
396380
Spec: bootstrapv1.KubeadmConfigSpec{
397381
JoinConfiguration: bootstrapv1.JoinConfiguration{
398-
NodeRegistration: bootstrapv1.NodeRegistrationOptions{Name: "test"}, // Machine gets a some JoinConfiguration.NodeRegistrationOptions
382+
ControlPlane: &bootstrapv1.JoinControlPlane{
383+
LocalAPIEndpoint: bootstrapv1.APIEndpoint{
384+
AdvertiseAddress: "1.2.3.4",
385+
BindPort: 6443,
386+
},
387+
},
399388
},
400389
},
401390
}
402391
cleanupConfigFields(kcpConfig, machineConfig)
403392
g.Expect(kcpConfig.JoinConfiguration).ToNot(BeNil())
404-
g.Expect(machineConfig.Spec.JoinConfiguration.NodeRegistration).To(BeComparableTo(bootstrapv1.NodeRegistrationOptions{}))
393+
g.Expect(machineConfig.Spec.JoinConfiguration.ControlPlane).ToNot(BeNil())
405394
})
406395
t.Run("drops omittable fields", func(t *testing.T) {
407396
g := NewWithT(t)
@@ -609,6 +598,53 @@ func TestMatchInitOrJoinConfiguration(t *testing.T) {
609598
Files: nil,
610599
DiskSetup: {},
611600
... // 9 identical fields
601+
}`))
602+
})
603+
t.Run("returns false if JoinConfiguration is NOT equal", func(t *testing.T) {
604+
g := NewWithT(t)
605+
kcp := &controlplanev1.KubeadmControlPlane{
606+
Spec: controlplanev1.KubeadmControlPlaneSpec{
607+
KubeadmConfigSpec: bootstrapv1.KubeadmConfigSpec{
608+
ClusterConfiguration: bootstrapv1.ClusterConfiguration{},
609+
InitConfiguration: bootstrapv1.InitConfiguration{},
610+
// JoinConfiguration not set anymore.
611+
},
612+
},
613+
}
614+
machineConfig := &bootstrapv1.KubeadmConfig{
615+
ObjectMeta: metav1.ObjectMeta{
616+
Namespace: "default",
617+
Name: "test",
618+
},
619+
Spec: bootstrapv1.KubeadmConfigSpec{
620+
JoinConfiguration: bootstrapv1.JoinConfiguration{
621+
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
622+
Name: "An old name", // This is a change
623+
},
624+
},
625+
},
626+
}
627+
match, diff, err := matchInitOrJoinConfiguration(machineConfig, kcp)
628+
g.Expect(err).ToNot(HaveOccurred())
629+
g.Expect(match).To(BeFalse())
630+
g.Expect(diff).To(BeComparableTo(`&v1beta2.KubeadmConfigSpec{
631+
ClusterConfiguration: {},
632+
InitConfiguration: {NodeRegistration: {ImagePullPolicy: "IfNotPresent"}},
633+
JoinConfiguration: v1beta2.JoinConfiguration{
634+
NodeRegistration: v1beta2.NodeRegistrationOptions{
635+
- Name: "An old name",
636+
+ Name: "",
637+
CRISocket: "",
638+
Taints: nil,
639+
... // 4 identical fields
640+
},
641+
CACertPath: "",
642+
Discovery: {},
643+
... // 4 identical fields
644+
},
645+
Files: nil,
646+
DiskSetup: {},
647+
... // 9 identical fields
612648
}`))
613649
})
614650
t.Run("returns true if returns true if only omittable configurations are not equal", func(t *testing.T) {

0 commit comments

Comments
 (0)