Skip to content

Instantly share code, notes, and snippets.

@pcn
Created January 28, 2019 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcn/712a6d4e1c24bd52f82b360cd8a7cd72 to your computer and use it in GitHub Desktop.
Save pcn/712a6d4e1c24bd52f82b360cd8a7cd72 to your computer and use it in GitHub Desktop.
Further down the skycfg/protobof/envoy rabbit hole
package main
import (
"context"
"fmt"
"reflect"
docopt "github.com/docopt/docopt-go"
"github.com/gogo/protobuf/jsonpb"
gogo_proto "github.com/gogo/protobuf/proto"
proto "github.com/golang/protobuf/proto"
_ "github.com/golang/protobuf/ptypes/wrappers"
"github.com/stripe/skycfg"
yaml "gopkg.in/yaml.v2"
_ "github.com/envoyproxy/go-control-plane/envoy/admin/v2alpha"
_ "github.com/envoyproxy/go-control-plane/envoy/api/v2"
_ "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v2"
)
type protoRegistry struct{}
func (*protoRegistry) UnstableProtoMessageType(name string) (reflect.Type, error) {
if t := proto.MessageType(name); t != nil {
return t, nil
}
if t := gogo_proto.MessageType(name); t != nil {
return t, nil
}
return nil, nil
}
func (*protoRegistry) UnstableEnumValueMap(name string) map[string]int32 {
if ev := proto.EnumValueMap(name); ev != nil {
return ev
}
if ev := gogo_proto.EnumValueMap(name); ev != nil {
return ev
}
return nil
}
func main() {
usage := `envoy-proto: creates an envoy configuration from a skycfg file
Usage:
envoy-proto <input-file>
envoy-proto -h | --help
Options:
<input-file> skycfg file to be read
-h --help Show this screen.`
arguments, _ := docopt.ParseDoc(usage)
ctx := context.Background()
config, err := skycfg.Load(ctx, arguments["<input-file>"].(string), skycfg.WithProtoRegistry(&protoRegistry{}))
if err != nil {
panic(err)
}
messages, err := config.Main(ctx)
if err != nil {
panic(err)
}
for _, msg := range messages {
var jsonMarshaler = &jsonpb.Marshaler{OrigName: true}
marshaled, err := jsonMarshaler.MarshalToString(msg)
sep := ""
var yamlMap yaml.MapSlice
if err := yaml.Unmarshal([]byte(marshaled), &yamlMap); err != nil {
panic(fmt.Sprintf("yaml.Unmarshal: %v", err))
}
yamlMarshaled, err := yaml.Marshal(yamlMap)
if err != nil {
panic(fmt.Sprintf("yaml.Marshal: %v", err))
}
marshaled = string(yamlMarshaled)
sep = "---\n"
fmt.Printf("%s%s\n", sep, marshaled)
}
}

Tried to get this working with the UInt32Value protobuf message, per the envoy protobuf xdi:

rm reproducer; go build; ./reproducer uint32_reproducer.sky
[uint32_reproducer.sky:14] dur is <google.protobuf.UInt32Value value:20 >
panic: TypeError: value <google.protobuf.UInt32Value value:20 > (type `google.protobuf.UInt32Value') can't be assigned to type `google.protobuf.UInt32Value'.

goroutine 1 [running]:
main.main()
	/home/spacey/go/src/github.com/pcn/reproducer/main.go:65 +0x64b
spacey@masonjar:~/go/src/github.com/pcn/reproducer$ 
# -*- python -*-
pb = proto.package("google.protobuf")
v2 = proto.package("envoy.api.v2")
v2_cluster = proto.package("envoy.api.v2.cluster")
def duration(dur):
thresh = v2_cluster.CircuitBreakers.Thresholds(
max_connections=dur
)
return v2.Cluster(thresholds = [thresh])
def main(context):
dur = proto.from_text(pb.UInt32Value, "value: 20")
print("dur is {}".format(dur))
duration(dur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment