Skip to content

Commit

Permalink
wired in console endpoint into agent, wired into 'zrok agent console' (
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Oct 4, 2024
1 parent b0178a0 commit d3568c0
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 62 deletions.
14 changes: 10 additions & 4 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

type Agent struct {
cfg *AgentConfig
root env_core.Root
agentSocket string
shares map[string]*share
Expand All @@ -28,11 +29,12 @@ type Agent struct {
rmAccess chan *access
}

func NewAgent(root env_core.Root) (*Agent, error) {
func NewAgent(cfg *AgentConfig, root env_core.Root) (*Agent, error) {
if !root.IsEnabled() {
return nil, errors.Errorf("unable to load environment; did you 'zrok enable'?")
}
return &Agent{
cfg: cfg,
root: root,
shares: make(map[string]*share),
addShare: make(chan *share),
Expand Down Expand Up @@ -61,7 +63,7 @@ func (a *Agent) Run() error {
a.agentSocket = agentSocket

go a.manager()
go a.gateway()
go a.gateway(a.cfg)

srv := grpc.NewServer()
agentGrpc.RegisterAgentServer(srv, &agentGrpcImpl{agent: a})
Expand All @@ -88,7 +90,11 @@ func (a *Agent) Shutdown() {
}
}

func (a *Agent) gateway() {
func (a *Agent) Config() *AgentConfig {
return a.cfg
}

func (a *Agent) gateway(cfg *AgentConfig) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -100,7 +106,7 @@ func (a *Agent) gateway() {
logrus.Fatalf("unable to register gateway: %v", err)
}

if err := http.ListenAndServe(":8888", agentUi.Middleware(mux)); err != nil {
if err := http.ListenAndServe(cfg.ConsoleEndpoint, agentUi.Middleware(mux)); err != nil {
logrus.Error(err)
}
}
Expand Down
89 changes: 50 additions & 39 deletions agent/agentGrpc/agent.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/agentGrpc/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,5 @@ message VersionRequest {

message VersionResponse {
string v = 1;
string consoleEndpoint = 2;
}
3 changes: 3 additions & 0 deletions agent/agentGrpc/agent.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@
"properties": {
"v": {
"type": "string"
},
"consoleEndpoint": {
"type": "string"
}
}
},
Expand Down
12 changes: 0 additions & 12 deletions agent/agentUi/src/api/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.babelrc
.gitignore
.openapi-generator-ignore
.travis.yml
README.md
docs/AccessDetail.md
Expand Down Expand Up @@ -30,14 +29,3 @@ src/model/SharePublicResponse.js
src/model/ShareReservedResponse.js
src/model/StatusResponse.js
src/model/VersionResponse.js
test/api/AgentApi.spec.js
test/model/AccessDetail.spec.js
test/model/AccessPrivateResponse.spec.js
test/model/ProtobufAny.spec.js
test/model/RpcStatus.spec.js
test/model/ShareDetail.spec.js
test/model/SharePrivateResponse.spec.js
test/model/SharePublicResponse.spec.js
test/model/ShareReservedResponse.spec.js
test/model/StatusResponse.spec.js
test/model/VersionResponse.spec.js
1 change: 1 addition & 0 deletions agent/agentUi/src/api/docs/VersionResponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**v** | **String** | | [optional]
**consoleEndpoint** | **String** | | [optional]


12 changes: 12 additions & 0 deletions agent/agentUi/src/api/src/model/VersionResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class VersionResponse {
if (data.hasOwnProperty('v')) {
obj['v'] = ApiClient.convertToType(data['v'], 'String');
}
if (data.hasOwnProperty('consoleEndpoint')) {
obj['consoleEndpoint'] = ApiClient.convertToType(data['consoleEndpoint'], 'String');
}
}
return obj;
}
Expand All @@ -64,6 +67,10 @@ class VersionResponse {
if (data['v'] && !(typeof data['v'] === 'string' || data['v'] instanceof String)) {
throw new Error("Expected the field `v` to be a primitive type in the JSON string but got " + data['v']);
}
// ensure the json data is a string
if (data['consoleEndpoint'] && !(typeof data['consoleEndpoint'] === 'string' || data['consoleEndpoint'] instanceof String)) {
throw new Error("Expected the field `consoleEndpoint` to be a primitive type in the JSON string but got " + data['consoleEndpoint']);
}

return true;
}
Expand All @@ -78,6 +85,11 @@ class VersionResponse {
*/
VersionResponse.prototype['v'] = undefined;

/**
* @member {String} consoleEndpoint
*/
VersionResponse.prototype['consoleEndpoint'] = undefined;




Expand Down
11 changes: 11 additions & 0 deletions agent/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package agent

type AgentConfig struct {
ConsoleEndpoint string
}

func DefaultAgentConfig() *AgentConfig {
return &AgentConfig{
ConsoleEndpoint: "127.0.0.1:8888",
}
}
5 changes: 4 additions & 1 deletion agent/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ import (
func (i *agentGrpcImpl) Version(_ context.Context, _ *agentGrpc.VersionRequest) (*agentGrpc.VersionResponse, error) {
v := build.String()
logrus.Debugf("responding to version inquiry with '%v'", v)
return &agentGrpc.VersionResponse{V: v}, nil
return &agentGrpc.VersionResponse{
V: v,
ConsoleEndpoint: i.agent.Config().ConsoleEndpoint,
}, nil
}
25 changes: 23 additions & 2 deletions cmd/zrok/agentConsole.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import (
"context"
"fmt"
"github.com/openziti/zrok/agent/agentClient"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/tui"
"github.com/spf13/cobra"
)
Expand All @@ -25,7 +30,23 @@ func newAgentConsoleCommand() *agentConsoleCommand {
}

func (cmd *agentConsoleCommand) run(_ *cobra.Command, _ []string) {
if err := openBrowser("http://localhost:8888"); err != nil {
tui.Error("unable to open agent console at 'http://localhost:8888'", err)
root, err := environment.LoadRoot()
if err != nil {
tui.Error("error loading zrokdir", err)
}

client, conn, err := agentClient.NewClient(root)
if err != nil {
tui.Error("error connecting to agent", err)
}
defer func() { _ = conn.Close() }()

v, err := client.Version(context.Background(), &agentGrpc.VersionRequest{})
if err != nil {
tui.Error("error getting agent version", err)
}

if err := openBrowser("http://" + v.ConsoleEndpoint); err != nil {
tui.Error(fmt.Sprintf("unable to open agent console at 'http://%v'", v.ConsoleEndpoint), err)
}
}
8 changes: 6 additions & 2 deletions cmd/zrok/agentStart.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func init() {
}

type agentStartCommand struct {
cmd *cobra.Command
cmd *cobra.Command
consoleEndpoint string
}

func newAgentStartCommand() *agentStartCommand {
Expand All @@ -26,6 +27,7 @@ func newAgentStartCommand() *agentStartCommand {
}
command := &agentStartCommand{cmd: cmd}
cmd.Run = command.run
cmd.Flags().StringVar(&command.consoleEndpoint, "console-endpoint", "127.0.0.1:8888", "gRPC gateway endpoint")
return command
}

Expand All @@ -39,7 +41,9 @@ func (cmd *agentStartCommand) run(_ *cobra.Command, _ []string) {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
}

a, err := agent.NewAgent(root)
cfg := agent.DefaultAgentConfig()
cfg.ConsoleEndpoint = cmd.consoleEndpoint
a, err := agent.NewAgent(cfg, root)
if err != nil {
tui.Error("error creating agent", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/zrok/agentVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"github.com/openziti/zrok/agent/agentClient"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/environment"
Expand Down Expand Up @@ -38,12 +39,12 @@ func (cmd *agentVersionCommand) run(_ *cobra.Command, _ []string) {
if err != nil {
tui.Error("error connecting to agent", err)
}
defer conn.Close()
defer func() { _ = conn.Close() }()

v, err := client.Version(context.Background(), &agentGrpc.VersionRequest{})
if err != nil {
tui.Error("error getting agent version", err)
}

println(v.GetV())
fmt.Printf("%v\n%v\n", v.GetV(), v.GetConsoleEndpoint())
}

0 comments on commit d3568c0

Please sign in to comment.