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 275cf76
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions apis/v1beta1/vspheremachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
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 275cf76

Please sign in to comment.