From 36b2f4560fc7332bd732a80a174b4c57d99d19b6 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Thu, 6 Jul 2017 10:59:05 +0200 Subject: [PATCH] deepcopy: add interface deepcopy funcs - add DeepCopyObject() to runtime.Object interface - add DeepCopyObject() via deepcopy-gen - add DeepCopyObject() manually - add DeepCopySelector() to selector interfaces - add custom DeepCopy func for TableRow.Cells Kubernetes-commit: 39d95b9b065fffebe5b6f233d978fe1723722085 --- pkg/apis/apiserver/types.go | 2 ++ pkg/apis/apiserver/v1alpha1/types.go | 2 ++ pkg/apis/audit/types.go | 8 ++++++++ pkg/apis/audit/v1alpha1/types.go | 8 ++++++++ pkg/apis/example/types.go | 4 ++++ pkg/apis/example/v1/types.go | 4 ++++ pkg/endpoints/apiserver_test.go | 10 ++++++++++ pkg/endpoints/handlers/rest_test.go | 8 ++++++++ pkg/endpoints/openapi/testing/types.go | 1 + pkg/endpoints/testing/types.go | 10 ++++++++++ pkg/registry/generic/rest/streamer.go | 4 ++++ pkg/registry/rest/rest.go | 2 ++ pkg/storage/selection_predicate_test.go | 6 ++++++ pkg/storage/testing/types.go | 2 ++ 14 files changed, 71 insertions(+) diff --git a/pkg/apis/apiserver/types.go b/pkg/apis/apiserver/types.go index 0af031e92..f84fd04a3 100644 --- a/pkg/apis/apiserver/types.go +++ b/pkg/apis/apiserver/types.go @@ -21,6 +21,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // AdmissionConfiguration provides versioned configuration for admission controllers. type AdmissionConfiguration struct { metav1.TypeMeta diff --git a/pkg/apis/apiserver/v1alpha1/types.go b/pkg/apis/apiserver/v1alpha1/types.go index bf0ce843a..522c41c41 100644 --- a/pkg/apis/apiserver/v1alpha1/types.go +++ b/pkg/apis/apiserver/v1alpha1/types.go @@ -21,6 +21,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // AdmissionConfiguration provides versioned configuration for admission controllers. type AdmissionConfiguration struct { metav1.TypeMeta `json:",inline"` diff --git a/pkg/apis/audit/types.go b/pkg/apis/audit/types.go index 8e8770d00..0cb206925 100644 --- a/pkg/apis/audit/types.go +++ b/pkg/apis/audit/types.go @@ -65,6 +65,8 @@ const ( StagePanic = "Panic" ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // Event captures all the information that can be included in an API audit log. type Event struct { metav1.TypeMeta @@ -118,6 +120,8 @@ type Event struct { ResponseObject *runtime.Unknown } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // EventList is a list of audit Events. type EventList struct { metav1.TypeMeta @@ -127,6 +131,8 @@ type EventList struct { Items []Event } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // Policy defines the configuration of audit logging, and the rules for how different request // categories are logged. type Policy struct { @@ -142,6 +148,8 @@ type Policy struct { Rules []PolicyRule } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // PolicyList is a list of audit Policies. type PolicyList struct { metav1.TypeMeta diff --git a/pkg/apis/audit/v1alpha1/types.go b/pkg/apis/audit/v1alpha1/types.go index a450b35df..d64f6a8d4 100644 --- a/pkg/apis/audit/v1alpha1/types.go +++ b/pkg/apis/audit/v1alpha1/types.go @@ -66,6 +66,8 @@ const ( StagePanic = "Panic" ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // Event captures all the information that can be included in an API audit log. type Event struct { metav1.TypeMeta `json:",inline"` @@ -119,6 +121,8 @@ type Event struct { ResponseObject *runtime.Unknown `json:"responseObject,omitempty" protobuf:"bytes,14,opt,name=responseObject"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // EventList is a list of audit Events. type EventList struct { metav1.TypeMeta `json:",inline"` @@ -128,6 +132,8 @@ type EventList struct { Items []Event `json:"items" protobuf:"bytes,2,rep,name=items"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // Policy defines the configuration of audit logging, and the rules for how different request // categories are logged. type Policy struct { @@ -143,6 +149,8 @@ type Policy struct { Rules []PolicyRule `json:"rules" protobuf:"bytes,2,rep,name=rules"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // PolicyList is a list of audit Policies. type PolicyList struct { metav1.TypeMeta `json:",inline"` diff --git a/pkg/apis/example/types.go b/pkg/apis/example/types.go index 340c0abeb..6dd3e2172 100644 --- a/pkg/apis/example/types.go +++ b/pkg/apis/example/types.go @@ -27,6 +27,8 @@ type ( RestartPolicy string ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // Pod is a collection of containers, used as either input (create, update) or as output (list, get). type Pod struct { metav1.TypeMeta @@ -124,6 +126,8 @@ type PodSpec struct { SchedulerName string } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // PodList is a list of Pods. type PodList struct { metav1.TypeMeta diff --git a/pkg/apis/example/v1/types.go b/pkg/apis/example/v1/types.go index 7154056ff..7be2a0c6f 100644 --- a/pkg/apis/example/v1/types.go +++ b/pkg/apis/example/v1/types.go @@ -27,6 +27,8 @@ type ( RestartPolicy string ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // Pod is a collection of containers, used as either input (create, update) or as output (list, get). type Pod struct { metav1.TypeMeta `json:",inline"` @@ -178,6 +180,8 @@ type PodSpec struct { SchedulerName string `json:"schedulername,omitempty" protobuf:"bytes,19,opt,name=schedulername"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // PodList is a list of Pods. type PodList struct { metav1.TypeMeta `json:",inline"` diff --git a/pkg/endpoints/apiserver_test.go b/pkg/endpoints/apiserver_test.go index b17ba4791..14f87fa82 100644 --- a/pkg/endpoints/apiserver_test.go +++ b/pkg/endpoints/apiserver_test.go @@ -467,6 +467,9 @@ func (s *SimpleStream) Close() error { } func (obj *SimpleStream) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } +func (obj *SimpleStream) DeepCopyObject() runtime.Object { + panic("SimpleStream does not support DeepCopy") +} func (s *SimpleStream) InputStream(version, accept string) (io.ReadCloser, bool, string, error) { s.version = version @@ -3761,6 +3764,13 @@ type UnregisteredAPIObject struct { func (obj *UnregisteredAPIObject) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } +func (obj *UnregisteredAPIObject) DeepCopyObject() runtime.Object { + if obj == nil { + return nil + } + clone := *obj + return &clone +} func TestWriteJSONDecodeError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { diff --git a/pkg/endpoints/handlers/rest_test.go b/pkg/endpoints/handlers/rest_test.go index 5b5cc60a7..931de27f9 100644 --- a/pkg/endpoints/handlers/rest_test.go +++ b/pkg/endpoints/handlers/rest_test.go @@ -64,6 +64,14 @@ type TestPatchSubType struct { StringField string `json:"theField"` } +func (obj *testPatchType) DeepCopyObject() runtime.Object { + if obj == nil { + return nil + } + clone := *obj + return &clone +} + func TestPatchAnonymousField(t *testing.T) { testGV := schema.GroupVersion{Group: "", Version: "v"} scheme.AddKnownTypes(testGV, &testPatchType{}) diff --git a/pkg/endpoints/openapi/testing/types.go b/pkg/endpoints/openapi/testing/types.go index 9b408f5f9..14a86419c 100644 --- a/pkg/endpoints/openapi/testing/types.go +++ b/pkg/endpoints/openapi/testing/types.go @@ -21,6 +21,7 @@ import ( ) // +k8s:deepcopy-gen=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type TestType struct { } diff --git a/pkg/endpoints/testing/types.go b/pkg/endpoints/testing/types.go index b197bb51e..0cfaacf31 100644 --- a/pkg/endpoints/testing/types.go +++ b/pkg/endpoints/testing/types.go @@ -20,6 +20,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + type Simple struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata"` @@ -29,6 +31,8 @@ type Simple struct { Labels map[string]string `json:"labels,omitempty"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + type SimpleRoot struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata"` @@ -38,6 +42,8 @@ type SimpleRoot struct { Labels map[string]string `json:"labels,omitempty"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + type SimpleGetOptions struct { metav1.TypeMeta `json:",inline"` Param1 string `json:"param1"` @@ -52,6 +58,8 @@ func (SimpleGetOptions) SwaggerDoc() map[string]string { } } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + type SimpleList struct { metav1.TypeMeta `json:",inline"` metav1.ListMeta `json:"metadata,inline"` @@ -59,6 +67,8 @@ type SimpleList struct { Items []Simple `json:"items,omitempty"` } +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + // SimpleXGSubresource is a cross group subresource, i.e. the subresource does not belong to the // same group as its parent resource. type SimpleXGSubresource struct { diff --git a/pkg/registry/generic/rest/streamer.go b/pkg/registry/generic/rest/streamer.go index 25477bd91..d84a01ab6 100644 --- a/pkg/registry/generic/rest/streamer.go +++ b/pkg/registry/generic/rest/streamer.go @@ -22,6 +22,7 @@ import ( "net/url" "strings" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/registry/rest" ) @@ -42,6 +43,9 @@ var _ rest.ResourceStreamer = &LocationStreamer{} func (obj *LocationStreamer) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } +func (obj *LocationStreamer) DeepCopyObject() runtime.Object { + panic("rest.LocationStreamer does not implement DeepCopyObject") +} // InputStream returns a stream with the contents of the URL location. If no location is provided, // a null stream is returned. diff --git a/pkg/registry/rest/rest.go b/pkg/registry/rest/rest.go index 07871c5bb..e7e9259a9 100644 --- a/pkg/registry/rest/rest.go +++ b/pkg/registry/rest/rest.go @@ -319,6 +319,8 @@ type StorageMetadata interface { ProducesObject(verb string) interface{} } +// +k8s:deepcopy-gen=true +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ConnectRequest is an object passed to admission control for Connect operations type ConnectRequest struct { // Name is the name of the object on which the connect request was made diff --git a/pkg/storage/selection_predicate_test.go b/pkg/storage/selection_predicate_test.go index 3c5da649a..8f6bcf7f8 100644 --- a/pkg/storage/selection_predicate_test.go +++ b/pkg/storage/selection_predicate_test.go @@ -36,6 +36,12 @@ type IgnoredList struct { func (obj *Ignored) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } func (obj *IgnoredList) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind } +func (obj *Ignored) DeepCopyObject() runtime.Object { + panic("Ignored does not support DeepCopy") +} +func (obj *IgnoredList) DeepCopyObject() runtime.Object { + panic("IgnoredList does not support DeepCopy") +} func TestSelectionPredicate(t *testing.T) { table := map[string]struct { diff --git a/pkg/storage/testing/types.go b/pkg/storage/testing/types.go index 6aaa1df26..37a70cbf5 100644 --- a/pkg/storage/testing/types.go +++ b/pkg/storage/testing/types.go @@ -20,6 +20,8 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + type TestResource struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata"`