From c30862a488919717b49ec5711cb40bc593044569 Mon Sep 17 00:00:00 2001 From: Clayton Coleman Date: Sat, 3 Dec 2016 22:42:29 -0500 Subject: [PATCH] Move OwnerReference to pkg/apis/meta/v1 and remove metatypes pkg OwnerReference is common. --- pkg/api/meta.go | 7 ++--- pkg/api/meta/interfaces.go | 9 +++--- pkg/api/meta/meta.go | 13 ++++---- pkg/api/meta/meta_test.go | 7 ++--- pkg/api/meta/metatypes/BUILD | 18 ----------- pkg/api/meta/metatypes/types.go | 30 ------------------- pkg/api/meta_test.go | 7 ++--- pkg/api/v1/meta.go | 7 ++--- pkg/apis/meta/v1/types.go | 22 ++++++++++++++ .../meta/v1/unstructured/unstructured_test.go | 5 ++-- .../garbagecollector/garbagecollector.go | 17 +++++------ .../garbagecollector/garbagecollector_test.go | 9 +++--- 12 files changed, 59 insertions(+), 92 deletions(-) delete mode 100644 pkg/api/meta/metatypes/BUILD delete mode 100644 pkg/api/meta/metatypes/types.go diff --git a/pkg/api/meta.go b/pkg/api/meta.go index 851e536c37f..57819496609 100644 --- a/pkg/api/meta.go +++ b/pkg/api/meta.go @@ -18,7 +18,6 @@ package api import ( "k8s.io/kubernetes/pkg/api/meta" - "k8s.io/kubernetes/pkg/api/meta/metatypes" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/conversion" "k8s.io/kubernetes/pkg/runtime" @@ -102,8 +101,8 @@ func (meta *ObjectMeta) SetAnnotations(annotations map[string]string) { meta.Ann func (meta *ObjectMeta) GetFinalizers() []string { return meta.Finalizers } func (meta *ObjectMeta) SetFinalizers(finalizers []string) { meta.Finalizers = finalizers } -func (meta *ObjectMeta) GetOwnerReferences() []metatypes.OwnerReference { - ret := make([]metatypes.OwnerReference, len(meta.OwnerReferences)) +func (meta *ObjectMeta) GetOwnerReferences() []metav1.OwnerReference { + ret := make([]metav1.OwnerReference, len(meta.OwnerReferences)) for i := 0; i < len(meta.OwnerReferences); i++ { ret[i].Kind = meta.OwnerReferences[i].Kind ret[i].Name = meta.OwnerReferences[i].Name @@ -117,7 +116,7 @@ func (meta *ObjectMeta) GetOwnerReferences() []metatypes.OwnerReference { return ret } -func (meta *ObjectMeta) SetOwnerReferences(references []metatypes.OwnerReference) { +func (meta *ObjectMeta) SetOwnerReferences(references []metav1.OwnerReference) { newReferences := make([]OwnerReference, len(references)) for i := 0; i < len(references); i++ { newReferences[i].Kind = references[i].Kind diff --git a/pkg/api/meta/interfaces.go b/pkg/api/meta/interfaces.go index c8a51517f1f..41cd0d013ba 100644 --- a/pkg/api/meta/interfaces.go +++ b/pkg/api/meta/interfaces.go @@ -17,8 +17,8 @@ limitations under the License. package meta import ( - "k8s.io/kubernetes/pkg/api/meta/metatypes" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" + "k8s.io/kubernetes/pkg/apis/meta/v1/unstructured" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime/schema" "k8s.io/kubernetes/pkg/types" @@ -61,13 +61,14 @@ type Object interface { SetAnnotations(annotations map[string]string) GetFinalizers() []string SetFinalizers(finalizers []string) - GetOwnerReferences() []metatypes.OwnerReference - SetOwnerReferences([]metatypes.OwnerReference) + GetOwnerReferences() []metav1.OwnerReference + SetOwnerReferences([]metav1.OwnerReference) GetClusterName() string SetClusterName(clusterName string) } -var _ Object = &runtime.Unstructured{} +// TODO: move me to pkg/apis/meta/v1/unstructured once Object is moved to pkg/apis/meta/v1 +var _ Object = &unstructured.Unstructured{} type ListMetaAccessor interface { GetListMeta() List diff --git a/pkg/api/meta/meta.go b/pkg/api/meta/meta.go index 6ef7c0f440f..363c7814245 100644 --- a/pkg/api/meta/meta.go +++ b/pkg/api/meta/meta.go @@ -20,7 +20,6 @@ import ( "fmt" "reflect" - "k8s.io/kubernetes/pkg/api/meta/metatypes" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/conversion" "k8s.io/kubernetes/pkg/runtime" @@ -314,7 +313,7 @@ func (resourceAccessor) SetResourceVersion(obj runtime.Object, version string) e } // extractFromOwnerReference extracts v to o. v is the OwnerReferences field of an object. -func extractFromOwnerReference(v reflect.Value, o *metatypes.OwnerReference) error { +func extractFromOwnerReference(v reflect.Value, o *metav1.OwnerReference) error { if err := runtime.Field(v, "APIVersion", &o.APIVersion); err != nil { return err } @@ -339,7 +338,7 @@ func extractFromOwnerReference(v reflect.Value, o *metatypes.OwnerReference) err } // setOwnerReference sets v to o. v is the OwnerReferences field of an object. -func setOwnerReference(v reflect.Value, o *metatypes.OwnerReference) error { +func setOwnerReference(v reflect.Value, o *metav1.OwnerReference) error { if err := runtime.SetField(o.APIVersion, v, "APIVersion"); err != nil { return err } @@ -521,8 +520,8 @@ func (a genericAccessor) SetFinalizers(finalizers []string) { *a.finalizers = finalizers } -func (a genericAccessor) GetOwnerReferences() []metatypes.OwnerReference { - var ret []metatypes.OwnerReference +func (a genericAccessor) GetOwnerReferences() []metav1.OwnerReference { + var ret []metav1.OwnerReference s := a.ownerReferences if s.Kind() != reflect.Ptr || s.Elem().Kind() != reflect.Slice { glog.Errorf("expect %v to be a pointer to slice", s) @@ -530,7 +529,7 @@ func (a genericAccessor) GetOwnerReferences() []metatypes.OwnerReference { } s = s.Elem() // Set the capacity to one element greater to avoid copy if the caller later append an element. - ret = make([]metatypes.OwnerReference, s.Len(), s.Len()+1) + ret = make([]metav1.OwnerReference, s.Len(), s.Len()+1) for i := 0; i < s.Len(); i++ { if err := extractFromOwnerReference(s.Index(i), &ret[i]); err != nil { glog.Errorf("extractFromOwnerReference failed: %v", err) @@ -540,7 +539,7 @@ func (a genericAccessor) GetOwnerReferences() []metatypes.OwnerReference { return ret } -func (a genericAccessor) SetOwnerReferences(references []metatypes.OwnerReference) { +func (a genericAccessor) SetOwnerReferences(references []metav1.OwnerReference) { s := a.ownerReferences if s.Kind() != reflect.Ptr || s.Elem().Kind() != reflect.Slice { glog.Errorf("expect %v to be a pointer to slice", s) diff --git a/pkg/api/meta/meta_test.go b/pkg/api/meta/meta_test.go index 7e920a0a1f7..7d95ddac1b4 100644 --- a/pkg/api/meta/meta_test.go +++ b/pkg/api/meta/meta_test.go @@ -24,7 +24,6 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/meta" - "k8s.io/kubernetes/pkg/api/meta/metatypes" "k8s.io/kubernetes/pkg/api/v1" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/runtime" @@ -337,14 +336,14 @@ type MyAPIObject2 struct { v1.ObjectMeta } -func getObjectMetaAndOwnerRefereneces() (myAPIObject2 MyAPIObject2, metaOwnerReferences []metatypes.OwnerReference) { +func getObjectMetaAndOwnerRefereneces() (myAPIObject2 MyAPIObject2, metaOwnerReferences []metav1.OwnerReference) { fuzz.New().NilChance(.5).NumElements(1, 5).Fuzz(&myAPIObject2) references := myAPIObject2.ObjectMeta.OwnerReferences // This is necessary for the test to pass because the getter will return a // non-nil slice. - metaOwnerReferences = make([]metatypes.OwnerReference, 0) + metaOwnerReferences = make([]metav1.OwnerReference, 0) for i := 0; i < len(references); i++ { - metaOwnerReferences = append(metaOwnerReferences, metatypes.OwnerReference{ + metaOwnerReferences = append(metaOwnerReferences, metav1.OwnerReference{ Kind: references[i].Kind, Name: references[i].Name, UID: references[i].UID, diff --git a/pkg/api/meta/metatypes/BUILD b/pkg/api/meta/metatypes/BUILD deleted file mode 100644 index ded660072f3..00000000000 --- a/pkg/api/meta/metatypes/BUILD +++ /dev/null @@ -1,18 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) - -load( - "@io_bazel_rules_go//go:def.bzl", - "go_binary", - "go_library", - "go_test", - "cgo_library", -) - -go_library( - name = "go_default_library", - srcs = ["types.go"], - tags = ["automanaged"], - deps = ["//pkg/types:go_default_library"], -) diff --git a/pkg/api/meta/metatypes/types.go b/pkg/api/meta/metatypes/types.go deleted file mode 100644 index 41e6596d771..00000000000 --- a/pkg/api/meta/metatypes/types.go +++ /dev/null @@ -1,30 +0,0 @@ -/* -Copyright 2014 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. -*/ - -// The types defined in this package are used by the meta package to represent -// the in-memory version of objects. We cannot reuse the __internal version of -// API objects because it causes import cycle. -package metatypes - -import "k8s.io/kubernetes/pkg/types" - -type OwnerReference struct { - APIVersion string - Kind string - UID types.UID - Name string - Controller *bool -} diff --git a/pkg/api/meta_test.go b/pkg/api/meta_test.go index 66053fef2c3..3b48d4fe3c6 100644 --- a/pkg/api/meta_test.go +++ b/pkg/api/meta_test.go @@ -24,7 +24,6 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/meta" - "k8s.io/kubernetes/pkg/api/meta/metatypes" "k8s.io/kubernetes/pkg/apimachinery/registered" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/runtime" @@ -67,12 +66,12 @@ func TestHasObjectMetaSystemFieldValues(t *testing.T) { } } -func getObjectMetaAndOwnerReferences() (objectMeta api.ObjectMeta, metaOwnerReferences []metatypes.OwnerReference) { +func getObjectMetaAndOwnerReferences() (objectMeta api.ObjectMeta, metaOwnerReferences []metav1.OwnerReference) { fuzz.New().NilChance(.5).NumElements(1, 5).Fuzz(&objectMeta) references := objectMeta.OwnerReferences - metaOwnerReferences = make([]metatypes.OwnerReference, 0) + metaOwnerReferences = make([]metav1.OwnerReference, 0) for i := 0; i < len(references); i++ { - metaOwnerReferences = append(metaOwnerReferences, metatypes.OwnerReference{ + metaOwnerReferences = append(metaOwnerReferences, metav1.OwnerReference{ Kind: references[i].Kind, Name: references[i].Name, UID: references[i].UID, diff --git a/pkg/api/v1/meta.go b/pkg/api/v1/meta.go index dde646c4eee..177fb685ea1 100644 --- a/pkg/api/v1/meta.go +++ b/pkg/api/v1/meta.go @@ -18,7 +18,6 @@ package v1 import ( "k8s.io/kubernetes/pkg/api/meta" - "k8s.io/kubernetes/pkg/api/meta/metatypes" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/types" ) @@ -54,8 +53,8 @@ func (meta *ObjectMeta) SetAnnotations(annotations map[string]string) { meta.Ann func (meta *ObjectMeta) GetFinalizers() []string { return meta.Finalizers } func (meta *ObjectMeta) SetFinalizers(finalizers []string) { meta.Finalizers = finalizers } -func (meta *ObjectMeta) GetOwnerReferences() []metatypes.OwnerReference { - ret := make([]metatypes.OwnerReference, len(meta.OwnerReferences)) +func (meta *ObjectMeta) GetOwnerReferences() []metav1.OwnerReference { + ret := make([]metav1.OwnerReference, len(meta.OwnerReferences)) for i := 0; i < len(meta.OwnerReferences); i++ { ret[i].Kind = meta.OwnerReferences[i].Kind ret[i].Name = meta.OwnerReferences[i].Name @@ -69,7 +68,7 @@ func (meta *ObjectMeta) GetOwnerReferences() []metatypes.OwnerReference { return ret } -func (meta *ObjectMeta) SetOwnerReferences(references []metatypes.OwnerReference) { +func (meta *ObjectMeta) SetOwnerReferences(references []metav1.OwnerReference) { newReferences := make([]OwnerReference, len(references)) for i := 0; i < len(references); i++ { newReferences[i].Kind = references[i].Kind diff --git a/pkg/apis/meta/v1/types.go b/pkg/apis/meta/v1/types.go index 66a166352d6..e900b719438 100644 --- a/pkg/apis/meta/v1/types.go +++ b/pkg/apis/meta/v1/types.go @@ -30,6 +30,8 @@ import ( "strings" "github.com/ugorji/go/codec" + + "k8s.io/kubernetes/pkg/types" ) // TypeMeta describes an individual object in an API response or request @@ -71,6 +73,26 @@ type ListMeta struct { ResourceVersion string `json:"resourceVersion,omitempty" protobuf:"bytes,2,opt,name=resourceVersion"` } +// OwnerReference contains enough information to let you identify an owning +// object. Currently, an owning object must be in the same namespace, so there +// is no namespace field. +type OwnerReference struct { + // API version of the referent. + APIVersion string `json:"apiVersion" protobuf:"bytes,5,opt,name=apiVersion"` + // Kind of the referent. + // More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#types-kinds + Kind string `json:"kind" protobuf:"bytes,1,opt,name=kind"` + // Name of the referent. + // More info: http://kubernetes.io/docs/user-guide/identifiers#names + Name string `json:"name" protobuf:"bytes,3,opt,name=name"` + // UID of the referent. + // More info: http://kubernetes.io/docs/user-guide/identifiers#uids + UID types.UID `json:"uid" protobuf:"bytes,4,opt,name=uid,casttype=k8s.io/kubernetes/pkg/types.UID"` + // If true, this reference points to the managing controller. + // +optional + Controller *bool `json:"controller,omitempty" protobuf:"varint,6,opt,name=controller"` +} + // ExportOptions is the query options to the standard REST get call. type ExportOptions struct { TypeMeta `json:",inline"` diff --git a/pkg/apis/meta/v1/unstructured/unstructured_test.go b/pkg/apis/meta/v1/unstructured/unstructured_test.go index c8ff60b19da..272e80ad0f9 100644 --- a/pkg/apis/meta/v1/unstructured/unstructured_test.go +++ b/pkg/apis/meta/v1/unstructured/unstructured_test.go @@ -24,7 +24,6 @@ import ( "time" "k8s.io/kubernetes/pkg/api" - "k8s.io/kubernetes/pkg/api/meta/metatypes" "k8s.io/kubernetes/pkg/api/testapi" "k8s.io/kubernetes/pkg/api/validation" "k8s.io/kubernetes/pkg/apimachinery/registered" @@ -215,7 +214,7 @@ func TestUnstructuredGetters(t *testing.T) { t.Errorf("GetAnnotations() = %s, want %s", got, want) } refs := unstruct.GetOwnerReferences() - expectedOwnerReferences := []metatypes.OwnerReference{ + expectedOwnerReferences := []metav1.OwnerReference{ { Kind: "Pod", Name: "poda", @@ -301,7 +300,7 @@ func TestUnstructuredSetters(t *testing.T) { unstruct.SetDeletionTimestamp(&date) unstruct.SetLabels(map[string]string{"test_label": "test_value"}) unstruct.SetAnnotations(map[string]string{"test_annotation": "test_value"}) - newOwnerReferences := []metatypes.OwnerReference{ + newOwnerReferences := []metav1.OwnerReference{ { Kind: "Pod", Name: "poda", diff --git a/pkg/controller/garbagecollector/garbagecollector.go b/pkg/controller/garbagecollector/garbagecollector.go index e94367fad7f..95e025bd42b 100644 --- a/pkg/controller/garbagecollector/garbagecollector.go +++ b/pkg/controller/garbagecollector/garbagecollector.go @@ -26,7 +26,6 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/meta" - "k8s.io/kubernetes/pkg/api/meta/metatypes" "k8s.io/kubernetes/pkg/api/v1" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" "k8s.io/kubernetes/pkg/client/cache" @@ -52,7 +51,7 @@ type monitor struct { } type objectReference struct { - metatypes.OwnerReference + metav1.OwnerReference // This is needed by the dynamic client Namespace string } @@ -72,7 +71,7 @@ type node struct { dependents map[*node]struct{} // When processing an Update event, we need to compare the updated // ownerReferences with the owners recorded in the graph. - owners []metatypes.OwnerReference + owners []metav1.OwnerReference } func (ownerNode *node) addDependent(dependent *node) { @@ -138,7 +137,7 @@ type Propagator struct { // exist in the p.uidToNode yet, a "virtual" node will be created to represent // the owner. The "virtual" node will be enqueued to the dirtyQueue, so that // processItem() will verify if the owner exists according to the API server. -func (p *Propagator) addDependentToOwners(n *node, owners []metatypes.OwnerReference) { +func (p *Propagator) addDependentToOwners(n *node, owners []metav1.OwnerReference) { for _, owner := range owners { ownerNode, ok := p.uidToNode.Read(owner.UID) if !ok { @@ -170,7 +169,7 @@ func (p *Propagator) insertNode(n *node) { } // removeDependentFromOwners remove n from owners' dependents list. -func (p *Propagator) removeDependentFromOwners(n *node, owners []metatypes.OwnerReference) { +func (p *Propagator) removeDependentFromOwners(n *node, owners []metav1.OwnerReference) { for _, owner := range owners { ownerNode, ok := p.uidToNode.Read(owner.UID) if !ok { @@ -189,13 +188,13 @@ func (p *Propagator) removeNode(n *node) { // TODO: profile this function to see if a naive N^2 algorithm performs better // when the number of references is small. -func referencesDiffs(old []metatypes.OwnerReference, new []metatypes.OwnerReference) (added []metatypes.OwnerReference, removed []metatypes.OwnerReference) { - oldUIDToRef := make(map[string]metatypes.OwnerReference) +func referencesDiffs(old []metav1.OwnerReference, new []metav1.OwnerReference) (added []metav1.OwnerReference, removed []metav1.OwnerReference) { + oldUIDToRef := make(map[string]metav1.OwnerReference) for i := 0; i < len(old); i++ { oldUIDToRef[string(old[i].UID)] = old[i] } oldUIDSet := sets.StringKeySet(oldUIDToRef) - newUIDToRef := make(map[string]metatypes.OwnerReference) + newUIDToRef := make(map[string]metav1.OwnerReference) for i := 0; i < len(new); i++ { newUIDToRef[string(new[i].UID)] = new[i] } @@ -372,7 +371,7 @@ func (p *Propagator) processEvent() { case (event.eventType == addEvent || event.eventType == updateEvent) && !found: newNode := &node{ identity: objectReference{ - OwnerReference: metatypes.OwnerReference{ + OwnerReference: metav1.OwnerReference{ APIVersion: typeAccessor.GetAPIVersion(), Kind: typeAccessor.GetKind(), UID: accessor.GetUID(), diff --git a/pkg/controller/garbagecollector/garbagecollector_test.go b/pkg/controller/garbagecollector/garbagecollector_test.go index 2efbebfc81f..79a555cfb96 100644 --- a/pkg/controller/garbagecollector/garbagecollector_test.go +++ b/pkg/controller/garbagecollector/garbagecollector_test.go @@ -27,7 +27,6 @@ import ( _ "k8s.io/kubernetes/pkg/api/install" - "k8s.io/kubernetes/pkg/api/meta/metatypes" "k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/apimachinery/registered" metav1 "k8s.io/kubernetes/pkg/apis/meta/v1" @@ -170,7 +169,7 @@ func TestProcessItem(t *testing.T) { gc := setupGC(t, clientConfig) item := &node{ identity: objectReference{ - OwnerReference: metatypes.OwnerReference{ + OwnerReference: metav1.OwnerReference{ Kind: pod.Kind, APIVersion: pod.APIVersion, Name: pod.Name, @@ -326,8 +325,8 @@ func TestDependentsRace(t *testing.T) { go func() { for i := 0; i < updates; i++ { dependent := &node{} - gc.propagator.addDependentToOwners(dependent, []metatypes.OwnerReference{{UID: ownerUID}}) - gc.propagator.removeDependentFromOwners(dependent, []metatypes.OwnerReference{{UID: ownerUID}}) + gc.propagator.addDependentToOwners(dependent, []metav1.OwnerReference{{UID: ownerUID}}) + gc.propagator.removeDependentFromOwners(dependent, []metav1.OwnerReference{{UID: ownerUID}}) } }() go func() { @@ -366,7 +365,7 @@ func TestGCListWatcher(t *testing.T) { func podToGCNode(pod *v1.Pod) *node { return &node{ identity: objectReference{ - OwnerReference: metatypes.OwnerReference{ + OwnerReference: metav1.OwnerReference{ Kind: pod.Kind, APIVersion: pod.APIVersion, Name: pod.Name,