Skip to content

Commit b18e2e0

Browse files
authored
*: Prevent anypb.New usage (#10735)
Signed-off-by: timflannagan <[email protected]>
1 parent 7e83c18 commit b18e2e0

File tree

8 files changed

+24
-17
lines changed

8 files changed

+24
-17
lines changed

.golangci.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ linters:
1515
# We are aiming to progressively enable linters.
1616
# If you see a linter that can be added, please do so.
1717
- bodyclose
18+
- forbidigo
1819
- ginkgolinter
1920
- goimports
2021
- misspell
@@ -59,6 +60,10 @@ linters-settings:
5960
misspell:
6061
ignore-words:
6162
- kgateway
63+
forbidigo:
64+
forbid:
65+
- p: "anypb.New"
66+
msg: "Do not use anypb.New; Use utils.MessageToAny instead."
6267

6368
issues:
6469
# Maximum count of issues with the same text.

internal/kgateway/extensions2/plugins/backend/aws.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
envoyauth "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
1313
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
1414
awspb "github.com/solo-io/envoy-gloo/go/config/filter/http/aws_lambda/v2"
15-
"google.golang.org/protobuf/types/known/anypb"
1615

1716
"github.com/kgateway-dev/kgateway/v2/api/v1alpha1"
1817
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/extensions2/pluginutils"
1918
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/ir"
19+
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils"
2020
)
2121

2222
const (
@@ -49,7 +49,7 @@ func processAws(ctx context.Context, in *v1alpha1.AwsBackend, ir *BackendIr, out
4949
// TODO(yuval-k): Add verification context
5050
Sni: lambdaHostname,
5151
}
52-
typedConfig, err := anypb.New(tlsContext)
52+
typedConfig, err := utils.MessageToAny(tlsContext)
5353
if err != nil {
5454
// return err
5555
return
@@ -122,7 +122,7 @@ func (p *backendPlugin) processBackendAws(
122122
//UnwrapAsAlb: destination.GetUnwrapAsAlb(),
123123
//TransformerConfig: transformerConfig,
124124
}
125-
lambdaRouteFuncAny, err := anypb.New(lambdaRouteFunc)
125+
lambdaRouteFuncAny, err := utils.MessageToAny(lambdaRouteFunc)
126126
if err != nil {
127127
return err
128128
}

internal/kgateway/extensions2/plugins/istio/plugin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strconv"
77
"time"
88

9-
"google.golang.org/protobuf/types/known/anypb"
109
"google.golang.org/protobuf/types/known/structpb"
1110
"istio.io/istio/pkg/kube/krt"
1211
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -22,6 +21,7 @@ import (
2221
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/extensions2/common"
2322
extensionsplug "github.com/kgateway-dev/kgateway/v2/internal/kgateway/extensions2/plugin"
2423
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/ir"
24+
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils"
2525
ourwellknown "github.com/kgateway-dev/kgateway/v2/internal/kgateway/wellknown"
2626
"github.com/kgateway-dev/kgateway/v2/pkg/utils/envutils"
2727
)
@@ -203,7 +203,7 @@ func createIstioMatch(sni string) *envoy_config_cluster_v3.Cluster_TransportSock
203203
},
204204
}
205205

