From ab9fc6c9fe66c4955709a12fdb14d4887f2f27ec Mon Sep 17 00:00:00 2001 From: Daniel Odvarka Date: Sat, 22 Jul 2023 14:18:12 +0200 Subject: [PATCH] Add guestinfo.hostname to config of a VM --- pkg/services/govmomi/extra/config.go | 22 +++++++++++++ .../govmomi/extra/extra_suite_test.go | 31 +++++++++++++++++++ pkg/services/govmomi/service.go | 4 +++ pkg/services/vimmachine.go | 4 +++ 4 files changed, 61 insertions(+) diff --git a/pkg/services/govmomi/extra/config.go b/pkg/services/govmomi/extra/config.go index 48ea293ad5..ba49fa1d2e 100644 --- a/pkg/services/govmomi/extra/config.go +++ b/pkg/services/govmomi/extra/config.go @@ -20,6 +20,7 @@ import ( "encoding/base64" "github.com/vmware/govmomi/vim25/types" + "sigs.k8s.io/yaml" ) // Config is data used with a VM's guestInfo RPC interface. @@ -30,6 +31,7 @@ const ( guestInfoIgnitionEncoding = "guestinfo.ignition.config.data.encoding" guestInfoCloudInitData = "guestinfo.userdata" guestInfoCloudInitEncoding = "guestinfo.userdata.encoding" + guestInfoHostName = "guestinfo.hostname" ) // SetCustomVMXKeys sets the custom VMX keys as @@ -86,6 +88,26 @@ func (e *Config) setUserData(userdataKey, encodingKey string, data []byte) { ) } +// SetCloudInitHostName sets the cloud init hostname at the key +// "guestinfo.hostname" as a string. +func (e *Config) SetCloudInitHostName(data []byte) error { + var guestinfoMetadata map[string]interface{} + + if err := yaml.Unmarshal(data, &guestinfoMetadata); err != nil { + return err + } + + localHostName, ok := guestinfoMetadata["local-hostname"].(string) + if ok { + *e = append(*e, &types.OptionValue{ + Key: "guestinfo.hostname", + Value: localHostName, + }) + } + + return nil +} + // encode first attempts to decode the data as many times as necessary // to ensure it is plain-text before returning the result as a base64 // encoded string. diff --git a/pkg/services/govmomi/extra/extra_suite_test.go b/pkg/services/govmomi/extra/extra_suite_test.go index ebf7c30d72..6690491709 100644 --- a/pkg/services/govmomi/extra/extra_suite_test.go +++ b/pkg/services/govmomi/extra/extra_suite_test.go @@ -77,6 +77,15 @@ var _ = Describe("Config_SetCloudInitMetadata", func() { ) }) +var _ = Describe("Config_SetCloudInitHostName", func() { + ConfigInitHostNameFnTester(func(config *Config, s string) { + config.SetCloudInitHostName([]byte(s)) + }, + "SetCloudInitHostName", + "guestinfo.hostname", + ) +}) + func base64Encode(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } @@ -140,3 +149,25 @@ func ConfigInitFnTester(method ConfigInitFn, methodName string, dataKey string, }) }) } + +// ConfigInitHostNameFnTester is a testing method for config.SetCloudInitHostName. +func ConfigInitHostNameFnTester(method ConfigInitFn, methodName string, dataKey string) { + const sampleData = "testKey: testValue\n" + "local-hostname: test-name" + expectedData := "test-name" + + Context(fmt.Sprintf("we call %q with some non-encoded sample data", methodName), func() { + var config Config + method(&config, sampleData) + + It("must set 1 key in the config", func() { + Expect(len(config)).To(Equal(1)) + }) + + It(fmt.Sprintf("must set data as a base64 encoded string with the key %q", dataKey), func() { + Expect(config).To(ContainElement(&types.OptionValue{ + Key: dataKey, + Value: expectedData, + })) + }) + }) +} diff --git a/pkg/services/govmomi/service.go b/pkg/services/govmomi/service.go index 355c139efa..51f14ff951 100644 --- a/pkg/services/govmomi/service.go +++ b/pkg/services/govmomi/service.go @@ -570,6 +570,10 @@ func (vms *VMService) setMetadata(ctx *virtualMachineContext, metadata []byte) ( extraConfig.SetCloudInitMetadata(metadata) + if err := extraConfig.SetCloudInitHostName(metadata); err != nil { + ctx.Logger.V(4).Error(err, "failed to set guestinfo.hostname") + } + task, err := ctx.Obj.Reconfigure(ctx, types.VirtualMachineConfigSpec{ ExtraConfig: extraConfig, }) diff --git a/pkg/services/vimmachine.go b/pkg/services/vimmachine.go index 4e66245b5e..ff10a65d5c 100644 --- a/pkg/services/vimmachine.go +++ b/pkg/services/vimmachine.go @@ -318,6 +318,10 @@ func (v *VimMachineService) reconcileNetwork(ctx *context.VIMMachineContext, vm Address: addr, }) } + machineAddresses = append(machineAddresses, clusterv1.MachineAddress{ + Type: clusterv1.MachineInternalDNS, + Address: ctx.Machine.Name, + }) ctx.VSphereMachine.Status.Addresses = machineAddresses }