Skip to content

Commit

Permalink
support network and machine light
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Oct 16, 2024
1 parent 50d7901 commit b8bcc85
Show file tree
Hide file tree
Showing 94 changed files with 3,977 additions and 1,318 deletions.
2 changes: 1 addition & 1 deletion grid-cli/cmd/deploy_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/threefoldtech/tfgrid-sdk-go/grid-cli/internal/filters"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/deployer"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/workloads"
"github.com/threefoldtech/zos/pkg/gridtypes/zos"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/zos"
)

var (
Expand Down
1 change: 1 addition & 0 deletions grid-cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226-d4e15e206974 // indirect
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.15.18 // indirect
github.com/threefoldtech/zos4 v0.5.6-0.20241008102757-02d898c580c4 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions grid-cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226-d4e15e206974/go.mod h1:dtDKAPiUDxAwIkfHV7xcAFZcOm+xwNIuOI1MLFS+MeQ=
github.com/threefoldtech/zos v0.5.6-0.20240902110349-172a0a29a6ee h1:pqpYVM0qkXujplHNfH6w5GDqcY5sLJAgOc4/hlR6+Xw=
github.com/threefoldtech/zos v0.5.6-0.20240902110349-172a0a29a6ee/go.mod h1:lut72yYMJhgK0QRvF0Wd/mB3+OfIoXWz04DQuXck3Sw=
github.com/threefoldtech/zos4 v0.5.6-0.20241008102757-02d898c580c4 h1:JCExxpPL32G7evO/+gHwlZLfAX1+l9QN9t55tnPDCp0=
github.com/threefoldtech/zos4 v0.5.6-0.20241008102757-02d898c580c4/go.mod h1:7KFtZaCcEFwQ1/cz/+hkYK616Ww04ISZgmMqLWHz6To=
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=
Expand Down
10 changes: 5 additions & 5 deletions grid-cli/internal/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/deployer"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/workloads"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/zos"
)

// DeployVM deploys a vm with mounts
Expand All @@ -32,7 +32,7 @@ func DeployVM(ctx context.Context, t deployer.TFPluginClient, vm workloads.VM, d
volumeMounts = append(volumeMounts, volumeMount)
}
vm.NetworkName = networkName
dl := workloads.NewDeployment(vm.Name, vm.NodeID, projectName, nil, networkName, diskMounts, nil, []workloads.VM{vm}, nil, volumeMounts)
dl := workloads.NewDeployment(vm.Name, vm.NodeID, projectName, nil, networkName, diskMounts, nil, []workloads.VM{vm}, nil, nil, volumeMounts)