206-
typedConfig, _ := anypb.New(sslSds)
206+
typedConfig, _ := utils.MessageToAny(sslSds)
207207
transportSocket := &envoy_config_core_v3.TransportSocket{
208208
Name: wellknown.TransportSocketTls,
209209
ConfigType: &envoy_config_core_v3.TransportSocket_TypedConfig{TypedConfig: typedConfig},
@@ -218,7 +218,7 @@ func createIstioMatch(sni string) *envoy_config_cluster_v3.Cluster_TransportSock
218218

219219
func createDefaultIstioMatch() *envoy_config_cluster_v3.Cluster_TransportSocketMatch {
220220
// Based on Istio's default match https://github.com/istio/istio/blob/fa321ebd2a1186325788b0f461aa9f36a1a8d90e/pilot/pkg/xds/filters/filters.go#L78
221-
typedConfig, _ := anypb.New(&sockets_raw_buffer.RawBuffer{})
221+
typedConfig, _ := utils.MessageToAny(&sockets_raw_buffer.RawBuffer{})
222222
rawBufferTransportSocket := &envoy_config_core_v3.TransportSocket{
223223
Name: wellknown.TransportSocketRawBuffer,
224224
ConfigType: &envoy_config_core_v3.TransportSocket_TypedConfig{TypedConfig: typedConfig},

internal/kgateway/proxy_syncer/xdswrapper.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
envoycachetypes "github.com/envoyproxy/go-control-plane/pkg/cache/types"
99
envoycache "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
1010

11+
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils"
1112
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/xds"
1213
"github.com/kgateway-dev/kgateway/v2/pkg/utils/envutils"
1314

@@ -181,7 +182,7 @@ func visitMessage(msg protoreflect.Message, fd protoreflect.FieldDescriptor, v p
181182
}
182183
visitFields(visitMsg, sensitive)
183184
if anyMsg != nil {
184-
anymsg, _ := anypb.New(anyMsg)
185+
anymsg, _ := utils.MessageToAny(anyMsg)
185186
msg.Set(fd, protoreflect.ValueOf(anymsg.ProtoReflect()))
186187
}
187188
}

internal/kgateway/proxy_syncer/xdswrapper_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import (
1313
"google.golang.org/protobuf/types/known/anypb"
1414

1515
. "github.com/kgateway-dev/kgateway/v2/internal/kgateway/proxy_syncer"
16+
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils"
1617
)
1718

1819
func mustAny(src proto.Message) *anypb.Any {
19-
a, e := anypb.New(src)
20+
a, e := utils.MessageToAny(src)
2021
if e != nil {
2122
panic(e)
2223
}

internal/kgateway/translator/irtranslator/fc.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
envoy_tls_inspector "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/listener/tls_inspector/v3"
1919
"github.com/solo-io/go-utils/contextutils"
2020
"google.golang.org/protobuf/proto"
21-
"google.golang.org/protobuf/types/known/anypb"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
2423

@@ -72,7 +71,7 @@ func computeListenerAddress(bindAddress string, port uint32, reporter reports.Ga
7271

7372
func tlsInspectorFilter() *envoy_config_listener_v3.ListenerFilter {
7473
configEnvoy := &envoy_tls_inspector.TlsInspector{}
75-
msg, _ := anypb.New(configEnvoy)
74+
msg, _ := utils.MessageToAny(configEnvoy)
7675
return &envoy_config_listener_v3.ListenerFilter{
7776
Name: wellknown.TlsInspector,
7877
ConfigType: &envoy_config_listener_v3.ListenerFilter_TypedConfig{
@@ -401,7 +400,7 @@ func NewFilterWithTypedConfig(name string, config proto.Message) (*envoy_config_
401400
}
402401

403402
if config != nil {
404-
marshalledConf, err := anypb.New(config)
403+
marshalledConf, err := utils.MessageToAny(config)
405404
if err != nil {
406405
// this should NEVER HAPPEN!
407406
return &envoy_config_listener_v3.Filter{}, err
@@ -482,7 +481,7 @@ func (info *FilterChainInfo) toTransportSocket() *envoy_config_core_v3.Transport
482481
out := &envoyauth.DownstreamTlsContext{
483482
CommonTlsContext: common,
484483
}
485-
typedConfig, _ := anypb.New(out)
484+
typedConfig, _ := utils.MessageToAny(out)
486485

487486
return &envoy_config_core_v3.TransportSocket{
488487
Name: wellknown.TransportSocketTls,

internal/kgateway/translator/utils/cluster.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
55
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
66
envoy_upstreams_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/upstreams/http/v3"
7+
proto "google.golang.org/protobuf/proto"
78
"google.golang.org/protobuf/types/known/anypb"
89

9-
proto "google.golang.org/protobuf/proto"
10+
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/utils"
1011
)
1112

1213
func MutateHttpOptions(c *envoy_config_cluster_v3.Cluster, m func(*envoy_upstreams_v3.HttpProtocolOptions)) error {
@@ -22,7 +23,7 @@ func MutateHttpOptions(c *envoy_config_cluster_v3.Cluster, m func(*envoy_upstrea
2223
}
2324
m(http2ProtocolOptions)
2425

25-
a, err := anypb.New(http2ProtocolOptions)
26+
a, err := utils.MessageToAny(http2ProtocolOptions)
2627
if err != nil {
2728
return err
2829
}

pkg/utils/envoyutils/bootstrap/bootstrap.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func FromEnvoyResources(resources *EnvoyResources) (string, error) {
5353
// per-filter config matching the arguments, marshals it to json, and returns
5454
// the stringified json or any error if it occurred.
5555
func FromFilter(filterName string, msg proto.Message) (string, error) {
56-
typedFilter, err := anypb.New(msg)
56+
typedFilter, err := utils.MessageToAny(msg)
5757
if err != nil {
5858
return "", err
5959
}
@@ -83,7 +83,7 @@ func FromFilter(filterName string, msg proto.Message) (string, error) {
8383
},
8484
}
8585

86-
hcmAny, err := anypb.New(hcm)
86+
hcmAny, err := utils.MessageToAny(hcm)
8787
if err != nil {
8888
return "", err
8989
}
@@ -324,7 +324,7 @@ func setStaticRouteConfig(
324324
RouteConfig: r,
325325
}
326326

327-
hcmAny, err := anypb.New(hcm)
327+
hcmAny, err := utils.MessageToAny(hcm)
328328
if err != nil {
329329
return err
330330
}

0 commit comments

Comments
 (0)