Skip to content

Commit e4407bb

Browse files
Adding MachineNamingStrategy in KubeadmControlPlane
Signed-off-by: Muhammad Adil Ghaffar <[email protected]>
1 parent ec6916b commit e4407bb

14 files changed

+498
-156
lines changed

controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_types.go

+22
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,23 @@ 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 generated name string exceeds 63 characters, it will be trimmed to 58 characters and will
248+
// get concatenated with a random suffix of length 5.
249+
// Length of the template string must not exceed 256 characters.
250+
// The template allows the following variables `.cluster.name`, `.kubeadmControlPlane.name` and `.random`.
251+
// The variable `.cluster.name` retrieves the name of the cluster object that owns the Machines being created.
252+
// The variable `.kubeadmControlPlane.name` retrieves the name of the KubeadmControlPlane object that owns the Machines being created.
253+
// The variable `.random` is substituted with random alphanumeric string, without vowels, of length 5.
254+
// +optional
255+
// +kubebuilder:validation:MaxLength=256
256+
Template string `json:"template,omitempty"`
257+
}
258+
237259
// KubeadmControlPlaneStatus defines the observed state of KubeadmControlPlane.
238260
type KubeadmControlPlaneStatus struct {
239261
// Selector is the label selector in string format to avoid introspection

controlplane/kubeadm/api/v1beta1/kubeadmcontrolplanetemplate_types.go

+5
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

+25
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

+19
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

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controlplane/kubeadm/internal/controllers/helpers.go

+14-1
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"
@@ -197,6 +198,7 @@ func (r *KubeadmControlPlaneReconciler) cloneConfigsAndGenerateMachine(ctx conte
197198
if r.DeprecatedInfraMachineNaming {
198199
infraMachineName = names.SimpleNameGenerator.GenerateName(kcp.Spec.MachineTemplate.InfrastructureRef.Name + "-")
199200
}
201+
200202
// Clone the infrastructure template
201203
infraRef, err := external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
202204
Client: r.Client,
@@ -362,7 +364,18 @@ func (r *KubeadmControlPlaneReconciler) computeDesiredMachine(kcp *controlplanev
362364
annotations := map[string]string{}
363365
if existingMachine == nil {
364366
// Creating a new machine
365-
machineName = names.SimpleNameGenerator.GenerateName(kcp.Name + "-")
367+
nameTemplate := "{{ .kubeadmControlPlane.name }}-{{ .random }}"
368+
if kcp.Spec.MachineNamingStrategy != nil && kcp.Spec.MachineNamingStrategy.Template != "" {
369+
nameTemplate = kcp.Spec.MachineNamingStrategy.Template
370+
if !strings.Contains(nameTemplate, "{{ .random }}") {
371+
return nil, errors.New("cannot generate KCP machine name: {{ .random }} is missing in machineNamingStrategy.template")
372+
}
373+
}
374+
generatedMachineName, err := topologynames.KCPMachineNameGenerator(nameTemplate, cluster.Name, kcp.Name).GenerateName()
375+
if err != nil {
376+
return nil, errors.Wrap(err, "failed to generate name for KCP Machine")
377+
}
378+
machineName = generatedMachineName
366379
version = &kcp.Spec.Version
367380

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

0 commit comments

Comments
 (0)