Skip to content

Commit

Permalink
Add NamingStrategy to MachineDeployment
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Adil Ghaffar <[email protected]>
  • Loading branch information
adilGhaffarDev committed Jan 8, 2025
1 parent 2c7219e commit 49ebdf5
Show file tree
Hide file tree
Showing 25 changed files with 600 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ linters-settings:
# CAPI utils
- pkg: sigs.k8s.io/cluster-api/util/conditions/v1beta2
alias: v1beta2conditions
- pkg: sigs.k8s.io/cluster-api/internal/topology/names
alias: topologynames
# CAPD
- pkg: sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1alpha3
alias: infrav1alpha3
Expand Down
22 changes: 22 additions & 0 deletions api/v1beta1/machinedeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ type MachineDeploymentSpec struct {
// +optional
Strategy *MachineDeploymentStrategy `json:"strategy,omitempty"`

// machineNamingStrategy allows changing the naming pattern used when creating Machines.
// Note: InfraMachines & BootstrapConfigs will use the same name as the corresponding Machines.
// +optional
MachineNamingStrategy *MachineNamingStrategy `json:"machineNamingStrategy,omitempty"`

// minReadySeconds is the minimum number of seconds for which a Node for a newly created machine should be ready before considering the replica available.
// Defaults to 0 (machine will be considered available as soon as the Node is ready)
// +optional
Expand Down Expand Up @@ -412,6 +417,23 @@ type RemediationStrategy struct {

// ANCHOR_END: RemediationStrategy

// MachineNamingStrategy allows changing the naming pattern used when creating Machines.
// Note: InfraMachines will use the same name as the corresponding Machines.
type MachineNamingStrategy struct {
// Template defines the template to use for generating the names of the Machine objects.
// If not defined, it will fallback to `{{ .machineSet.name }}-{{ .random }}`.
// If the generated name string exceeds 63 characters, it will be trimmed to 58 characters and will
// get concatenated with a random suffix of length 5.
// Length of the template string must not exceed 256 characters.
// The template allows the following variables `.cluster.name`, `.machineSet.name` and `.random`.
// The variable `.cluster.name` retrieves the name of the cluster object that owns the Machines being created.
// The variable `.machineSet.name` retrieves the name of the MachineSet object that owns the Machines being created.
// The variable `.random` is substituted with random alphanumeric string, without vowels, of length 5.
// +optional
// +kubebuilder:validation:MaxLength=256
Template string `json:"template,omitempty"`
}

// ANCHOR: MachineDeploymentStatus

// MachineDeploymentStatus defines the observed state of MachineDeployment.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/machineset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ type MachineSetSpec struct {
// Object references to custom resources are treated as templates.
// +optional
Template MachineTemplateSpec `json:"template,omitempty"`

// machineNamingStrategy allows changing the naming pattern used when creating Machines.
// Note: InfraMachines & BootstrapConfigs will use the same name as the corresponding Machines.
// +optional
MachineNamingStrategy *MachineNamingStrategy `json:"machineNamingStrategy,omitempty"`
}

// MachineSet's ScalingUp condition and corresponding reasons that will be used in v1Beta2 API version.
Expand Down
25 changes: 25 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions api/v1beta1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions config/crd/bases/cluster.x-k8s.io_machinedeployments.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions config/crd/bases/cluster.x-k8s.io_machinesets.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/topology/names"
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
"sigs.k8s.io/cluster-api/internal/util/kubeadm"
"sigs.k8s.io/cluster-api/util/container"
"sigs.k8s.io/cluster-api/util/version"
Expand Down Expand Up @@ -475,7 +475,7 @@ func validateNamingStrategy(machineNamingStrategy *controlplanev1.MachineNamingS
))
return allErrs
}
name, err := names.KCPMachineNameGenerator(machineNamingStrategy.Template, "cluster", "kubeadmcontrolplane").GenerateName()
name, err := topologynames.KCPMachineNameGenerator(machineNamingStrategy.Template, "cluster", "kubeadmcontrolplane").GenerateName()
if err != nil {
allErrs = append(allErrs,
field.Invalid(
Expand Down
10 changes: 5 additions & 5 deletions exp/topology/desiredstate/desired_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import (
"sigs.k8s.io/cluster-api/internal/hooks"
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
"sigs.k8s.io/cluster-api/internal/topology/clustershim"
"sigs.k8s.io/cluster-api/internal/topology/names"
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
"sigs.k8s.io/cluster-api/internal/topology/ownerrefs"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/test/builder"
Expand Down Expand Up @@ -2689,7 +2689,7 @@ func TestTemplateToObject(t *testing.T) {
template: template,
templateClonedFromRef: fakeRef1,
cluster: cluster,
nameGenerator: names.SimpleNameGenerator(cluster.Name),
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
currentObjectRef: nil,
})
g.Expect(err).ToNot(HaveOccurred())
Expand All @@ -2709,7 +2709,7 @@ func TestTemplateToObject(t *testing.T) {
template: template,
templateClonedFromRef: fakeRef1,
cluster: cluster,
nameGenerator: names.SimpleNameGenerator(cluster.Name),
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
currentObjectRef: fakeRef2,
})
g.Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -2750,7 +2750,7 @@ func TestTemplateToTemplate(t *testing.T) {
template: template,
templateClonedFromRef: fakeRef1,
cluster: cluster,
nameGenerator: names.SimpleNameGenerator(cluster.Name),
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
currentObjectRef: nil,
})
g.Expect(err).ToNot(HaveOccurred())
Expand All @@ -2769,7 +2769,7 @@ func TestTemplateToTemplate(t *testing.T) {
template: template,
templateClonedFromRef: fakeRef1,
cluster: cluster,
nameGenerator: names.SimpleNameGenerator(cluster.Name),
nameGenerator: topologynames.SimpleNameGenerator(cluster.Name),
currentObjectRef: fakeRef2,
})
g.Expect(err).ToNot(HaveOccurred())
Expand Down
7 changes: 7 additions & 0 deletions internal/apis/core/v1alpha3/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ func (src *MachineSet) ConvertTo(dstRaw conversion.Hub) error {
dst.Status.Conditions = restored.Status.Conditions
dst.Status.V1Beta2 = restored.Status.V1Beta2

if restored.Spec.MachineNamingStrategy != nil {
dst.Spec.MachineNamingStrategy = restored.Spec.MachineNamingStrategy
}
return nil
}

