-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add agent commands functionality + flare (#19)
This is much of the scafolding needed to support sending commands to the agent. To begin with, we will be sending commands to request diagnostic data about the Linkerd proxy. Signed-off-by: Zahari Dichev <[email protected]>
- Loading branch information
1 parent
a733b03
commit 4ea5a52
Showing
25 changed files
with
1,681 additions
and
349 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,9 @@ | ||
package api | ||
|
||
import ( | ||
pb "github.com/buoyantio/linkerd-buoyant/gen/bcloud" | ||
) | ||
|
||
func (c *Client) AgentCommands() <-chan *pb.AgentCommand { | ||
return c.manageAgentStream.commands | ||
} |
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,83 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
pb "github.com/buoyantio/linkerd-buoyant/gen/bcloud" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// manageAgentStream wraps the Buoyant Cloud API ManageAgent gRPC endpoint, and | ||
// manages the stream. | ||
type manageAgentStream struct { | ||
auth *pb.Auth | ||
client pb.ApiClient | ||
stream pb.Api_ManageAgentClient | ||
log *log.Entry | ||
|
||
commands chan *pb.AgentCommand | ||
|
||
// protects stream | ||
sync.Mutex | ||
} | ||
|
||
func newManageAgentStream(auth *pb.Auth, client pb.ApiClient) *manageAgentStream { | ||
return &manageAgentStream{ | ||
auth: auth, | ||
client: client, | ||
log: log.WithField("stream", "ManageAgentStream"), | ||
commands: make(chan *pb.AgentCommand), | ||
} | ||
} | ||
|
||
func (s *manageAgentStream) startStream() { | ||
for { | ||
command, err := s.recvCommand() | ||
if err != nil { | ||
s.log.Infof("stream closed, reseting: %s", err) | ||
s.resetStream() | ||
continue | ||
} | ||
s.commands <- command | ||
} | ||
} | ||
|
||
func (s *manageAgentStream) recvCommand() (*pb.AgentCommand, error) { | ||
s.Lock() | ||
defer s.Unlock() | ||
if s.stream == nil { | ||
s.stream = s.newStream() | ||
} | ||
|
||
return s.stream.Recv() | ||
} | ||
|
||
func (s *manageAgentStream) newStream() pb.Api_ManageAgentClient { | ||
var stream pb.Api_ManageAgentClient | ||
|
||
// loop until the request to initiate a stream succeeds | ||
for { | ||
var err error | ||
stream, err = s.client.ManageAgent(context.Background(), s.auth) | ||
if err != nil { | ||
s.log.Errorf("failed to initiate stream: %s", err) | ||
time.Sleep(100 * time.Millisecond) | ||
continue | ||
} | ||
break | ||
} | ||
|
||
s.log.Info("ManageAgentStream connected") | ||
return stream | ||
} | ||
|
||
func (s *manageAgentStream) resetStream() { | ||
s.Lock() | ||
defer s.Unlock() | ||
if s.stream != nil { | ||
s.stream.CloseSend() | ||
s.stream = 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,79 @@ | ||
package api | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
pb "github.com/buoyantio/linkerd-buoyant/gen/bcloud" | ||
) | ||
|
||
func TestManageAgentStream(t *testing.T) { | ||
t.Run("streams and resets", func(t *testing.T) { | ||
fixtures := []*struct { | ||
testName string | ||
commandsFromApi []*pb.AgentCommand | ||
}{ | ||
{ | ||
"receives commands", | ||
[]*pb.AgentCommand{ | ||
createDiagnosticCommand("id1", "pod1", "ns1"), | ||
createDiagnosticCommand("id2", "pod2", "ns2"), | ||
createDiagnosticCommand("id3", "pod3", "ns3"), | ||
createDiagnosticCommand("id4", "pod4", "ns4"), | ||
createDiagnosticCommand("id5", "pod5", "ns5"), | ||
createDiagnosticCommand("id6", "pod6", "ns6"), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range fixtures { | ||
tc := tc | ||
t.Run(tc.testName, func(t *testing.T) { | ||
m := &MockBcloudClient{agentCommandMessages: tc.commandsFromApi} | ||
c := NewClient("", "", m) | ||
go c.Start() | ||
|
||
receivedCommands := []*pb.AgentCommand{} | ||
|
||
timeout := time.After(time.Second * 10) | ||
|
||
out: | ||
for { | ||
select { | ||
case cmd := <-c.AgentCommands(): | ||
receivedCommands = append(receivedCommands, cmd) | ||
if len(receivedCommands) >= len(tc.commandsFromApi) { | ||
break out | ||
} | ||
case <-timeout: | ||
t.Fatal("test timed out") | ||
} | ||
} | ||
|
||
if len(receivedCommands) != len(tc.commandsFromApi) { | ||
t.Fatalf("Expected to receive %d commands, got: %d", len(tc.commandsFromApi), len(receivedCommands)) | ||
} | ||
|
||
for i, expectedCommand := range tc.commandsFromApi { | ||
actualCommand := receivedCommands[i] | ||
if !reflect.DeepEqual(expectedCommand, actualCommand) { | ||
t.Fatalf("Expected command %d to be %+v, got %+v", i, expectedCommand, actualCommand) | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
func createDiagnosticCommand(diagnosticID, podName string, podNamespace string) *pb.AgentCommand { | ||
return &pb.AgentCommand{ | ||
Command: &pb.AgentCommand_GetProxyDiagnostics{ | ||
GetProxyDiagnostics: &pb.GetProxyDiagnostics{ | ||
DiagnosticId: diagnosticID, | ||
PodName: podName, | ||
PodNamespace: podNamespace, | ||
}, | ||
}, | ||
} | ||
} |
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,33 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
||
pb "github.com/buoyantio/linkerd-buoyant/gen/bcloud" | ||
"google.golang.org/protobuf/encoding/prototext" | ||
) | ||
|
||
// ProxyDiagnostics wraps the Buoyant Cloud API ProxyDiagnostics gRPC unary endpoint. | ||
func (c *Client) ProxyDiagnostics( | ||
diagnosticID string, | ||
logs []byte, | ||
metrics [][]byte, | ||
podManifest *pb.Pod, | ||
linkerdConfigMap *pb.ConfigMap, | ||
nodes []*pb.Node, | ||
k8sServiceManifest *pb.Service) error { | ||
diagnostic := &pb.ProxyDiagnostic{ | ||
Auth: c.auth, | ||
DiagnosticId: diagnosticID, | ||
Logs: logs, | ||
Metrics: metrics, | ||
PodManifest: podManifest, | ||
LinkerdConfigMap: linkerdConfigMap, | ||
Nodes: nodes, | ||
K8SServiceManifest: k8sServiceManifest, | ||
} | ||
c.log.Tracef("ProxyDiagnostics: %s", prototext.Format(diagnostic)) | ||
|
||
_, err := c.client.ProxyDiagnostics(context.Background(), diagnostic) | ||
return err | ||
} |
Oops, something went wrong.