diff --git a/apis/v1beta1/vspheremachine_types.go b/apis/v1beta1/vspheremachine_types.go index fb9a58493..75293248c 100644 --- a/apis/v1beta1/vspheremachine_types.go +++ b/apis/v1beta1/vspheremachine_types.go @@ -80,6 +80,7 @@ type VSphereVMNamingStrategy struct { // 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"` diff --git a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml index f09c4e69f..7493370fd 100644 --- a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml +++ b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml @@ -1036,6 +1036,7 @@ spec: 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"` diff --git a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml index 49ccbb054..3070a366f 100644 --- a/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml +++ b/config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml @@ -906,6 +906,7 @@ spec: 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"` diff --git a/pkg/services/vimmachine.go b/pkg/services/vimmachine.go index 81236bcf4..025089477 100644 --- a/pkg/services/vimmachine.go +++ b/pkg/services/vimmachine.go @@ -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" @@ -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. @@ -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 {