Skip to content

Commit

Permalink
Add guestinfo.hostname to config of a VM
Browse files Browse the repository at this point in the history
  • Loading branch information
odvarkadaniel committed Jul 22, 2023
1 parent 80a870c commit ab9fc6c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/services/govmomi/extra/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions pkg/services/govmomi/extra/extra_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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,
}))
})
})
}
4 changes: 4 additions & 0 deletions pkg/services/govmomi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/services/vimmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit ab9fc6c

Please sign in to comment.