-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
18 changed files
with
603 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"github.com/cirruslabs/orchard/internal/responder" | ||
v1 "github.com/cirruslabs/orchard/pkg/resource/v1" | ||
"github.com/cirruslabs/orchard/rpc" | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func (controller *Controller) ip(ctx *gin.Context) responder.Responder { | ||
if responder := controller.authorize(ctx, v1.ServiceAccountRoleComputeWrite); responder != nil { | ||
return responder | ||
} | ||
|
||
// Retrieve and parse path and query parameters | ||
name := ctx.Param("name") | ||
|
||
waitRaw := ctx.Query("wait") | ||
wait, err := strconv.ParseUint(waitRaw, 10, 16) | ||
if err != nil { | ||
return responder.Code(http.StatusBadRequest) | ||
} | ||
waitContext, waitContextCancel := context.WithTimeout(ctx, time.Duration(wait)*time.Second) | ||
defer waitContextCancel() | ||
|
||
// Look-up the VM | ||
vm, responderImpl := controller.waitForVM(waitContext, name) | ||
if responderImpl != nil { | ||
return responderImpl | ||
} | ||
|
||
// Send an IP resolution request and wait for the result | ||
session := uuid.New().String() | ||
boomerangConnCh, cancel := controller.ipRendezvous.Request(ctx, session) | ||
defer cancel() | ||
|
||
err = controller.workerNotifier.Notify(ctx, vm.Worker, &rpc.WatchInstruction{ | ||
Action: &rpc.WatchInstruction_ResolveIpAction{ | ||
ResolveIpAction: &rpc.WatchInstruction_ResolveIP{ | ||
Session: session, | ||
VmUid: vm.UID, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
controller.logger.Warnf("failed to request VM's IP from the worker %s: %v", | ||
vm.Worker, err) | ||
|
||
return responder.Code(http.StatusServiceUnavailable) | ||
} | ||
|
||
select { | ||
case ip := <-boomerangConnCh: | ||
result := struct { | ||
IP string `json:"ip"` | ||
}{ | ||
IP: ip, | ||
} | ||
|
||
return responder.JSON(http.StatusOK, &result) | ||
case <-ctx.Done(): | ||
return responder.Error(ctx.Err()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package rendezvous | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/cirruslabs/orchard/internal/concurrentmap" | ||
) | ||
|
||
var ErrInvalidToken = errors.New("invalid rendezvous token") | ||
|
||
type Rendezvous[T any] struct { | ||
sessions *concurrentmap.ConcurrentMap[*TokenSlot[T]] | ||
} | ||
|
||
type TokenSlot[T any] struct { | ||
ctx context.Context | ||
ch chan T | ||
} | ||
|
||
func New[T any]() *Rendezvous[T] { | ||
return &Rendezvous[T]{ | ||
sessions: concurrentmap.NewConcurrentMap[*TokenSlot[T]](), | ||
} | ||
} | ||
|
||
func (rendezvous *Rendezvous[T]) Request(ctx context.Context, session string) (chan T, func()) { | ||
tokenSlot := &TokenSlot[T]{ | ||
ctx: ctx, | ||
ch: make(chan T), | ||
} | ||
|
||
rendezvous.sessions.Store(session, tokenSlot) | ||
|
||
return tokenSlot.ch, func() { | ||
rendezvous.sessions.Delete(session) | ||
} | ||
} | ||
|
||
func (rendezvous *Rendezvous[T]) Respond(session string, conn T) (context.Context, error) { | ||
tokenSlot, ok := rendezvous.sessions.Load(session) | ||
if !ok { | ||
return nil, ErrInvalidToken | ||
} | ||
|
||
tokenSlot.ch <- conn | ||
|
||
return tokenSlot.ctx, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.