Skip to content

Commit 849db80

Browse files
committed
Use the same names for Machine objects generated by KCP & MachineSets
Signed-off-by: Stefan Büringer [email protected]
1 parent 351a463 commit 849db80

File tree

7 files changed

+54
-39
lines changed

7 files changed

+54
-39
lines changed

controllers/external/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ type CreateFromTemplateInput struct {
7070
// Namespace is the Kubernetes namespace the cloned object should be created into.
7171
Namespace string
7272

73+
// Name is used as the name of the generated object, if set.
74+
// If it isn't set the template name will be used as prefix to generate a name instead.
75+
Name string
76+
7377
// ClusterName is the cluster this object is linked to.
7478
ClusterName string
7579

@@ -96,6 +100,7 @@ func CreateFromTemplate(ctx context.Context, in *CreateFromTemplateInput) (*core
96100
Template: from,
97101
TemplateRef: in.TemplateRef,
98102
Namespace: in.Namespace,
103+
Name: in.Name,
99104
ClusterName: in.ClusterName,
100105
OwnerRef: in.OwnerRef,
101106
Labels: in.Labels,
@@ -125,6 +130,10 @@ type GenerateTemplateInput struct {
125130
// Namespace is the Kubernetes namespace the cloned object should be created into.
126131
Namespace string
127132

133+
// Name is used as the name of the generated object, if set.
134+
// If it isn't set the template name will be used as prefix to generate a name instead.
135+
Name string
136+
128137
// ClusterName is the cluster this object is linked to.
129138
ClusterName string
130139

@@ -157,6 +166,9 @@ func GenerateTemplate(in *GenerateTemplateInput) (*unstructured.Unstructured, er
157166
to.SetUID("")
158167
to.SetSelfLink("")
159168
to.SetName(names.SimpleNameGenerator.GenerateName(in.Template.GetName() + "-"))
169+
if in.Name != "" {
170+
to.SetName(in.Name)
171+
}
160172
to.SetNamespace(in.Namespace)
161173

162174
// Set annotations.

controllers/external/util_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,15 @@ func TestCloneTemplateResourceFoundNoOwner(t *testing.T) {
265265
Client: fakeClient,
266266
TemplateRef: templateRef,
267267
Namespace: metav1.NamespaceDefault,
268+
Name: "object-name",
268269
ClusterName: testClusterName,
269270
})
270271
g.Expect(err).ToNot(HaveOccurred())
271272
g.Expect(ref).NotTo(BeNil())
272273
g.Expect(ref.Kind).To(Equal(expectedKind))
273274
g.Expect(ref.APIVersion).To(Equal(expectedAPIVersion))
274275
g.Expect(ref.Namespace).To(Equal(metav1.NamespaceDefault))
275-
g.Expect(ref.Name).To(HavePrefix(templateRef.Name))
276+
g.Expect(ref.Name).To(Equal("object-name"))
276277

277278
clone := &unstructured.Unstructured{}
278279
clone.SetKind(expectedKind)

controlplane/kubeadm/internal/controllers/helpers.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ func (r *KubeadmControlPlaneReconciler) reconcileExternalReference(ctx context.C
165165
func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx context.Context, cluster *clusterv1.Cluster, kcp *controlplanev1.KubeadmControlPlane, bootstrapSpec *bootstrapv1.KubeadmConfigSpec, failureDomain *string) error {
166166
var errs []error
167167

168+
// Compute desired Machine
169+
machine, err := r.computeDesiredMachine(kcp, cluster, failureDomain, nil)
170+
if err != nil {
171+
return errors.Wrap(err, "failed to create Machine: failed to compute desired Machine")
172+
}
173+
168174
// Since the cloned resource should eventually have a controller ref for the Machine, we create an
169175
// OwnerReference here without the Controller field set
170176
infraCloneOwner := &metav1.OwnerReference{
@@ -179,6 +185,7 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
179185
Client: r.Client,
180186
TemplateRef: &kcp.Spec.MachineTemplate.InfrastructureRef,
181187
Namespace: kcp.Namespace,
188+
Name: machine.Name,
182189
OwnerRef: infraCloneOwner,
183190
ClusterName: cluster.Name,
184191
Labels: internal.ControlPlaneMachineLabelsForCluster(kcp, cluster.Name),
@@ -190,9 +197,10 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
190197
clusterv1.ConditionSeverityError, err.Error())
191198
return errors.Wrap(err, "failed to clone infrastructure template")
192199
}
200+
machine.Spec.InfrastructureRef = *infraRef
193201

194202
// Clone the bootstrap configuration
195-
bootstrapRef, err := r.generateKubeadmConfig(ctx, kcp, cluster, bootstrapSpec)
203+
bootstrapRef, err := r.generateKubeadmConfig(ctx, kcp, cluster, bootstrapSpec, machine.Name)
196204
if err != nil {
197205
conditions.MarkFalse(kcp, controlplanev1.MachinesCreatedCondition, controlplanev1.BootstrapTemplateCloningFailedReason,
198206
clusterv1.ConditionSeverityError, err.Error())
@@ -201,7 +209,9 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
201209

202210
// Only proceed to generating the Machine if we haven't encountered an error
203211
if len(errs) == 0 {
204-
if err := r.createMachine(ctx, kcp, cluster, infraRef, bootstrapRef, failureDomain); err != nil {
212+
machine.Spec.Bootstrap.ConfigRef = bootstrapRef
213+
214+
if err := r.createMachine(ctx, kcp, machine); err != nil {
205215
conditions.MarkFalse(kcp, controlplanev1.MachinesCreatedCondition, controlplanev1.MachineGenerationFailedReason,
206216
clusterv1.ConditionSeverityError, err.Error())
207217
errs = append(errs, errors.Wrap(err, "failed to create Machine"))
@@ -241,7 +251,7 @@ func (r *KubeadmControlPlaneReconciler) cleanupFromGeneration(ctx context.Contex
241251
return kerrors.NewAggregate(errs)
242252
}
243253

244-
func (r *KubeadmControlPlaneReconciler) generateKubeadmConfig(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster, spec *bootstrapv1.KubeadmConfigSpec) (*corev1.ObjectReference, error) {
254+
func (r *KubeadmControlPlaneReconciler) generateKubeadmConfig(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster, spec *bootstrapv1.KubeadmConfigSpec, name string) (*corev1.ObjectReference, error) {
245255
// Create an owner reference without a controller reference because the owning controller is the machine controller
246256
owner := metav1.OwnerReference{
247257
APIVersion: controlplanev1.GroupVersion.String(),
@@ -252,7 +262,7 @@ func (r *KubeadmControlPlaneReconciler) generateKubeadmConfig(ctx context.Contex
252262

253263
bootstrapConfig := &bootstrapv1.KubeadmConfig{
254264
ObjectMeta: metav1.ObjectMeta{
255-
Name: names.SimpleNameGenerator.GenerateName(kcp.Name + "-"),
265+
Name: name,
256266
Namespace: kcp.Namespace,
257267
Labels: internal.ControlPlaneMachineLabelsForCluster(kcp, cluster.Name),
258268
Annotations: kcp.Spec.MachineTemplate.ObjectMeta.Annotations,
@@ -297,11 +307,7 @@ func (r *KubeadmControlPlaneReconciler) updateExternalObject(ctx context.Context
297307
return nil
298308
}
299309

300-
func (r *KubeadmControlPlaneReconciler) createMachine(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster, infraRef, bootstrapRef *corev1.ObjectReference, failureDomain *string) error {
301-
machine, err := r.computeDesiredMachine(kcp, cluster, infraRef, bootstrapRef, failureDomain, nil)
302-
if err != nil {
303-
return errors.Wrap(err, "failed to create Machine: failed to compute desired Machine")
304-
}
310+
func (r *KubeadmControlPlaneReconciler) createMachine(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane, machine *clusterv1.Machine) error {
305311
if err := ssa.Patch(ctx, r.Client, kcpManagerName, machine); err != nil {
306312
return errors.Wrap(err, "failed to create Machine")
307313
}
@@ -312,11 +318,7 @@ func (r *KubeadmControlPlaneReconciler) createMachine(ctx context.Context, kcp *
312318
}
313319

314320
func (r *KubeadmControlPlaneReconciler) updateMachine(ctx context.Context, machine *clusterv1.Machine, kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster) (*clusterv1.Machine, error) {
315-
updatedMachine, err := r.computeDesiredMachine(
316-
kcp, cluster,
317-
&machine.Spec.InfrastructureRef, machine.Spec.Bootstrap.ConfigRef,
318-
machine.Spec.FailureDomain, machine,
319-
)
321+
updatedMachine, err := r.computeDesiredMachine(kcp, cluster, machine.Spec.FailureDomain, machine)
320322
if err != nil {
321323
return nil, errors.Wrap(err, "failed to update Machine: failed to compute desired Machine")
322324
}
@@ -336,7 +338,7 @@ func (r *KubeadmControlPlaneReconciler) updateMachine(ctx context.Context, machi
336338
// There are small differences in how we calculate the Machine depending on if it
337339
// is a create or update. Example: for a new Machine we have to calculate a new name,
338340
// while for an existing Machine we have to use the name of the existing Machine.
339-
func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster, infraRef, bootstrapRef *corev1.ObjectReference, failureDomain *string, existingMachine *clusterv1.Machine) (*clusterv1.Machine, error) {
341+
func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster, failureDomain *string, existingMachine *clusterv1.Machine) (*clusterv1.Machine, error) {
340342
var machineName string
341343
var machineUID types.UID
342344
var version *string
@@ -399,13 +401,9 @@ func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev
399401
Annotations: map[string]string{},
400402
},
401403
Spec: clusterv1.MachineSpec{
402-
ClusterName: cluster.Name,
403-
Version: version,
404-
FailureDomain: failureDomain,
405-
InfrastructureRef: *infraRef,
406-
Bootstrap: clusterv1.Bootstrap{
407-
ConfigRef: bootstrapRef,
408-
},
404+
ClusterName: cluster.Name,
405+
Version: version,
406+
FailureDomain: failureDomain,
409407
},
410408
}
411409

@@ -431,5 +429,10 @@ func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev
431429
desiredMachine.Spec.NodeDeletionTimeout = kcp.Spec.MachineTemplate.NodeDeletionTimeout
432430
desiredMachine.Spec.NodeVolumeDetachTimeout = kcp.Spec.MachineTemplate.NodeVolumeDetachTimeout
433431

432+
if existingMachine != nil {
433+
desiredMachine.Spec.InfrastructureRef = existingMachine.Spec.InfrastructureRef
434+
desiredMachine.Spec.Bootstrap.ConfigRef = existingMachine.Spec.Bootstrap.ConfigRef
435+
}
436+
434437
return desiredMachine, nil
435438
}

controlplane/kubeadm/internal/controllers/helpers_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ func TestCloneConfigsAndGenerateMachine(t *testing.T) {
386386
g.Expect(infraObj.GetAnnotations()).To(HaveKeyWithValue(clusterv1.TemplateClonedFromGroupKindAnnotation, genericInfrastructureMachineTemplate.GroupVersionKind().GroupKind().String()))
387387

388388
g.Expect(m.Spec.InfrastructureRef.Namespace).To(Equal(cluster.Namespace))
389-
g.Expect(m.Spec.InfrastructureRef.Name).To(HavePrefix(genericInfrastructureMachineTemplate.GetName()))
389+
g.Expect(m.Spec.InfrastructureRef.Name).To(Equal(m.Name))
390390
g.Expect(m.Spec.InfrastructureRef.APIVersion).To(Equal(genericInfrastructureMachineTemplate.GetAPIVersion()))
391391
g.Expect(m.Spec.InfrastructureRef.Kind).To(Equal("GenericInfrastructureMachine"))
392392

393393
g.Expect(m.Spec.Bootstrap.ConfigRef.Namespace).To(Equal(cluster.Namespace))
394-
g.Expect(m.Spec.Bootstrap.ConfigRef.Name).To(HavePrefix(kcp.Name))
394+
g.Expect(m.Spec.Bootstrap.ConfigRef.Name).To(Equal(m.Name))
395395
g.Expect(m.Spec.Bootstrap.ConfigRef.APIVersion).To(Equal(bootstrapv1.GroupVersion.String()))
396396
g.Expect(m.Spec.Bootstrap.ConfigRef.Kind).To(Equal("KubeadmConfig"))
397397
}
@@ -528,18 +528,13 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
528528
failureDomain := pointer.String("fd1")
529529
createdMachine, err := (&KubeadmControlPlaneReconciler{}).computeDesiredMachine(
530530
kcp, cluster,
531-
infraRef, bootstrapRef,
532531
failureDomain, nil,
533532
)
534533
g.Expect(err).ToNot(HaveOccurred())
535534

536535
expectedMachineSpec := clusterv1.MachineSpec{
537-
ClusterName: cluster.Name,
538-
Version: pointer.String(kcp.Spec.Version),
539-
Bootstrap: clusterv1.Bootstrap{
540-
ConfigRef: bootstrapRef,
541-
},
542-
InfrastructureRef: *infraRef,
536+
ClusterName: cluster.Name,
537+
Version: pointer.String(kcp.Spec.Version),
543538
FailureDomain: failureDomain,
544539
NodeDrainTimeout: kcp.Spec.MachineTemplate.NodeDrainTimeout,
545540
NodeDeletionTimeout: kcp.Spec.MachineTemplate.NodeDeletionTimeout,
@@ -600,12 +595,15 @@ func TestKubeadmControlPlaneReconciler_computeDesiredMachine(t *testing.T) {
600595
NodeDrainTimeout: duration10s,
601596
NodeDeletionTimeout: duration10s,
602597
NodeVolumeDetachTimeout: duration10s,
598+
Bootstrap: clusterv1.Bootstrap{
599+
ConfigRef: bootstrapRef,
600+
},
601+
InfrastructureRef: *infraRef,
603602
},
604603
}
605604

606605
updatedMachine, err := (&KubeadmControlPlaneReconciler{}).computeDesiredMachine(
607606
kcp, cluster,
608-
infraRef, bootstrapRef,
609607
existingMachine.Spec.FailureDomain, existingMachine,
610608
)
611609
g.Expect(err).ToNot(HaveOccurred())
@@ -689,10 +687,10 @@ func TestKubeadmControlPlaneReconciler_generateKubeadmConfig(t *testing.T) {
689687
recorder: record.NewFakeRecorder(32),
690688
}
691689

692-
got, err := r.generateKubeadmConfig(ctx, kcp, cluster, spec.DeepCopy())
690+
got, err := r.generateKubeadmConfig(ctx, kcp, cluster, spec.DeepCopy(), "kubeadmconfig-name")
693691
g.Expect(err).ToNot(HaveOccurred())
694692
g.Expect(got).NotTo(BeNil())
695-
g.Expect(got.Name).To(HavePrefix(kcp.Name))
693+
g.Expect(got.Name).To(Equal("kubeadmconfig-name"))
696694
g.Expect(got.Namespace).To(Equal(kcp.Namespace))
697695
g.Expect(got.Kind).To(Equal(expectedReferenceKind))
698696
g.Expect(got.APIVersion).To(Equal(expectedReferenceAPIVersion))

controlplane/kubeadm/internal/controllers/scale_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ func TestKubeadmControlPlaneReconciler_initializeControlPlane(t *testing.T) {
9494
g.Expect(machineList.Items[0].Name).To(HavePrefix(kcp.Name))
9595

9696
g.Expect(machineList.Items[0].Spec.InfrastructureRef.Namespace).To(Equal(cluster.Namespace))
97-
g.Expect(machineList.Items[0].Spec.InfrastructureRef.Name).To(HavePrefix(genericInfrastructureMachineTemplate.GetName()))
97+
g.Expect(machineList.Items[0].Spec.InfrastructureRef.Name).To(Equal(machineList.Items[0].Name))
9898
g.Expect(machineList.Items[0].Spec.InfrastructureRef.APIVersion).To(Equal(genericInfrastructureMachineTemplate.GetAPIVersion()))
9999
g.Expect(machineList.Items[0].Spec.InfrastructureRef.Kind).To(Equal("GenericInfrastructureMachine"))
100100

101101
g.Expect(machineList.Items[0].Spec.Bootstrap.ConfigRef.Namespace).To(Equal(cluster.Namespace))
102-
g.Expect(machineList.Items[0].Spec.Bootstrap.ConfigRef.Name).To(HavePrefix(kcp.Name))
102+
g.Expect(machineList.Items[0].Spec.Bootstrap.ConfigRef.Name).To(Equal(machineList.Items[0].Name))
103103
g.Expect(machineList.Items[0].Spec.Bootstrap.ConfigRef.APIVersion).To(Equal(bootstrapv1.GroupVersion.String()))
104104
g.Expect(machineList.Items[0].Spec.Bootstrap.ConfigRef.Kind).To(Equal("KubeadmConfig"))
105105
}

exp/internal/controllers/machinepool_controller_phases.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3030
kerrors "k8s.io/apimachinery/pkg/util/errors"
3131
"k8s.io/apimachinery/pkg/util/wait"
32-
"k8s.io/apiserver/pkg/storage/names"
3332
"k8s.io/klog/v2"
3433
"k8s.io/utils/pointer"
3534
ctrl "sigs.k8s.io/controller-runtime"
@@ -443,7 +442,7 @@ func computeDesiredMachine(mp *expv1.MachinePool, infraMachine *unstructured.Uns
443442

444443
machine := &clusterv1.Machine{
445444
ObjectMeta: metav1.ObjectMeta{
446-
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("%s-", mp.Name)),
445+
Name: infraMachine.GetName(),
447446
// Note: by setting the ownerRef on creation we signal to the Machine controller that this is not a stand-alone Machine.
448447
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(mp, machinePoolKind)},
449448
Namespace: mp.Namespace,

internal/controllers/machineset/machineset_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ func (r *Reconciler) syncReplicas(ctx context.Context, cluster *clusterv1.Cluste
475475
Client: r.UnstructuredCachingClient,
476476
TemplateRef: ms.Spec.Template.Spec.Bootstrap.ConfigRef,
477477
Namespace: machine.Namespace,
478+
Name: machine.Name,
478479
ClusterName: machine.Spec.ClusterName,
479480
Labels: machine.Labels,
480481
Annotations: machine.Annotations,
@@ -500,6 +501,7 @@ func (r *Reconciler) syncReplicas(ctx context.Context, cluster *clusterv1.Cluste
500501
Client: r.UnstructuredCachingClient,
501502
TemplateRef: &ms.Spec.Template.Spec.InfrastructureRef,
502503
Namespace: machine.Namespace,
504+
Name: machine.Name,
503505
ClusterName: machine.Spec.ClusterName,
504506
Labels: machine.Labels,
505507
Annotations: machine.Annotations,

0 commit comments

Comments
 (0)