From 404a407426f9ceef024def0b34906c7606dc36f0 Mon Sep 17 00:00:00 2001 From: Erik Godding Boye Date: Fri, 7 Jun 2024 07:42:58 +0200 Subject: [PATCH] feat: start serving v2 API --- api/accurate/v2/conversion_test.go | 29 ++++ api/accurate/v2/doc.go | 1 + api/accurate/v2/groupversion_info.go | 2 + api/accurate/v2/subnamespace_conversion.go | 43 +++++ api/accurate/v2/subnamespace_types.go | 1 - api/accurate/v2/zz_generated.conversion.go | 163 ++++++++++++++++++ charts/accurate/templates/generated/crds.yaml | 2 +- .../accurate.cybozu.com_subnamespaces.yaml | 2 +- 8 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 api/accurate/v2/conversion_test.go create mode 100644 api/accurate/v2/subnamespace_conversion.go create mode 100644 api/accurate/v2/zz_generated.conversion.go diff --git a/api/accurate/v2/conversion_test.go b/api/accurate/v2/conversion_test.go new file mode 100644 index 0000000..3b17501 --- /dev/null +++ b/api/accurate/v2/conversion_test.go @@ -0,0 +1,29 @@ +package v2 + +import ( + "testing" + + accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1" + utilconversion "github.com/cybozu-go/accurate/internal/util/conversion" + fuzz "github.com/google/gofuzz" + "k8s.io/apimachinery/pkg/api/apitesting/fuzzer" + runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer" +) + +func TestFuzzyConversion(t *testing.T) { + t.Run("for SubNamespace", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{ + Hub: &accuratev2alpha1.SubNamespace{}, + Spoke: &SubNamespace{}, + FuzzerFuncs: []fuzzer.FuzzerFuncs{SubNamespaceStatusFuzzFunc}, + })) +} + +func SubNamespaceStatusFuzzFunc(_ runtimeserializer.CodecFactory) []interface{} { + return []interface{}{ + SubNamespaceStatusFuzzer, + } +} + +func SubNamespaceStatusFuzzer(in *SubNamespace, c fuzz.Continue) { + c.FuzzNoCustom(in) +} diff --git a/api/accurate/v2/doc.go b/api/accurate/v2/doc.go index 9d677ac..6ec0003 100644 --- a/api/accurate/v2/doc.go +++ b/api/accurate/v2/doc.go @@ -1,3 +1,4 @@ // +kubebuilder:object:generate=true // +groupName=accurate.cybozu.com +// +k8s:conversion-gen=github.com/cybozu-go/accurate/api/accurate/v2alpha1 package v2 diff --git a/api/accurate/v2/groupversion_info.go b/api/accurate/v2/groupversion_info.go index 2214c30..db1ad72 100644 --- a/api/accurate/v2/groupversion_info.go +++ b/api/accurate/v2/groupversion_info.go @@ -15,4 +15,6 @@ var ( // AddToScheme adds the types in this group-version to the given scheme. AddToScheme = SchemeBuilder.AddToScheme + + localSchemeBuilder = SchemeBuilder.SchemeBuilder ) diff --git a/api/accurate/v2/subnamespace_conversion.go b/api/accurate/v2/subnamespace_conversion.go new file mode 100644 index 0000000..bb41bde --- /dev/null +++ b/api/accurate/v2/subnamespace_conversion.go @@ -0,0 +1,43 @@ +package v2 + +import ( + accuratev2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1" + "github.com/go-logr/logr" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/conversion" +) + +// ConvertTo converts this SubNamespace to the Hub version (v2alpha1). +func (src *SubNamespace) ConvertTo(dstRaw conversion.Hub) error { + dst := dstRaw.(*accuratev2alpha1.SubNamespace) + + logger := getConversionLogger(src).WithValues( + "source", SchemeGroupVersion.Version, + "destination", accuratev2alpha1.SchemeGroupVersion.Version, + ) + logger.V(5).Info("converting") + + return Convert_v2_SubNamespace_To_v2alpha1_SubNamespace(src, dst, nil) +} + +// ConvertFrom converts from the Hub version (v2alpha1) to this version. +func (dst *SubNamespace) ConvertFrom(srcRaw conversion.Hub) error { + src := srcRaw.(*accuratev2alpha1.SubNamespace) + + logger := getConversionLogger(src).WithValues( + "source", accuratev2alpha1.SchemeGroupVersion.Version, + "destination", SchemeGroupVersion.Version, + ) + logger.V(5).Info("converting") + + return Convert_v2alpha1_SubNamespace_To_v2_SubNamespace(src, dst, nil) +} + +func getConversionLogger(obj client.Object) logr.Logger { + return ctrl.Log.WithName("conversion").WithValues( + "kind", "SubNamespace", + "namespace", obj.GetNamespace(), + "name", obj.GetName(), + ) +} diff --git a/api/accurate/v2/subnamespace_types.go b/api/accurate/v2/subnamespace_types.go index cc6390c..dcbc804 100644 --- a/api/accurate/v2/subnamespace_types.go +++ b/api/accurate/v2/subnamespace_types.go @@ -32,7 +32,6 @@ type SubNamespaceSpec struct { } //+kubebuilder:object:root=true -//+kubebuilder:unservedversion //+kubebuilder:subresource:status //+genclient diff --git a/api/accurate/v2/zz_generated.conversion.go b/api/accurate/v2/zz_generated.conversion.go new file mode 100644 index 0000000..a575e06 --- /dev/null +++ b/api/accurate/v2/zz_generated.conversion.go @@ -0,0 +1,163 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +// Code generated by conversion-gen. DO NOT EDIT. + +package v2 + +import ( + unsafe "unsafe" + + v2alpha1 "github.com/cybozu-go/accurate/api/accurate/v2alpha1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + conversion "k8s.io/apimachinery/pkg/conversion" + runtime "k8s.io/apimachinery/pkg/runtime" +) + +func init() { + localSchemeBuilder.Register(RegisterConversions) +} + +// RegisterConversions adds conversion functions to the given scheme. +// Public to allow building arbitrary schemes. +func RegisterConversions(s *runtime.Scheme) error { + if err := s.AddGeneratedConversionFunc((*SubNamespace)(nil), (*v2alpha1.SubNamespace)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2_SubNamespace_To_v2alpha1_SubNamespace(a.(*SubNamespace), b.(*v2alpha1.SubNamespace), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*v2alpha1.SubNamespace)(nil), (*SubNamespace)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2alpha1_SubNamespace_To_v2_SubNamespace(a.(*v2alpha1.SubNamespace), b.(*SubNamespace), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*SubNamespaceList)(nil), (*v2alpha1.SubNamespaceList)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2_SubNamespaceList_To_v2alpha1_SubNamespaceList(a.(*SubNamespaceList), b.(*v2alpha1.SubNamespaceList), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*v2alpha1.SubNamespaceList)(nil), (*SubNamespaceList)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2alpha1_SubNamespaceList_To_v2_SubNamespaceList(a.(*v2alpha1.SubNamespaceList), b.(*SubNamespaceList), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*SubNamespaceSpec)(nil), (*v2alpha1.SubNamespaceSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2_SubNamespaceSpec_To_v2alpha1_SubNamespaceSpec(a.(*SubNamespaceSpec), b.(*v2alpha1.SubNamespaceSpec), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*v2alpha1.SubNamespaceSpec)(nil), (*SubNamespaceSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2alpha1_SubNamespaceSpec_To_v2_SubNamespaceSpec(a.(*v2alpha1.SubNamespaceSpec), b.(*SubNamespaceSpec), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*SubNamespaceStatus)(nil), (*v2alpha1.SubNamespaceStatus)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2_SubNamespaceStatus_To_v2alpha1_SubNamespaceStatus(a.(*SubNamespaceStatus), b.(*v2alpha1.SubNamespaceStatus), scope) + }); err != nil { + return err + } + if err := s.AddGeneratedConversionFunc((*v2alpha1.SubNamespaceStatus)(nil), (*SubNamespaceStatus)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v2alpha1_SubNamespaceStatus_To_v2_SubNamespaceStatus(a.(*v2alpha1.SubNamespaceStatus), b.(*SubNamespaceStatus), scope) + }); err != nil { + return err + } + return nil +} + +func autoConvert_v2_SubNamespace_To_v2alpha1_SubNamespace(in *SubNamespace, out *v2alpha1.SubNamespace, s conversion.Scope) error { + out.ObjectMeta = in.ObjectMeta + if err := Convert_v2_SubNamespaceSpec_To_v2alpha1_SubNamespaceSpec(&in.Spec, &out.Spec, s); err != nil { + return err + } + if err := Convert_v2_SubNamespaceStatus_To_v2alpha1_SubNamespaceStatus(&in.Status, &out.Status, s); err != nil { + return err + } + return nil +} + +// Convert_v2_SubNamespace_To_v2alpha1_SubNamespace is an autogenerated conversion function. +func Convert_v2_SubNamespace_To_v2alpha1_SubNamespace(in *SubNamespace, out *v2alpha1.SubNamespace, s conversion.Scope) error { + return autoConvert_v2_SubNamespace_To_v2alpha1_SubNamespace(in, out, s) +} + +func autoConvert_v2alpha1_SubNamespace_To_v2_SubNamespace(in *v2alpha1.SubNamespace, out *SubNamespace, s conversion.Scope) error { + out.ObjectMeta = in.ObjectMeta + if err := Convert_v2alpha1_SubNamespaceSpec_To_v2_SubNamespaceSpec(&in.Spec, &out.Spec, s); err != nil { + return err + } + if err := Convert_v2alpha1_SubNamespaceStatus_To_v2_SubNamespaceStatus(&in.Status, &out.Status, s); err != nil { + return err + } + return nil +} + +// Convert_v2alpha1_SubNamespace_To_v2_SubNamespace is an autogenerated conversion function. +func Convert_v2alpha1_SubNamespace_To_v2_SubNamespace(in *v2alpha1.SubNamespace, out *SubNamespace, s conversion.Scope) error { + return autoConvert_v2alpha1_SubNamespace_To_v2_SubNamespace(in, out, s) +} + +func autoConvert_v2_SubNamespaceList_To_v2alpha1_SubNamespaceList(in *SubNamespaceList, out *v2alpha1.SubNamespaceList, s conversion.Scope) error { + out.ListMeta = in.ListMeta + out.Items = *(*[]v2alpha1.SubNamespace)(unsafe.Pointer(&in.Items)) + return nil +} + +// Convert_v2_SubNamespaceList_To_v2alpha1_SubNamespaceList is an autogenerated conversion function. +func Convert_v2_SubNamespaceList_To_v2alpha1_SubNamespaceList(in *SubNamespaceList, out *v2alpha1.SubNamespaceList, s conversion.Scope) error { + return autoConvert_v2_SubNamespaceList_To_v2alpha1_SubNamespaceList(in, out, s) +} + +func autoConvert_v2alpha1_SubNamespaceList_To_v2_SubNamespaceList(in *v2alpha1.SubNamespaceList, out *SubNamespaceList, s conversion.Scope) error { + out.ListMeta = in.ListMeta + out.Items = *(*[]SubNamespace)(unsafe.Pointer(&in.Items)) + return nil +} + +// Convert_v2alpha1_SubNamespaceList_To_v2_SubNamespaceList is an autogenerated conversion function. +func Convert_v2alpha1_SubNamespaceList_To_v2_SubNamespaceList(in *v2alpha1.SubNamespaceList, out *SubNamespaceList, s conversion.Scope) error { + return autoConvert_v2alpha1_SubNamespaceList_To_v2_SubNamespaceList(in, out, s) +} + +func autoConvert_v2_SubNamespaceSpec_To_v2alpha1_SubNamespaceSpec(in *SubNamespaceSpec, out *v2alpha1.SubNamespaceSpec, s conversion.Scope) error { + out.Labels = *(*map[string]string)(unsafe.Pointer(&in.Labels)) + out.Annotations = *(*map[string]string)(unsafe.Pointer(&in.Annotations)) + return nil +} + +// Convert_v2_SubNamespaceSpec_To_v2alpha1_SubNamespaceSpec is an autogenerated conversion function. +func Convert_v2_SubNamespaceSpec_To_v2alpha1_SubNamespaceSpec(in *SubNamespaceSpec, out *v2alpha1.SubNamespaceSpec, s conversion.Scope) error { + return autoConvert_v2_SubNamespaceSpec_To_v2alpha1_SubNamespaceSpec(in, out, s) +} + +func autoConvert_v2alpha1_SubNamespaceSpec_To_v2_SubNamespaceSpec(in *v2alpha1.SubNamespaceSpec, out *SubNamespaceSpec, s conversion.Scope) error { + out.Labels = *(*map[string]string)(unsafe.Pointer(&in.Labels)) + out.Annotations = *(*map[string]string)(unsafe.Pointer(&in.Annotations)) + return nil +} + +// Convert_v2alpha1_SubNamespaceSpec_To_v2_SubNamespaceSpec is an autogenerated conversion function. +func Convert_v2alpha1_SubNamespaceSpec_To_v2_SubNamespaceSpec(in *v2alpha1.SubNamespaceSpec, out *SubNamespaceSpec, s conversion.Scope) error { + return autoConvert_v2alpha1_SubNamespaceSpec_To_v2_SubNamespaceSpec(in, out, s) +} + +func autoConvert_v2_SubNamespaceStatus_To_v2alpha1_SubNamespaceStatus(in *SubNamespaceStatus, out *v2alpha1.SubNamespaceStatus, s conversion.Scope) error { + out.ObservedGeneration = in.ObservedGeneration + out.Conditions = *(*[]v1.Condition)(unsafe.Pointer(&in.Conditions)) + return nil +} + +// Convert_v2_SubNamespaceStatus_To_v2alpha1_SubNamespaceStatus is an autogenerated conversion function. +func Convert_v2_SubNamespaceStatus_To_v2alpha1_SubNamespaceStatus(in *SubNamespaceStatus, out *v2alpha1.SubNamespaceStatus, s conversion.Scope) error { + return autoConvert_v2_SubNamespaceStatus_To_v2alpha1_SubNamespaceStatus(in, out, s) +} + +func autoConvert_v2alpha1_SubNamespaceStatus_To_v2_SubNamespaceStatus(in *v2alpha1.SubNamespaceStatus, out *SubNamespaceStatus, s conversion.Scope) error { + out.ObservedGeneration = in.ObservedGeneration + out.Conditions = *(*[]v1.Condition)(unsafe.Pointer(&in.Conditions)) + return nil +} + +// Convert_v2alpha1_SubNamespaceStatus_To_v2_SubNamespaceStatus is an autogenerated conversion function. +func Convert_v2alpha1_SubNamespaceStatus_To_v2_SubNamespaceStatus(in *v2alpha1.SubNamespaceStatus, out *SubNamespaceStatus, s conversion.Scope) error { + return autoConvert_v2alpha1_SubNamespaceStatus_To_v2_SubNamespaceStatus(in, out, s) +} diff --git a/charts/accurate/templates/generated/crds.yaml b/charts/accurate/templates/generated/crds.yaml index 72e379f..cda91fa 100644 --- a/charts/accurate/templates/generated/crds.yaml +++ b/charts/accurate/templates/generated/crds.yaml @@ -180,7 +180,7 @@ spec: type: integer type: object type: object - served: false + served: true storage: false subresources: status: {} diff --git a/config/crd/bases/accurate.cybozu.com_subnamespaces.yaml b/config/crd/bases/accurate.cybozu.com_subnamespaces.yaml index 75a7fdf..6c44289 100644 --- a/config/crd/bases/accurate.cybozu.com_subnamespaces.yaml +++ b/config/crd/bases/accurate.cybozu.com_subnamespaces.yaml @@ -168,7 +168,7 @@ spec: type: integer type: object type: object - served: false + served: true storage: false subresources: status: {}