Skip to content

Commit

Permalink
reserved sharing infrastructure in the agent (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Sep 17, 2024
1 parent cd253e4 commit df65230
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 68 deletions.
187 changes: 119 additions & 68 deletions agent/agentGrpc/agent.pb.go

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

5 changes: 5 additions & 0 deletions agent/agentGrpc/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ message ReleaseShareReply {
}

message ReservedShareReply {
string token = 1;
string backendMode = 2;
string shareMode = 3;
repeated string frontendEndpoints = 4;
string target = 5;
}

message ReservedShareRequest {
Expand Down
61 changes: 61 additions & 0 deletions agent/reservedShare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package agent

import (
"context"
"errors"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/agent/proctree"
"github.com/openziti/zrok/environment"
"os"
)

func (i *agentGrpcImpl) ReservedShare(_ context.Context, req *agentGrpc.ReservedShareRequest) (*agentGrpc.ReservedShareReply, error) {
root, err := environment.LoadRoot()
if err != nil {
return nil, err
}

if !root.IsEnabled() {
return nil, errors.New("unable to load environment; did you 'zrok enable'?")
}

shrCmd := []string{os.Args[0], "share", "reserved", "--agent"}
shr := &share{
reserved: true,
bootComplete: make(chan struct{}),
a: i.a,
}

if req.OverrideEndpoint != "" {
shrCmd = append(shrCmd, "--override-endpoint", req.OverrideEndpoint)
}

if req.Insecure {
shrCmd = append(shrCmd, "--insecure")
}
shr.insecure = req.Insecure

shrCmd = append(shrCmd, req.Token)
shr.token = req.Token

shr.process, err = proctree.StartChild(shr.tail, shrCmd...)
if err != nil {
return nil, err
}

go shr.monitor()
<-shr.bootComplete

if shr.bootErr == nil {
i.a.inShares <- shr
return &agentGrpc.ReservedShareReply{
Token: shr.token,
BackendMode: string(shr.backendMode),
ShareMode: string(shr.shareMode),
FrontendEndpoints: shr.frontendEndpoints,
Target: shr.target,
}, nil
}

return nil, shr.bootErr
}
15 changes: 15 additions & 0 deletions agent/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ func (s *share) tail(data []byte) {
s.token = str
}
}
if v, found := in["backend_mode"]; found {
if str, ok := v.(string); ok {
s.backendMode = sdk.BackendMode(str)
}
}
if v, found := in["share_mode"]; found {
if str, ok := v.(string); ok {
s.shareMode = sdk.ShareMode(str)
}
}
if v, found := in["frontend_endpoints"]; found {
if vArr, ok := v.([]interface{}); ok {
for _, v := range vArr {
Expand All @@ -70,6 +80,11 @@ func (s *share) tail(data []byte) {
}
}
}
if v, found := in["target"]; found {
if str, ok := v.(string); ok {
s.target = str
}
}
s.booted = true
} else {
s.bootErr = errors.New(line)
Expand Down
2 changes: 2 additions & 0 deletions cmd/zrok/shareReserved.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func (cmd *shareReservedCommand) run(_ *cobra.Command, args []string) {
if cmd.agent {
data := make(map[string]interface{})
data["token"] = resp.Payload.Token
data["backend_mode"] = resp.Payload.BackendMode
data["share_mode"] = resp.Payload.ShareMode
if resp.Payload.FrontendEndpoint != "" {
data["frontend_endpoints"] = resp.Payload.FrontendEndpoint
}
Expand Down

0 comments on commit df65230

Please sign in to comment.