From 360a1b2ce6969bf35499d87e375409321aa4bf27 Mon Sep 17 00:00:00 2001 From: James Peach Date: Tue, 12 May 2020 14:40:58 +1000 Subject: [PATCH] api: add kubebuilder-compatible type registration exports (#2514) Restore the kubebuilder-compatible API type registration exports for IngressRoute and HTTPProxy. Keep exporting AddKnownTypes() for projects that already started consuming this symmbol, but note that it is not should no longer be used. This updates #2487. Signed-off-by: James Peach --- apis/contour/v1beta1/register.go | 39 +++++++++++++++++++++++------- apis/projectcontour/v1/register.go | 39 +++++++++++++++++++++++------- internal/k8s/converter.go | 9 +++++-- internal/k8s/status_test.go | 12 +++++++-- 4 files changed, 77 insertions(+), 22 deletions(-) diff --git a/apis/contour/v1beta1/register.go b/apis/contour/v1beta1/register.go index dd46acc8bb5..84855044fc7 100644 --- a/apis/contour/v1beta1/register.go +++ b/apis/contour/v1beta1/register.go @@ -1,4 +1,4 @@ -// Copyright © 2019 VMware +// Copyright © 2020 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -24,22 +24,43 @@ const ( GroupName = "contour.heptio.com" ) -// SchemeGroupVersion is the GroupVersion for the Contour API -var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1beta1"} -var IngressRouteGVR = SchemeGroupVersion.WithResource("ingressroutes") -var TLSCertificateDelegationGVR = SchemeGroupVersion.WithResource("tlscertificatedelegations") +// SchemeGroupVersion is a compatibility name for the GroupVersion. +// New code should use GroupVersion. +var SchemeGroupVersion = GroupVersion + +var IngressRouteGVR = GroupVersion.WithResource("ingressroutes") +var TLSCertificateDelegationGVR = GroupVersion.WithResource("tlscertificatedelegations") // Resource gets an Contour GroupResource for a specified resource func Resource(resource string) schema.GroupResource { - return SchemeGroupVersion.WithResource(resource).GroupResource() + return GroupVersion.WithResource(resource).GroupResource() } -func AddKnownTypes(scheme *runtime.Scheme) { - scheme.AddKnownTypes(SchemeGroupVersion, +// AddKnownTypes is exported for backwards compatibility with third +// parties who depend on this symbol, but all new code should use +// AddToScheme. +func AddKnownTypes(scheme *runtime.Scheme) error { + scheme.AddKnownTypes( + GroupVersion, &IngressRoute{}, &IngressRouteList{}, &TLSCertificateDelegation{}, &TLSCertificateDelegationList{}, ) - metav1.AddToGroupVersion(scheme, SchemeGroupVersion) + metav1.AddToGroupVersion(scheme, GroupVersion) + return nil } + +// The following declarations are kubebuilder-compatible and will be expected +// by third parties who import the Contour API types. + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1beta1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = runtime.NewSchemeBuilder(AddKnownTypes) + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/apis/projectcontour/v1/register.go b/apis/projectcontour/v1/register.go index 7f49df2270e..3b3dc1053d3 100644 --- a/apis/projectcontour/v1/register.go +++ b/apis/projectcontour/v1/register.go @@ -1,4 +1,4 @@ -// Copyright © 2019 VMware +// Copyright © 2020 VMware // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -23,22 +23,43 @@ const ( GroupName = "projectcontour.io" ) -// SchemeGroupVersion is the GroupVersion for the Contour API -var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1"} -var HTTPProxyGVR = SchemeGroupVersion.WithResource("httpproxies") -var TLSCertificateDelegationGVR = SchemeGroupVersion.WithResource("tlscertificatedelegations") +// SchemeGroupVersion is a compatibility name for the GroupVersion. +// New code should use GroupVersion. +var SchemeGroupVersion = GroupVersion + +var HTTPProxyGVR = GroupVersion.WithResource("httpproxies") +var TLSCertificateDelegationGVR = GroupVersion.WithResource("tlscertificatedelegations") // Resource gets an Contour GroupResource for a specified resource func Resource(resource string) schema.GroupResource { - return SchemeGroupVersion.WithResource(resource).GroupResource() + return GroupVersion.WithResource(resource).GroupResource() } -func AddKnownTypes(scheme *runtime.Scheme) { - scheme.AddKnownTypes(SchemeGroupVersion, +// AddKnownTypes is exported for backwards compatibility with third +// parties who depend on this symbol, but all new code should use +// AddToScheme. +func AddKnownTypes(scheme *runtime.Scheme) error { + scheme.AddKnownTypes( + GroupVersion, &HTTPProxy{}, &HTTPProxyList{}, &TLSCertificateDelegation{}, &TLSCertificateDelegationList{}, ) - metav1.AddToGroupVersion(scheme, SchemeGroupVersion) + metav1.AddToGroupVersion(scheme, GroupVersion) + return nil } + +// The following declarations are kubebuilder-compatible and will be expected +// by third parties who import the Contour API types. + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = runtime.NewSchemeBuilder(AddKnownTypes) + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/internal/k8s/converter.go b/internal/k8s/converter.go index ed4a13f44d0..43d58f5f7c1 100644 --- a/internal/k8s/converter.go +++ b/internal/k8s/converter.go @@ -93,8 +93,13 @@ func NewUnstructuredConverter() (*UnstructuredConverter, error) { } // Setup converter to understand custom CRD types - projectcontour.AddKnownTypes(uc.scheme) - ingressroutev1.AddKnownTypes(uc.scheme) + if err := projectcontour.AddToScheme(uc.scheme); err != nil { + return nil, err + } + + if err := ingressroutev1.AddToScheme(uc.scheme); err != nil { + return nil, err + } // Add the core types we need if err := scheme.AddToScheme(uc.scheme); err != nil { diff --git a/internal/k8s/status_test.go b/internal/k8s/status_test.go index d869a0160f3..46b0057074f 100644 --- a/internal/k8s/status_test.go +++ b/internal/k8s/status_test.go @@ -46,8 +46,12 @@ func TestSetIngressRouteStatus(t *testing.T) { t.Run(name, func(t *testing.T) { t.Helper() var gotPatchBytes []byte + s := runtime.NewScheme() - ingressroutev1.AddKnownTypes(s) + if err := ingressroutev1.AddToScheme(s); err != nil { + t.Fatalf("adding to scheme: %s", err) + } + client := fake.NewSimpleDynamicClient(s, tc.existing) client.PrependReactor("patch", "ingressroutes", func(action k8stesting.Action) (bool, runtime.Object, error) { @@ -143,8 +147,12 @@ func TestSetHTTPProxyStatus(t *testing.T) { t.Run(name, func(t *testing.T) { t.Helper() var gotObj runtime.Object + s := runtime.NewScheme() - projcontour.AddKnownTypes(s) + if err := projcontour.AddToScheme(s); err != nil { + t.Fatalf("adding to scheme: %s", err) + } + usc, err := NewUnstructuredConverter() if err != nil { t.Fatal(err)