API endpoint and associated RPC changes to resolve VMs IP's (#188)

* API endpoint and associated RPC changes to resolve VMs IP's

* Fix "Missing expected argument '<name>'" error when doing "tart set"

* Implement TestIPEndpoint() and IP() method in controller HTTP client
This commit is contained in:
Nikolay Edigaryev
2024-07-03 22:56:43 +04:00
committed by GitHub
parent 8119b22817
commit 76f192bdb0
18 changed files with 603 additions and 124 deletions
+45
View File
@@ -57,6 +57,8 @@ func (worker *Worker) watchRPC(ctx context.Context) error {
go worker.handlePortForward(ctxWithMetadata, client, action.PortForwardAction)
case *rpc.WatchInstruction_SyncVmsAction:
worker.requestVMSyncing()
case *rpc.WatchInstruction_ResolveIpAction:
worker.handleGetIP(ctxWithMetadata, client, action.ResolveIpAction)
}
}
}
@@ -129,3 +131,46 @@ func (worker *Worker) handlePortForward(
_ = proxy.Connections(vmConn, grpcConn)
}
func (worker *Worker) handleGetIP(
ctx context.Context,
client rpc.ControllerClient,
resolveIP *rpc.WatchInstruction_ResolveIP,
) {
grpcMetadata := metadata.Join(
worker.grpcMetadata(),
metadata.Pairs(rpc.MetadataWorkerPortForwardingSessionKey, resolveIP.Session),
)
ctxWithMetadata := metadata.NewOutgoingContext(ctx, grpcMetadata)
// Find the desired VM
vm, ok := lo.Find(worker.vmm.List(), func(item *vmmanager.VM) bool {
return item.Resource.UID == resolveIP.VmUid
})
if !ok {
worker.logger.Warnf("failed to resolve IP for the VM with UID %q: VM not found",
resolveIP.VmUid)
return
}
// Obtain VM's IP address
ip, err := vm.IP(ctx)
if err != nil {
worker.logger.Warnf("failed to resolve IP for the VM with UID %q: \"tart ip\" failed: %v",
resolveIP.VmUid, err)
return
}
_, err = client.ResolveIP(ctxWithMetadata, &rpc.ResolveIPResult{
Session: resolveIP.Session,
Ip: ip,
})
if err != nil {
worker.logger.Warnf("failed to resolve IP for the VM with UID %q: "+
"failed to call back to the controller: %v", resolveIP.VmUid, err)
return
}
}