Skip to content

Commit

Permalink
Add volume workload (#1026)
Browse files Browse the repository at this point in the history
* Add volume workload

* Fix filter
  • Loading branch information
AbdelrahmanElawady authored Jul 31, 2024
1 parent 0c91f12 commit 2ae20ce
Show file tree
Hide file tree
Showing 25 changed files with 304 additions and 34 deletions.
39 changes: 39 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions grid-cli/internal/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func DeployVM(ctx context.Context, t deployer.TFPluginClient, vm workloads.VM, m
mounts = append(mounts, mount)
}
vm.NetworkName = networkName
dl := workloads.NewDeployment(vm.Name, node, projectName, nil, networkName, mounts, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(vm.Name, node, projectName, nil, networkName, mounts, nil, []workloads.VM{vm}, nil, nil)

log.Info().Msg("deploying network")
err = t.NetworkDeployer.Deploy(ctx, &network)
Expand Down Expand Up @@ -128,7 +128,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)
dl := workloads.NewDeployment(projectName, node, projectName, nil, "", nil, zdbs, nil, nil, nil)
log.Info().Msgf("deploying zdbs")
err := t.DeploymentDeployer.Deploy(ctx, &dl)
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion grid-client/deployer/deployment_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (d *DeploymentDeployer) GenerateVersionlessDeployments(ctx context.Context,
for _, disk := range dl.Disks {
newDl.Workloads = append(newDl.Workloads, disk.ZosWorkload())
}
for _, volume := range dl.Volumes {
newDl.Workloads = append(newDl.Workloads, volume.ZosWorkload())
}
for _, zdb := range dl.Zdbs {
newDl.Workloads = append(newDl.Workloads, zdb.ZosWorkload())
}
Expand Down Expand Up @@ -418,6 +421,7 @@ func (d *DeploymentDeployer) Sync(ctx context.Context, dl *workloads.Deployment)
zdbs := make([]workloads.ZDB, 0)
qsfs := make([]workloads.QSFS, 0)
disks := make([]workloads.Disk, 0)
volumes := make([]workloads.Volume, 0)

for _, w := range deployment.Workloads {
if !w.Result.State.IsOkay() {
Expand Down Expand Up @@ -458,15 +462,24 @@ func (d *DeploymentDeployer) Sync(ctx context.Context, dl *workloads.Deployment)
}

disks = append(disks, disk)
case zos.VolumeType:
volume, err := workloads.NewVolumeFromWorkload(&w)
if err != nil {
log.Error().Err(err).Msgf("error parsing volume")
continue
}

volumes = append(volumes, volume)
}
}

dl.Match(disks, qsfs, zdbs, vms)
dl.Match(disks, qsfs, zdbs, vms, volumes)

dl.Disks = disks
dl.QSFS = qsfs
dl.Zdbs = zdbs
dl.Vms = vms
dl.Volumes = volumes

return nil
}
6 changes: 3 additions & 3 deletions grid-client/deployer/deployment_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func ExampleDeploymentDeployer_Deploy() {
return
}

