From 641347fe14a3629fc0d359ab4fa5c6079cefc186 Mon Sep 17 00:00:00 2001 From: Dan Upton Date: Mon, 17 Oct 2022 16:30:35 +0100 Subject: [PATCH] proto: deep-copy PeeringTrustBundle using proto.Clone (#15004) Fixes a `go vet` warning caused by the pragma.DoNotCopy on the protobuf message type. Originally I'd hoped we wouldn't need any reflection in the proxycfg hot path, but it seems proto.Clone is the only supported way to copy a message. --- GNUmakefile | 1 - proto/pbpeering/deep-copy.sh | 11 ----------- proto/pbpeering/peering.deepcopy.go | 17 ----------------- proto/pbpeering/peering.go | 12 ++++++++++++ 4 files changed, 12 insertions(+), 29 deletions(-) delete mode 100755 proto/pbpeering/deep-copy.sh delete mode 100644 proto/pbpeering/peering.deepcopy.go diff --git a/GNUmakefile b/GNUmakefile index 81240ef59b..ad5454991c 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -329,7 +329,6 @@ codegen-tools: .PHONY: deep-copy deep-copy: @$(SHELL) $(CURDIR)/agent/structs/deep-copy.sh - @$(SHELL) $(CURDIR)/proto/pbpeering/deep-copy.sh @$(SHELL) $(CURDIR)/agent/proxycfg/deep-copy.sh version: diff --git a/proto/pbpeering/deep-copy.sh b/proto/pbpeering/deep-copy.sh deleted file mode 100755 index 48acdd4a13..0000000000 --- a/proto/pbpeering/deep-copy.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -readonly PACKAGE_DIR="$(dirname "${BASH_SOURCE[0]}")" -cd $PACKAGE_DIR - -# Uses: https://github.com/globusdigital/deep-copy -deep-copy -pointer-receiver \ - -type PeeringTrustBundle \ - -o ./peering.deepcopy.go \ - ./ - diff --git a/proto/pbpeering/peering.deepcopy.go b/proto/pbpeering/peering.deepcopy.go deleted file mode 100644 index 7ba92d425c..0000000000 --- a/proto/pbpeering/peering.deepcopy.go +++ /dev/null @@ -1,17 +0,0 @@ -// generated by deep-copy -pointer-receiver -type PeeringTrustBundle -o ./peering.deepcopy.go ./; DO NOT EDIT. - -package pbpeering - -// DeepCopy generates a deep copy of *PeeringTrustBundle -func (o *PeeringTrustBundle) DeepCopy() *PeeringTrustBundle { - var cp PeeringTrustBundle = *o - if o.unknownFields != nil { - cp.unknownFields = make([]byte, len(o.unknownFields)) - copy(cp.unknownFields, o.unknownFields) - } - if o.RootPEMs != nil { - cp.RootPEMs = make([]string, len(o.RootPEMs)) - copy(cp.RootPEMs, o.RootPEMs) - } - return &cp -} diff --git a/proto/pbpeering/peering.go b/proto/pbpeering/peering.go index b5d36e73dd..511de42c57 100644 --- a/proto/pbpeering/peering.go +++ b/proto/pbpeering/peering.go @@ -10,6 +10,7 @@ import ( "github.com/golang/protobuf/ptypes/timestamp" "google.golang.org/grpc" "google.golang.org/grpc/credentials" + "google.golang.org/protobuf/proto" "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/api" @@ -302,3 +303,14 @@ func TimePtrToProto(s *time.Time) *timestamp.Timestamp { } return structs.TimeToProto(*s) } + +// DeepCopy returns a copy of the PeeringTrustBundle that can be passed around +// without worrying about the receiver unsafely modifying it. It is used by the +// generated DeepCopy methods in proxycfg. +func (o *PeeringTrustBundle) DeepCopy() *PeeringTrustBundle { + cp, ok := proto.Clone(o).(*PeeringTrustBundle) + if !ok { + panic(fmt.Sprintf("failed to clone *PeeringTrustBundle, got: %T", cp)) + } + return cp +}