cloud/bmaa/reflection: render known protos as prototext
This extends the type and value structures of the reflection code to
support arbitrary Protobuf serialized messages. We currently identify
what message type is contained in a column by a hardcoded lookup table.
Change-Id: I31a260b7ed5582678803d27bf6ba30028cbea266
Reviewed-on: https://review.monogon.dev/c/monogon/+/1539
Reviewed-by: Leopold Schabel <leo@monogon.tech>
Tested-by: Jenkins CI
diff --git a/cloud/bmaas/bmdb/reflection/reflection.go b/cloud/bmaas/bmdb/reflection/reflection.go
index 1e2a414..6a45d27 100644
--- a/cloud/bmaas/bmdb/reflection/reflection.go
+++ b/cloud/bmaas/bmdb/reflection/reflection.go
@@ -17,7 +17,11 @@
"strings"
"time"
+ "k8s.io/klog/v2"
+
"github.com/google/uuid"
+ "google.golang.org/protobuf/encoding/prototext"
+ "google.golang.org/protobuf/proto"
)
// GetMachinesOpts influences the behaviour of GetMachines.
@@ -321,12 +325,19 @@
text *string
bytes *[]byte
time *time.Time
+ proto proto.Message
}
// HumanValue returns a human-readable (best effort) representation of the field
// value.
func (r *TagField) HumanValue() string {
switch {
+ case r.proto != nil:
+ opts := prototext.MarshalOptions{
+ Multiline: true,
+ Indent: "\t",
+ }
+ return opts.Format(r.proto)
case r.text != nil:
return *r.text
case r.bytes != nil:
@@ -385,6 +396,16 @@
copied := make([]byte, len(src2))
copy(copied[:], src2)
r.bytes = &copied
+
+ if r.Type.ProtoType != nil {
+ msg := r.Type.ProtoType.New().Interface()
+ err := proto.Unmarshal(*r.bytes, msg)
+ if err != nil {
+ klog.Warningf("Could not unmarshal %s: %v", r.Type.NativeName, err)
+ } else {
+ r.proto = msg
+ }
+ }
case "USER-DEFINED":
switch r.Type.NativeUDTName {
case "provider":