From b69025ce30c0ac23ec8c07b1bb10a9cace104cd2 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 26 Aug 2024 13:13:59 -0400 Subject: [PATCH] rudimentary 'zrok agent status' implementation (#463) --- agent/agent.go | 2 +- agent/agentGrpc/agent.pb.go | 113 +++++++++++++----------------- agent/agentGrpc/agent.proto | 15 ++-- agent/agentGrpc/agent_grpc.pb.go | 38 ++++++++++ agent/model.go | 11 ++- agent/status.go | 32 +++++++++ agent/{grpcImpl.go => version.go} | 6 +- cmd/zrok/agentStatus.go | 74 +++++++++++++++++++ cmd/zrok/agentVersion.go | 2 +- 9 files changed, 213 insertions(+), 80 deletions(-) create mode 100644 agent/status.go rename agent/{grpcImpl.go => version.go} (71%) create mode 100644 cmd/zrok/agentStatus.go diff --git a/agent/agent.go b/agent/agent.go index 2d01211d..adea2da9 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -42,7 +42,7 @@ func (a *Agent) Run() error { a.agentSocket = agentSocket srv := grpc.NewServer() - agentGrpc.RegisterAgentServer(srv, &agentGrpcImpl{}) + agentGrpc.RegisterAgentServer(srv, &agentGrpcImpl{a: a}) if err := srv.Serve(l); err != nil { return err } diff --git a/agent/agentGrpc/agent.pb.go b/agent/agentGrpc/agent.pb.go index 2cbd9e73..7c24933d 100644 --- a/agent/agentGrpc/agent.pb.go +++ b/agent/agentGrpc/agent.pb.go @@ -92,11 +92,10 @@ type ShareDetail struct { ShareMode string `protobuf:"bytes,2,opt,name=shareMode,proto3" json:"shareMode,omitempty"` BackendMode string `protobuf:"bytes,3,opt,name=backendMode,proto3" json:"backendMode,omitempty"` Reserved bool `protobuf:"varint,4,opt,name=reserved,proto3" json:"reserved,omitempty"` - UniqueName string `protobuf:"bytes,5,opt,name=uniqueName,proto3" json:"uniqueName,omitempty"` - FrontendEndpoint []string `protobuf:"bytes,6,rep,name=frontendEndpoint,proto3" json:"frontendEndpoint,omitempty"` - BackendEndpoint string `protobuf:"bytes,7,opt,name=backendEndpoint,proto3" json:"backendEndpoint,omitempty"` - Closed bool `protobuf:"varint,8,opt,name=closed,proto3" json:"closed,omitempty"` - Status string `protobuf:"bytes,9,opt,name=status,proto3" json:"status,omitempty"` + FrontendEndpoint []string `protobuf:"bytes,5,rep,name=frontendEndpoint,proto3" json:"frontendEndpoint,omitempty"` + BackendEndpoint string `protobuf:"bytes,6,opt,name=backendEndpoint,proto3" json:"backendEndpoint,omitempty"` + Closed bool `protobuf:"varint,7,opt,name=closed,proto3" json:"closed,omitempty"` + Status string `protobuf:"bytes,8,opt,name=status,proto3" json:"status,omitempty"` } func (x *ShareDetail) Reset() { @@ -159,13 +158,6 @@ func (x *ShareDetail) GetReserved() bool { return false } -func (x *ShareDetail) GetUniqueName() string { - if x != nil { - return x.UniqueName - } - return "" -} - func (x *ShareDetail) GetFrontendEndpoint() []string { if x != nil { return x.FrontendEndpoint @@ -198,9 +190,6 @@ type StatusRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - - Accesses []*AccessDetail `protobuf:"bytes,1,rep,name=accesses,proto3" json:"accesses,omitempty"` - Shares []*ShareDetail `protobuf:"bytes,2,rep,name=shares,proto3" json:"shares,omitempty"` } func (x *StatusRequest) Reset() { @@ -235,26 +224,13 @@ func (*StatusRequest) Descriptor() ([]byte, []int) { return file_agent_agentGrpc_agent_proto_rawDescGZIP(), []int{2} } -func (x *StatusRequest) GetAccesses() []*AccessDetail { - if x != nil { - return x.Accesses - } - return nil -} - -func (x *StatusRequest) GetShares() []*ShareDetail { - if x != nil { - return x.Shares - } - return nil -} - type StatusReply struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - V []string `protobuf:"bytes,1,rep,name=v,proto3" json:"v,omitempty"` + Accesses []*AccessDetail `protobuf:"bytes,1,rep,name=accesses,proto3" json:"accesses,omitempty"` + Shares []*ShareDetail `protobuf:"bytes,2,rep,name=shares,proto3" json:"shares,omitempty"` } func (x *StatusReply) Reset() { @@ -289,9 +265,16 @@ func (*StatusReply) Descriptor() ([]byte, []int) { return file_agent_agentGrpc_agent_proto_rawDescGZIP(), []int{3} } -func (x *StatusReply) GetV() []string { +func (x *StatusReply) GetAccesses() []*AccessDetail { if x != nil { - return x.V + return x.Accesses + } + return nil +} + +func (x *StatusReply) GetShares() []*ShareDetail { + if x != nil { + return x.Shares } return nil } @@ -393,7 +376,7 @@ var file_agent_agentGrpc_agent_proto_rawDesc = []byte{ 0x64, 0x72, 0x65, 0x73, 0x73, 0x12, 0x28, 0x0a, 0x0f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0f, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x22, - 0xa5, 0x02, 0x0a, 0x0b, 0x53, 0x68, 0x61, 0x72, 0x65, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12, + 0x85, 0x02, 0x0a, 0x0b, 0x53, 0x68, 0x61, 0x72, 0x65, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, 0x6e, 0x12, 0x1c, 0x0a, 0x09, 0x73, 0x68, 0x61, 0x72, 0x65, 0x4d, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x73, 0x68, 0x61, 0x72, 0x65, 0x4d, @@ -401,34 +384,34 @@ var file_agent_agentGrpc_agent_proto_rawDesc = []byte{ 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, 0x62, 0x61, 0x63, 0x6b, 0x65, 0x6e, 0x64, 0x4d, 0x6f, 0x64, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x72, 0x65, 0x73, 0x65, 0x72, 0x76, 0x65, - 0x64, 0x12, 0x1e, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x18, - 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x4e, 0x61, 0x6d, - 0x65, 0x12, 0x2a, 0x0a, 0x10, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x64, 0x45, 0x6e, 0x64, - 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x06, 0x20, 0x03, 0x28, 0x09, 0x52, 0x10, 0x66, 0x72, 0x6f, + 0x64, 0x12, 0x2a, 0x0a, 0x10, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x64, 0x45, 0x6e, 0x64, + 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x03, 0x28, 0x09, 0x52, 0x10, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x64, 0x45, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x28, 0x0a, 0x0f, 0x62, 0x61, 0x63, 0x6b, 0x65, 0x6e, 0x64, 0x45, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, - 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x62, 0x61, 0x63, 0x6b, 0x65, 0x6e, 0x64, 0x45, + 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x62, 0x61, 0x63, 0x6b, 0x65, 0x6e, 0x64, 0x45, 0x6e, 0x64, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6c, 0x6f, 0x73, 0x65, - 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x64, 0x12, - 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, - 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x60, 0x0a, 0x0d, 0x53, 0x74, 0x61, 0x74, 0x75, - 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x29, 0x0a, 0x08, 0x61, 0x63, 0x63, 0x65, - 0x73, 0x73, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x41, 0x63, 0x63, - 0x65, 0x73, 0x73, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x52, 0x08, 0x61, 0x63, 0x63, 0x65, 0x73, - 0x73, 0x65, 0x73, 0x12, 0x24, 0x0a, 0x06, 0x73, 0x68, 0x61, 0x72, 0x65, 0x73, 0x18, 0x02, 0x20, - 0x03, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x53, 0x68, 0x61, 0x72, 0x65, 0x44, 0x65, 0x74, 0x61, 0x69, - 0x6c, 0x52, 0x06, 0x73, 0x68, 0x61, 0x72, 0x65, 0x73, 0x22, 0x1b, 0x0a, 0x0b, 0x53, 0x74, 0x61, - 0x74, 0x75, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0c, 0x0a, 0x01, 0x76, 0x18, 0x01, 0x20, - 0x03, 0x28, 0x09, 0x52, 0x01, 0x76, 0x22, 0x10, 0x0a, 0x0e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, - 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x1c, 0x0a, 0x0c, 0x56, 0x65, 0x72, 0x73, - 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0c, 0x0a, 0x01, 0x76, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x01, 0x76, 0x32, 0x34, 0x0a, 0x05, 0x41, 0x67, 0x65, 0x6e, 0x74, 0x12, - 0x2b, 0x0a, 0x07, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x0f, 0x2e, 0x56, 0x65, 0x72, - 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0d, 0x2e, 0x56, 0x65, - 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x2a, 0x5a, 0x28, - 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x7a, - 0x69, 0x74, 0x69, 0x2f, 0x7a, 0x72, 0x6f, 0x6b, 0x2f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x2f, 0x61, - 0x67, 0x65, 0x6e, 0x74, 0x47, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x64, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x64, 0x12, + 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x22, 0x0f, 0x0a, 0x0d, 0x53, 0x74, 0x61, 0x74, 0x75, + 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x5e, 0x0a, 0x0b, 0x53, 0x74, 0x61, 0x74, + 0x75, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x29, 0x0a, 0x08, 0x61, 0x63, 0x63, 0x65, 0x73, + 0x73, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x41, 0x63, 0x63, 0x65, + 0x73, 0x73, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, 0x52, 0x08, 0x61, 0x63, 0x63, 0x65, 0x73, 0x73, + 0x65, 0x73, 0x12, 0x24, 0x0a, 0x06, 0x73, 0x68, 0x61, 0x72, 0x65, 0x73, 0x18, 0x02, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x53, 0x68, 0x61, 0x72, 0x65, 0x44, 0x65, 0x74, 0x61, 0x69, 0x6c, + 0x52, 0x06, 0x73, 0x68, 0x61, 0x72, 0x65, 0x73, 0x22, 0x10, 0x0a, 0x0e, 0x56, 0x65, 0x72, 0x73, + 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x22, 0x1c, 0x0a, 0x0c, 0x56, 0x65, + 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x0c, 0x0a, 0x01, 0x76, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, 0x76, 0x32, 0x5e, 0x0a, 0x05, 0x41, 0x67, 0x65, 0x6e, + 0x74, 0x12, 0x28, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x0e, 0x2e, 0x53, 0x74, + 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0c, 0x2e, 0x53, 0x74, + 0x61, 0x74, 0x75, 0x73, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x12, 0x2b, 0x0a, 0x07, 0x56, + 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x0f, 0x2e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0d, 0x2e, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, + 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x2a, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, + 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x7a, 0x69, 0x74, 0x69, 0x2f, + 0x7a, 0x72, 0x6f, 0x6b, 0x2f, 0x61, 0x67, 0x65, 0x6e, 0x74, 0x2f, 0x61, 0x67, 0x65, 0x6e, 0x74, + 0x47, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -453,12 +436,14 @@ var file_agent_agentGrpc_agent_proto_goTypes = []any{ (*VersionReply)(nil), // 5: VersionReply } var file_agent_agentGrpc_agent_proto_depIdxs = []int32{ - 0, // 0: StatusRequest.accesses:type_name -> AccessDetail - 1, // 1: StatusRequest.shares:type_name -> ShareDetail - 4, // 2: Agent.Version:input_type -> VersionRequest - 5, // 3: Agent.Version:output_type -> VersionReply - 3, // [3:4] is the sub-list for method output_type - 2, // [2:3] is the sub-list for method input_type + 0, // 0: StatusReply.accesses:type_name -> AccessDetail + 1, // 1: StatusReply.shares:type_name -> ShareDetail + 2, // 2: Agent.Status:input_type -> StatusRequest + 4, // 3: Agent.Version:input_type -> VersionRequest + 3, // 4: Agent.Status:output_type -> StatusReply + 5, // 5: Agent.Version:output_type -> VersionReply + 4, // [4:6] is the sub-list for method output_type + 2, // [2:4] is the sub-list for method input_type 2, // [2:2] is the sub-list for extension type_name 2, // [2:2] is the sub-list for extension extendee 0, // [0:2] is the sub-list for field type_name diff --git a/agent/agentGrpc/agent.proto b/agent/agentGrpc/agent.proto index ae3676f9..a022e63d 100644 --- a/agent/agentGrpc/agent.proto +++ b/agent/agentGrpc/agent.proto @@ -3,6 +3,7 @@ syntax = "proto3"; option go_package = "github.com/openziti/zrok/agent/agentGrpc"; service Agent { + rpc Status(StatusRequest) returns (StatusReply) {} rpc Version(VersionRequest) returns (VersionReply) {} } @@ -17,20 +18,18 @@ message ShareDetail { string shareMode = 2; string backendMode = 3; bool reserved = 4; - string uniqueName = 5; - repeated string frontendEndpoint = 6; - string backendEndpoint = 7; - bool closed = 8; - string status = 9; + repeated string frontendEndpoint = 5; + string backendEndpoint = 6; + bool closed = 7; + string status = 8; } message StatusRequest { - repeated AccessDetail accesses = 1; - repeated ShareDetail shares = 2; } message StatusReply { - repeated string v = 1; + repeated AccessDetail accesses = 1; + repeated ShareDetail shares = 2; } message VersionRequest { diff --git a/agent/agentGrpc/agent_grpc.pb.go b/agent/agentGrpc/agent_grpc.pb.go index 397aa46c..26cefa8f 100644 --- a/agent/agentGrpc/agent_grpc.pb.go +++ b/agent/agentGrpc/agent_grpc.pb.go @@ -19,6 +19,7 @@ import ( const _ = grpc.SupportPackageIsVersion9 const ( + Agent_Status_FullMethodName = "/Agent/Status" Agent_Version_FullMethodName = "/Agent/Version" ) @@ -26,6 +27,7 @@ const ( // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type AgentClient interface { + Status(ctx context.Context, in *StatusRequest, opts ...grpc.CallOption) (*StatusReply, error) Version(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionReply, error) } @@ -37,6 +39,16 @@ func NewAgentClient(cc grpc.ClientConnInterface) AgentClient { return &agentClient{cc} } +func (c *agentClient) Status(ctx context.Context, in *StatusRequest, opts ...grpc.CallOption) (*StatusReply, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(StatusReply) + err := c.cc.Invoke(ctx, Agent_Status_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *agentClient) Version(ctx context.Context, in *VersionRequest, opts ...grpc.CallOption) (*VersionReply, error) { cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) out := new(VersionReply) @@ -51,6 +63,7 @@ func (c *agentClient) Version(ctx context.Context, in *VersionRequest, opts ...g // All implementations must embed UnimplementedAgentServer // for forward compatibility. type AgentServer interface { + Status(context.Context, *StatusRequest) (*StatusReply, error) Version(context.Context, *VersionRequest) (*VersionReply, error) mustEmbedUnimplementedAgentServer() } @@ -62,6 +75,9 @@ type AgentServer interface { // pointer dereference when methods are called. type UnimplementedAgentServer struct{} +func (UnimplementedAgentServer) Status(context.Context, *StatusRequest) (*StatusReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Status not implemented") +} func (UnimplementedAgentServer) Version(context.Context, *VersionRequest) (*VersionReply, error) { return nil, status.Errorf(codes.Unimplemented, "method Version not implemented") } @@ -86,6 +102,24 @@ func RegisterAgentServer(s grpc.ServiceRegistrar, srv AgentServer) { s.RegisterService(&Agent_ServiceDesc, srv) } +func _Agent_Status_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(StatusRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AgentServer).Status(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Agent_Status_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AgentServer).Status(ctx, req.(*StatusRequest)) + } + return interceptor(ctx, in, info, handler) +} + func _Agent_Version_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(VersionRequest) if err := dec(in); err != nil { @@ -111,6 +145,10 @@ var Agent_ServiceDesc = grpc.ServiceDesc{ ServiceName: "Agent", HandlerType: (*AgentServer)(nil), Methods: []grpc.MethodDesc{ + { + MethodName: "Status", + Handler: _Agent_Status_Handler, + }, { MethodName: "Version", Handler: _Agent_Version_Handler, diff --git a/agent/model.go b/agent/model.go index 1c103705..fd0bf81d 100644 --- a/agent/model.go +++ b/agent/model.go @@ -1,16 +1,20 @@ package agent import ( + "github.com/openziti/zrok/agent/agentGrpc" "github.com/openziti/zrok/sdk/golang/sdk" "time" ) type share struct { - token string + token string + target string basicAuth []string frontendSelection []string + shareMode sdk.ShareMode backendMode sdk.BackendMode + reserved bool insecure bool oauthProvider string oauthEmailAddressPatterns []string @@ -25,3 +29,8 @@ type access struct { bindAddress string responseHeaders []string } + +type agentGrpcImpl struct { + agentGrpc.UnimplementedAgentServer + a *Agent +} diff --git a/agent/status.go b/agent/status.go new file mode 100644 index 00000000..e0824bc9 --- /dev/null +++ b/agent/status.go @@ -0,0 +1,32 @@ +package agent + +import ( + "context" + "github.com/openziti/zrok/agent/agentGrpc" +) + +func (i *agentGrpcImpl) Status(_ context.Context, _ *agentGrpc.StatusRequest) (*agentGrpc.StatusReply, error) { + var accesses []*agentGrpc.AccessDetail + for token, acc := range i.a.accesses { + accesses = append(accesses, &agentGrpc.AccessDetail{ + Token: token, + BindAddress: acc.bindAddress, + ResponseHeaders: acc.responseHeaders, + }) + } + + var shares []*agentGrpc.ShareDetail + for token, shr := range i.a.shares { + shares = append(shares, &agentGrpc.ShareDetail{ + Token: token, + ShareMode: string(shr.shareMode), + BackendMode: string(shr.backendMode), + Reserved: shr.reserved, + FrontendEndpoint: shr.frontendSelection, + BackendEndpoint: shr.target, + Closed: shr.closed, + }) + } + + return &agentGrpc.StatusReply{Accesses: accesses, Shares: shares}, nil +} diff --git a/agent/grpcImpl.go b/agent/version.go similarity index 71% rename from agent/grpcImpl.go rename to agent/version.go index ba7ff568..02788c24 100644 --- a/agent/grpcImpl.go +++ b/agent/version.go @@ -7,11 +7,7 @@ import ( "github.com/sirupsen/logrus" ) -type agentGrpcImpl struct { - agentGrpc.UnimplementedAgentServer -} - -func (s *agentGrpcImpl) Version(_ context.Context, _ *agentGrpc.VersionRequest) (*agentGrpc.VersionReply, error) { +func (i *agentGrpcImpl) Version(_ context.Context, _ *agentGrpc.VersionRequest) (*agentGrpc.VersionReply, error) { v := build.String() logrus.Infof("responding to version inquiry with '%v'", v) return &agentGrpc.VersionReply{V: v}, nil diff --git a/cmd/zrok/agentStatus.go b/cmd/zrok/agentStatus.go new file mode 100644 index 00000000..184dbe14 --- /dev/null +++ b/cmd/zrok/agentStatus.go @@ -0,0 +1,74 @@ +package main + +import ( + "context" + "fmt" + "github.com/jedib0t/go-pretty/v6/table" + "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" + "os" +) + +func init() { + agentCmd.AddCommand(newAgentStatusCommand().cmd) +} + +type agentStatusCommand struct { + cmd *cobra.Command +} + +func newAgentStatusCommand() *agentStatusCommand { + cmd := &cobra.Command{ + Use: "status", + Short: "Show the status of the running zrok Agent", + Args: cobra.NoArgs, + } + command := &agentStatusCommand{cmd: cmd} + cmd.Run = command.run + return command +} + +func (cmd *agentStatusCommand) run(_ *cobra.Command, _ []string) { + 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 conn.Close() + + status, err := client.Status(context.Background(), &agentGrpc.StatusRequest{}) + if err != nil { + tui.Error("error getting status", err) + } + + fmt.Println() + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.SetStyle(table.StyleColoredDark) + t.AppendHeader(table.Row{"Token", "Bind Address"}) + for _, access := range status.GetAccesses() { + t.AppendRow(table.Row{access.Token, access.BindAddress}) + } + t.Render() + fmt.Printf("%d accesses in agent\n", len(status.GetAccesses())) + + fmt.Println() + t = table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.SetStyle(table.StyleColoredDark) + t.AppendHeader(table.Row{"Token", "Reserved", "Share Mode", "Backend Mode", "Target"}) + for _, share := range status.GetShares() { + t.AppendRow(table.Row{share.Token, share.Reserved, share.ShareMode, share.BackendMode, share.BackendEndpoint}) + } + t.Render() + fmt.Printf("%d shares in agent\n", len(status.GetShares())) + + fmt.Println() +} diff --git a/cmd/zrok/agentVersion.go b/cmd/zrok/agentVersion.go index a25afa27..dc522a63 100644 --- a/cmd/zrok/agentVersion.go +++ b/cmd/zrok/agentVersion.go @@ -36,7 +36,7 @@ func (cmd *agentVersionCommand) run(_ *cobra.Command, _ []string) { client, conn, err := agentClient.NewClient(root) if err != nil { - tui.Error("error getting agent client", err) + tui.Error("error connecting to agent", err) } defer conn.Close()