Skip to content

Commit

Permalink
chore(deps): remove deprecated protobuf dependency (#12038)
Browse files Browse the repository at this point in the history
## Motivation

<!-- Why are we doing this change -->

The repo `github.com/golang/protobuf` is deprecated and we should switch
to use `google.golang.org/protobuf`.

However we can't totally get rid of it because we still need the
`github.com/golang/protobuf/jsonpb` and there's a incompatibility for
`google.golang.org/protobuf/encoding/protojson`. So, I add one golangci
linter to warn it.


## Implementation information

<!-- Explain how this was done and potentially alternatives considered
and discarded -->

## Supporting documentation

<!-- Is there a MADR? An Issue? A related PR? -->

Fix #3499 

<!--
> Changelog: skip
-->
<!--
Uncomment the above section to explicitly set a [`> Changelog:` entry
here](https://github.com/kumahq/kuma/blob/master/CONTRIBUTING.md#submitting-a-patch)?
-->

---------

Signed-off-by: Icarus Wu <[email protected]>
  • Loading branch information
Icarus9913 authored Nov 23, 2024
1 parent 1a4408a commit 6e6683b
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 34 deletions.
20 changes: 13 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ linters-settings:
deny:
- pkg: "github.com/kumahq/kuma/app"
desc: "pkg can't import app"
DenyGolangProtobuf:
files:
- $all
deny:
- pkg: github.com/golang/protobuf
desc: "github.com/golang/protobuf is deprecated; use google.golang.org/protobuf instead"
DenyGoogleProtoJson:
files:
- $all
deny:
- pkg: google.golang.org/protobuf/encoding/protojson
desc: "don't use the protojson package, it's incompatible and might cause issues(https://github.com/golang/protobuf/issues/1374); use github.com/golang/protobuf/jsonpb instead"

misspell:
locale: US
Expand All @@ -119,13 +131,7 @@ issues:
exclude-rules:
- linters:
- staticcheck
text: 'SA1019: "github.com/golang/protobuf' # TODO ignore deprecation of proto library. We don't want to migrate yet because go-control-plane is not ready
- linters:
- staticcheck
text: "SA1019: proto.MessageName is deprecated" # TODO ignore deprecation of proto library. We don't want to migrate yet because go-control-plane is not ready
- linters:
- staticcheck
text: "SA1019: proto.MessageType is deprecated" # TODO ignore deprecation of proto library. We don't want to migrate yet because go-control-plane is not ready
text: 'SA1019: "github.com/golang/protobuf/jsonpb"' # We need to use the deprecated module since the jsonpb replacement is not backwards compatible.
- linters:
- staticcheck
text: "SA1019: l.UseOriginalDst is deprecated: Do not use." # TODO What is the up-to-date alternative ?
Expand Down
5 changes: 2 additions & 3 deletions pkg/core/xds/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

envoy_sd "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
protov1 "github.com/golang/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
Expand All @@ -31,7 +30,7 @@ type ResourceList []*Resource
func (rs ResourceList) ToDeltaDiscoveryResponse() (*envoy_sd.DeltaDiscoveryResponse, error) {
resp := &envoy_sd.DeltaDiscoveryResponse{}
for _, r := range rs {
pbany, err := anypb.New(protov1.MessageV2(r.Resource))
pbany, err := anypb.New(r.Resource)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -154,7 +153,7 @@ func (s *ResourceSet) AddSet(set *ResourceSet) *ResourceSet {
}

func (s *ResourceSet) typeName(resource ResourcePayload) string {
return "type.googleapis.com/" + string(protov1.MessageV2(resource).ProtoReflect().Descriptor().FullName())
return "type.googleapis.com/" + string(resource.ProtoReflect().Descriptor().FullName())
}

func (s *ResourceSet) List() ResourceList {
Expand Down
2 changes: 1 addition & 1 deletion pkg/mads/v1/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/emicklei/go-restful/v3"
v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
cache_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/jsonpb" // nolint: depguard

"github.com/kumahq/kuma/pkg/core"
rest_errors "github.com/kumahq/kuma/pkg/core/rest/errors"
Expand Down
2 changes: 1 addition & 1 deletion pkg/mads/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
"github.com/go-logr/logr"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/jsonpb" // nolint: depguard
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/types/known/anypb"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package xds
import (
envoy_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
"github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/protobuf/types/known/wrapperspb"

common_api "github.com/kumahq/kuma/api/common/v1alpha1"
api "github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/api/v1alpha1"
Expand Down Expand Up @@ -49,11 +49,11 @@ func (c *LoadBalancerConfigurer) Configure(cluster *envoy_cluster.Cluster) error
if c.LoadBalancer.RingHash == nil {
return nil
}
var minimumRingSize *wrappers.UInt64Value
var minimumRingSize *wrapperspb.UInt64Value
if min := c.LoadBalancer.RingHash.MinRingSize; min != nil {
minimumRingSize = util_proto.UInt64(uint64(*min))
}
var maximumRingSize *wrappers.UInt64Value
var maximumRingSize *wrapperspb.UInt64Value
if max := c.LoadBalancer.RingHash.MaxRingSize; max != nil {
maximumRingSize = util_proto.UInt64(uint64(*max))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/plugins/runtime/gateway/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
cache_v3 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
protov1 "github.com/golang/protobuf/proto"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -51,7 +50,7 @@ type ProtoMessage struct {
}

func (p ProtoMessage) MarshalJSON() ([]byte, error) {
return util_proto.ToJSON(protov1.MessageV2(p.Message))
return util_proto.ToJSON(p.Message)
}

type ProtoResource struct {
Expand Down
3 changes: 1 addition & 2 deletions pkg/test/kds/verifier/executables.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_sd "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
protov1 "github.com/golang/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

"github.com/kumahq/kuma/pkg/core/resources/model"
Expand Down Expand Up @@ -115,7 +114,7 @@ func DiscoveryResponse(rs model.ResourceList, nonce, version string) Executable
}
resources := make([]*anypb.Any, 0, len(envoyRes))
for i := 0; i < len(envoyRes); i++ {
pbaby, err := proto.MarshalAnyDeterministic(protov1.MessageV2(envoyRes[i]))
pbaby, err := proto.MarshalAnyDeterministic(envoyRes[i])
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/cache/v3/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
ctl_cache "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
protov1 "github.com/golang/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)

Expand All @@ -14,7 +13,7 @@ func ToDeltaDiscoveryResponse(s ctl_cache.Snapshot) (*v3.DeltaDiscoveryResponse,
for _, rs := range s.Resources {
for _, name := range sortedResourceNames(rs) {
r := rs.Items[name]
pbany, err := anypb.New(protov1.MessageV2(r.Resource))
pbany, err := anypb.New(r.Resource)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/envoy/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"

envoy_types "github.com/envoyproxy/go-control-plane/pkg/cache/types"
"github.com/golang/protobuf/ptypes/any"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"sigs.k8s.io/yaml"

util_proto "github.com/kumahq/kuma/pkg/util/proto"
Expand All @@ -17,7 +17,7 @@ func ResourceFromYaml(resYaml string) (proto.Message, error) {
json = []byte(resYaml)
}

var anything any.Any
var anything anypb.Any
if err := util_proto.FromJSON(json, &anything); err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/proto/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proto
import (
"strings"

protov1 "github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -22,7 +21,7 @@ func MarshalAnyDeterministic(pb proto.Message) (*anypb.Any, error) {
if err != nil {
return nil, err
}
name := string(protov1.MessageV2(pb).ProtoReflect().Descriptor().FullName())
name := string(pb.ProtoReflect().Descriptor().FullName())
return &anypb.Any{TypeUrl: googleApis + name, Value: bytes}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/util/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"fmt"

"github.com/golang/protobuf/jsonpb"
protov1 "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/jsonpb" // nolint: depguard
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/protoadapt"
"google.golang.org/protobuf/types/known/structpb"
"sigs.k8s.io/yaml"
)
Expand All @@ -25,7 +25,7 @@ func FromYAML(content []byte, pb proto.Message) error {

func ToYAML(pb proto.Message) ([]byte, error) {
marshaler := &jsonpb.Marshaler{}
json, err := marshaler.MarshalToString(protov1.MessageV1(pb))
json, err := marshaler.MarshalToString(protoadapt.MessageV1Of(pb))
if err != nil {
return nil, err
}
Expand All @@ -35,7 +35,7 @@ func ToYAML(pb proto.Message) ([]byte, error) {
func ToJSON(pb proto.Message) ([]byte, error) {
var buf bytes.Buffer
marshaler := &jsonpb.Marshaler{}
if err := marshaler.Marshal(&buf, protov1.MessageV1(pb)); err != nil {
if err := marshaler.Marshal(&buf, protoadapt.MessageV1Of(pb)); err != nil {
return nil, err
}
return buf.Bytes(), nil
Expand All @@ -52,7 +52,7 @@ func MustMarshalJSON(in proto.Message) []byte {

func FromJSON(content []byte, out proto.Message) error {
unmarshaler := &jsonpb.Unmarshaler{AllowUnknownFields: true}
return unmarshaler.Unmarshal(bytes.NewReader(content), protov1.MessageV1(out))
return unmarshaler.Unmarshal(bytes.NewReader(content), protoadapt.MessageV1Of(out))
}

// Converts loosely typed Struct to strongly typed Message
Expand Down
3 changes: 1 addition & 2 deletions pkg/xds/generator/modifications/v3/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v3
import (
envoy_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
envoy_resource "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
protov1 "github.com/golang/protobuf/proto"
"github.com/pkg/errors"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
Expand Down Expand Up @@ -34,7 +33,7 @@ func (c *clusterModificator) apply(resources *core_xds.ResourceSet) error {
func (c *clusterModificator) patch(resources *core_xds.ResourceSet, clusterMod *envoy_cluster.Cluster) {
for _, cluster := range resources.Resources(envoy_resource.ClusterType) {
if c.clusterMatches(cluster) {
util_proto.Merge(protov1.MessageV2(cluster.Resource), clusterMod)
util_proto.Merge(cluster.Resource, clusterMod)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/xds/generator/modifications/v3/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v3
import (
envoy_listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
envoy_resource "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
protov1 "github.com/golang/protobuf/proto"
"github.com/pkg/errors"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (l *listenerModificator) apply(resources *core_xds.ResourceSet) error {
func (l *listenerModificator) patch(resources *core_xds.ResourceSet, listenerPatch *envoy_listener.Listener) {
for _, listener := range resources.Resources(envoy_resource.ListenerType) {
if l.listenerMatches(listener) {
util_proto.Merge(protov1.MessageV2(listener.Resource), listenerPatch)
util_proto.Merge(listener.Resource, listenerPatch)
}
}
}
Expand Down

0 comments on commit 6e6683b

Please sign in to comment.