Skip to content

Commit

Permalink
Merge pull request openservicemesh#3534 from shashankram/protoutil
Browse files Browse the repository at this point in the history
utils/proto: consolidate proto helpers
  • Loading branch information
shashankram authored Jun 7, 2021
2 parents c086a47 + ee23bc0 commit 102419a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 42 deletions.
41 changes: 2 additions & 39 deletions pkg/injector/envoy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (
"fmt"
"time"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand All @@ -27,6 +24,7 @@ import (
"github.com/openservicemesh/osm/pkg/certificate"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/utils"
"github.com/openservicemesh/osm/pkg/version"
)

Expand Down Expand Up @@ -93,7 +91,7 @@ func getEnvoyConfigYAML(config envoyBootstrapConfigMeta, cfg configurator.Config
}
bootstrap.StaticResources = staticResources

configYAML, err := protoToYAML(bootstrap)
configYAML, err := utils.ProtoToYAML(bootstrap)
if err != nil {
log.Error().Err(err).Msgf("Failed to marshal envoy bootstrap config to yaml")
return nil, err
Expand Down Expand Up @@ -299,38 +297,3 @@ func getXdsCluster(config envoyBootstrapConfigMeta) (*xds_cluster.Cluster, error
},
}, nil
}

func protoToYAML(m protoreflect.ProtoMessage) ([]byte, error) {
marshalOptions := protojson.MarshalOptions{
UseProtoNames: true,
}
configJSON, err := marshalOptions.Marshal(m)
if err != nil {
return nil, err
}

configYAML, err := jsonToYAML(configJSON)
if err != nil {
log.Error().Err(err).Msgf("Error marshaling xDS struct into YAML")
return nil, err
}
return configYAML, err
}

// Reference impl taken from https://github.com/ghodss/yaml/blob/master/yaml.go#L87
func jsonToYAML(jb []byte) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal([]byte(jb), &jsonObj)
if err != nil {
return nil, err
}

// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
}
7 changes: 4 additions & 3 deletions pkg/injector/envoy_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/constants"
k8s "github.com/openservicemesh/osm/pkg/kubernetes"
"github.com/openservicemesh/osm/pkg/utils"
"github.com/openservicemesh/osm/pkg/version"
)

Expand Down Expand Up @@ -189,7 +190,7 @@ var _ = Describe("Test functions creating Envoy bootstrap configuration", func()
actualXds, err := getXdsCluster(config)
Expect(err).To(BeNil())

actualYAML, err := protoToYAML(actualXds)
actualYAML, err := utils.ProtoToYAML(actualXds)
Expect(err).To(BeNil())
saveActualEnvoyYAML(actualXDSClusterWithoutProbesFileName, actualYAML)
// The "marshalAndSaveToFile" function converts the complex struct into a human readable text, which helps us spot the
Expand All @@ -206,7 +207,7 @@ var _ = Describe("Test functions creating Envoy bootstrap configuration", func()
actualXds, err := getXdsCluster(config)
Expect(err).To(BeNil())

actualYAML, err := protoToYAML(actualXds)
actualYAML, err := utils.ProtoToYAML(actualXds)
Expect(err).To(BeNil())
saveActualEnvoyYAML(actualXDSClusterWithProbesFileName, actualYAML)
// The "marshalAndSaveToFile" function converts the complex struct into a human readable text, which helps us spot the
Expand All @@ -225,7 +226,7 @@ var _ = Describe("Test functions creating Envoy bootstrap configuration", func()
actualXds, err := getStaticResources(config)
Expect(err).To(BeNil())

actualYAML, err := protoToYAML(actualXds)
actualYAML, err := utils.ProtoToYAML(actualXds)
Expect(err).To(BeNil())
saveActualEnvoyYAML(actualXDSStaticResourcesWithProbesFileName, actualYAML)

Expand Down
44 changes: 44 additions & 0 deletions pkg/utils/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utils

import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/reflect/protoreflect"
yaml "gopkg.in/yaml.v2"
)

// ProtoToYAML converts a Proto message to it's YAML representation in bytes
func ProtoToYAML(m protoreflect.ProtoMessage) ([]byte, error) {
marshalOptions := protojson.MarshalOptions{
UseProtoNames: true,
}
configJSON, err := marshalOptions.Marshal(m)
if err != nil {
return nil, err
}

configYAML, err := jsonToYAML(configJSON)
if err != nil {
log.Error().Err(err).Msgf("Error marshaling xDS struct into YAML")
return nil, err
}
return configYAML, err
}

// jsonToYAML converts a JSON representation in bytes to the corresponding YAML representation in bytes
// Reference impl taken from https://github.com/ghodss/yaml/blob/master/yaml.go#L87
func jsonToYAML(jb []byte) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshalling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal([]byte(jb), &jsonObj)
if err != nil {
return nil, err
}

// Marshal this object into YAML.
return yaml.Marshal(jsonObj)
}

0 comments on commit 102419a

Please sign in to comment.