Skip to content

Commit

Permalink
replace cache.GetNodes with NodeWatcher in lib/web
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy committed Apr 28, 2022
1 parent ff8723d commit 18f9570
Show file tree
Hide file tree
Showing 7 changed files with 568 additions and 544 deletions.
6 changes: 6 additions & 0 deletions lib/services/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,12 @@ func (n *nodeCollector) GetNodes(fn func(n Node) bool) []types.Server {
return matched
}

func (n *nodeCollector) NodeCount() int {
n.rw.RLock()
defer n.rw.RUnlock()
return len(n.current)
}

// resourceKind specifies the resource kind to watch.
func (n *nodeCollector) resourceKind() string {
return types.KindNode
Expand Down
6 changes: 3 additions & 3 deletions lib/srv/regular/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ func (t *proxySubsys) proxyToHost(
return nil
}

// NodeGetter is a function that retrieves a subset of nodes matching
// NodesGetter is a function that retrieves a subset of nodes matching
// the filter criteria.
type NodeGetter interface {
type NodesGetter interface {
GetNodes(fn func(n services.Node) bool) []types.Server
}

Expand All @@ -464,7 +464,7 @@ type NodeGetter interface {
// is types.RoutingStrategy_UNAMBIGUOUS_MATCH. When the strategy is types.RoutingStrategy_MOST_RECENT then
// the server that has heartbeated most recently will be returned instead of an error. If no matches are found then
// both the types.Server and error returned will be nil.
func (t *proxySubsys) getMatchingServer(watcher NodeGetter, strategy types.RoutingStrategy) (types.Server, error) {
func (t *proxySubsys) getMatchingServer(watcher NodesGetter, strategy types.RoutingStrategy) (types.Server, error) {
if watcher == nil {
return nil, trace.NotFound("unable to retrieve nodes matching host %s", t.host)
}
Expand Down
13 changes: 4 additions & 9 deletions lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1934,12 +1934,12 @@ func (h *Handler) siteNodeConnect(
req.ProxyHostPort = h.ProxyHostPort()
req.Cluster = site.GetName()

clt, err := ctx.GetUserClient(site)
watcher, err := site.NodeWatcher()
if err != nil {
return nil, trace.Wrap(err)
}

term, err := NewTerminal(r.Context(), *req, clt, ctx)
term, err := NewTerminal(*req, watcher, ctx)
if err != nil {
h.log.WithError(err).Error("Unable to create terminal.")
return nil, trace.Wrap(err)
Expand All @@ -1963,24 +1963,19 @@ type siteSessionGenerateResponse struct {
// siteSessionCreate generates a new site session that can be used by UI
// The ServerID from request can be in the form of hostname, uuid, or ip address.
func (h *Handler) siteSessionGenerate(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *SessionContext, site reversetunnel.RemoteSite) (interface{}, error) {
clt, err := ctx.GetUserClient(site)
if err != nil {
return nil, trace.Wrap(err)
}

var req *siteSessionGenerateReq
if err := httplib.ReadJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
}

namespace := apidefaults.Namespace
if req.Session.ServerID != "" {
servers, err := clt.GetNodes(r.Context(), namespace)
watcher, err := site.NodeWatcher()
if err != nil {
return nil, trace.Wrap(err)
}

hostname, _, err := resolveServerHostPort(req.Session.ServerID, servers)
hostname, _, err := resolveServerHostPort(req.Session.ServerID, watcher)
if err != nil {
return nil, trace.Wrap(err)
}
Expand Down
Loading

0 comments on commit 18f9570

Please sign in to comment.