From 9e45a0d37be4c788aabb7c55c167f0671aacafb3 Mon Sep 17 00:00:00 2001 From: Dmitriy Matrenichev Date: Wed, 2 Oct 2024 21:43:20 +0300 Subject: [PATCH] chore: add proto-codec/codec Unify usage of proto codec v2 across our projects. For https://github.com/siderolabs/talos/issues/9404 Signed-off-by: Dmitriy Matrenichev --- cmd/talosctl/cmd/talos/root.go | 2 +- go.mod | 3 +- go.sum | 6 +- pkg/grpc/codec/codec.go | 102 --------------------------------- pkg/grpc/factory/factory.go | 2 +- 5 files changed, 8 insertions(+), 107 deletions(-) delete mode 100644 pkg/grpc/codec/codec.go diff --git a/cmd/talosctl/cmd/talos/root.go b/cmd/talosctl/cmd/talos/root.go index 46a6d300337..5f14f990866 100644 --- a/cmd/talosctl/cmd/talos/root.go +++ b/cmd/talosctl/cmd/talos/root.go @@ -12,13 +12,13 @@ import ( "strings" "github.com/siderolabs/gen/maps" + _ "github.com/siderolabs/proto-codec/codec" // register codec v2 "github.com/spf13/cobra" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/peer" "github.com/siderolabs/talos/cmd/talosctl/pkg/talos/global" - _ "github.com/siderolabs/talos/pkg/grpc/codec" // register codec "github.com/siderolabs/talos/pkg/machinery/api/common" machineapi "github.com/siderolabs/talos/pkg/machinery/api/machine" "github.com/siderolabs/talos/pkg/machinery/client" diff --git a/go.mod b/go.mod index 065dc559199..91df62052b1 100644 --- a/go.mod +++ b/go.mod @@ -164,6 +164,7 @@ require ( github.com/siderolabs/grpc-proxy v0.5.1 github.com/siderolabs/kms-client v0.1.0 github.com/siderolabs/net v0.4.0 + github.com/siderolabs/proto-codec v0.1.1 github.com/siderolabs/protoenc v0.2.1 github.com/siderolabs/siderolink v0.3.11 github.com/siderolabs/talos/pkg/machinery v1.8.0-alpha.2 @@ -323,7 +324,7 @@ require ( github.com/pierrec/lz4/v4 v4.1.18 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect + github.com/planetscale/vtprotobuf v0.6.1-0.20240917153116-6f2963f01587 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.20.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect diff --git a/go.sum b/go.sum index e5e34d6ee32..5aea9a05e16 100644 --- a/go.sum +++ b/go.sum @@ -534,8 +534,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pkg/xattr v0.4.10 h1:Qe0mtiNFHQZ296vRgUjRCoPHPqH7VdTOrZx3g0T+pGA= github.com/pkg/xattr v0.4.10/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU= -github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo= -github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8= +github.com/planetscale/vtprotobuf v0.6.1-0.20240917153116-6f2963f01587 h1:xzZOeCMQLA/W198ZkdVdt4EKFKJtS26B773zNU377ZY= +github.com/planetscale/vtprotobuf v0.6.1-0.20240917153116-6f2963f01587/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -625,6 +625,8 @@ github.com/siderolabs/kms-client v0.1.0 h1:rCDWzcDDsNlp6zdyLngOuuhchVILn+vwUQy3t github.com/siderolabs/kms-client v0.1.0/go.mod h1:4UQkRhuEh3kaK7VhJxez4YyJLv6lPEff7g3Pa6Y9okg= github.com/siderolabs/net v0.4.0 h1:1bOgVay/ijPkJz4qct98nHsiB/ysLQU0KLoBC4qLm7I= github.com/siderolabs/net v0.4.0/go.mod h1:/ibG+Hm9HU27agp5r9Q3eZicEfjquzNzQNux5uEk0kM= +github.com/siderolabs/proto-codec v0.1.1 h1:4jiUwW/vaXTZ+YNgZDs37B4aj/1mzV/erIkzUUCRY9g= +github.com/siderolabs/proto-codec v0.1.1/go.mod h1:rIvmhKJG8+JwSCGPX+cQljpOMDmuHhLKPkt6KaFwEaU= github.com/siderolabs/protoenc v0.2.1 h1:BqxEmeWQeMpNP3R6WrPqDatX8sM/r4t97OP8mFmg6GA= github.com/siderolabs/protoenc v0.2.1/go.mod h1:StTHxjet1g11GpNAWiATgc8K0HMKiFSEVVFOa/H0otc= github.com/siderolabs/siderolink v0.3.11 h1:teJ/LMjSyLekhJVy2+nDIuOBPrVRAMwusJQzxdA95K0= diff --git a/pkg/grpc/codec/codec.go b/pkg/grpc/codec/codec.go deleted file mode 100644 index 91a2625592f..00000000000 --- a/pkg/grpc/codec/codec.go +++ /dev/null @@ -1,102 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -// Package codec registers the gRPC for optimized marshaling. -// -// Package should be dummy imported to enable. -package codec - -import ( - "fmt" - - "google.golang.org/grpc/encoding" - "google.golang.org/grpc/encoding/proto" - "google.golang.org/grpc/mem" - - talosproto "github.com/siderolabs/talos/pkg/machinery/proto" -) - -// gogoMessage is the interface for gogoproto additions. -// -// We use only a subset of that interface but include additional methods -// to prevent accidental successful type assertion for unrelated types. -type gogoMessage interface { - Marshal() ([]byte, error) - MarshalTo([]byte) (int, error) - MarshalToSizedBuffer([]byte) (int, error) - Unmarshal([]byte) error -} - -// Codec provides protobuf encoding.Codec. -type Codec struct{} - -// Marshal implements encoding.Codec. -func (Codec) Marshal(v any) ([]byte, error) { - // some third-party types (like from etcd and containerd) implement gogoMessage - if gm, ok := v.(gogoMessage); ok { - return gm.Marshal() - } - - // our types implement Message (with or without vtproto additions depending on build configuration) - if m, ok := v.(talosproto.Message); ok { - return talosproto.Marshal(m) - } - - // no types implement protobuf API v1 only, so don't check for it - - return nil, fmt.Errorf("failed to marshal %T", v) -} - -// Unmarshal implements encoding.Codec. -func (Codec) Unmarshal(data []byte, v any) error { - // some third-party types (like from etcd and containerd) implement gogoMessage - if gm, ok := v.(gogoMessage); ok { - return gm.Unmarshal(data) - } - - // our types implement Message (with or without vtproto additions depending on build configuration) - if m, ok := v.(talosproto.Message); ok { - return talosproto.Unmarshal(data, m) - } - - // no types implement protobuf API v1 only, so don't check for it - - return fmt.Errorf("failed to unmarshal %T", v) -} - -// Name implements encoding.Codec. -func (Codec) Name() string { - return proto.Name // overrides google.golang.org/grpc/encoding/proto codec -} - -type codecV1Bridge struct { - codec interface { - Marshal(v any) ([]byte, error) - Unmarshal(data []byte, v any) error - Name() string - } -} - -var _ encoding.CodecV2 = codecV1Bridge{} - -func (c codecV1Bridge) Marshal(v any) (mem.BufferSlice, error) { - data, err := c.codec.Marshal(v) - if err != nil { - return nil, err - } - - return mem.BufferSlice{mem.NewBuffer(&data, nil)}, nil -} - -func (c codecV1Bridge) Unmarshal(data mem.BufferSlice, v any) (err error) { - return c.codec.Unmarshal(data.Materialize(), v) -} - -func (c codecV1Bridge) Name() string { - return c.codec.Name() -} - -func init() { - encoding.RegisterCodecV2(codecV1Bridge{codec: Codec{}}) -} diff --git a/pkg/grpc/factory/factory.go b/pkg/grpc/factory/factory.go index da98e7f44fd..f3ff9128fa1 100644 --- a/pkg/grpc/factory/factory.go +++ b/pkg/grpc/factory/factory.go @@ -17,12 +17,12 @@ import ( "strconv" grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery" + _ "github.com/siderolabs/proto-codec/codec" // register codec v2 "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/reflection" "google.golang.org/grpc/status" - _ "github.com/siderolabs/talos/pkg/grpc/codec" // register codec grpclog "github.com/siderolabs/talos/pkg/grpc/middleware/log" )