|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/projecteru2/core/resource/plugins/cpumem/types" |
| 9 | +) |
| 10 | + |
| 11 | +type cpuType interface { |
| 12 | + string | int64 |
| 13 | +} |
| 14 | + |
| 15 | +// NodeResourceRequest includes all possible fields passed by eru-core for editing node, it not parsed! |
| 16 | +type NodeResourceRequest[T cpuType] struct { |
| 17 | + CPU T `json:"cpu"` |
| 18 | + Share int64 `json:"share"` |
| 19 | + Memory int64 `json:"memory"` |
| 20 | + NUMA []string `json:"numa-cpu"` |
| 21 | + NUMAMemory []int64 `json:"numa-memory"` |
| 22 | + |
| 23 | + NUMACPUMap types.NUMA `json:"-"` |
| 24 | + NUMAMemoryMap types.NUMAMemory `json:"-"` |
| 25 | +} |
| 26 | + |
| 27 | +// NewNodeResourceRequest . |
| 28 | +func NewNodeResourceRequest[T cpuType](cpu T, share, memory int64, NUMACPUMap types.NUMA, NUMAMemoryMap types.NUMAMemory) (*NodeResourceRequest[T], error) { |
| 29 | + r := &NodeResourceRequest[T]{ |
| 30 | + CPU: cpu, |
| 31 | + Share: share, |
| 32 | + Memory: memory, |
| 33 | + } |
| 34 | + tmpNUMA := map[int][]string{} |
| 35 | + for cpuID, nodeID := range NUMACPUMap { |
| 36 | + nID, err := strconv.Atoi(nodeID) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + if tmpNUMA[nID] == nil { |
| 41 | + tmpNUMA[nID] = []string{cpuID} |
| 42 | + continue |
| 43 | + } |
| 44 | + tmpNUMA[nID] = append(tmpNUMA[nID], cpuID) |
| 45 | + } |
| 46 | + r.NUMA = make([]string, len(tmpNUMA)) |
| 47 | + for nodeID, cpus := range tmpNUMA { |
| 48 | + r.NUMA[nodeID] = strings.Join(cpus, ",") |
| 49 | + } |
| 50 | + |
| 51 | + tmpNUMAMemory := map[int]int64{} |
| 52 | + for nodeID, memory := range NUMAMemoryMap { |
| 53 | + nID, err := strconv.Atoi(nodeID) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + tmpNUMAMemory[nID] = memory |
| 58 | + } |
| 59 | + r.NUMAMemory = make([]int64, len(tmpNUMAMemory)) |
| 60 | + for nodeID, memory := range tmpNUMAMemory { |
| 61 | + r.NUMAMemory[nodeID] = memory |
| 62 | + } |
| 63 | + |
| 64 | + return r, nil |
| 65 | +} |
| 66 | + |
| 67 | +// Encode . |
| 68 | +func (n NodeResourceRequest[T]) Encode() ([]byte, error) { |
| 69 | + return json.Marshal(n) |
| 70 | +} |
0 commit comments