log.Info().Msg("deploying network")
err = t.NetworkDeployer.Deploy(ctx, &network)
Expand Down Expand Up @@ -138,7 +138,7 @@ func DeployGatewayFQDN(ctx context.Context, t deployer.TFPluginClient, gateway w

// DeployZDBs deploys multiple zdbs
func DeployZDBs(ctx context.Context, t deployer.TFPluginClient, projectName string, zdbs []workloads.ZDB, n int, node uint32) ([]workloads.ZDB, error) {
dl := workloads.NewDeployment(projectName, node, projectName, nil, "", nil, zdbs, nil, nil, nil)
dl := workloads.NewDeployment(projectName, node, projectName, nil, "", nil, zdbs, nil, nil, nil, nil)
log.Info().Msgf("deploying zdbs")
err := t.DeploymentDeployer.Deploy(ctx, &dl)
if err != nil {
Expand Down Expand Up @@ -172,10 +172,10 @@ func buildNetwork(name, projectName string, nodes []uint32, addMycelium bool) (w
return workloads.ZNet{
Name: name,
Nodes: nodes,
IPRange: gridtypes.NewIPNet(net.IPNet{
IPRange: zos.IPNet{IPNet: net.IPNet{
IP: net.IPv4(10, 20, 0, 0),
Mask: net.CIDRMask(16, 32),
}),
}},
MyceliumKeys: keys,
SolutionType: projectName,
}, nil
Expand Down
94 changes: 50 additions & 44 deletions grid-client/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ import (
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
client "github.com/threefoldtech/tfgrid-sdk-go/grid-client/node"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/subi"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/zos"
proxy "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/client"
proxyTypes "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/zos/pkg/gridtypes/zos"
"golang.org/x/sync/errgroup"
)

// MockDeployer to be used for any deployer in mock testing
type MockDeployer interface { // TODO: Change Name && separate them
Deploy(ctx context.Context,
oldDeploymentIDs map[uint32]uint64,
newDeployments map[uint32]gridtypes.Deployment,
newDeployments map[uint32]zos.Deployment,
newDeploymentSolutionProvider map[uint32]*uint64,
) (map[uint32]uint64, error)

Cancel(ctx context.Context,
contractID uint64,
) error

GetDeployments(ctx context.Context, dls map[uint32]uint64) (map[uint32]gridtypes.Deployment, error)
GetDeployments(ctx context.Context, dls map[uint32]uint64) (map[uint32]zos.Deployment, error)
BatchDeploy(ctx context.Context,
deployments map[uint32][]gridtypes.Deployment,
deployments map[uint32][]zos.Deployment,
deploymentsSolutionProvider map[uint32][]*uint64,
) (map[uint32][]gridtypes.Deployment, error)
) (map[uint32][]zos.Deployment, error)
}

// Deployer to be used for any deployer
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewDeployer(
// Deploy deploys or updates a new deployment given the old deployments' IDs
func (d *Deployer) Deploy(ctx context.Context,
oldDeploymentIDs map[uint32]uint64,
newDeployments map[uint32]gridtypes.Deployment,
newDeployments map[uint32]zos.Deployment,
newDeploymentSolutionProvider map[uint32]*uint64,
) (map[uint32]uint64, error) {
oldDeployments, oldErr := d.GetDeployments(ctx, oldDeploymentIDs)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (d *Deployer) Deploy(ctx context.Context,
func (d *Deployer) deploy(
ctx context.Context,
oldDeployments map[uint32]uint64,
newDeployments map[uint32]gridtypes.Deployment,
newDeployments map[uint32]zos.Deployment,
newDeploymentSolutionProvider map[uint32]*uint64,
revertOnFailure bool,
) (currentDeployments map[uint32]uint64, err error) {
Expand Down Expand Up @@ -173,7 +173,7 @@ func (d *Deployer) deploy(
currentDeployments[node] = dl.ContractID
newWorkloadVersions := make(map[string]uint32)
for _, w := range dl.Workloads {
newWorkloadVersions[w.Name.String()] = 0
newWorkloadVersions[w.Name] = 0
}
err = d.Wait(ctx, client, dl.ContractID, newWorkloadVersions)
if err != nil {
Expand Down Expand Up @@ -271,8 +271,8 @@ func (d *Deployer) Cancel(ctx context.Context,
}

// GetDeployments returns deployments from a map of nodes IDs and deployments IDs
func (d *Deployer) GetDeployments(ctx context.Context, dls map[uint32]uint64) (map[uint32]gridtypes.Deployment, error) {
res := make(map[uint32]gridtypes.Deployment)
func (d *Deployer) GetDeployments(ctx context.Context, dls map[uint32]uint64) (map[uint32]zos.Deployment, error) {
res := make(map[uint32]zos.Deployment)

for nodeID, dlID := range dls {
nc, err := d.ncPool.GetNodeClient(d.substrateConn, nodeID)
Expand All @@ -284,6 +284,7 @@ func (d *Deployer) GetDeployments(ctx context.Context, dls map[uint32]uint64) (m
if err != nil {
return nil, errors.Wrapf(err, "failed to get deployment %d of node %d", dlID, nodeID)
}

res[nodeID] = dl
}

Expand Down Expand Up @@ -324,18 +325,18 @@ func (d *Deployer) Wait(
}

for _, wl := range deploymentChanges {
if _, ok := workloadVersions[wl.Name.String()]; ok && wl.Version == workloadVersions[wl.Name.String()] {
if _, ok := workloadVersions[wl.Name]; ok && wl.Version == workloadVersions[wl.Name] {
var errString string
switch wl.Result.State {
case gridtypes.StateOk:
case zos.StateOk:
stateOk++
case gridtypes.StateError:
case zos.StateError:
errString = fmt.Sprintf("workload %s within deployment %d failed with error: %s", wl.Name, deploymentID, wl.Result.Error)
case gridtypes.StateDeleted:
case zos.StateDeleted:
errString = fmt.Sprintf("workload %s state within deployment %d is deleted: %s", wl.Name, deploymentID, wl.Result.Error)
case gridtypes.StatePaused:
case zos.StatePaused:
errString = fmt.Sprintf("workload %s state within deployment %d is paused: %s", wl.Name, deploymentID, wl.Result.Error)
case gridtypes.StateUnChanged:
case zos.StateUnChanged:
errString = fmt.Sprintf("workload %s within deployment %d was not updated: %s", wl.Name, deploymentID, wl.Result.Error)
}
if errString != "" {
Expand Down Expand Up @@ -364,8 +365,12 @@ func (d *Deployer) Wait(
}

// BatchDeploy deploys a batch of deployments, successful deployments should have ContractID fields set
func (d *Deployer) BatchDeploy(ctx context.Context, deployments map[uint32][]gridtypes.Deployment, deploymentsSolutionProvider map[uint32][]*uint64) (map[uint32][]gridtypes.Deployment, error) {
deploymentsSlice := make([]gridtypes.Deployment, 0)
func (d *Deployer) BatchDeploy(
ctx context.Context,
deployments map[uint32][]zos.Deployment,
deploymentsSolutionProvider map[uint32][]*uint64,
) (map[uint32][]zos.Deployment, error) {
deploymentsSlice := make([]zos.Deployment, 0)
contractsData := make([]substrate.BatchCreateContractData, 0)

mu := sync.Mutex{}
Expand All @@ -375,7 +380,7 @@ func (d *Deployer) BatchDeploy(ctx context.Context, deployments map[uint32][]gri
// loading node clients first before creating any contract and caching the clients
_, err := d.ncPool.GetNodeClient(d.substrateConn, node)
if err != nil {
return map[uint32][]gridtypes.Deployment{}, errors.Wrap(err, "failed to get node client")
return map[uint32][]zos.Deployment{}, errors.Wrap(err, "failed to get node client")
}
for i, dl := range dls {
i := i
Expand Down Expand Up @@ -432,12 +437,12 @@ func (d *Deployer) BatchDeploy(ctx context.Context, deployments map[uint32][]gri
}

if err := group.Wait(); err != nil {
return map[uint32][]gridtypes.Deployment{}, err
return map[uint32][]zos.Deployment{}, err
}

contracts, index, err := d.substrateConn.BatchCreateContract(d.identity, contractsData)
if err != nil && index == nil {
return map[uint32][]gridtypes.Deployment{}, errors.Wrap(err, "failed to create contracts")
return map[uint32][]zos.Deployment{}, errors.Wrap(err, "failed to create contracts")
}

var multiErr error
Expand Down Expand Up @@ -477,7 +482,7 @@ func (d *Deployer) BatchDeploy(ctx context.Context, deployments map[uint32][]gri
}
newWorkloadVersions := make(map[string]uint32)
for _, w := range dl.Workloads {
newWorkloadVersions[w.Name.String()] = 0
newWorkloadVersions[w.Name] = 0
}
err = d.Wait(ctx, client, dl.ContractID, newWorkloadVersions)
mu.Lock()
Expand All @@ -492,7 +497,7 @@ func (d *Deployer) BatchDeploy(ctx context.Context, deployments map[uint32][]gri
}
wg.Wait()

resDeployments := make(map[uint32][]gridtypes.Deployment, len(deployments))
resDeployments := make(map[uint32][]zos.Deployment, len(deployments))
for i, dl := range deploymentsSlice {
resDeployments[contractsData[i].Node] = append(resDeployments[contractsData[i].Node], dl)
}
Expand All @@ -508,21 +513,21 @@ func (d *Deployer) BatchDeploy(ctx context.Context, deployments map[uint32][]gri
}

// matchOldVersions assigns deployment and workloads versions of the new versionless deployment to the ones of the old deployment
func matchOldVersions(oldDl *gridtypes.Deployment, newDl *gridtypes.Deployment) {
func matchOldVersions(oldDl *zos.Deployment, newDl *zos.Deployment) {
oldWlVersions := map[string]uint32{}
for _, wl := range oldDl.Workloads {
oldWlVersions[wl.Name.String()] = wl.Version
oldWlVersions[wl.Name] = wl.Version
}

newDl.Version = oldDl.Version

for idx, wl := range newDl.Workloads {
newDl.Workloads[idx].Version = oldWlVersions[wl.Name.String()]
newDl.Workloads[idx].Version = oldWlVersions[wl.Name]
}
}

// assignVersions determines and assigns the versions of the new deployment and its workloads
func assignVersions(oldDl *gridtypes.Deployment, newDl *gridtypes.Deployment) (map[string]uint32, error) {
func assignVersions(oldDl *zos.Deployment, newDl *zos.Deployment) (map[string]uint32, error) {
oldHashes, err := GetWorkloadHashes(*oldDl)
if err != nil {
return nil, errors.Wrap(err, "could not get old workloads hashes")
Expand All @@ -537,12 +542,12 @@ func assignVersions(oldDl *gridtypes.Deployment, newDl *gridtypes.Deployment) (m
newDl.Version = oldDl.Version + 1

for idx, w := range newDl.Workloads {
newHash := newHashes[string(w.Name)]
oldHash, ok := oldHashes[string(w.Name)]
newHash := newHashes[w.Name]
oldHash, ok := oldHashes[w.Name]
if !ok || newHash != oldHash {
newDl.Workloads[idx].Version = newDl.Version
}
newWorkloadsVersions[w.Name.String()] = newDl.Workloads[idx].Version
newWorkloadsVersions[w.Name] = newDl.Workloads[idx].Version
}

return newWorkloadsVersions, nil
Expand All @@ -558,7 +563,7 @@ func assignVersions(oldDl *gridtypes.Deployment, newDl *gridtypes.Deployment) (m
//
// errors that may arise because of dead nodes are ignored.
// if a real error dodges the validation, it'll be fail anyway in the deploying phase
func (d *Deployer) Validate(ctx context.Context, oldDeployments map[uint32]gridtypes.Deployment, newDeployments map[uint32]gridtypes.Deployment) error {
func (d *Deployer) Validate(ctx context.Context, oldDeployments map[uint32]zos.Deployment, newDeployments map[uint32]zos.Deployment) error {
farmIPs := make(map[int]int)
nodeMap := make(map[uint32]proxyTypes.NodeWithNestedCapacity)

Expand Down Expand Up @@ -619,11 +624,12 @@ func (d *Deployer) Validate(ctx context.Context, oldDeployments map[uint32]gridt
}

for node, dl := range newDeployments {
oldDl, alreadyExists := oldDeployments[node]
if err := dl.Valid(); err != nil {
return errors.Wrap(err, "invalid deployment")
}

oldDl, alreadyExists := oldDeployments[node]

needed, err := Capacity(dl)
if err != nil {
return err
Expand Down Expand Up @@ -669,13 +675,13 @@ func (d *Deployer) Validate(ctx context.Context, oldDeployments map[uint32]gridt
mru := nodeInfo.Capacity.Total.MRU - nodeInfo.Capacity.Used.MRU
hru := nodeInfo.Capacity.Total.HRU - nodeInfo.Capacity.Used.HRU
sru := 2*nodeInfo.Capacity.Total.SRU - nodeInfo.Capacity.Used.SRU
if mru < needed.MRU ||
sru < needed.SRU ||
hru < needed.HRU {
free := gridtypes.Capacity{
HRU: hru,
MRU: mru,
SRU: sru,
if uint64(mru) < needed.MRU ||
uint64(sru) < needed.SRU ||
uint64(hru) < needed.HRU {
free := zos.Capacity{
HRU: uint64(hru),
MRU: uint64(mru),
SRU: uint64(sru),
}
return errors.Errorf("node %d does not have enough resources. needed: %v, free: %v", node, capacityPrettyPrint(needed), capacityPrettyPrint(free))
}
Expand All @@ -684,14 +690,14 @@ func (d *Deployer) Validate(ctx context.Context, oldDeployments map[uint32]gridt
}

// capacityPrettyPrint prints the capacity data
func capacityPrettyPrint(cap gridtypes.Capacity) string {
func capacityPrettyPrint(cap zos.Capacity) string {
return fmt.Sprintf("[mru: %d, sru: %d, hru: %d]", cap.MRU, cap.SRU, cap.HRU)
}

// addCapacity adds a new data for capacity
func addCapacity(cap *proxyTypes.Capacity, add *gridtypes.Capacity) {
func addCapacity(cap *proxyTypes.Capacity, add *zos.Capacity) {
cap.CRU += add.CRU
cap.MRU += add.MRU
cap.SRU += add.SRU
cap.HRU += add.HRU
cap.MRU += gridtypes.Unit(add.MRU)
cap.SRU += gridtypes.Unit(add.SRU)
cap.HRU += gridtypes.Unit(add.HRU)
}
Loading

0 comments on commit b8bcc85

Please sign in to comment.