Expand Down Expand Up @@ -209,6 +212,10 @@ func (src *MachineDeployment) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Strategy.Remediation = restored.Spec.Strategy.Remediation
}

if restored.Spec.MachineNamingStrategy != nil {
dst.Spec.MachineNamingStrategy = restored.Spec.MachineNamingStrategy
}

dst.Spec.Template.Spec.ReadinessGates = restored.Spec.Template.Spec.ReadinessGates
dst.Spec.Template.Spec.NodeDeletionTimeout = restored.Spec.Template.Spec.NodeDeletionTimeout
dst.Spec.Template.Spec.NodeVolumeDetachTimeout = restored.Spec.Template.Spec.NodeVolumeDetachTimeout
Expand Down
2 changes: 2 additions & 0 deletions internal/apis/core/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions internal/apis/core/v1alpha4/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func (src *MachineSet) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Template.Spec.NodeVolumeDetachTimeout = restored.Spec.Template.Spec.NodeVolumeDetachTimeout
dst.Status.V1Beta2 = restored.Status.V1Beta2

if restored.Spec.MachineNamingStrategy != nil {
dst.Spec.MachineNamingStrategy = restored.Spec.MachineNamingStrategy
}

return nil
}

Expand Down Expand Up @@ -293,6 +297,11 @@ func (src *MachineDeployment) ConvertTo(dstRaw conversion.Hub) error {
}
dst.Spec.Strategy.Remediation = restored.Spec.Strategy.Remediation
}

if restored.Spec.MachineNamingStrategy != nil {
dst.Spec.MachineNamingStrategy = restored.Spec.MachineNamingStrategy
}

dst.Status.V1Beta2 = restored.Status.V1Beta2

return nil
Expand Down
2 changes: 2 additions & 0 deletions internal/apis/core/v1alpha4/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 49ebdf5

Please sign in to comment.