Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Add NamingStrategy to MachineDeployment #11172

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions api/v1beta1/machinedeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ type MachineDeploymentSpec struct {
// +optional
Strategy *MachineDeploymentStrategy `json:"strategy,omitempty"`

// machineNamingStrategy allows changing the naming pattern used when creating Machines.
// Note: InfraMachines 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 @@ -393,6 +398,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 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.

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.

Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,11 @@ func (r *Reconciler) reconcile(ctx context.Context, s *scope) error {
return errors.Wrapf(err, "failed to apply %s label to MachineSet %q", clusterv1.MachineDeploymentNameLabel, machineSet.Name)
}
machineSet.Labels[clusterv1.MachineDeploymentNameLabel] = md.Name
if machineSet.Spec.MachineNamingStrategy == nil && md.Spec.MachineNamingStrategy != nil {
machineSet.Spec.MachineNamingStrategy.Template = md.Spec.MachineNamingStrategy.Template
}
if err := helper.Patch(ctx, machineSet); err != nil {
return errors.Wrapf(err, "failed to apply %s label to MachineSet %q", clusterv1.MachineDeploymentNameLabel, machineSet.Name)
return errors.Wrapf(err, "failed to apply %s label and %s machineNamingStrategy template to MachineSet %q", clusterv1.MachineDeploymentNameLabel, md.Spec.MachineNamingStrategy.Template, machineSet.Name)
}
}

Expand Down
Loading