Skip to content

Commit

Permalink
feat: adds GPU support to packer plugin nutanix
Browse files Browse the repository at this point in the history
  • Loading branch information
faiq committed May 2, 2024
1 parent 9a6752c commit 043ef61
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
8 changes: 7 additions & 1 deletion builder/nutanix/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,Category,ClusterConfig,VmConfig,VmDisk,VmNIC
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,Category,ClusterConfig,VmConfig,VmDisk,VmNIC,GPU

package nutanix

Expand Down Expand Up @@ -45,6 +45,11 @@ type Config struct {
ctx interpolate.Context
}

type GPU struct {
Name string `mapstructure:"name" json:"name" required:"false"`
Mode string `mapstructure:"mode" json:"mode" required:"false"`
}

type Category struct {
Key string `mapstructure:"key" json:"key" required:"false"`
Value string `mapstructure:"value" json:"value" required:"false"`
Expand Down Expand Up @@ -86,6 +91,7 @@ type VmConfig struct {
UserData string `mapstructure:"user_data" json:"user_data" required:"false"`
VMCategories []Category `mapstructure:"vm_categories" required:"false"`
Project string `mapstructure:"project" required:"false"`
GPU []GPU `mapstructure:"gpu" required:"false"`
}

func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
Expand Down
29 changes: 29 additions & 0 deletions builder/nutanix/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions builder/nutanix/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ func findSubnetByName(conn *v3.Client, name string) (*v3.SubnetIntentResponse, e
return found[0], nil
}

func findGPUByName(conn *v3.Client, name string) (*v3.VMGpu, error) {
hosts, err := conn.V3.ListAllHost()
if err != nil {
return nil, err
}

for _, host := range hosts.Entities {
if host == nil ||
host.Status == nil ||
host.Status.ClusterReference == nil ||
host.Status.Resources == nil ||
len(host.Status.Resources.GPUList) == 0 {
continue
}

for _, peGpu := range host.Status.Resources.GPUList {
if peGpu == nil {
continue
}
if peGpu.Name == name {
return &v3.VMGpu{
DeviceID: peGpu.DeviceID,
Vendor: &peGpu.Vendor,
}, nil
}
}
}
return nil, fmt.Errorf("failed to find GPU %s", name)
}

func sourceImageExists(conn *v3.Client, name string, uri string) (*v3.ImageIntentResponse, error) {
filter := fmt.Sprintf("name==%s", name)
resp, err := conn.V3.ListAllImage(filter)
Expand Down Expand Up @@ -441,6 +471,16 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig, state multistep.StateBag) (*v
}
NICList = append(NICList, &newNIC)
}
GPUList := make([]*v3.VMGpu, 0, len(vm.GPU))
for _, gpu := range vm.GPU {
vmGPU, err := findGPUByName(conn, gpu.Name)
if err != nil {
return nil, fmt.Errorf("error while findGPUByName %s", err.Error())
}
vmGPU.Mode = &gpu.Mode
GPUList = append(GPUList, vmGPU)
}

PowerStateOn := "ON"

cluster := &v3.ClusterIntentResponse{}
Expand All @@ -466,6 +506,7 @@ func (d *NutanixDriver) CreateRequest(vm VmConfig, state multistep.StateBag) (*v
PowerState: &PowerStateOn,
DiskList: DiskList,
NicList: NICList,
GpuList: GPUList,
},
ClusterReference: BuildReference(*cluster.Metadata.UUID, "cluster"),
Description: StringPtr(fmt.Sprintf(vmDescription, d.Config.VmConfig.ImageName)),
Expand Down

0 comments on commit 043ef61

Please sign in to comment.