fix: Respect metadata supplied for GCE builders.
Previously, any per instance metadata supplied via the GCE builder was ignored. Test plan: - make test - Manual testing via: -- Created a packer config that contained a GCE builder with custom metadata set. -- Ran `packer build`. -- Verified the instance had the correct metadata in the GCE console.
This commit is contained in:
parent
2aa5a1988b
commit
0fe61fa1e0
|
@ -24,6 +24,25 @@ func (config *Config) getImage() Image {
|
||||||
return Image{Name: config.SourceImage, ProjectId: project}
|
return Image{Name: config.SourceImage, ProjectId: project}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config *Config) getInstanceMetadata(sshPublicKey string) map[string]string {
|
||||||
|
instanceMetadata := make(map[string]string)
|
||||||
|
|
||||||
|
// Copy metadata from config
|
||||||
|
for k, v := range config.Metadata {
|
||||||
|
instanceMetadata[k] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge any existing ssh keys with our public key
|
||||||
|
sshMetaKey := "sshKeys"
|
||||||
|
sshKeys := fmt.Sprintf("%s:%s", config.SSHUsername, sshPublicKey)
|
||||||
|
if confSshKeys, exists := instanceMetadata[sshMetaKey]; exists {
|
||||||
|
sshKeys = fmt.Sprintf("%s\n%s", sshKeys, confSshKeys)
|
||||||
|
}
|
||||||
|
instanceMetadata[sshMetaKey] = sshKeys
|
||||||
|
|
||||||
|
return instanceMetadata
|
||||||
|
}
|
||||||
|
|
||||||
// Run executes the Packer build step that creates a GCE instance.
|
// Run executes the Packer build step that creates a GCE instance.
|
||||||
func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
config := state.Get("config").(*Config)
|
config := state.Get("config").(*Config)
|
||||||
|
@ -39,9 +58,7 @@ func (s *StepCreateInstance) Run(state multistep.StateBag) multistep.StepAction
|
||||||
DiskSizeGb: config.DiskSizeGb,
|
DiskSizeGb: config.DiskSizeGb,
|
||||||
Image: config.getImage(),
|
Image: config.getImage(),
|
||||||
MachineType: config.MachineType,
|
MachineType: config.MachineType,
|
||||||
Metadata: map[string]string{
|
Metadata: config.getInstanceMetadata(sshPublicKey),
|
||||||
"sshKeys": fmt.Sprintf("%s:%s", config.SSHUsername, sshPublicKey),
|
|
||||||
},
|
|
||||||
Name: name,
|
Name: name,
|
||||||
Network: config.Network,
|
Network: config.Network,
|
||||||
Tags: config.Tags,
|
Tags: config.Tags,
|
||||||
|
|
Loading…
Reference in New Issue