Skip to content

Commit b60f1fd

Browse files
Add NamingStrategy to MachineDeployment
Signed-off-by: Muhammad Adil Ghaffar <[email protected]>
1 parent 5adf951 commit b60f1fd

26 files changed

+622
-75
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ linters-settings:
173173
# CAPI utils
174174
- pkg: sigs.k8s.io/cluster-api/util/conditions/v1beta2
175175
alias: v1beta2conditions
176+
- pkg: sigs.k8s.io/cluster-api/internal/topology/names
177+
alias: topologynames
176178
# CAPD
177179
- pkg: sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1alpha3
178180
alias: infrav1alpha3

api/v1beta1/machinedeployment_types.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ type MachineDeploymentSpec struct {
286286
// +optional
287287
Strategy *MachineDeploymentStrategy `json:"strategy,omitempty"`
288288

289+
// machineNamingStrategy allows changing the naming pattern used when creating Machines.
290+
// Note: InfraMachines & BootstrapConfigs will use the same name as the corresponding Machines.
291+
// +optional
292+
MachineNamingStrategy *MachineNamingStrategy `json:"machineNamingStrategy,omitempty"`
293+
289294
// minReadySeconds is the minimum number of seconds for which a Node for a newly created machine should be ready before considering the replica available.
290295
// Defaults to 0 (machine will be considered available as soon as the Node is ready)
291296
// +optional
@@ -412,6 +417,31 @@ type RemediationStrategy struct {
412417

413418
// ANCHOR_END: RemediationStrategy
414419

420+
// MachineNamingStrategy allows changing the naming pattern used when creating
421+
// Machines.
422+
// Note: InfraMachines will use the same name as the corresponding Machines.
423+
type MachineNamingStrategy struct {
424+
// Template defines the template to use for generating the names of the
425+
// Machine objects.
426+
// If not defined, it will fallback to `{{ .machineSet.name }}-{{ .random }}`.
427+
// If the generated name string exceeds 63 characters, it will be trimmed to
428+
// 58 characters and will
429+
// get concatenated with a random suffix of length 5.
430+
// Length of the template string must not exceed 256 characters.
431+
// The template allows the following variables `.cluster.name`,
432+
// `.machineSet.name` and `.random`.
433+
// The variable `.cluster.name` retrieves the name of the cluster object
434+
// that owns the Machines being created.
435+
// The variable `.machineSet.name` retrieves the name of the MachineSet
436+
// object that owns the Machines being created.
437+
// The variable `.random` is substituted with random alphanumeric string,
438+
// without vowels, of length 5. This variable is required part of the
439+
// template. If not provided, validation will fail.
440+
// +optional
441+
// +kubebuilder:validation:MaxLength=256
442+
Template string `json:"template,omitempty"`
443+
}
444+
415445
// ANCHOR: MachineDeploymentStatus
416446

417447
// MachineDeploymentStatus defines the observed state of MachineDeployment.

api/v1beta1/machineset_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ type MachineSetSpec struct {
8585
// Object references to custom resources are treated as templates.
8686
// +optional
8787
Template MachineTemplateSpec `json:"template,omitempty"`
88+
89+
// machineNamingStrategy allows changing the naming pattern used when creating Machines.
90+
// Note: InfraMachines & BootstrapConfigs will use the same name as the corresponding Machines.
91+
// +optional
92+
MachineNamingStrategy *MachineNamingStrategy `json:"machineNamingStrategy,omitempty"`
8893
}
8994

9095
// MachineSet's ScalingUp condition and corresponding reasons that will be used in v1Beta2 API version.

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.

api/v1beta1/zz_generated.openapi.go

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

config/crd/bases/cluster.x-k8s.io_machinedeployments.yaml

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

config/crd/bases/cluster.x-k8s.io_machinesets.yaml

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

controlplane/kubeadm/internal/webhooks/kubeadm_control_plane.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3939
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
4040
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
41-
"sigs.k8s.io/cluster-api/internal/topology/names"
41+
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
4242
"sigs.k8s.io/cluster-api/internal/util/kubeadm"
4343
"sigs.k8s.io/cluster-api/util/container"
4444
"sigs.k8s.io/cluster-api/util/version"
@@ -475,7 +475,7 @@ func validateNamingStrategy(machineNamingStrategy *controlplanev1.MachineNamingS
475475
))
476476
return allErrs
477477
}
478-
name, err := names.KCPMachineNameGenerator(machineNamingStrategy.Template, "cluster", "kubeadmcontrolplane").GenerateName()
478+
name, err := topologynames.KCPMachineNameGenerator(machineNamingStrategy.Template, "cluster", "kubeadmcontrolplane").GenerateName()
479479
if err != nil {
480480
allErrs = append(allErrs,
481481
field.Invalid(

controlplane/kubeadm/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func InitFlags(fs *pflag.FlagSet) {
188188

189189
fs.BoolVar(&useDeprecatedInfraMachineNaming, "use-deprecated-infra-machine-naming", false,
190190
"Use the deprecated naming convention for infra machines where they are named after the InfraMachineTemplate.")
191-
_ = fs.MarkDeprecated("use-deprecated-infra-machine-naming", "This flag will be removed in v1.9.")
191+
_ = fs.MarkDeprecated("use-deprecated-infra-machine-naming", "This flag will be removed in v1.10.")
192192

193193
flags.AddManagerOptions(fs, &managerOptions)
194194

exp/topology/desiredstate/desired_state_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646
"sigs.k8s.io/cluster-api/internal/hooks"
4747
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
4848
"sigs.k8s.io/cluster-api/internal/topology/clustershim"
49-
"sigs.k8s.io/cluster-api/internal/topology/names"
49+
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
5050
"sigs.k8s.io/cluster-api/internal/topology/ownerrefs"
5151
"sigs.k8s.io/cluster-api/util"
5252
"sigs.k8s.io/cluster-api/util/test/builder"
@@ -2689,7 +2689,7 @@ func TestTemplateToObject(t *testing.T) {
26892689
template: template,
26902690
templateClonedFromRef: fakeRef1,
26912691
cluster: cluster,
2692-
nameGenerator: names.SimpleNameGenerator(cluster.Name),
2692+
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
26932693
currentObjectRef: nil,
26942694
})
26952695
g.Expect(err).ToNot(HaveOccurred())
@@ -2709,7 +2709,7 @@ func TestTemplateToObject(t *testing.T) {
27092709
template: template,
27102710
templateClonedFromRef: fakeRef1,
27112711
cluster: cluster,
2712-
nameGenerator: names.SimpleNameGenerator(cluster.Name),
2712+
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
27132713
currentObjectRef: fakeRef2,
27142714
})
27152715
g.Expect(err).ToNot(HaveOccurred())
@@ -2750,7 +2750,7 @@ func TestTemplateToTemplate(t *testing.T) {
27502750
template: template,
27512751
templateClonedFromRef: fakeRef1,
27522752
cluster: cluster,
2753-
nameGenerator: names.SimpleNameGenerator(cluster.Name),
2753+
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
27542754
currentObjectRef: nil,
27552755
})
27562756
g.Expect(err).ToNot(HaveOccurred())
@@ -2769,7 +2769,7 @@ func TestTemplateToTemplate(t *testing.T) {
27692769
template: template,
27702770
templateClonedFromRef: fakeRef1,
27712771
cluster: cluster,
2772-
nameGenerator: names.SimpleNameGenerator(cluster.Name),
2772+
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
27732773
currentObjectRef: fakeRef2,
27742774
})
27752775
g.Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)