dl := workloads.NewDeployment("vmdeployment", nodeID, "", nil, n.Name, nil, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment("vmdeployment", nodeID, "", nil, n.Name, nil, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -779,8 +779,8 @@ func ExampleDeploymentDeployer_BatchDeploy() {
return
}

d1 := workloads.NewDeployment("vm1deployment", nodeID, "", nil, n.Name, nil, nil, []workloads.VM{vm1}, nil)
d2 := workloads.NewDeployment("vm2deployment", nodeID, "", nil, n.Name, nil, nil, []workloads.VM{vm2}, nil)
d1 := workloads.NewDeployment("vm1deployment", nodeID, "", nil, n.Name, nil, nil, []workloads.VM{vm1}, nil, nil)
d2 := workloads.NewDeployment("vm2deployment", nodeID, "", nil, n.Name, nil, nil, []workloads.VM{vm2}, nil, nil)
err = tfPluginClient.DeploymentDeployer.BatchDeploy(context.Background(), []*workloads.Deployment{&d1, &d2})
if err != nil {
fmt.Println(err)
Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/batch_gateway_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBatchGatewayNameDeployment(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID1, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID1, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions grid-client/integration_tests/batch_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func TestBatchVMDeployment(t *testing.T) {
require.NoError(t, err)
})

dl1 := workloads.NewDeployment(fmt.Sprintf("dl1_%s", generateRandString(10)), nodeID1, "", nil, network1.Name, nil, nil, []workloads.VM{vm1}, nil)
dl2 := workloads.NewDeployment(fmt.Sprintf("dl2_%s", generateRandString(10)), nodeID2, "", nil, network2.Name, nil, nil, []workloads.VM{vm2}, nil)
dl1 := workloads.NewDeployment(fmt.Sprintf("dl1_%s", generateRandString(10)), nodeID1, "", nil, network1.Name, nil, nil, []workloads.VM{vm1}, nil, nil)
dl2 := workloads.NewDeployment(fmt.Sprintf("dl2_%s", generateRandString(10)), nodeID2, "", nil, network2.Name, nil, nil, []workloads.VM{vm2}, nil, nil)
err = tfPluginClient.DeploymentDeployer.BatchDeploy(context.Background(), []*workloads.Deployment{&dl1, &dl2})
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestDiskDeployment(t *testing.T) {
Description: "disk test",
}

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, "", []workloads.Disk{disk}, nil, nil, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, "", []workloads.Disk{disk}, nil, nil, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/gateway_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestGatewayNameDeployment(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/gatway_fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestGatewayFQDNDeployment(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions grid-client/integration_tests/qsfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestQSFSDeployment(t *testing.T) {
metaZDBs = append(metaZDBs, zdb)
}

dl1 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, "", nil, append(dataZDBs, metaZDBs...), nil, nil)
dl1 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, "", nil, append(dataZDBs, metaZDBs...), nil, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl1)
require.NoError(t, err)

Expand Down Expand Up @@ -173,7 +173,7 @@ func TestQSFSDeployment(t *testing.T) {
require.NoError(t, err)
})

dl2 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, append(dataZDBs, metaZDBs...), []workloads.VM{vm}, []workloads.QSFS{qsfs})
dl2 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, append(dataZDBs, metaZDBs...), []workloads.VM{vm}, []workloads.QSFS{qsfs}, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl2)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/two_vms_same_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestTwoVMsSameNetworkWithPublicIPV6(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm2}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm2}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/vm_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestVMWithTwoDisk(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, []workloads.Disk{disk1, disk2}, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, []workloads.Disk{disk1, disk2}, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/vm_gpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestVMWithGPUDeployment(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, []workloads.Disk{disk}, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, []workloads.Disk{disk}, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/vm_public_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestVMDeployment(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
89 changes: 89 additions & 0 deletions grid-client/integration_tests/vm_volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package integration

import (
"context"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/deployer"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/workloads"
)

func TestVMWithVolume(t *testing.T) {
tfPluginClient, err := setup()
if err != nil {
t.Skipf("plugin creation failed: %v", err)
}

publicKey, privateKey, err := GenerateSSHKeyPair()
require.NoError(t, err)

nodes, err := deployer.FilterNodes(
context.Background(),
tfPluginClient,
generateNodeFilter(WithFreeSRU(3)),
[]uint64{*convertGBToBytes(1)},
nil,
[]uint64{*convertGBToBytes(minRootfs)},
1,
)
if err != nil {
t.Skipf("no available nodes found: %v", err)
}

nodeID := uint32(nodes[0].NodeID)

network := generateBasicNetwork([]uint32{nodeID})

volume := workloads.Volume{
Name: "volume",
SizeGB: 1,
}

vm := workloads.VM{
Name: "vm",
NetworkName: network.Name,
CPU: minCPU,
Memory: int(minMemory) * 1024,
RootfsSize: int(minRootfs) * 1024,
Planetary: true,
Flist: "https://hub.grid.tf/tf-official-apps/base:latest.flist",
Entrypoint: "/sbin/zinit init",
EnvVars: map[string]string{
"SSH_KEY": publicKey,
},
Mounts: []workloads.Mount{
{DiskName: volume.Name, MountPoint: "/volume"},
},
}

err = tfPluginClient.NetworkDeployer.Deploy(context.Background(), &network)
require.NoError(t, err)

t.Cleanup(func() {
err = tfPluginClient.NetworkDeployer.Cancel(context.Background(), &network)
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, []workloads.Volume{volume})
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

t.Cleanup(func() {
err = tfPluginClient.DeploymentDeployer.Cancel(context.Background(), &dl)
require.NoError(t, err)
})

v, err := tfPluginClient.State.LoadVMFromGrid(context.Background(), nodeID, vm.Name, dl.Name)
require.NoError(t, err)
require.NotEmpty(t, v.PlanetaryIP)

resVolume, err := tfPluginClient.State.LoadVolumeFromGrid(context.Background(), nodeID, volume.Name, dl.Name)
require.NoError(t, err)
require.Equal(t, volume, resVolume)
res, err := RemoteRun("root", v.PlanetaryIP, "mount", privateKey)
require.NoError(t, err)
strings.Contains(res, "volume on /volume type virtiofs")
}
10 changes: 5 additions & 5 deletions grid-client/integration_tests/vms_private_ips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestDeploymentsDeploy(t *testing.T) {
}
})

d1 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm1}, nil)
d1 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm1}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &d1)
if err != nil {
t.Fatal(err)
Expand All @@ -75,7 +75,7 @@ func TestDeploymentsDeploy(t *testing.T) {
}
})

