From 1ebb4d56f726ca514dc95c1afff3fa75fc3c7999 Mon Sep 17 00:00:00 2001 From: Ville Aikas <11279988+vaikas@users.noreply.github.com> Date: Wed, 29 Apr 2020 16:34:42 -0700 Subject: [PATCH] populate the v1alpha1.Hostname from the URI as per the spec (#1264) --- apis/duck/v1alpha1/addressable_types.go | 6 ++ apis/duck/v1alpha1/addressable_types_test.go | 83 ++++++++++---------- 2 files changed, 48 insertions(+), 41 deletions(-) diff --git a/apis/duck/v1alpha1/addressable_types.go b/apis/duck/v1alpha1/addressable_types.go index 70c9b0135..328246f6f 100644 --- a/apis/duck/v1alpha1/addressable_types.go +++ b/apis/duck/v1alpha1/addressable_types.go @@ -106,9 +106,15 @@ func (a *Addressable) ConvertFrom(ctx context.Context, from apis.Convertible) er switch source := from.(type) { case *v1.Addressable: a.URL = source.URL.DeepCopy() + if a.URL != nil { + a.Hostname = a.URL.Host + } return nil case *v1beta1.Addressable: a.URL = source.URL.DeepCopy() + if a.URL != nil { + a.Hostname = a.URL.Host + } return nil default: return fmt.Errorf("unknown version, got: %T", from) diff --git a/apis/duck/v1alpha1/addressable_types_test.go b/apis/duck/v1alpha1/addressable_types_test.go index 0daabf993..ad65a7cb7 100644 --- a/apis/duck/v1alpha1/addressable_types_test.go +++ b/apis/duck/v1alpha1/addressable_types_test.go @@ -30,45 +30,30 @@ func TestGetURL(t *testing.T) { tests := []struct { name string addr Addressable - want apis.URL + want *apis.URL }{{ name: "just hostname", addr: Addressable{ Hostname: "foo.com", }, - want: apis.URL{ - Scheme: "http", - Host: "foo.com", - }, + want: apis.HTTP("foo.com"), }, { name: "just url", addr: Addressable{ Addressable: v1beta1.Addressable{ - URL: &apis.URL{ - Scheme: "https", - Host: "bar.com", - }, + URL: apis.HTTP("bar.com"), }, }, - want: apis.URL{ - Scheme: "https", - Host: "bar.com", - }, + want: apis.HTTP("bar.com"), }, { name: "both fields", addr: Addressable{ Hostname: "foo.bar.svc.cluster.local", Addressable: v1beta1.Addressable{ - URL: &apis.URL{ - Scheme: "https", - Host: "baz.com", - }, + URL: apis.HTTPS("baz.com"), }, }, - want: apis.URL{ - Scheme: "https", - Host: "baz.com", - }, + want: apis.HTTPS("baz.com"), }} for _, test := range tests { @@ -93,11 +78,9 @@ func TestConversion(t *testing.T) { name: "v1", addr: &Addressable{ Addressable: v1beta1.Addressable{ - URL: &apis.URL{ - Scheme: "https", - Host: "bar.com", - }, + URL: apis.HTTP("bar.com"), }, + Hostname: "bar.com", }, conv: &v1.Addressable{}, wantErrUp: false, @@ -106,11 +89,9 @@ func TestConversion(t *testing.T) { name: "v1beta1", addr: &Addressable{ Addressable: v1beta1.Addressable{ - URL: &apis.URL{ - Scheme: "https", - Host: "bar.com", - }, + URL: apis.HTTP("bar.com"), }, + Hostname: "bar.com", }, conv: &v1beta1.Addressable{}, wantErrUp: false, @@ -119,10 +100,7 @@ func TestConversion(t *testing.T) { name: "v1alpha1", addr: &Addressable{ Addressable: v1beta1.Addressable{ - URL: &apis.URL{ - Scheme: "https", - Host: "bar.com", - }, + URL: apis.HTTPS("bar.com"), }, }, conv: &Addressable{}, @@ -186,10 +164,7 @@ func TestConvertTo(t *testing.T) { }, conv: &v1beta1.Addressable{}, want: &v1beta1.Addressable{ - URL: &apis.URL{ - Scheme: "http", - Host: "bar.com", - }, + URL: apis.HTTP("bar.com"), }, wantErrUp: false, wantErrDown: false, @@ -207,10 +182,7 @@ func TestConvertTo(t *testing.T) { }, conv: &v1.Addressable{}, want: &v1.Addressable{ - URL: &apis.URL{ - Scheme: "http", - Host: "bar.com", - }, + URL: apis.HTTP("bar.com"), }, wantErrUp: false, wantErrDown: false, @@ -233,3 +205,32 @@ func TestConvertTo(t *testing.T) { }) } } + +func TestConvertFrom(t *testing.T) { + tests := []struct { + name string + in apis.Convertible + want *Addressable + }{{ + name: "v1beta1", + in: &v1beta1.Addressable{URL: apis.HTTP("foo.example.com")}, + want: &Addressable{Addressable: v1beta1.Addressable{URL: apis.HTTP("foo.example.com")}, Hostname: "foo.example.com"}, + }, { + name: "v1", + in: &v1.Addressable{URL: apis.HTTP("bar.example.com")}, + want: &Addressable{Addressable: v1beta1.Addressable{URL: apis.HTTP("bar.example.com")}, Hostname: "bar.example.com"}, + }} + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := &Addressable{} + if err := got.ConvertFrom(context.Background(), test.in); err != nil { + t.Errorf("ConvertFrom() = %v", err) + } + + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("roundtrip (-want, +got) = %v", diff) + } + }) + } +}