Skip to content

Commit 61f19f1

Browse files
Adding MachineNamingStrategy in KubeadmControlPlane
Signed-off-by: Muhammad Adil Ghaffar <[email protected]>
1 parent 8df788d commit 61f19f1

File tree

11 files changed

+331
-130
lines changed

11 files changed

+331
-130
lines changed

controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ type KubeadmControlPlaneSpec struct {
123123
// The RemediationStrategy that controls how control plane machine remediation happens.
124124
// +optional
125125
RemediationStrategy *RemediationStrategy `json:"remediationStrategy,omitempty"`
126+
127+
// MachineNamingStrategy allows changing the naming pattern used when creating Machines.
128+
// InfraMachines & KubeadmConfigs will use the same name as the corresponding Machines.
129+
// +optional
130+
MachineNamingStrategy *MachineNamingStrategy `json:"machineNamingStrategy,omitempty"`
126131
}
127132

128133
// KubeadmControlPlaneMachineTemplate defines the template for Machines
@@ -234,6 +239,20 @@ type RemediationStrategy struct {
234239
MinHealthyPeriod *metav1.Duration `json:"minHealthyPeriod,omitempty"`
235240
}
236241

242+
// MachineNamingStrategy allows changing the naming pattern used when creating Machines.
243+
// InfraMachines & KubeadmConfigs will use the same name as the corresponding Machines.
244+
type MachineNamingStrategy struct {
245+
// Template defines the template to use for generating the names of the Machine objects.
246+
// If not defined, it will fallback to `{{ .kubeadmcontrolplane.name }}-{{ .random }}`.
247+
// If the templated string exceeds 63 characters, it will be trimmed to 58 characters and will
248+
// get concatenated with a random suffix of length 5.
249+
// The templating mechanism provides the following arguments:
250+
// * `.kubeadmcontrolplane.name`: The name of the KubeadmControlPlane object.
251+
// * `.random`: A random alphanumeric string, without vowels, of length 5.
252+
// +optional
253+
Template string `json:"template,omitempty"`
254+
}
255+
237256
// KubeadmControlPlaneStatus defines the observed state of KubeadmControlPlane.
238257
type KubeadmControlPlaneStatus struct {
239258
// Selector is the label selector in string format to avoid introspection

controlplane/kubeadm/api/v1beta1/kubeadmcontrolplanetemplate_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ type KubeadmControlPlaneTemplateResourceSpec struct {
101101
// The RemediationStrategy that controls how control plane machine remediation happens.
102102
// +optional
103103
RemediationStrategy *RemediationStrategy `json:"remediationStrategy,omitempty"`
104+
105+
// MachineNamingStrategy allows changing the naming pattern used when creating Machines.
106+
// InfraMachines & KubeadmConfigs will use the same name as the corresponding Machines.
107+
// +optional
108+
MachineNamingStrategy *MachineNamingStrategy `json:"machineNamingStrategy,omitempty"`
104109
}
105110

106111
// KubeadmControlPlaneTemplateMachineTemplate defines the template for Machines

controlplane/kubeadm/api/v1beta1/zz_generated.deepcopy.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanes.yaml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/config/crd/bases/controlplane.cluster.x-k8s.io_kubeadmcontrolplanetemplates.yaml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/internal/controllers/helpers.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sigs.k8s.io/cluster-api/controllers/external"
3838
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
3939
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
40+
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
4041
"sigs.k8s.io/cluster-api/internal/util/ssa"
4142
"sigs.k8s.io/cluster-api/util"
4243
"sigs.k8s.io/cluster-api/util/certs"
@@ -184,6 +185,7 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
184185
if r.DeprecatedInfraMachineNaming {
185186
infraMachineName = names.SimpleNameGenerator.GenerateName(kcp.Spec.MachineTemplate.InfrastructureRef.Name + "-")
186187
}
188+
187189
// Clone the infrastructure template
188190
infraRef, err := external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
189191
Client: r.Client,
@@ -349,7 +351,15 @@ func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev
349351
annotations := map[string]string{}
350352
if existingMachine == nil {
351353
// Creating a new machine
352-
machineName = names.SimpleNameGenerator.GenerateName(kcp.Name + "-")
354+
nameTemplate := "{{ .kubeadmcontrolplane.name }}-{{ .random }}"
355+
if kcp.Spec.MachineNamingStrategy != nil && kcp.Spec.MachineNamingStrategy.Template != "" {
356+
nameTemplate = kcp.Spec.MachineNamingStrategy.Template
357+
}
358+
generatedMachineName, err := topologynames.KCPMachineNameGenerator(nameTemplate, kcp.Name).GenerateName()
359+
if err != nil {
360+
return nil, errors.Wrap(err, "failed to generate name for KCP machine")
361+
}
362+
machineName = generatedMachineName
353363
version = &kcp.Spec.Version
354364

355365
// Machine's bootstrap config may be missing ClusterConfiguration if it is not the first machine in the control plane.

0 commit comments

Comments
 (0)