d2 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm1}, nil)
d2 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm1}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &d2)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -181,15 +181,15 @@ func TestDeploymentsBatchDeploy(t *testing.T) {
}
})

d1 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID1, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm1, vm1}, nil)
d1 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID1, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm1, vm1}, nil, nil)
d1.Vms[1].Name = vm2Name
d1.Vms[2].Name = vm3Name

d2 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID1, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm1, vm1}, nil)
d2 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID1, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm1, vm1}, nil, nil)
d2.Vms[1].Name = vm2Name
d2.Vms[2].Name = vm3Name

d3 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID2, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm1, vm1}, nil)
d3 := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID2, "", nil, network.Name, nil, nil, []workloads.VM{vm1, vm1, vm1}, nil, nil)
d3.Vms[1].Name = vm2Name
d3.Vms[2].Name = vm3Name

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/wireguard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestWG(t *testing.T) {
require.NoError(t, err)
})

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, network.Name, nil, nil, []workloads.VM{vm}, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion grid-client/integration_tests/zdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestZDBDeployment(t *testing.T) {
Mode: zos.ZDBModeUser,
}

dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, "", nil, []workloads.ZDB{zdb}, nil, nil)
dl := workloads.NewDeployment(fmt.Sprintf("dl_%s", generateRandString(10)), nodeID, "", nil, "", nil, []workloads.ZDB{zdb}, nil, nil, nil)
err = tfPluginClient.DeploymentDeployer.Deploy(context.Background(), &dl)
require.NoError(t, err)

Expand Down
10 changes: 10 additions & 0 deletions grid-client/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func (st *State) LoadDiskFromGrid(ctx context.Context, nodeID uint32, name strin
return workloads.NewDiskFromWorkload(&wl)
}

// LoadVolumeFromGrid loads a volume from grid
func (st *State) LoadVolumeFromGrid(ctx context.Context, nodeID uint32, name string, deploymentName string) (workloads.Volume, error) {
wl, dl, err := st.GetWorkloadInDeployment(ctx, nodeID, name, deploymentName)
if err != nil {
return workloads.Volume{}, errors.Wrapf(err, "could not get workload from node %d within deployment %v", nodeID, dl)
}

return workloads.NewVolumeFromWorkload(&wl)
}

// LoadGatewayFQDNFromGrid loads a gateway FQDN proxy from grid
func (st *State) LoadGatewayFQDNFromGrid(ctx context.Context, nodeID uint32, name string, deploymentName string) (workloads.GatewayFQDNProxy, error) {
wl, dl, err := st.GetWorkloadInDeployment(ctx, nodeID, name, deploymentName)
Expand Down
Loading

0 comments on commit 2ae20ce

Please sign in to comment.