mirror of https://github.com/knative/pkg.git
parent
5bc49f27e6
commit
d771641c91
|
@ -18,30 +18,30 @@ package apis
|
|||
|
||||
import "context"
|
||||
|
||||
// ConvertUpViaProxy attempts to convert a specific source to a sink
|
||||
// ConvertToViaProxy attempts to convert a specific source to a sink
|
||||
// through a proxy
|
||||
func ConvertUpViaProxy(
|
||||
func ConvertToViaProxy(
|
||||
ctx context.Context,
|
||||
source, proxy, sink Convertible,
|
||||
) error {
|
||||
|
||||
if err := source.ConvertUp(ctx, proxy); err != nil {
|
||||
if err := source.ConvertTo(ctx, proxy); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return proxy.ConvertUp(ctx, sink)
|
||||
return proxy.ConvertTo(ctx, sink)
|
||||
}
|
||||
|
||||
// ConvertDownViaProxy attempts to convert a specific sink from a source
|
||||
// ConvertFromViaProxy attempts to convert a specific sink from a source
|
||||
// through a proxy
|
||||
func ConvertDownViaProxy(
|
||||
func ConvertFromViaProxy(
|
||||
ctx context.Context,
|
||||
source, proxy, sink Convertible,
|
||||
) error {
|
||||
|
||||
if err := proxy.ConvertDown(ctx, source); err != nil {
|
||||
if err := proxy.ConvertFrom(ctx, source); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return sink.ConvertDown(ctx, proxy)
|
||||
return sink.ConvertFrom(ctx, proxy)
|
||||
}
|
||||
|
|
|
@ -22,15 +22,15 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestConvertUpViaProxy(t *testing.T) {
|
||||
func TestConvertToViaProxy(t *testing.T) {
|
||||
sink := &testResource{}
|
||||
proxy := &testResource{}
|
||||
source := &testResource{proxy: proxy}
|
||||
|
||||
err := ConvertUpViaProxy(context.Background(), source, proxy, sink)
|
||||
err := ConvertToViaProxy(context.Background(), source, proxy, sink)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("ConvertUpViaProxy returned unexpected err: %s", err)
|
||||
t.Errorf("ConvertToViaProxy returned unexpected err: %s", err)
|
||||
}
|
||||
|
||||
if source.to != proxy {
|
||||
|
@ -42,7 +42,7 @@ func TestConvertUpViaProxy(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConvertUpViaProxyError(t *testing.T) {
|
||||
func TestConvertToViaProxyError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
source, proxy testResource
|
||||
|
@ -62,7 +62,7 @@ func TestConvertUpViaProxyError(t *testing.T) {
|
|||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ConvertUpViaProxy(context.Background(),
|
||||
err := ConvertToViaProxy(context.Background(),
|
||||
&test.source,
|
||||
&test.proxy,
|
||||
nil, /* sink */
|
||||
|
@ -75,15 +75,15 @@ func TestConvertUpViaProxyError(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConvertDownViaProxy(t *testing.T) {
|
||||
func TestConvertFromViaProxy(t *testing.T) {
|
||||
proxy := &testResource{}
|
||||
sink := &testResource{}
|
||||
source := &testResource{}
|
||||
|
||||
err := ConvertDownViaProxy(context.Background(), source, proxy, sink)
|
||||
err := ConvertFromViaProxy(context.Background(), source, proxy, sink)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("ConvertDownViaProxy returned unexpected err: %s", err)
|
||||
t.Errorf("ConvertFromViaProxy returned unexpected err: %s", err)
|
||||
}
|
||||
|
||||
if proxy.from != source {
|
||||
|
@ -95,7 +95,7 @@ func TestConvertDownViaProxy(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConvertDownViaProxyError(t *testing.T) {
|
||||
func TestConvertFromViaProxyError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
sink, proxy testResource
|
||||
|
@ -116,7 +116,7 @@ func TestConvertDownViaProxyError(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
err := ConvertDownViaProxy(context.Background(),
|
||||
err := ConvertFromViaProxy(context.Background(),
|
||||
nil, /* source */
|
||||
&test.proxy,
|
||||
&test.sink,
|
||||
|
@ -135,12 +135,12 @@ type testResource struct {
|
|||
|
||||
var _ Convertible = (*testResource)(nil)
|
||||
|
||||
func (r *testResource) ConvertUp(ctx context.Context, to Convertible) error {
|
||||
func (r *testResource) ConvertTo(ctx context.Context, to Convertible) error {
|
||||
r.to = to
|
||||
return r.err
|
||||
}
|
||||
|
||||
func (r *testResource) ConvertDown(ctx context.Context, from Convertible) error {
|
||||
func (r *testResource) ConvertFrom(ctx context.Context, from Convertible) error {
|
||||
r.from = from
|
||||
return r.err
|
||||
}
|
||||
|
|
|
@ -76,13 +76,13 @@ func (*Addressable) GetFullType() duck.Populatable {
|
|||
return &AddressableType{}
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (a *Addressable) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (a *Addressable) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
return fmt.Errorf("v1 is the highest known version, got: %T", to)
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (a *Addressable) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (a *Addressable) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
return fmt.Errorf("v1 is the highest known version, got: %T", from)
|
||||
}
|
||||
|
||||
|
|
|
@ -48,21 +48,21 @@ func TestConversion(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
conv := test.conv
|
||||
if err := test.addr.ConvertUp(context.Background(), conv); err != nil {
|
||||
if err := test.addr.ConvertTo(context.Background(), conv); err != nil {
|
||||
if !test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %v", err)
|
||||
t.Errorf("ConvertTo() = %v", err)
|
||||
}
|
||||
} else if test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %#v, wanted error", conv)
|
||||
t.Errorf("ConvertTo() = %#v, wanted error", conv)
|
||||
}
|
||||
got := &Addressable{}
|
||||
if err := got.ConvertDown(context.Background(), conv); err != nil {
|
||||
if err := got.ConvertFrom(context.Background(), conv); err != nil {
|
||||
if !test.wantErrDown {
|
||||
t.Errorf("ConvertDown() = %v", err)
|
||||
t.Errorf("ConvertFrom() = %v", err)
|
||||
}
|
||||
return
|
||||
} else if test.wantErrDown {
|
||||
t.Errorf("ConvertDown() = %#v, wanted error", conv)
|
||||
t.Errorf("ConvertFrom() = %#v, wanted error", conv)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -80,8 +80,8 @@ func (*Addressable) GetFullType() duck.Populatable {
|
|||
return &AddressableType{}
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (a *Addressable) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (a *Addressable) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
var url *apis.URL
|
||||
if a.URL != nil {
|
||||
url = a.URL
|
||||
|
@ -101,8 +101,8 @@ func (a *Addressable) ConvertUp(ctx context.Context, to apis.Convertible) error
|
|||
}
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (a *Addressable) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (a *Addressable) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
switch source := from.(type) {
|
||||
case *v1.Addressable:
|
||||
a.URL = source.URL.DeepCopy()
|
||||
|
|
|
@ -139,21 +139,21 @@ func TestConversion(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
conv := test.conv
|
||||
if err := test.addr.ConvertUp(context.Background(), conv); err != nil {
|
||||
if err := test.addr.ConvertTo(context.Background(), conv); err != nil {
|
||||
if !test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %v", err)
|
||||
t.Errorf("ConvertTo() = %v", err)
|
||||
}
|
||||
} else if test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %#v, wanted error", conv)
|
||||
t.Errorf("ConvertTo() = %#v, wanted error", conv)
|
||||
}
|
||||
got := &Addressable{}
|
||||
if err := got.ConvertDown(context.Background(), conv); err != nil {
|
||||
if err := got.ConvertFrom(context.Background(), conv); err != nil {
|
||||
if !test.wantErrDown {
|
||||
t.Errorf("ConvertDown() = %v", err)
|
||||
t.Errorf("ConvertFrom() = %v", err)
|
||||
}
|
||||
return
|
||||
} else if test.wantErrDown {
|
||||
t.Errorf("ConvertDown() = %#v, wanted error", conv)
|
||||
t.Errorf("ConvertFrom() = %#v, wanted error", conv)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -164,7 +164,7 @@ func TestConversion(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConvertUp(t *testing.T) {
|
||||
func TestConvertTo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr *Addressable
|
||||
|
@ -219,12 +219,12 @@ func TestConvertUp(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got := test.conv
|
||||
if err := test.addr.ConvertUp(context.Background(), got); err != nil {
|
||||
if err := test.addr.ConvertTo(context.Background(), got); err != nil {
|
||||
if !test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %v", err)
|
||||
t.Errorf("ConvertTo() = %v", err)
|
||||
}
|
||||
} else if test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %#v, wanted error", got)
|
||||
t.Errorf("ConvertTo() = %#v, wanted error", got)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(test.want, got); diff != "" {
|
||||
|
|
|
@ -77,8 +77,8 @@ func (*Addressable) GetFullType() duck.Populatable {
|
|||
return &AddressableType{}
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (a *Addressable) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (a *Addressable) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
switch sink := to.(type) {
|
||||
case *v1.Addressable:
|
||||
sink.URL = a.URL.DeepCopy()
|
||||
|
@ -88,8 +88,8 @@ func (a *Addressable) ConvertUp(ctx context.Context, to apis.Convertible) error
|
|||
}
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (a *Addressable) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (a *Addressable) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
switch source := from.(type) {
|
||||
case *v1.Addressable:
|
||||
a.URL = source.URL.DeepCopy()
|
||||
|
|
|
@ -72,21 +72,21 @@ func TestConversion(t *testing.T) {
|
|||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
conv := test.conv
|
||||
if err := test.addr.ConvertUp(context.Background(), conv); err != nil {
|
||||
if err := test.addr.ConvertTo(context.Background(), conv); err != nil {
|
||||
if !test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %v", err)
|
||||
t.Errorf("ConvertTo() = %v", err)
|
||||
}
|
||||
} else if test.wantErrUp {
|
||||
t.Errorf("ConvertUp() = %#v, wanted error", conv)
|
||||
t.Errorf("ConvertTo() = %#v, wanted error", conv)
|
||||
}
|
||||
got := &Addressable{}
|
||||
if err := got.ConvertDown(context.Background(), conv); err != nil {
|
||||
if err := got.ConvertFrom(context.Background(), conv); err != nil {
|
||||
if !test.wantErrDown {
|
||||
t.Errorf("ConvertDown() = %v", err)
|
||||
t.Errorf("ConvertFrom() = %v", err)
|
||||
}
|
||||
return
|
||||
} else if test.wantErrDown {
|
||||
t.Errorf("ConvertDown() = %#v, wanted error", conv)
|
||||
t.Errorf("ConvertFrom() = %#v, wanted error", conv)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -37,11 +37,11 @@ type Validatable interface {
|
|||
// Convertible indicates that a particular type supports conversions to/from
|
||||
// "higher" versions of the same type.
|
||||
type Convertible interface {
|
||||
// ConvertUp up-converts the receiver into `to`.
|
||||
ConvertUp(ctx context.Context, to Convertible) error
|
||||
// ConvertTo converts the receiver into `to`.
|
||||
ConvertTo(ctx context.Context, to Convertible) error
|
||||
|
||||
// ConvertDown down-converts from `from` into the receiver.
|
||||
ConvertDown(ctx context.Context, from Convertible) error
|
||||
// ConvertFrom converts `from` into the receiver.
|
||||
ConvertFrom(ctx context.Context, from Convertible) error
|
||||
}
|
||||
|
||||
// Listable indicates that a particular type can be returned via the returned
|
||||
|
|
|
@ -173,7 +173,7 @@ func roundTripViaHub(t *testing.T, gvk schema.GroupVersionKind, scheme, hubs *ru
|
|||
return
|
||||
}
|
||||
|
||||
if err := hub.ConvertDown(ctx, obj); err != nil {
|
||||
if err := hub.ConvertFrom(ctx, obj); err != nil {
|
||||
t.Errorf("Conversion to hub (%s) failed: %s", hubGVK, err)
|
||||
}
|
||||
|
||||
|
@ -183,7 +183,7 @@ func roundTripViaHub(t *testing.T, gvk schema.GroupVersionKind, scheme, hubs *ru
|
|||
}
|
||||
|
||||
newObj := objForGVK(t, gvk, scheme)
|
||||
if err := hub.ConvertUp(ctx, newObj); err != nil {
|
||||
if err := hub.ConvertTo(ctx, newObj); err != nil {
|
||||
t.Errorf("Conversion from hub (%s) failed: %s", hubGVK, err)
|
||||
t.Errorf("object: %#v", obj)
|
||||
return
|
||||
|
|
|
@ -41,8 +41,8 @@ import (
|
|||
// ConversionController will apply defaults before returning
|
||||
// the response
|
||||
type ConvertibleObject interface {
|
||||
// ConvertUp(ctx)
|
||||
// ConvertDown(ctx)
|
||||
// ConvertTo(ctx, to)
|
||||
// ConvertFrom(ctx, from)
|
||||
apis.Convertible
|
||||
|
||||
// DeepCopyObject()
|
||||
|
|
|
@ -133,14 +133,14 @@ func (r *reconciler) convert(
|
|||
|
||||
if inGVK.Version == conv.HubVersion {
|
||||
hub = in
|
||||
} else if err = hub.ConvertDown(ctx, in); err != nil {
|
||||
} else if err = hub.ConvertFrom(ctx, in); err != nil {
|
||||
err = fmt.Errorf("conversion failed to version %s for type %s - %s", outGVK.Version, formatGVK(inGVK), err)
|
||||
return
|
||||
}
|
||||
|
||||
if outGVK.Version == conv.HubVersion {
|
||||
out = hub
|
||||
} else if err = hub.ConvertUp(ctx, out); err != nil {
|
||||
} else if err = hub.ConvertTo(ctx, out); err != nil {
|
||||
err = fmt.Errorf("conversion failed to version %s for type %s - %s", outGVK.Version, formatGVK(inGVK), err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -418,11 +418,11 @@ func TestConversionFailureToConvert(t *testing.T) {
|
|||
name string
|
||||
errorOn string
|
||||
}{{
|
||||
name: "error converting down",
|
||||
errorOn: internal.ErrorConvertDown,
|
||||
name: "error converting from",
|
||||
errorOn: internal.ErrorConvertFrom,
|
||||
}, {
|
||||
name: "error converting up",
|
||||
errorOn: internal.ErrorConvertUp,
|
||||
name: "error converting to",
|
||||
errorOn: internal.ErrorConvertTo,
|
||||
}}
|
||||
|
||||
for _, test := range tests {
|
||||
|
|
|
@ -42,13 +42,13 @@ const (
|
|||
// will cause json unmarshalling of the resource to fail
|
||||
ErrorUnmarshal = "unmarshal"
|
||||
|
||||
// ErrorConvertUp when assigned to the Spec.Property of the ErrorResource
|
||||
// will cause ConvertUp to fail
|
||||
ErrorConvertUp = "convertUp"
|
||||
// ErrorConvertTo when assigned to the Spec.Property of the ErrorResource
|
||||
// will cause ConvertTo to fail
|
||||
ErrorConvertTo = "convertTo"
|
||||
|
||||
// ErrorConvertDown when assigned to the Spec.Property of the ErrorResource
|
||||
// will cause ConvertDown to fail
|
||||
ErrorConvertDown = "convertDown"
|
||||
// ErrorConvertFrom when assigned to the Spec.Property of the ErrorResource
|
||||
// will cause ConvertFrom to fail
|
||||
ErrorConvertFrom = "convertFrom"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -180,8 +180,8 @@ func NewErrorResource(failure string) *ErrorResource {
|
|||
}
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (r *V1Resource) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (r *V1Resource) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
switch sink := to.(type) {
|
||||
case *V2Resource:
|
||||
sink.Spec.Property = "prefix/" + r.Spec.Property
|
||||
|
@ -197,8 +197,8 @@ func (r *V1Resource) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (r *V1Resource) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (r *V1Resource) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
switch source := from.(type) {
|
||||
case *V2Resource:
|
||||
r.Spec.Property = strings.TrimPrefix(source.Spec.Property, "prefix/")
|
||||
|
@ -221,40 +221,40 @@ func (r *V3Resource) SetDefaults(ctx context.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (*V2Resource) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (*V2Resource) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (*V2Resource) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (*V2Resource) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (*V3Resource) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (*V3Resource) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (*V3Resource) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (*V3Resource) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// ConvertUp implements apis.Convertible
|
||||
func (e *ErrorResource) ConvertUp(ctx context.Context, to apis.Convertible) error {
|
||||
if e.Spec.Property == ErrorConvertUp {
|
||||
// ConvertTo implements apis.Convertible
|
||||
func (e *ErrorResource) ConvertTo(ctx context.Context, to apis.Convertible) error {
|
||||
if e.Spec.Property == ErrorConvertTo {
|
||||
return errors.New("boooom - convert up")
|
||||
}
|
||||
|
||||
return e.V1Resource.ConvertUp(ctx, to)
|
||||
return e.V1Resource.ConvertTo(ctx, to)
|
||||
}
|
||||
|
||||
// ConvertDown implements apis.Convertible
|
||||
func (e *ErrorResource) ConvertDown(ctx context.Context, from apis.Convertible) error {
|
||||
err := e.V1Resource.ConvertDown(ctx, from)
|
||||
// ConvertFrom implements apis.Convertible
|
||||
func (e *ErrorResource) ConvertFrom(ctx context.Context, from apis.Convertible) error {
|
||||
err := e.V1Resource.ConvertFrom(ctx, from)
|
||||
|
||||
if err == nil && e.Spec.Property == ErrorConvertDown {
|
||||
if err == nil && e.Spec.Property == ErrorConvertFrom {
|
||||
err = errors.New("boooom - convert down")
|
||||
}
|
||||
return err
|
||||
|
|
Loading…
Reference in New Issue