Skip to content

Commit ed30520

Browse files
authored
✨ add VM naming strategy for VMs in govmomi mode (#3286)
* ✨ add VM naming strategy for VMs in govami mode - Add VM naming stragey for vsphere vms and VMs in govami mode * rename the type to VSphereVMNamingStrategy - Rename the type to VSphereVMNamingStrategy and update docs, function name etc * add support for a random function * remove random function support * add webhook validation and tests * fix lint issues * add sprig package as depedency * add slim sprig package Add a slim sprig package to avoid additional dependencies and move the common logic to util package
1 parent d7af9d7 commit ed30520

21 files changed

+518
-80
lines changed

apis/v1alpha3/vspheremachine_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (src *VSphereMachine) ConvertTo(dstRaw conversion.Hub) error {
4040
dst.Spec.TagIDs = restored.Spec.TagIDs
4141
dst.Spec.PowerOffMode = restored.Spec.PowerOffMode
4242
dst.Spec.GuestSoftPowerOffTimeout = restored.Spec.GuestSoftPowerOffTimeout
43+
dst.Spec.NamingStrategy = restored.Spec.NamingStrategy
4344
for i := range dst.Spec.Network.Devices {
4445
dst.Spec.Network.Devices[i].AddressesFromPools = restored.Spec.Network.Devices[i].AddressesFromPools
4546
dst.Spec.Network.Devices[i].DHCP4Overrides = restored.Spec.Network.Devices[i].DHCP4Overrides

apis/v1alpha3/vspheremachinetemplate_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (src *VSphereMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
4343
dst.Spec.Template.Spec.AdditionalDisksGiB = restored.Spec.Template.Spec.AdditionalDisksGiB
4444
dst.Spec.Template.Spec.PowerOffMode = restored.Spec.Template.Spec.PowerOffMode
4545
dst.Spec.Template.Spec.GuestSoftPowerOffTimeout = restored.Spec.Template.Spec.GuestSoftPowerOffTimeout
46+
dst.Spec.Template.Spec.NamingStrategy = restored.Spec.Template.Spec.NamingStrategy
4647
for i := range dst.Spec.Template.Spec.Network.Devices {
4748
dst.Spec.Template.Spec.Network.Devices[i].AddressesFromPools = restored.Spec.Template.Spec.Network.Devices[i].AddressesFromPools
4849
dst.Spec.Template.Spec.Network.Devices[i].DHCP4Overrides = restored.Spec.Template.Spec.Network.Devices[i].DHCP4Overrides

apis/v1alpha3/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/v1alpha4/vspheremachine_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (src *VSphereMachine) ConvertTo(dstRaw conversion.Hub) error {
4040
dst.Spec.TagIDs = restored.Spec.TagIDs
4141
dst.Spec.PowerOffMode = restored.Spec.PowerOffMode
4242
dst.Spec.GuestSoftPowerOffTimeout = restored.Spec.GuestSoftPowerOffTimeout
43+
dst.Spec.NamingStrategy = restored.Spec.NamingStrategy
4344
for i := range dst.Spec.Network.Devices {
4445
dst.Spec.Network.Devices[i].AddressesFromPools = restored.Spec.Network.Devices[i].AddressesFromPools
4546
dst.Spec.Network.Devices[i].DHCP4Overrides = restored.Spec.Network.Devices[i].DHCP4Overrides

apis/v1alpha4/vspheremachinetemplate_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (src *VSphereMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
4343
dst.Spec.Template.Spec.AdditionalDisksGiB = restored.Spec.Template.Spec.AdditionalDisksGiB
4444
dst.Spec.Template.Spec.PowerOffMode = restored.Spec.Template.Spec.PowerOffMode
4545
dst.Spec.Template.Spec.GuestSoftPowerOffTimeout = restored.Spec.Template.Spec.GuestSoftPowerOffTimeout
46+
dst.Spec.Template.Spec.NamingStrategy = restored.Spec.Template.Spec.NamingStrategy
4647
for i := range dst.Spec.Template.Spec.Network.Devices {
4748
dst.Spec.Template.Spec.Network.Devices[i].AddressesFromPools = restored.Spec.Template.Spec.Network.Devices[i].AddressesFromPools
4849
dst.Spec.Template.Spec.Network.Devices[i].DHCP4Overrides = restored.Spec.Template.Spec.Network.Devices[i].DHCP4Overrides

apis/v1alpha4/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/v1beta1/vspheremachine_types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ type VSphereMachineSpec struct {
6868
//
6969
// +optional
7070
GuestSoftPowerOffTimeout *metav1.Duration `json:"guestSoftPowerOffTimeout,omitempty"`
71+
72+
// NamingStrategy allows configuring the naming strategy used when calculating the name of the VSphereVM.
73+
// +optional
74+
NamingStrategy *VSphereVMNamingStrategy `json:"namingStrategy,omitempty"`
75+
}
76+
77+
// VSphereVMNamingStrategy defines the naming strategy for the VSphereVMs.
78+
type VSphereVMNamingStrategy struct {
79+
// Template defines the template to use for generating the name of the VSphereVM object.
80+
// If not defined, it will fall back to `{{ .machine.name }}`.
81+
// The templating has the following data available:
82+
// * `.machine.name`: The name of the Machine object.
83+
// The templating also has the following funcs available:
84+
// * `trimSuffix`: same as strings.TrimSuffix
85+
// * `trunc`: truncates a string, e.g. `trunc 2 "hello"` or `trunc -2 "hello"`
86+
// Notes:
87+
// * While the template offers some flexibility, we would like the name to link to the Machine name
88+
// to ensure better user experience when troubleshooting
89+
// * Generated names must be valid Kubernetes names as they are used to create a VSphereVM object
90+
// and usually also as the name of the Node object.
91+
// * Names are automatically truncated at 63 characters. Please note that this can lead to name conflicts,
92+
// so we highly recommend to use a template which leads to a name shorter than 63 characters.
93+
// +optional
94+
Template *string `json:"template,omitempty"`
7195
}
7296

7397
// VSphereMachineStatus defines the observed state of VSphereMachine.

apis/v1beta1/zz_generated.deepcopy.go

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,28 @@ spec:
10511051
virtual machine is cloned.
10521052
format: int64
10531053
type: integer
1054+
namingStrategy:
1055+
description: NamingStrategy allows configuring the naming strategy
1056+
used when calculating the name of the VSphereVM.
1057+
properties:
1058+
template:
1059+
description: |-
1060+
Template defines the template to use for generating the name of the VSphereVM object.
1061+
If not defined, it will fall back to `{{ .machine.name }}`.
1062+
The templating has the following data available:
1063+
* `.machine.name`: The name of the Machine object.
1064+
The templating also has the following funcs available:
1065+
* `trimSuffix`: same as strings.TrimSuffix
1066+
* `trunc`: truncates a string, e.g. `trunc 2 "hello"` or `trunc -2 "hello"`
1067+
Notes:
1068+
* While the template offers some flexibility, we would like the name to link to the Machine name
1069+
to ensure better user experience when troubleshooting
1070+
* Generated names must be valid Kubernetes names as they are used to create a VSphereVM object
1071+
and usually also as the name of the Node object.
1072+
* Names are automatically truncated at 63 characters. Please note that this can lead to name conflicts,
1073+
so we highly recommend to use a template which leads to a name shorter than 63 characters.
1074+
type: string
1075+
type: object
10541076
network:
10551077
description: Network is the network configuration for this machine's
10561078
VM.

config/default/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,28 @@ spec:
921921
virtual machine is cloned.
922922
format: int64
923923
type: integer
924+
namingStrategy:
925+
description: NamingStrategy allows configuring the naming
926+
strategy used when calculating the name of the VSphereVM.
927+
properties:
928+
template:
929+
description: |-
930+
Template defines the template to use for generating the name of the VSphereVM object.
931+
If not defined, it will fall back to `{{ .machine.name }}`.
932+
The templating has the following data available:
933+
* `.machine.name`: The name of the Machine object.
934+
The templating also has the following funcs available:
935+
* `trimSuffix`: same as strings.TrimSuffix
936+
* `trunc`: truncates a string, e.g. `trunc 2 "hello"` or `trunc -2 "hello"`
937+
Notes:
938+
* While the template offers some flexibility, we would like the name to link to the Machine name
939+
to ensure better user experience when troubleshooting
940+
* Generated names must be valid Kubernetes names as they are used to create a VSphereVM object
941+
and usually also as the name of the Node object.
942+
* Names are automatically truncated at 63 characters. Please note that this can lead to name conflicts,
943+
so we highly recommend to use a template which leads to a name shorter than 63 characters.
944+
type: string
945+
type: object
924946
network:
925947
description: Network is the network configuration for this
926948
machine's VM.

0 commit comments

Comments
 (0)