added resources

This commit is contained in:
ston1th 2020-10-25 18:26:14 +01:00
commit 3d68cef8ba

View file

@ -40,8 +40,8 @@ func (s *Server) UserDataSnippet(storage string) string {
func (s *Server) body() (b httpbody) { func (s *Server) body() (b httpbody) {
b = httpbody{ b = httpbody{
"memory": strconv.FormatInt(s.Resources.Memory, 10), "memory": s.Resources.Memory.String(),
"cores": strconv.FormatInt(s.Resources.Cores, 10), "cores": s.Resources.Cores.String(),
"name": s.Name, "name": s.Name,
"nameserver": s.Nameserver.String(), "nameserver": s.Nameserver.String(),
"searchdomain": s.SearchDomain, "searchdomain": s.SearchDomain,
@ -76,10 +76,31 @@ func (s *Server) K8sID() string {
} }
type Resources struct { type Resources struct {
Memory int64 Cores Cores `json:"cores"`
Cores int64 Memory Memory `json:"memory"`
BootDisk string Disk Disk `json:"disk"`
BootDiskSize string }
type Cores uint64
func (c Cores) String() string {
return strconv.FormatUint(uint64(c), 10)
}
type Memory uint64
func (m Memory) String() string {
return strconv.FormatUint(uint64(m)*1024, 10)
}
type Disk struct {
Storage string `json:"storage"`
Name string `json:"name"`
Size uint64 `json:"size"`
}
func (d Disk) String() string {
return strconv.FormatUint(d.Size, 10) + "G"
} }
type Nameserver []string type Nameserver []string
@ -117,19 +138,27 @@ func (sc serverConfig) Server(node, id string) (s *Server, err error) {
// } // }
case "memory": case "memory":
if v, ok := sc[k].(float64); ok { if v, ok := sc[k].(float64); ok {
s.Resources.Memory = int64(v) s.Resources.Memory = Memory(uint64(v) / 1024)
} }
case "cores": case "cores":
if v, ok := sc[k].(float64); ok { if v, ok := sc[k].(float64); ok {
s.Resources.Cores = int64(v) s.Resources.Cores = Cores(uint64(v))
} }
case "bootdisk": case "bootdisk":
if v, ok := sc[k].(string); ok { if v, ok := sc[k].(string); ok {
s.Resources.BootDisk = v s.Resources.Disk.Name = v
if val, ok := sc[v].(string); ok { if val, ok := sc[v].(string); ok {
for _, o := range strings.Split(val, ",") { vals := strings.Split(val, ",")
if len(vals) > 0 {
storage := strings.Split(vals[0], ":")
if len(storage) > 0 {
s.Resources.Disk.Storage = storage[0]
}
}
for _, o := range vals {
if strings.HasPrefix(o, "size=") { if strings.HasPrefix(o, "size=") {
s.Resources.BootDiskSize = o[5:] size, _ := strconv.ParseUint(o[5:], 10, 64)
s.Resources.Disk.Size = size
break break
} }
} }
@ -388,8 +417,8 @@ func (c *ServerClient) updateConfig(ctx context.Context, s *Server, body httpbod
func (c *ServerClient) resizeDisk(ctx context.Context, s *Server) (err error) { func (c *ServerClient) resizeDisk(ctx context.Context, s *Server) (err error) {
body := httpbody{ body := httpbody{
"disk": s.Resources.BootDisk, "disk": s.Resources.Disk.Name,
"size": s.Resources.BootDiskSize, "size": s.Resources.Disk.String(),
} }
req, err := c.client.NewRequest(ctx, "PUT", fmt.Sprintf("/nodes/%s/qemu/%s/resize", s.Node, s.ID), body.Reader()) req, err := c.client.NewRequest(ctx, "PUT", fmt.Sprintf("/nodes/%s/qemu/%s/resize", s.Node, s.ID), body.Reader())
if err != nil { if err != nil {
@ -421,7 +450,7 @@ func (c *ServerClient) Update(ctx context.Context, s *Server) (t *Task, err erro
e = append(e, fmt.Sprintf("config: %s", cfg)) e = append(e, fmt.Sprintf("config: %s", cfg))
} }
} }
if cur.Resources.BootDiskSize != s.Resources.BootDiskSize { if cur.Resources.Disk.Size != s.Resources.Disk.Size {
resize := c.resizeDisk(ctx, s) resize := c.resizeDisk(ctx, s)
if resize != nil { if resize != nil {
e = append(e, fmt.Sprintf("resize: %s", resize)) e = append(e, fmt.Sprintf("resize: %s", resize))