Skip to content

Commit

Permalink
add support for a random function
Browse files Browse the repository at this point in the history
  • Loading branch information
viveksyngh committed Dec 11, 2024
1 parent 782c264 commit bd815fd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
7 changes: 4 additions & 3 deletions apis/v1beta1/vspheremachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,25 @@ type VSphereMachineSpec struct {
// +optional
GuestSoftPowerOffTimeout *metav1.Duration `json:"guestSoftPowerOffTimeout,omitempty"`

// NamingStrategy allows configuring the naming strategy used when calculating the name of the VSphereVMs.
// NamingStrategy allows configuring the naming strategy used when calculating the name of the VSphereVM.
// +optional
NamingStrategy *VSphereVMNamingStrategy `json:"namingStrategy,omitempty"`
}

// VSphereVMNamingStrategy defines the naming strategy for the VSphereVMs.
type VSphereVMNamingStrategy struct {
// Template defines the template to use for generating the name of the VSphereVMs object.
// Template defines the template to use for generating the name of the VSphereVM object.
// If not defined, it will fall back to `{{ .machine.name }}`.
// The templating has the following data available:
// * `.machine.name`: The name of the Machine object.
// * `.random`: random alphanumeric string, without vowels, of length 5
// The templating also has the following funcs available:
// * `trimSuffix`: same as strings.TrimSuffix
// * `trunc`: truncates a string, e.g. `trunc 2 "hello"` or `trunc -2 "hello"`
// Notes:
// * While the template offers some flexibility, we would like the name to link to the Machine name
// to ensure better user experience when troubleshooting
// * Generated names must be valid Kubernetes names as they are used to create a VSphereVMs object
// * Generated names must be valid Kubernetes names as they are used to create a VSphereVM object
// and usually also as the name of the Node object.
// * Names are automatically truncated at 63 characters. Please note that this can lead to name conflicts,
// so we highly recommend to use a template which leads to a name shorter than 63 characters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,21 +1028,22 @@ spec:
type: integer
namingStrategy:
description: NamingStrategy allows configuring the naming strategy
used when calculating the name of the VSphereVMs.
used when calculating the name of the VSphereVM.
properties:
template:
description: |-
Template defines the template to use for generating the name of the VSphereVMs object.
Template defines the template to use for generating the name of the VSphereVM object.
If not defined, it will fall back to `{{ .machine.name }}`.
The templating has the following data available:
* `.machine.name`: The name of the Machine object.
* `.random`: random alphanumeric string, without vowels, of length 5
The templating also has the following funcs available:
* `trimSuffix`: same as strings.TrimSuffix
* `trunc`: truncates a string, e.g. `trunc 2 "hello"` or `trunc -2 "hello"`
Notes:
* While the template offers some flexibility, we would like the name to link to the Machine name
to ensure better user experience when troubleshooting
* Generated names must be valid Kubernetes names as they are used to create a VSphereVMs object
* Generated names must be valid Kubernetes names as they are used to create a VSphereVM object
and usually also as the name of the Node object.
* Names are automatically truncated at 63 characters. Please note that this can lead to name conflicts,
so we highly recommend to use a template which leads to a name shorter than 63 characters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,21 +898,22 @@ spec:
type: integer
namingStrategy:
description: NamingStrategy allows configuring the naming
strategy used when calculating the name of the VSphereVMs.
strategy used when calculating the name of the VSphereVM.
properties:
template:
description: |-
Template defines the template to use for generating the name of the VSphereVMs object.
Template defines the template to use for generating the name of the VSphereVM object.
If not defined, it will fall back to `{{ .machine.name }}`.
The templating has the following data available:
* `.machine.name`: The name of the Machine object.
* `.random`: random alphanumeric string, without vowels, of length 5
The templating also has the following funcs available:
* `trimSuffix`: same as strings.TrimSuffix
* `trunc`: truncates a string, e.g. `trunc 2 "hello"` or `trunc -2 "hello"`
Notes:
* While the template offers some flexibility, we would like the name to link to the Machine name
to ensure better user experience when troubleshooting
* Generated names must be valid Kubernetes names as they are used to create a VSphereVMs object
* Generated names must be valid Kubernetes names as they are used to create a VSphereVM object
and usually also as the name of the Node object.
* Names are automatically truncated at 63 characters. Please note that this can lead to name conflicts,
so we highly recommend to use a template which leads to a name shorter than 63 characters.
Expand Down
3 changes: 3 additions & 0 deletions pkg/services/vimmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

utilrand "k8s.io/apimachinery/pkg/util/rand"
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
capvcontext "sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
infrautilv1 "sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
Expand Down Expand Up @@ -424,6 +425,7 @@ func generateVMObjectName(vimMachineCtx *capvcontext.VIMMachineContext, machineN

const (
maxNameLength = 63
randomLength = 5
)

// Note: Inlining these functions from sprig to avoid introducing a dependency.
Expand Down Expand Up @@ -456,6 +458,7 @@ func GenerateVSphereVMName(machineName string, namingStrategy *infrav1.VSphereVM
"name": machineName,
},
}
data["random"] = utilrand.String(randomLength)

tpl, err := nameTpl.Parse(nameTemplate)
if err != nil {
Expand Down

0 comments on commit bd815fd

Please sign in to comment.