Skip to content

Commit ff25fdb

Browse files
authored
Merge pull request #13035 from sbueringer/pr-fix-KCP-isjoin
🐛 Fix KCP KubeadmConfig isJoin detection
2 parents 27a242b + 0046d1c commit ff25fdb

File tree

7 files changed

+59
-109
lines changed

7 files changed

+59
-109
lines changed

api/bootstrap/kubeadm/v1beta2/kubeadm_types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,6 @@ type Discovery struct {
695695
TLSBootstrapToken string `json:"tlsBootstrapToken,omitempty"`
696696
}
697697

698-
// IsDefined returns true if the Discovery is defined.
699-
func (r *Discovery) IsDefined() bool {
700-
return !reflect.DeepEqual(r, &Discovery{})
701-
}
702-
703698
// BootstrapTokenDiscovery is used to set the options for bootstrap token based discovery.
704699
// +kubebuilder:validation:MinProperties=1
705700
type BootstrapTokenDiscovery struct {

controlplane/kubeadm/internal/controllers/helpers_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,9 @@ func TestCloneConfigsAndGenerateMachineAndSyncMachines(t *testing.T) {
417417
UID: kcp.UID,
418418
}))
419419
g.Expect(kubeadmConfig.Spec.InitConfiguration).To(BeComparableTo(bootstrapv1.InitConfiguration{}))
420-
g.Expect(kubeadmConfig.Spec.JoinConfiguration).To(BeComparableTo(kcp.Spec.KubeadmConfigSpec.JoinConfiguration))
420+
expectedJoinConfiguration := kcp.Spec.KubeadmConfigSpec.JoinConfiguration.DeepCopy()
421+
expectedJoinConfiguration.ControlPlane = &bootstrapv1.JoinControlPlane{}
422+
g.Expect(kubeadmConfig.Spec.JoinConfiguration).To(BeComparableTo(*expectedJoinConfiguration))
421423
// Note: capi-kubeadmcontrolplane should own ownerReferences and spec, labels and annotations should be orphaned.
422424
// Labels and annotations will be owned by capi-kubeadmcontrolplane-metadata after the next update
423425
// of labels and annotations.
@@ -433,6 +435,7 @@ func TestCloneConfigsAndGenerateMachineAndSyncMachines(t *testing.T) {
433435
},
434436
"f:spec":{
435437
"f:joinConfiguration":{
438+
"f:controlPlane":{},
436439
"f:nodeRegistration":{
437440
"f:kubeletExtraArgs":{
438441
"k:{\"name\":\"v\",\"value\":\"8\"}":{
@@ -508,6 +511,7 @@ func TestCloneConfigsAndGenerateMachineAndSyncMachines(t *testing.T) {
508511
},
509512
"f:spec":{
510513
"f:joinConfiguration":{
514+
"f:controlPlane":{},
511515
"f:nodeRegistration":{
512516
"f:kubeletExtraArgs":{
513517
"k:{\"name\":\"v\",\"value\":\"8\"}":{

controlplane/kubeadm/internal/controllers/inplace_canupdatemachine_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ func Test_canExtensionsUpdateMachine(t *testing.T) {
202202
},
203203
},
204204
},
205+
JoinConfiguration: bootstrapv1.JoinConfiguration{
206+
ControlPlane: &bootstrapv1.JoinControlPlane{},
207+
},
205208
},
206209
}
207210
desiredKubeadmConfig := currentKubeadmConfig.DeepCopy()
@@ -323,7 +326,7 @@ func Test_canExtensionsUpdateMachine(t *testing.T) {
323326
+ "clusterConfiguration": map[string]any{"etcd": map[string]any{"local": map[string]any{"imageTag": string("3.6.4-0")}}},
324327
"format": string("cloud-config"),
325328
"initConfiguration": map[string]any{"nodeRegistration": map[string]any{"imagePullPolicy": string("IfNotPresent")}},
326-
"joinConfiguration": map[string]any{"nodeRegistration": map[string]any{"imagePullPolicy": string("IfNotPresent")}},
329+
"joinConfiguration": map[string]any{"controlPlane": map[string]any{}, "nodeRegistration": map[string]any{"imagePullPolicy": string("IfNotPresent")}},
327330
},
328331
},
329332
}`,
@@ -424,7 +427,7 @@ func Test_canExtensionsUpdateMachine(t *testing.T) {
424427
+ "clusterConfiguration": map[string]any{"etcd": map[string]any{"local": map[string]any{"imageTag": string("3.6.4-0")}}},
425428
"format": string("cloud-config"),
426429
"initConfiguration": map[string]any{"nodeRegistration": map[string]any{"imagePullPolicy": string("IfNotPresent")}},
427-
"joinConfiguration": map[string]any{"nodeRegistration": map[string]any{"imagePullPolicy": string("IfNotPresent")}},
430+
"joinConfiguration": map[string]any{"controlPlane": map[string]any{}, "nodeRegistration": map[string]any{"imagePullPolicy": string("IfNotPresent")}},
428431
},
429432
},
430433
}`,
@@ -619,12 +622,7 @@ func Test_createRequest(t *testing.T) {
619622
},
620623
},
621624
JoinConfiguration: bootstrapv1.JoinConfiguration{
622-
// This field is technically set by CABPK, but adding it here so that matchesKubeadmConfig detects this correctly as a join KubeadmConfig.
623-
Discovery: bootstrapv1.Discovery{
624-
BootstrapToken: bootstrapv1.BootstrapTokenDiscovery{
625-
APIServerEndpoint: "1.2.3.4:6443",
626-
},
627-
},
625+
ControlPlane: &bootstrapv1.JoinControlPlane{},
628626
NodeRegistration: bootstrapv1.NodeRegistrationOptions{
629627
KubeletExtraArgs: []bootstrapv1.Arg{{
630628
Name: "v",
@@ -641,7 +639,6 @@ func Test_createRequest(t *testing.T) {
641639
currentKubeadmConfigCleanedUp.SetGroupVersionKind(bootstrapv1.GroupVersion.WithKind("KubeadmConfig")) // cleanupKubeadmConfig adds GVK.
642640
currentKubeadmConfigCleanedUp.Status = bootstrapv1.KubeadmConfigStatus{} // cleanupKubeadmConfig drops status.
643641
defaulting.ApplyPreviousKubeadmConfigDefaults(&currentKubeadmConfigCleanedUp.Spec) // PrepareKubeadmConfigsForDiff applies defaults.
644-
currentKubeadmConfigCleanedUp.Spec.JoinConfiguration.Discovery = bootstrapv1.Discovery{} // PrepareKubeadmConfigsForDiff cleans up Discovery.
645642
currentKubeadmConfigWithOutdatedLabelsAndAnnotations := currentKubeadmConfig.DeepCopy()
646643
currentKubeadmConfigWithOutdatedLabelsAndAnnotations.Labels["outdated-label-1"] = "outdated-label-value-1"
647644
currentKubeadmConfigWithOutdatedLabelsAndAnnotations.Annotations["outdated-annotation-1"] = "outdated-annotation-value-1"
@@ -655,7 +652,6 @@ func Test_createRequest(t *testing.T) {
655652
desiredKubeadmConfigCleanedUp.SetGroupVersionKind(bootstrapv1.GroupVersion.WithKind("KubeadmConfig")) // cleanupKubeadmConfig adds GVK.
656653
desiredKubeadmConfigCleanedUp.Status = bootstrapv1.KubeadmConfigStatus{} // cleanupKubeadmConfig drops status.
657654
defaulting.ApplyPreviousKubeadmConfigDefaults(&desiredKubeadmConfigCleanedUp.Spec) // PrepareKubeadmConfigsForDiff applies defaults.
658-
desiredKubeadmConfigCleanedUp.Spec.JoinConfiguration.Discovery = bootstrapv1.Discovery{} // PrepareKubeadmConfigsForDiff cleans up Discovery.
659655

660656
currentInfraMachine := &unstructured.Unstructured{
661657
Object: map[string]interface{}{

controlplane/kubeadm/internal/desiredstate/desired_state.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,13 @@ func ComputeDesiredKubeadmConfig(kcp *controlplanev1.KubeadmControlPlane, cluste
212212
if isJoin {
213213
// Note: When building a KubeadmConfig for a joining CP machine empty out the unnecessary InitConfiguration.
214214
spec.InitConfiguration = bootstrapv1.InitConfiguration{}
215-
// NOTE: For the joining we are preserving the ClusterConfiguration in order to determine if the
215+
// Note: For the joining we are preserving the ClusterConfiguration in order to determine if the
216216
// cluster is using an external etcd in the kubeadm bootstrap provider (even if this is not required by kubeadm Join).
217+
// Note: We are always setting JoinConfiguration.ControlPlane so we can later identify this KubeadmConfig as a
218+
// join KubeadmConfig.
219+
if spec.JoinConfiguration.ControlPlane == nil {
220+
spec.JoinConfiguration.ControlPlane = &bootstrapv1.JoinControlPlane{}
221+
}
217222
} else {
218223
// Note: When building a KubeadmConfig for the first CP machine empty out the unnecessary JoinConfiguration.
219224
spec.JoinConfiguration = bootstrapv1.JoinConfiguration{}

controlplane/kubeadm/internal/desiredstate/desired_state_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ func Test_ComputeDesiredKubeadmConfig(t *testing.T) {
619619
if isJoin {
620620
expectedKubeadmConfigWithoutOwner.Spec.InitConfiguration = bootstrapv1.InitConfiguration{}
621621
expectedKubeadmConfigWithoutOwner.Spec.JoinConfiguration = kcp.Spec.KubeadmConfigSpec.JoinConfiguration
622+
expectedKubeadmConfigWithoutOwner.Spec.JoinConfiguration.ControlPlane = &bootstrapv1.JoinControlPlane{}
622623
} else {
623624
expectedKubeadmConfigWithoutOwner.Spec.InitConfiguration = kcp.Spec.KubeadmConfigSpec.InitConfiguration
624625
expectedKubeadmConfigWithoutOwner.Spec.JoinConfiguration = bootstrapv1.JoinConfiguration{}

controlplane/kubeadm/internal/filters.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package internal
1919
import (
2020
"context"
2121
"fmt"
22-
"reflect"
2322

2423
"github.com/pkg/errors"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -308,10 +307,8 @@ func PrepareKubeadmConfigsForDiff(desiredKubeadmConfig, currentKubeadmConfig *bo
308307
currentKubeadmConfig.Spec.JoinConfiguration.Patches = currentKubeadmConfig.Spec.InitConfiguration.Patches
309308
currentKubeadmConfig.Spec.JoinConfiguration.SkipPhases = currentKubeadmConfig.Spec.InitConfiguration.SkipPhases
310309
currentKubeadmConfig.Spec.JoinConfiguration.NodeRegistration = currentKubeadmConfig.Spec.InitConfiguration.NodeRegistration
311-
if currentKubeadmConfig.Spec.InitConfiguration.LocalAPIEndpoint.IsDefined() {
312-
currentKubeadmConfig.Spec.JoinConfiguration.ControlPlane = &bootstrapv1.JoinControlPlane{
313-
LocalAPIEndpoint: currentKubeadmConfig.Spec.InitConfiguration.LocalAPIEndpoint,
314-
}
310+
currentKubeadmConfig.Spec.JoinConfiguration.ControlPlane = &bootstrapv1.JoinControlPlane{
311+
LocalAPIEndpoint: currentKubeadmConfig.Spec.InitConfiguration.LocalAPIEndpoint,
315312
}
316313
currentKubeadmConfig.Spec.InitConfiguration = bootstrapv1.InitConfiguration{}
317314

@@ -351,14 +348,6 @@ func PrepareKubeadmConfigsForDiff(desiredKubeadmConfig, currentKubeadmConfig *bo
351348
currentKubeadmConfig.Spec.InitConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds = nil
352349
currentKubeadmConfig.Spec.JoinConfiguration.Timeouts.ControlPlaneComponentHealthCheckSeconds = nil
353350

354-
// If KCP JoinConfiguration.ControlPlane is nil and the Machine JoinConfiguration.ControlPlane is empty,
355-
// set Machine JoinConfiguration.ControlPlane to nil.
356-
// NOTE: This is required because CABPK applies an empty JoinConfiguration.ControlPlane in case it is nil.
357-
if desiredKubeadmConfig.Spec.JoinConfiguration.ControlPlane == nil &&
358-
reflect.DeepEqual(currentKubeadmConfig.Spec.JoinConfiguration.ControlPlane, &bootstrapv1.JoinControlPlane{}) {
359-
currentKubeadmConfig.Spec.JoinConfiguration.ControlPlane = nil
360-
}
361-
362351
// Drop differences that do not lead to changes to Machines, but that might exist due
363352
// to changes in how we serialize objects or how webhooks work.
364353
dropOmittableFields(&desiredKubeadmConfig.Spec)
@@ -542,15 +531,15 @@ func dropOmittableFields(spec *bootstrapv1.KubeadmConfigSpec) {
542531
// isKubeadmConfigForJoin returns true if the KubeadmConfig is for a control plane
543532
// or a worker machine that joined an existing cluster.
544533
// Note: This check is based on the assumption that KubeadmConfig for joining
545-
// control plane and workers nodes always have a non-empty JoinConfiguration.Discovery, while
546-
// instead the JoinConfiguration for the first control plane machine in the
534+
// control plane always have a non-empty JoinConfiguration.ControlPlane, while
535+
// instead the entire JoinConfiguration for the first control plane machine in the
547536
// cluster is emptied out by KCP.
548537
// Note: Previously we checked if the entire JoinConfiguration is defined, but that
549538
// is not safe because apiServer.timeoutForControlPlane in v1beta1 is also converted to
550539
// joinConfiguration.timeouts.controlPlaneComponentHealthCheckSeconds in v1beta2 and
551540
// accordingly we would also detect init KubeadmConfigs as join.
552541
func isKubeadmConfigForJoin(c *bootstrapv1.KubeadmConfig) bool {
553-
return c.Spec.JoinConfiguration.Discovery.IsDefined()
542+
return c.Spec.JoinConfiguration.ControlPlane != nil
554543
}
555544

556545
// isKubeadmConfigForInit returns true if the KubeadmConfig is for the first control plane

0 commit comments

Comments
 (0)