Skip to content

Commit

Permalink
print grpc function names when fds is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjumani committed Jul 4, 2024
1 parent 76fa978 commit 3288526
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog/v1.14.32/infer-gloo-deploy-name-114.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ changelog:
issueLink: https://github.com/solo-io/gloo/issues/9163
resolvesIssue: false
description: Infer the gloo deployment name in cases where the deployment name is not the default `gloo`. The gloo deployment is identified by the `gloo=gloo` label.
- type: FIX
issueLink: https://github.com/solo-io/gloo/issues/9644
resolvesIssue: false
description: Fix a bug where the service and function names of a discovered gRPC service are not printed when running glooctl get upstreams
44 changes: 41 additions & 3 deletions projects/gloo/cli/pkg/printers/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"os"
"sort"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"

"github.com/solo-io/gloo/projects/gloo/cli/pkg/xdsinspection"
plugins "github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/aws/ec2"
Expand Down Expand Up @@ -50,7 +55,6 @@ func UpstreamTable(xdsDump *xdsinspection.XdsDump, upstreams []*v1.Upstream, w i
table.Append([]string{"", "", "", line})
}
}

}

table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand Down Expand Up @@ -216,13 +220,47 @@ func linesForServiceSpec(serviceSpec *plugins.ServiceSpec) []string {
add(fmt.Sprintf(" - %v", fn))
}
}
case *plugins.ServiceSpec_GrpcJsonTranscoder:
add("gRPC service:")
descriptorBin := plug.GrpcJsonTranscoder.GetProtoDescriptorBin()
for _, grpcService := range plug.GrpcJsonTranscoder.GetServices() {
add(fmt.Sprintf(" %v", grpcService))
methodDescriptors := getMethodDescriptors(grpcService, descriptorBin)
for i := 0; i < methodDescriptors.Len(); i++ {
add(fmt.Sprintf(" - %v", methodDescriptors.Get(i).Name()))
}
}
}

return spec
}

func getMethodDescriptors(service string, descriptorSet []byte) protoreflect.MethodDescriptors {
fds := &descriptor.FileDescriptorSet{}
err := proto.Unmarshal(descriptorSet, fds)
if err != nil {
fmt.Println("unable to unmarshal descriptor")
return nil
}
files, err := protodesc.NewFiles(fds)
if err != nil {
fmt.Println("unable to create proto registry files")
return nil
}
descriptor, err := files.FindDescriptorByName(protoreflect.FullName(service))
if err != nil {
fmt.Println("unable to fin descriptor")
return nil
}
serviceDescriptor, ok := descriptor.(protoreflect.ServiceDescriptor)
if !ok {
fmt.Println("unable to decode service descriptor")
return nil
}
return serviceDescriptor.Methods()
}

// stringifyKey for a resource likely could be done more nicely with spew
// or a better accessor but minimall this avoids panicing on nested references to nils
// or a better accessor but minimal this avoids panicing on nested references to nils
func stringifyKey(plausiblyNilRef *core.ResourceRef) string {

if plausiblyNilRef == nil {
Expand Down
36 changes: 36 additions & 0 deletions projects/gloo/cli/pkg/printers/upstream_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package printers

import (
"bytes"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/grpc_json"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/options/kubernetes"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
)

var _ = Describe("UpstreamTable", func() {
Expand All @@ -13,4 +19,34 @@ var _ = Describe("UpstreamTable", func() {
UpstreamTable(nil, []*v1.Upstream{us}, GinkgoWriter)
}).NotTo(Panic())
})

It("prints grpc upstream function names", func() {
us := &v1.Upstream{
Metadata: &core.Metadata{
Name: "test-us",
},
UpstreamType: &v1.Upstream_Kube{
Kube: &kubernetes.UpstreamSpec{
ServiceName: "test",
ServiceNamespace: "gloo-system",
ServiceSpec: &options.ServiceSpec{
PluginType: &options.ServiceSpec_GrpcJsonTranscoder{
GrpcJsonTranscoder: &grpc_json.GrpcJsonTranscoder{
DescriptorSet: &grpc_json.GrpcJsonTranscoder_ProtoDescriptorBin{
ProtoDescriptorBin: []byte{10, 230, 1, 10, 16, 104, 101, 108, 108, 111, 119, 111, 114, 108, 100, 46, 112, 114, 111, 116, 111, 18, 10, 104, 101, 108, 108, 111, 119, 111, 114, 108, 100, 34, 28, 10, 12, 72, 101, 108, 108, 111, 82, 101, 113, 117, 101, 115, 116, 18, 12, 10, 4, 110, 97, 109, 101, 24, 1, 32, 1, 40, 9, 34, 29, 10, 10, 72, 101, 108, 108, 111, 82, 101, 112, 108, 121, 18, 15, 10, 7, 109, 101, 115, 115, 97, 103, 101, 24, 1, 32, 1, 40, 9, 50, 73, 10, 7, 71, 114, 101, 101, 116, 101, 114, 18, 62, 10, 8, 83, 97, 121, 72, 101, 108, 108, 111, 18, 24, 46, 104, 101, 108, 108, 111, 119, 111, 114, 108, 100, 46, 72, 101, 108, 108, 111, 82, 101, 113, 117, 101, 115, 116, 26, 22, 46, 104, 101, 108, 108, 111, 119, 111, 114, 108, 100, 46, 72, 101, 108, 108, 111, 82, 101, 112, 108, 121, 34, 0, 66, 54, 10, 27, 105, 111, 46, 103, 114, 112, 99, 46, 101, 120, 97, 109, 112, 108, 101, 115, 46, 104, 101, 108, 108, 111, 119, 111, 114, 108, 100, 66, 15, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 80, 114, 111, 116, 111, 80, 1, 162, 2, 3, 72, 76, 87, 98, 6, 112, 114, 111, 116, 111, 51},
},
Services: []string{"helloworld.Greeter"},
},
},
},
},
},
}

var out bytes.Buffer
UpstreamTable(nil, []*v1.Upstream{us}, &out)
// The `SayHello` method exists in the ProtoDescriptorBin. This should be printed when listing upstreams.
// Since there is only one service, it is safe to assume that this method belongs to it
Expect(out.String()).To(ContainSubstring("- SayHello"))
})
})

0 comments on commit 3288526

Please sign in to comment.