-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assign
ssh.localPort
automatically to an available port
- The hostagent now speaks REST API over `ha.sock` to provide the port information. See `pkg/hostagent/api`. - For backward compatibility, the "default" instance uses port 60022 by default. Close issue 131 Signed-off-by: Akihiro Suda <[email protected]>
- Loading branch information
1 parent
1b260c2
commit e5da6ee
Showing
29 changed files
with
286 additions
and
77 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
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
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
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
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,5 @@ | ||
package api | ||
|
||
type Info struct { | ||
SSHLocalPort int `json:"sshLocalPort,omitempty"` | ||
} |
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,64 @@ | ||
package client | ||
|
||
// Forked from https://github.com/rootless-containers/rootlesskit/blob/v0.14.2/pkg/api/client/client.go | ||
// Apache License 2.0 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/lima-vm/lima/pkg/hostagent/api" | ||
"github.com/lima-vm/lima/pkg/httpclientutil" | ||
) | ||
|
||
type HostAgentClient interface { | ||
HTTPClient() *http.Client | ||
Info(context.Context) (*api.Info, error) | ||
} | ||
|
||
// NewHostAgentClient creates a client. | ||
// socketPath is a path to the UNIX socket, without unix:// prefix. | ||
func NewHostAgentClient(socketPath string) (HostAgentClient, error) { | ||
hc, err := httpclientutil.NewHTTPClientWithSocketPath(socketPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewHostAgentClientWithHTTPClient(hc), nil | ||
} | ||
|
||
func NewHostAgentClientWithHTTPClient(hc *http.Client) HostAgentClient { | ||
return &client{ | ||
Client: hc, | ||
version: "v1", | ||
dummyHost: "lima-hostagent", | ||
} | ||
} | ||
|
||
type client struct { | ||
*http.Client | ||
// version is always "v1" | ||
// TODO(AkihiroSuda): negotiate the version | ||
version string | ||
dummyHost string | ||
} | ||
|
||
func (c *client) HTTPClient() *http.Client { | ||
return c.Client | ||
} | ||
|
||
func (c *client) Info(ctx context.Context) (*api.Info, error) { | ||
u := fmt.Sprintf("http://%s/%s/info", c.dummyHost, c.version) | ||
resp, err := httpclientutil.Get(ctx, c.HTTPClient(), u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
var info api.Info | ||
dec := json.NewDecoder(resp.Body) | ||
if err := dec.Decode(&info); err != nil { | ||
return nil, err | ||
} | ||
return &info, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/lima-vm/lima/pkg/hostagent" | ||
"github.com/lima-vm/lima/pkg/httputil" | ||
) | ||
|
||
type Backend struct { | ||
Agent *hostagent.HostAgent | ||
} | ||
|
||
func (b *Backend) onError(w http.ResponseWriter, r *http.Request, err error, ec int) { | ||
w.WriteHeader(ec) | ||
w.Header().Set("Content-Type", "application/json") | ||
// it is safe to return the err to the client, because the client is reliable | ||
e := httputil.ErrorJSON{ | ||
Message: err.Error(), | ||
} | ||
_ = json.NewEncoder(w).Encode(e) | ||
} | ||
|
||
// GetInfo is the handler for GET /v{N}/info | ||
func (b *Backend) GetInfo(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
info, err := b.Agent.Info(ctx) | ||
if err != nil { | ||
b.onError(w, r, err, http.StatusInternalServerError) | ||
return | ||
} | ||
m, err := json.Marshal(info) | ||
if err != nil { | ||
b.onError(w, r, err, http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(m) | ||
} | ||
|
||
func AddRoutes(r *mux.Router, b *Backend) { | ||
v1 := r.PathPrefix("/v1").Subrouter() | ||
v1.Path("/info").Methods("GET").HandlerFunc(b.GetInfo) | ||
} |
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.