refactor(tfgrid3deployer): use more reliable nodes

- pick reliable nodes by pinging them before attempting deployment
This commit is contained in:
2025-01-15 17:59:15 +02:00
parent 98ce764992
commit 5256ab6f23
4 changed files with 99 additions and 124 deletions

View File

@@ -145,7 +145,9 @@ fn (mut self TFDeployment) set_nodes() ! {
return error('Requested the Grid Proxy and no nodes found.')
}
vm.node_id = self.pick_node(nodes) or { return error('Failed to pick valid node: ${err}') }
vm.node_id = u32(pick_node(mut self.deployer, nodes) or {
return error('Failed to pick valid node: ${err}')
}.node_id)
}
for mut zdb in self.zdbs {
@@ -165,7 +167,9 @@ fn (mut self TFDeployment) set_nodes() ! {
return error('Requested the Grid Proxy and no nodes found.')
}
zdb.node_id = self.pick_node(nodes) or { return error('Failed to pick valid node: ${err}') }
zdb.node_id = u32(pick_node(mut self.deployer, nodes) or {
return error('Failed to pick valid node: ${err}')
}.node_id)
}
for mut webname in self.webnames {
@@ -186,9 +190,9 @@ fn (mut self TFDeployment) set_nodes() ! {
return error('Requested the Grid Proxy and no nodes found.')
}
webname.node_id = self.pick_node(nodes) or {
webname.node_id = u32(pick_node(mut self.deployer, nodes) or {
return error('Failed to pick valid node: ${err}')
}
}.node_id)
}
}
@@ -508,36 +512,3 @@ pub fn (mut self TFDeployment) list_deployments() !map[u32]grid_models.Deploymen
return dls
}
fn (mut self TFDeployment) pick_node(nodes []gridproxy_models.Node) !u32 {
mut node_id := ?u32(none)
mut checked := []bool{len: nodes.len}
mut checked_cnt := 0
for checked_cnt < nodes.len {
idx := int(rand.u32() % u32(nodes.len))
if checked[idx] {
continue
}
checked[idx] = true
checked_cnt += 1
if self.ping_node(u32(nodes[idx].twin_id)) {
node_id = u32(nodes[idx].node_id)
break
}
}
if v := node_id {
return v
} else {
return error('No node is reachable.')
}
}
fn (mut self TFDeployment) ping_node(twin_id u32) bool {
if _ := self.deployer.client.get_zos_version(twin_id) {
return true
} else {
return false
}
}

View File

@@ -43,7 +43,7 @@ fn (mut self NetworkHandler) create_network(vmachines []VMachine) ! {
}
}
console.print_header('Loaded nodes: ${self.nodes}.')
console.print_header('Network nodes: ${self.nodes}.')
self.setup_wireguard_data()!
self.setup_access_node()!
}
@@ -110,15 +110,24 @@ fn (mut self NetworkHandler) setup_access_node() ! {
console.print_header('No public nodes found based on your specs.')
console.print_header('Requesting the Proxy to assign a public node.')
mut myfilter := gridproxy.nodefilter()!
myfilter.ipv4 = true // Only consider nodes with IPv4
myfilter.status = 'up'
myfilter.healthy = true
nodes := filter_nodes(myfilter)!
access_node := nodes[0]
nodes := filter_nodes(
ipv4: true
status: 'up'
healthy: true
available_for: u64(self.deployer.twin_id)
features: [
'zmachine',
]
)!
if nodes.len == 0 {
return error('Requested the Grid Proxy and no nodes found.')
}
access_node := pick_node(mut self.deployer, nodes) or {
return error('Failed to pick valid node: ${err}')
}
self.public_node = u32(access_node.node_id)
console.print_header('Public node ${self.public_node}')
self.nodes << self.public_node

View File

@@ -1,9 +1,11 @@
module tfgrid3deployer
import freeflowuniverse.herolib.threefold.gridproxy
import freeflowuniverse.herolib.threefold.grid
import freeflowuniverse.herolib.threefold.grid.models as grid_models
import freeflowuniverse.herolib.threefold.gridproxy.model as gridproxy_models
import rand
import freeflowuniverse.herolib.ui.console
// Resolves the correct grid network based on the `cn.network` value.
//
@@ -53,3 +55,37 @@ pub fn filter_nodes(filter gridproxy_models.NodeFilter) ![]gridproxy_models.Node
fn convert_to_gigabytes(bytes u64) u64 {
return bytes * 1024 * 1024 * 1024
}
fn pick_node(mut deployer grid.Deployer, nodes []gridproxy_models.Node) !gridproxy_models.Node {
mut node := ?gridproxy_models.Node(none)
mut checked := []bool{len: nodes.len}
mut checked_cnt := 0
for checked_cnt < nodes.len {
idx := int(rand.u32() % u32(nodes.len))
if checked[idx] {
continue
}
checked[idx] = true
checked_cnt += 1
if ping_node(mut deployer, u32(nodes[idx].twin_id)) {
node = nodes[idx]
break
}
}
if v := node {
return v
} else {
return error('No node is reachable.')
}
}
fn ping_node(mut deployer grid.Deployer, twin_id u32) bool {
if _ := deployer.client.get_zos_version(twin_id) {
return true
} else {
console.print_stderr('Failed to ping node with twin: ${twin_id}')
return false
}
}