Merge pull request #10997 from dmcgowan/backport-2.0-typeurl

[release/2.0] bump github.com/containerd/typeurl/v2 from 2.2.2 to 2.2.3
This commit is contained in:
Phil Estes 2024-11-12 15:29:20 -07:00 committed by GitHub
commit abd8c4c39f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 22 deletions

2
go.mod
View File

@ -27,7 +27,7 @@ require (
github.com/containerd/platforms v1.0.0-rc.0 github.com/containerd/platforms v1.0.0-rc.0
github.com/containerd/plugin v1.0.0 github.com/containerd/plugin v1.0.0
github.com/containerd/ttrpc v1.2.6 github.com/containerd/ttrpc v1.2.6
github.com/containerd/typeurl/v2 v2.2.2 github.com/containerd/typeurl/v2 v2.2.3
github.com/containerd/zfs/v2 v2.0.0-rc.0 github.com/containerd/zfs/v2 v2.0.0-rc.0
github.com/containernetworking/cni v1.2.3 github.com/containernetworking/cni v1.2.3
github.com/containernetworking/plugins v1.5.1 github.com/containernetworking/plugins v1.5.1

4
go.sum
View File

@ -698,8 +698,8 @@ github.com/containerd/plugin v1.0.0/go.mod h1:hQfJe5nmWfImiqT1q8Si3jLv3ynMUIBB47
github.com/containerd/ttrpc v1.2.2/go.mod h1:sIT6l32Ph/H9cvnJsfXM5drIVzTr5A2flTf1G5tYZak= github.com/containerd/ttrpc v1.2.2/go.mod h1:sIT6l32Ph/H9cvnJsfXM5drIVzTr5A2flTf1G5tYZak=
github.com/containerd/ttrpc v1.2.6 h1:zG+Kn5EZ6MUYCS1t2Hmt2J4tMVaLSFEJVOraDQwNPC4= github.com/containerd/ttrpc v1.2.6 h1:zG+Kn5EZ6MUYCS1t2Hmt2J4tMVaLSFEJVOraDQwNPC4=
github.com/containerd/ttrpc v1.2.6/go.mod h1:YCXHsb32f+Sq5/72xHubdiJRQY9inL4a4ZQrAbN1q9o= github.com/containerd/ttrpc v1.2.6/go.mod h1:YCXHsb32f+Sq5/72xHubdiJRQY9inL4a4ZQrAbN1q9o=
github.com/containerd/typeurl/v2 v2.2.2 h1:3jN/k2ysKuPCsln5Qv8bzR9cxal8XjkxPogJfSNO31k= github.com/containerd/typeurl/v2 v2.2.3 h1:yNA/94zxWdvYACdYO8zofhrTVuQY73fFU1y++dYSw40=
github.com/containerd/typeurl/v2 v2.2.2/go.mod h1:95ljDnPfD3bAbDJRugOiShd/DlAAsxGtUBhJxIn7SCk= github.com/containerd/typeurl/v2 v2.2.3/go.mod h1:95ljDnPfD3bAbDJRugOiShd/DlAAsxGtUBhJxIn7SCk=
github.com/containerd/zfs/v2 v2.0.0-rc.0 h1:0dRlgpoaepW7HuovtcvYQMF7NlpceQVdn7+3Udeth4M= github.com/containerd/zfs/v2 v2.0.0-rc.0 h1:0dRlgpoaepW7HuovtcvYQMF7NlpceQVdn7+3Udeth4M=
github.com/containerd/zfs/v2 v2.0.0-rc.0/go.mod h1:g36g/XCEGDRxUXIFdM3oWAEvmTvhfz/eKWElqg4Secw= github.com/containerd/zfs/v2 v2.0.0-rc.0/go.mod h1:g36g/XCEGDRxUXIFdM3oWAEvmTvhfz/eKWElqg4Secw=
github.com/containernetworking/cni v1.2.3 h1:hhOcjNVUQTnzdRJ6alC5XF+wd9mfGIUaj8FuJbEslXM= github.com/containernetworking/cni v1.2.3 h1:hhOcjNVUQTnzdRJ6alC5XF+wd9mfGIUaj8FuJbEslXM=

View File

@ -39,7 +39,7 @@ type handler interface {
Marshaller(interface{}) func() ([]byte, error) Marshaller(interface{}) func() ([]byte, error)
Unmarshaller(interface{}) func([]byte) error Unmarshaller(interface{}) func([]byte) error
TypeURL(interface{}) string TypeURL(interface{}) string
GetType(url string) reflect.Type GetType(url string) (reflect.Type, bool)
} }
// Definitions of common error types used throughout typeurl. // Definitions of common error types used throughout typeurl.
@ -240,7 +240,7 @@ func MarshalAnyToProto(from interface{}) (*anypb.Any, error) {
} }
func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error) { func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error) {
t, err := getTypeByUrl(typeURL) t, isProto, err := getTypeByUrl(typeURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -258,6 +258,7 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
} }
} }
if isProto {
pm, ok := v.(proto.Message) pm, ok := v.(proto.Message)
if ok { if ok {
return v, proto.Unmarshal(value, pm) return v, proto.Unmarshal(value, pm)
@ -268,17 +269,18 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
return v, unmarshal(value) return v, unmarshal(value)
} }
} }
}
// fallback to json unmarshaller // fallback to json unmarshaller
return v, json.Unmarshal(value, v) return v, json.Unmarshal(value, v)
} }
func getTypeByUrl(url string) (reflect.Type, error) { func getTypeByUrl(url string) (_ reflect.Type, isProto bool, _ error) {
mu.RLock() mu.RLock()
for t, u := range registry { for t, u := range registry {
if u == url { if u == url {
mu.RUnlock() mu.RUnlock()
return t, nil return t, false, nil
} }
} }
mu.RUnlock() mu.RUnlock()
@ -286,15 +288,15 @@ func getTypeByUrl(url string) (reflect.Type, error) {
if err != nil { if err != nil {
if errors.Is(err, protoregistry.NotFound) { if errors.Is(err, protoregistry.NotFound) {
for _, h := range handlers { for _, h := range handlers {
if t := h.GetType(url); t != nil { if t, isProto := h.GetType(url); t != nil {
return t, nil return t, isProto, nil
} }
} }
} }
return nil, fmt.Errorf("type with url %s: %w", url, ErrNotFound) return nil, false, fmt.Errorf("type with url %s: %w", url, ErrNotFound)
} }
empty := mt.New().Interface() empty := mt.New().Interface()
return reflect.TypeOf(empty).Elem(), nil return reflect.TypeOf(empty).Elem(), true, nil
} }
func tryDereference(v interface{}) reflect.Type { func tryDereference(v interface{}) reflect.Type {

View File

@ -59,10 +59,10 @@ func (gogoHandler) TypeURL(v interface{}) string {
return gogoproto.MessageName(pm) return gogoproto.MessageName(pm)
} }
func (gogoHandler) GetType(url string) reflect.Type { func (gogoHandler) GetType(url string) (reflect.Type, bool) {
t := gogoproto.MessageType(url) t := gogoproto.MessageType(url)
if t == nil { if t == nil {
return nil return nil, false
} }
return t.Elem() return t.Elem(), true
} }

2
vendor/modules.txt vendored
View File

@ -194,7 +194,7 @@ github.com/containerd/plugin/registry
# github.com/containerd/ttrpc v1.2.6 # github.com/containerd/ttrpc v1.2.6
## explicit; go 1.19 ## explicit; go 1.19
github.com/containerd/ttrpc github.com/containerd/ttrpc
# github.com/containerd/typeurl/v2 v2.2.2 # github.com/containerd/typeurl/v2 v2.2.3
## explicit; go 1.21 ## explicit; go 1.21
github.com/containerd/typeurl/v2 github.com/containerd/typeurl/v2
# github.com/containerd/zfs/v2 v2.0.0-rc.0 # github.com/containerd/zfs/v2 v2.0.0-rc.0