llm_base_pod.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package models
  2. import (
  3. "context"
  4. "fmt"
  5. "yunion.io/x/jsonutils"
  6. "yunion.io/x/onecloud/pkg/apis"
  7. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  8. api "yunion.io/x/onecloud/pkg/apis/llm"
  9. "yunion.io/x/onecloud/pkg/mcclient"
  10. )
  11. const (
  12. POD_METADATA_POST_STOP_CLEANUP_CONFIG = "post_stop_cleanup_config"
  13. )
  14. type PodPostStopCleanupConfig struct {
  15. Dirs []string `json:"dirs"`
  16. }
  17. func GetLLMBasePodCreateInput(
  18. ctx context.Context,
  19. userCred mcclient.TokenCredential,
  20. input *api.LLMBaseCreateInput,
  21. llmBase *SLLMBase,
  22. skuBase *SLLMSkuBase,
  23. eip string,
  24. ) (*computeapi.ServerCreateInput, error) {
  25. data := computeapi.ServerCreateInput{}
  26. data.AutoStart = input.AutoStart
  27. data.ServerConfigs = computeapi.NewServerConfigs()
  28. data.Hypervisor = computeapi.HYPERVISOR_POD
  29. postStopCleanupConfgi := PodPostStopCleanupConfig{
  30. Dirs: []string{
  31. GetTmpHostPath(llmBase.GetName()),
  32. },
  33. }
  34. data.Metadata = map[string]string{
  35. POD_METADATA_POST_STOP_CLEANUP_CONFIG: jsonutils.Marshal(postStopCleanupConfgi).String(),
  36. }
  37. data.VcpuCount = skuBase.Cpu
  38. data.VmemSize = skuBase.Memory
  39. // data.Name = input.Name + "-" + seclib.RandomPassword(6)
  40. data.Name = input.Name
  41. // disks
  42. data.Disks = make([]*computeapi.DiskConfig, 0)
  43. if skuBase.Volumes != nil && !skuBase.Volumes.IsZero() {
  44. for idx, volume := range *skuBase.Volumes {
  45. data.Disks = append(data.Disks, &computeapi.DiskConfig{
  46. DiskType: "data",
  47. Format: "raw",
  48. Fs: "ext4",
  49. SizeMb: volume.SizeMB,
  50. Index: idx,
  51. })
  52. }
  53. }
  54. // isolated devices
  55. if skuBase.Devices != nil && !skuBase.Devices.IsZero() {
  56. data.IsolatedDevices = make([]*computeapi.IsolatedDeviceConfig, 0)
  57. devices := *skuBase.Devices
  58. for i := 0; i < len(devices); i++ {
  59. isolatedDevice := &computeapi.IsolatedDeviceConfig{
  60. DevType: devices[i].DevType,
  61. Model: devices[i].Model,
  62. DevicePath: devices[i].DevicePath,
  63. }
  64. data.IsolatedDevices = append(data.IsolatedDevices, isolatedDevice)
  65. }
  66. }
  67. // port mappings
  68. // var portRange *computeapi.GuestPortMappingPortRange
  69. portMappings := computeapi.GuestPortMappings{}
  70. if skuBase.PortMappings != nil && !skuBase.PortMappings.IsZero() {
  71. // hostTcpPortRange := computeapi.GuestPortMappingPortRange{
  72. // Start: options.Options.HostTcpPortStart,
  73. // End: options.Options.HostTcpPortEnd,
  74. // }
  75. // hostUdpPortRange := computeapi.GuestPortMappingPortRange{
  76. // Start: options.Options.HostUdpPortStart,
  77. // End: options.Options.HostUdpPortEnd,
  78. // }
  79. for _, portInfo := range *skuBase.PortMappings {
  80. remoteIps := portInfo.RemoteIps
  81. if len(remoteIps) == 0 {
  82. remoteIps = nil
  83. }
  84. // if portInfo.Protocol == "tcp" {
  85. // portRange = &hostTcpPortRange
  86. // } else {
  87. // portRange = &hostUdpPortRange
  88. // }
  89. portMappings = append(portMappings, &computeapi.GuestPortMapping{
  90. Port: portInfo.ContainerPort,
  91. Protocol: computeapi.GuestPortMappingProtocol(portInfo.Protocol),
  92. RemoteIps: remoteIps,
  93. // HostPortRange: portRange,
  94. Rule: &computeapi.GuestPortMappingRule{
  95. FirstPortOffset: portInfo.FirstPortOffset,
  96. },
  97. Envs: portInfo.Envs,
  98. })
  99. }
  100. }
  101. var network *computeapi.NetworkConfig
  102. if len(input.Nets) > 0 {
  103. network = input.Nets[0]
  104. networkCopy := *network
  105. network = &networkCopy
  106. network.Index = 0
  107. }
  108. bandwidth := input.BandwidthMB
  109. if bandwidth == 0 && network.BwLimit != 0 {
  110. bandwidth = network.BwLimit
  111. }
  112. if bandwidth == 0 && skuBase.Bandwidth != 0 {
  113. bandwidth = skuBase.Bandwidth
  114. }
  115. network.BwLimit = bandwidth
  116. if len(network.PortMappings) == 0 {
  117. network.PortMappings = portMappings
  118. }
  119. data.Networks = []*computeapi.NetworkConfig{
  120. network,
  121. }
  122. data.Count = 1
  123. data.PreferHost = input.PreferHost
  124. data.ProjectId = input.ProjectId
  125. if len(data.ProjectId) == 0 {
  126. data.ProjectId = userCred.GetProjectId()
  127. data.TenantId = userCred.GetTenantId()
  128. }
  129. return &data, nil
  130. }
  131. func NewHostDev(path string) *computeapi.ContainerDevice {
  132. return &computeapi.ContainerDevice{
  133. Type: apis.CONTAINER_DEVICE_TYPE_HOST,
  134. Host: &computeapi.ContainerHostDevice{
  135. HostPath: path,
  136. ContainerPath: path,
  137. Permissions: "rwm",
  138. },
  139. }
  140. }
  141. func NewEnv(key, val string) *apis.ContainerKeyValue {
  142. return &apis.ContainerKeyValue{
  143. Key: key,
  144. Value: val,
  145. }
  146. }
  147. func GetTmpHostPath(name string) string {
  148. return fmt.Sprintf("/tmp/%s", name)
  149. }
  150. func GetSvrLLMContainer(ctrs []*computeapi.PodContainerDesc) *computeapi.PodContainerDesc {
  151. return ctrs[0]
  152. }