Skip to content

Commit

Permalink
Revert "Revert "Set resource cache when a LXD instance creating""
Browse files Browse the repository at this point in the history
This reverts commit fb71dc9.
  • Loading branch information
site0801 committed Oct 11, 2023
1 parent 02926dc commit 8dd3cc4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/whywaita/shoes-lxd-multi/server
go 1.19

require (
github.com/docker/go-units v0.4.0
github.com/docker/go-units v0.5.0
github.com/lxc/lxd v0.0.0-20211202222358-a293da71aeb0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.12.1
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
4 changes: 4 additions & 0 deletions server/pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net"
"sync"

myshoespb "github.com/whywaita/myshoes/api/proto.go"
pb "github.com/whywaita/shoes-lxd-multi/proto.go"
Expand All @@ -20,6 +21,8 @@ type ShoesLXDMultiServer struct {
resourceMapping map[myshoespb.ResourceType]config.Mapping

overCommitPercent uint64

mu sync.Mutex
}

// New create gRPC server
Expand All @@ -28,6 +31,7 @@ func New(hostConfigs *config.HostConfigMap, mapping map[myshoespb.ResourceType]c
hostConfigs: hostConfigs,
resourceMapping: mapping,
overCommitPercent: overCommitPercent,
mu: sync.Mutex{},
}, nil
}

Expand Down
24 changes: 24 additions & 0 deletions server/pkg/api/server_add_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
pb "github.com/whywaita/shoes-lxd-multi/proto.go"
"github.com/whywaita/shoes-lxd-multi/server/pkg/lxdclient"

"github.com/docker/go-units"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -66,6 +68,28 @@ func (s *ShoesLXDMultiServer) AddInstance(ctx context.Context, req *pb.AddInstan
Source: *instanceSource,
}

cpu, err := strconv.ParseUint(reqInstance.InstancePut.Config["limits.cpu"], 10, 64)
if err != nil {
return nil, fmt.Errorf("failde to parse limits.cpu: %w", err)
}

memory, err := units.FromHumanSize(reqInstance.InstancePut.Config["limits.memory"])
if err != nil {
return nil, fmt.Errorf("failde to parse limits.memory: %w", err)
}

s.mu.Lock()
cache, err := lxdclient.GetStatusCache(host.HostConfig.LxdHost)
if err != nil {
return nil, err
}
cache.Resource.CPUUsed += cpu
cache.Resource.MemoryUsed += uint64(memory)
if err := lxdclient.SetStatusCache(host.HostConfig.LxdHost, cache); err != nil {
return nil, fmt.Errorf("failed to set status cache: %s", err)
}
s.mu.Unlock()

client = host.Client
op, err := client.CreateInstance(reqInstance)
if err != nil {
Expand Down

0 comments on commit 8dd3cc4

Please sign in to comment.