From acb1b364b8fcf338e1854831e94a7eb06588594d Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Mon, 9 Sep 2024 15:27:07 +0200 Subject: [PATCH] Add an example with all possible extensions Signed-off-by: Stephen Kitt --- .../examples/crd/apis/extensions/v1/doc.go | 22 +++ .../crd/apis/extensions/v1/register.go | 59 ++++++++ .../examples/crd/apis/extensions/v1/types.go | 65 +++++++++ .../extensions/v1/zz_generated.deepcopy.go | 127 ++++++++++++++++++ .../extensions/v1/zz_generated.defaults.go | 33 +++++ 5 files changed, 306 insertions(+) create mode 100644 staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/doc.go create mode 100644 staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/register.go create mode 100644 staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/types.go create mode 100644 staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.deepcopy.go create mode 100644 staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.defaults.go diff --git a/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/doc.go b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/doc.go new file mode 100644 index 00000000000..0c7a9100f33 --- /dev/null +++ b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/doc.go @@ -0,0 +1,22 @@ +/* +Copyright 2024 The Kubernetes Authors. + +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// +k8s:deepcopy-gen=package +// +k8s:defaulter-gen=TypeMeta +// +groupName=extensions.test.crd.code-generator.k8s.io +// +groupGoName=ExtensionsExample + +package v1 // import "k8s.io/code-generator/examples/crd/apis/extensions/v1" diff --git a/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/register.go b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/register.go new file mode 100644 index 00000000000..df9912421da --- /dev/null +++ b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/register.go @@ -0,0 +1,59 @@ +/* +Copyright 2024 The Kubernetes Authors. + +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var SchemeGroupVersion = schema.GroupVersion{Group: "extensions.test.crd.code-generator.k8s.io", Version: "v1"} + +var ( + // TODO: move SchemeBuilder with zz_generated.deepcopy.go to k8s.io/api. + // localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes. + SchemeBuilder runtime.SchemeBuilder + localSchemeBuilder = &SchemeBuilder + AddToScheme = localSchemeBuilder.AddToScheme +) + +func init() { + // We only register manually written functions here. The registration of the + // generated functions takes place in the generated files. The separation + // makes the code compile even when the generated files are missing. + localSchemeBuilder.Register(addKnownTypes) +} + +// Resource takes an unqualified resource and returns a Group qualified GroupResource +func Resource(resource string) schema.GroupResource { + return SchemeGroupVersion.WithResource(resource).GroupResource() +} + +// Adds the list of known types to api.Scheme. +func addKnownTypes(scheme *runtime.Scheme) error { + scheme.AddKnownTypes(SchemeGroupVersion, + &TestType{}, + &TestTypeList{}, + ) + + scheme.AddKnownTypes(SchemeGroupVersion, + &metav1.Status{}, + ) + metav1.AddToGroupVersion(scheme, SchemeGroupVersion) + return nil +} diff --git a/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/types.go b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/types.go new file mode 100644 index 00000000000..55fbdc132dd --- /dev/null +++ b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/types.go @@ -0,0 +1,65 @@ +/* +Copyright 2024 The Kubernetes Authors. + +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + +// +genclient +// +genclient:method=GetExtended,verb=get +// +genclient:method=ListExtended,verb=list +// +genclient:method=CreateExtended,verb=create +// +genclient:method=UpdateExtended,verb=update +// +genclient:method=PatchExtended,verb=patch +// +genclient:method=ApplyExtended,verb=apply +// +genclient:method=GetSubresource,verb=get,subresource=testsubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource +// +genclient:method=CreateSubresource,verb=create,subresource=testsubresource,input=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource +// +genclient:method=UpdateSubresource,verb=update,subresource=subresource,input=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource +// +genclient:method=ApplySubresource,verb=apply,subresource=subresource,input=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource,result=k8s.io/code-generator/examples/crd/apis/extensions/v1.TestSubresource +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// TestType is a top-level type. A client is created for it. +type TestType struct { + metav1.TypeMeta `json:",inline"` + // +optional + metav1.ObjectMeta `json:"metadata,omitempty"` + // +optional + Status TestTypeStatus `json:"status,omitempty"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +type TestSubresource struct { + metav1.TypeMeta `json:",inline"` + + Name string `json:"name"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// TestTypeList is a top-level list type. The client methods for lists are automatically created. +// You are not supposed to create a separate client for this one. +type TestTypeList struct { + metav1.TypeMeta `json:",inline"` + // +optional + metav1.ListMeta `json:"metadata,omitempty"` + + Items []TestType `json:"items"` +} + +type TestTypeStatus struct { + Blah string `json:"blah"` +} diff --git a/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.deepcopy.go b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.deepcopy.go new file mode 100644 index 00000000000..906252fab9b --- /dev/null +++ b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.deepcopy.go @@ -0,0 +1,127 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The Kubernetes Authors. + +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by deepcopy-gen. DO NOT EDIT. + +package v1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TestSubresource) DeepCopyInto(out *TestSubresource) { + *out = *in + out.TypeMeta = in.TypeMeta + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestSubresource. +func (in *TestSubresource) DeepCopy() *TestSubresource { + if in == nil { + return nil + } + out := new(TestSubresource) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *TestSubresource) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TestType) DeepCopyInto(out *TestType) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Status = in.Status + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestType. +func (in *TestType) DeepCopy() *TestType { + if in == nil { + return nil + } + out := new(TestType) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *TestType) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TestTypeList) DeepCopyInto(out *TestTypeList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]TestType, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestTypeList. +func (in *TestTypeList) DeepCopy() *TestTypeList { + if in == nil { + return nil + } + out := new(TestTypeList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *TestTypeList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *TestTypeStatus) DeepCopyInto(out *TestTypeStatus) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestTypeStatus. +func (in *TestTypeStatus) DeepCopy() *TestTypeStatus { + if in == nil { + return nil + } + out := new(TestTypeStatus) + in.DeepCopyInto(out) + return out +} diff --git a/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.defaults.go b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.defaults.go new file mode 100644 index 00000000000..dac177e93bd --- /dev/null +++ b/staging/src/k8s.io/code-generator/examples/crd/apis/extensions/v1/zz_generated.defaults.go @@ -0,0 +1,33 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +Copyright The Kubernetes Authors. + +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Code generated by defaulter-gen. DO NOT EDIT. + +package v1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// RegisterDefaults adds defaulters functions to the given scheme. +// Public to allow building arbitrary schemes. +// All generated defaulters are covering - they call all nested defaulters. +func RegisterDefaults(scheme *runtime.Scheme) error { + return nil +}