From 2699be2e7ecfaf448427122cf5c2f0ef8a352642 Mon Sep 17 00:00:00 2001 From: Wojciech Tyczynski Date: Fri, 25 Mar 2016 09:57:45 +0100 Subject: [PATCH] Switch api.ObjetaMetaFor to meta.Accessor --- pkg/admission/errors.go | 12 +++--- pkg/api/meta.go | 33 +++++++++------ pkg/api/meta/interfaces.go | 4 ++ pkg/api/meta/meta.go | 42 ++++++++++++++----- pkg/client/testing/core/fixture.go | 9 ++-- pkg/client/unversioned/testclient/fixture.go | 9 ++-- pkg/controller/controller_utils.go | 11 ++--- .../framework/fake_controller_source.go | 10 ++--- pkg/kubectl/cmd/annotate.go | 21 ++++++---- pkg/kubectl/cmd/apply_test.go | 20 +++++---- pkg/kubectl/cmd/label.go | 28 ++++++++----- pkg/kubectl/cmd/util/helpers.go | 11 ++--- pkg/kubectl/history.go | 5 ++- pkg/registry/generic/etcd/etcd.go | 22 +++++----- pkg/registry/registrytest/etcd.go | 13 +++--- pkg/storage/etcd/api_object_versioner.go | 11 ++--- .../pkg/admission/resourcequota/admission.go | 7 ++-- 17 files changed, 160 insertions(+), 108 deletions(-) diff --git a/pkg/admission/errors.go b/pkg/admission/errors.go index 390f9dab17847..cdb53e482f467 100644 --- a/pkg/admission/errors.go +++ b/pkg/admission/errors.go @@ -17,8 +17,8 @@ limitations under the License. package admission import ( - "k8s.io/kubernetes/pkg/api" apierrors "k8s.io/kubernetes/pkg/api/errors" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" utilerrors "k8s.io/kubernetes/pkg/util/errors" ) @@ -28,16 +28,16 @@ func extractResourceName(a Attributes) (name string, resource unversioned.GroupR resource = a.GetResource() obj := a.GetObject() if obj != nil { - objectMeta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return "", unversioned.GroupResource{}, err } // this is necessary because name object name generation has not occurred yet - if len(objectMeta.Name) > 0 { - name = objectMeta.Name - } else if len(objectMeta.GenerateName) > 0 { - name = objectMeta.GenerateName + if len(accessor.GetName()) > 0 { + name = accessor.GetName() + } else if len(accessor.GetGenerateName()) > 0 { + name = accessor.GetGenerateName() } } return name, resource, nil diff --git a/pkg/api/meta.go b/pkg/api/meta.go index ec84c3c8a92fa..d5590f49d246f 100644 --- a/pkg/api/meta.go +++ b/pkg/api/meta.go @@ -39,6 +39,7 @@ func HasObjectMetaSystemFieldValues(meta *ObjectMeta) bool { // ObjectMetaFor returns a pointer to a provided object's ObjectMeta. // TODO: allow runtime.Unknown to extract this object +// TODO: Remove this function and use meta.Accessor() instead. func ObjectMetaFor(obj runtime.Object) (*ObjectMeta, error) { v, err := conversion.EnforcePtr(obj) if err != nil { @@ -64,18 +65,26 @@ func ListMetaFor(obj runtime.Object) (*unversioned.ListMeta, error) { // Namespace implements meta.Object for any object with an ObjectMeta typed field. Allows // fast, direct access to metadata fields for API objects. -func (meta *ObjectMeta) GetNamespace() string { return meta.Namespace } -func (meta *ObjectMeta) SetNamespace(namespace string) { meta.Namespace = namespace } -func (meta *ObjectMeta) GetName() string { return meta.Name } -func (meta *ObjectMeta) SetName(name string) { meta.Name = name } -func (meta *ObjectMeta) GetGenerateName() string { return meta.GenerateName } -func (meta *ObjectMeta) SetGenerateName(generateName string) { meta.GenerateName = generateName } -func (meta *ObjectMeta) GetUID() types.UID { return meta.UID } -func (meta *ObjectMeta) SetUID(uid types.UID) { meta.UID = uid } -func (meta *ObjectMeta) GetResourceVersion() string { return meta.ResourceVersion } -func (meta *ObjectMeta) SetResourceVersion(version string) { meta.ResourceVersion = version } -func (meta *ObjectMeta) GetSelfLink() string { return meta.SelfLink } -func (meta *ObjectMeta) SetSelfLink(selfLink string) { meta.SelfLink = selfLink } +func (meta *ObjectMeta) GetNamespace() string { return meta.Namespace } +func (meta *ObjectMeta) SetNamespace(namespace string) { meta.Namespace = namespace } +func (meta *ObjectMeta) GetName() string { return meta.Name } +func (meta *ObjectMeta) SetName(name string) { meta.Name = name } +func (meta *ObjectMeta) GetGenerateName() string { return meta.GenerateName } +func (meta *ObjectMeta) SetGenerateName(generateName string) { meta.GenerateName = generateName } +func (meta *ObjectMeta) GetUID() types.UID { return meta.UID } +func (meta *ObjectMeta) SetUID(uid types.UID) { meta.UID = uid } +func (meta *ObjectMeta) GetResourceVersion() string { return meta.ResourceVersion } +func (meta *ObjectMeta) SetResourceVersion(version string) { meta.ResourceVersion = version } +func (meta *ObjectMeta) GetSelfLink() string { return meta.SelfLink } +func (meta *ObjectMeta) SetSelfLink(selfLink string) { meta.SelfLink = selfLink } +func (meta *ObjectMeta) GetCreationTimestamp() unversioned.Time { return meta.CreationTimestamp } +func (meta *ObjectMeta) SetCreationTimestamp(creationTimestamp unversioned.Time) { + meta.CreationTimestamp = creationTimestamp +} +func (meta *ObjectMeta) GetDeletionTimestamp() *unversioned.Time { return meta.DeletionTimestamp } +func (meta *ObjectMeta) SetDeletionTimestamp(deletionTimestamp *unversioned.Time) { + meta.DeletionTimestamp = deletionTimestamp +} func (meta *ObjectMeta) GetLabels() map[string]string { return meta.Labels } func (meta *ObjectMeta) SetLabels(labels map[string]string) { meta.Labels = labels } func (meta *ObjectMeta) GetAnnotations() map[string]string { return meta.Annotations } diff --git a/pkg/api/meta/interfaces.go b/pkg/api/meta/interfaces.go index f1402e7ad79e3..8001d57e845a9 100644 --- a/pkg/api/meta/interfaces.go +++ b/pkg/api/meta/interfaces.go @@ -49,6 +49,10 @@ type Object interface { SetResourceVersion(version string) GetSelfLink() string SetSelfLink(selfLink string) + GetCreationTimestamp() unversioned.Time + SetCreationTimestamp(timestamp unversioned.Time) + GetDeletionTimestamp() *unversioned.Time + SetDeletionTimestamp(timestamp *unversioned.Time) GetLabels() map[string]string SetLabels(labels map[string]string) GetAnnotations() map[string]string diff --git a/pkg/api/meta/meta.go b/pkg/api/meta/meta.go index bf11abd18703f..8a08ded2600b1 100644 --- a/pkg/api/meta/meta.go +++ b/pkg/api/meta/meta.go @@ -24,6 +24,8 @@ import ( "k8s.io/kubernetes/pkg/conversion" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/types" + + "github.com/golang/glog" ) // Accessor takes an arbitrary object pointer and returns meta.Interface. @@ -40,6 +42,8 @@ func Accessor(obj interface{}) (Object, error) { if oi, ok := obj.(Object); ok { return oi, nil } + + glog.V(4).Infof("Calling Accessor on non-internal object: %v", reflect.TypeOf(obj)) // legacy path for objects that do not implement Object and ObjectMetaAccessor via // reflection - very slow code path. v, err := conversion.EnforcePtr(obj) @@ -321,16 +325,18 @@ func (resourceAccessor) SetResourceVersion(obj runtime.Object, version string) e // genericAccessor contains pointers to strings that can modify an arbitrary // struct and implements the Accessor interface. type genericAccessor struct { - namespace *string - name *string - generateName *string - uid *types.UID - apiVersion *string - kind *string - resourceVersion *string - selfLink *string - labels *map[string]string - annotations *map[string]string + namespace *string + name *string + generateName *string + uid *types.UID + apiVersion *string + kind *string + resourceVersion *string + selfLink *string + creationTimestamp *unversioned.Time + deletionTimestamp **unversioned.Time + labels *map[string]string + annotations *map[string]string } func (a genericAccessor) GetNamespace() string { @@ -421,6 +427,22 @@ func (a genericAccessor) SetSelfLink(selfLink string) { *a.selfLink = selfLink } +func (a genericAccessor) GetCreationTimestamp() unversioned.Time { + return *a.creationTimestamp +} + +func (a genericAccessor) SetCreationTimestamp(timestamp unversioned.Time) { + *a.creationTimestamp = timestamp +} + +func (a genericAccessor) GetDeletionTimestamp() *unversioned.Time { + return *a.deletionTimestamp +} + +func (a genericAccessor) SetDeletionTimestamp(timestamp *unversioned.Time) { + *a.deletionTimestamp = timestamp +} + func (a genericAccessor) GetLabels() map[string]string { if a.labels == nil { return nil diff --git a/pkg/client/testing/core/fixture.go b/pkg/client/testing/core/fixture.go index 312051b58ec7f..0406c4659b6ea 100644 --- a/pkg/client/testing/core/fixture.go +++ b/pkg/client/testing/core/fixture.go @@ -22,7 +22,6 @@ import ( "reflect" "strings" - "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" @@ -79,19 +78,19 @@ func ObjectReaction(o ObjectRetriever, mapper meta.RESTMapper) ReactionFunc { return true, resource, err case CreateAction: - meta, err := api.ObjectMetaFor(castAction.GetObject()) + accessor, err := meta.Accessor(castAction.GetObject()) if err != nil { return true, nil, err } - resource, err := o.Kind(kind, meta.Name) + resource, err := o.Kind(kind, accessor.GetName()) return true, resource, err case UpdateAction: - meta, err := api.ObjectMetaFor(castAction.GetObject()) + accessor, err := meta.Accessor(castAction.GetObject()) if err != nil { return true, nil, err } - resource, err := o.Kind(kind, meta.Name) + resource, err := o.Kind(kind, accessor.GetName()) return true, resource, err default: diff --git a/pkg/client/unversioned/testclient/fixture.go b/pkg/client/unversioned/testclient/fixture.go index 4956a1b90fb47..341beeac29ac3 100644 --- a/pkg/client/unversioned/testclient/fixture.go +++ b/pkg/client/unversioned/testclient/fixture.go @@ -22,7 +22,6 @@ import ( "reflect" "strings" - "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" @@ -80,19 +79,19 @@ func ObjectReaction(o ObjectRetriever, mapper meta.RESTMapper) ReactionFunc { return true, resource, err case CreateAction: - meta, err := api.ObjectMetaFor(castAction.GetObject()) + accessor, err := meta.Accessor(castAction.GetObject()) if err != nil { return true, nil, err } - resource, err := o.Kind(kind, meta.Name) + resource, err := o.Kind(kind, accessor.GetName()) return true, resource, err case UpdateAction: - meta, err := api.ObjectMetaFor(castAction.GetObject()) + accessor, err := meta.Accessor(castAction.GetObject()) if err != nil { return true, nil, err } - resource, err := o.Kind(kind, meta.Name) + resource, err := o.Kind(kind, accessor.GetName()) return true, resource, err default: diff --git a/pkg/controller/controller_utils.go b/pkg/controller/controller_utils.go index fc377134d9300..ff043d24e0d1f 100644 --- a/pkg/controller/controller_utils.go +++ b/pkg/controller/controller_utils.go @@ -24,6 +24,7 @@ import ( "github.com/golang/glog" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/validation" "k8s.io/kubernetes/pkg/apis/extensions" @@ -418,11 +419,11 @@ func (r RealPodControl) createPods(nodeName, namespace string, template *api.Pod if err != nil { return err } - meta, err := api.ObjectMetaFor(object) + accessor, err := meta.Accessor(object) if err != nil { return fmt.Errorf("object does not have ObjectMeta, %v", err) } - prefix := getPodsPrefix(meta.Name) + prefix := getPodsPrefix(accessor.GetName()) pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ @@ -444,14 +445,14 @@ func (r RealPodControl) createPods(nodeName, namespace string, template *api.Pod r.Recorder.Eventf(object, api.EventTypeWarning, "FailedCreate", "Error creating: %v", err) return fmt.Errorf("unable to create pods: %v", err) } else { - glog.V(4).Infof("Controller %v created pod %v", meta.Name, newPod.Name) + glog.V(4).Infof("Controller %v created pod %v", accessor.GetName(), newPod.Name) r.Recorder.Eventf(object, api.EventTypeNormal, "SuccessfulCreate", "Created pod: %v", newPod.Name) } return nil } func (r RealPodControl) DeletePod(namespace string, podID string, object runtime.Object) error { - meta, err := api.ObjectMetaFor(object) + accessor, err := meta.Accessor(object) if err != nil { return fmt.Errorf("object does not have ObjectMeta, %v", err) } @@ -459,7 +460,7 @@ func (r RealPodControl) DeletePod(namespace string, podID string, object runtime r.Recorder.Eventf(object, api.EventTypeWarning, "FailedDelete", "Error deleting: %v", err) return fmt.Errorf("unable to delete pods: %v", err) } else { - glog.V(4).Infof("Controller %v deleted pod %v", meta.Name, podID) + glog.V(4).Infof("Controller %v deleted pod %v", accessor.GetName(), podID) r.Recorder.Eventf(object, api.EventTypeNormal, "SuccessfulDelete", "Deleted pod: %v", podID) } return nil diff --git a/pkg/controller/framework/fake_controller_source.go b/pkg/controller/framework/fake_controller_source.go index fa28171137c42..bebacb531a636 100644 --- a/pkg/controller/framework/fake_controller_source.go +++ b/pkg/controller/framework/fake_controller_source.go @@ -89,8 +89,8 @@ func (f *FakeControllerSource) DeleteDropWatch(lastValue runtime.Object) { f.Change(watch.Event{Type: watch.Deleted, Object: lastValue}, 0) } -func (f *FakeControllerSource) key(meta *api.ObjectMeta) nnu { - return nnu{meta.Namespace, meta.Name, meta.UID} +func (f *FakeControllerSource) key(accessor meta.Object) nnu { + return nnu{accessor.GetNamespace(), accessor.GetName(), accessor.GetUID()} } // Change records the given event (setting the object's resource version) and @@ -99,15 +99,15 @@ func (f *FakeControllerSource) Change(e watch.Event, watchProbability float64) { f.lock.Lock() defer f.lock.Unlock() - objMeta, err := api.ObjectMetaFor(e.Object) + accessor, err := meta.Accessor(e.Object) if err != nil { panic(err) // this is test code only } resourceVersion := len(f.changes) + 1 - objMeta.ResourceVersion = strconv.Itoa(resourceVersion) + accessor.SetResourceVersion(strconv.Itoa(resourceVersion)) f.changes = append(f.changes, e) - key := f.key(objMeta) + key := f.key(accessor) switch e.Type { case watch.Added, watch.Modified: f.items[key] = e.Object diff --git a/pkg/kubectl/cmd/annotate.go b/pkg/kubectl/cmd/annotate.go index 1737152b877ae..ddfa8261fee09 100644 --- a/pkg/kubectl/cmd/annotate.go +++ b/pkg/kubectl/cmd/annotate.go @@ -26,6 +26,7 @@ import ( "github.com/golang/glog" "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/resource" @@ -307,14 +308,14 @@ func validateAnnotations(removeAnnotations []string, newAnnotations map[string]s } // validateNoAnnotationOverwrites validates that when overwrite is false, to-be-updated annotations don't exist in the object annotation map (yet) -func validateNoAnnotationOverwrites(meta *api.ObjectMeta, annotations map[string]string) error { +func validateNoAnnotationOverwrites(accessor meta.Object, annotations map[string]string) error { var buf bytes.Buffer for key := range annotations { // change-cause annotation can always be overwritten if key == kubectl.ChangeCauseAnnotation { continue } - if value, found := meta.Annotations[key]; found { + if value, found := accessor.GetAnnotations()[key]; found { if buf.Len() > 0 { buf.WriteString("; ") } @@ -329,29 +330,31 @@ func validateNoAnnotationOverwrites(meta *api.ObjectMeta, annotations map[string // updateAnnotations updates annotations of obj func (o AnnotateOptions) updateAnnotations(obj runtime.Object) error { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return err } if !o.overwrite { - if err := validateNoAnnotationOverwrites(meta, o.newAnnotations); err != nil { + if err := validateNoAnnotationOverwrites(accessor, o.newAnnotations); err != nil { return err } } - if meta.Annotations == nil { - meta.Annotations = make(map[string]string) + annotations := accessor.GetAnnotations() + if annotations == nil { + annotations = make(map[string]string) } for key, value := range o.newAnnotations { - meta.Annotations[key] = value + annotations[key] = value } for _, annotation := range o.removeAnnotations { - delete(meta.Annotations, annotation) + delete(annotations, annotation) } + accessor.SetAnnotations(annotations) if len(o.resourceVersion) != 0 { - meta.ResourceVersion = o.resourceVersion + accessor.SetResourceVersion(o.resourceVersion) } return nil } diff --git a/pkg/kubectl/cmd/apply_test.go b/pkg/kubectl/cmd/apply_test.go index 93a4d304e52bd..a46e0699d22b7 100644 --- a/pkg/kubectl/cmd/apply_test.go +++ b/pkg/kubectl/cmd/apply_test.go @@ -28,6 +28,7 @@ import ( "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/client/unversioned/fake" "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" @@ -94,33 +95,36 @@ func readServiceFromFile(t *testing.T, filename string) *api.Service { } func annotateRuntimeObject(t *testing.T, originalObj, currentObj runtime.Object, kind string) (string, []byte) { - originalMeta, err := api.ObjectMetaFor(originalObj) + originalAccessor, err := meta.Accessor(originalObj) if err != nil { t.Fatal(err) } - originalMeta.Labels["DELETE_ME"] = "DELETE_ME" + originalLabels := originalAccessor.GetLabels() + originalLabels["DELETE_ME"] = "DELETE_ME" + originalAccessor.SetLabels(originalLabels) original, err := json.Marshal(originalObj) if err != nil { t.Fatal(err) } - currentMeta, err := api.ObjectMetaFor(currentObj) + currentAccessor, err := meta.Accessor(currentObj) if err != nil { t.Fatal(err) } - if currentMeta.Annotations == nil { - currentMeta.Annotations = map[string]string{} + currentAnnotations := currentAccessor.GetAnnotations() + if currentAnnotations == nil { + currentAnnotations = make(map[string]string) } - - currentMeta.Annotations[kubectl.LastAppliedConfigAnnotation] = string(original) + currentAnnotations[kubectl.LastAppliedConfigAnnotation] = string(original) + currentAccessor.SetAnnotations(currentAnnotations) current, err := json.Marshal(currentObj) if err != nil { t.Fatal(err) } - return currentMeta.Name, current + return currentAccessor.GetName(), current } func readAndAnnotateReplicationController(t *testing.T, filename string) (string, []byte) { diff --git a/pkg/kubectl/cmd/label.go b/pkg/kubectl/cmd/label.go index f0d82a6b70285..138121309cda2 100644 --- a/pkg/kubectl/cmd/label.go +++ b/pkg/kubectl/cmd/label.go @@ -26,6 +26,7 @@ import ( "github.com/golang/glog" "github.com/spf13/cobra" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/resource" @@ -104,10 +105,10 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { return cmd } -func validateNoOverwrites(meta *api.ObjectMeta, labels map[string]string) error { +func validateNoOverwrites(accessor meta.Object, labels map[string]string) error { allErrs := []error{} for key := range labels { - if value, found := meta.Labels[key]; found { + if value, found := accessor.GetLabels()[key]; found { allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value)) } } @@ -139,29 +140,31 @@ func parseLabels(spec []string) (map[string]string, []string, error) { } func labelFunc(obj runtime.Object, overwrite bool, resourceVersion string, labels map[string]string, remove []string) error { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return err } if !overwrite { - if err := validateNoOverwrites(meta, labels); err != nil { + if err := validateNoOverwrites(accessor, labels); err != nil { return err } } - if meta.Labels == nil { - meta.Labels = make(map[string]string) + objLabels := accessor.GetLabels() + if objLabels == nil { + objLabels = make(map[string]string) } for key, value := range labels { - meta.Labels[key] = value + objLabels[key] = value } for _, label := range remove { - delete(meta.Labels, label) + delete(objLabels, label) } + accessor.SetLabels(objLabels) if len(resourceVersion) != 0 { - meta.ResourceVersion = resourceVersion + accessor.SetResourceVersion(resourceVersion) } return nil } @@ -250,9 +253,12 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri if err != nil { return err } - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) + if err != nil { + return err + } for _, label := range remove { - if _, ok := meta.Labels[label]; !ok { + if _, ok := accessor.GetLabels()[label]; !ok { fmt.Fprintf(out, "label %q not found.\n", label) } } diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index 4c674f97c0c92..5eace4f38a4bf 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -28,7 +28,6 @@ import ( "strings" "time" - "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" @@ -473,14 +472,16 @@ func GetRecordFlag(cmd *cobra.Command) bool { // RecordChangeCause annotate change-cause to input runtime object. func RecordChangeCause(obj runtime.Object, changeCause string) error { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return err } - if meta.Annotations == nil { - meta.Annotations = make(map[string]string) + annotations := accessor.GetAnnotations() + if annotations == nil { + annotations = make(map[string]string) } - meta.Annotations[kubectl.ChangeCauseAnnotation] = changeCause + annotations[kubectl.ChangeCauseAnnotation] = changeCause + accessor.SetAnnotations(annotations) return nil } diff --git a/pkg/kubectl/history.go b/pkg/kubectl/history.go index e587e52cace49..09b8e6b9721cb 100644 --- a/pkg/kubectl/history.go +++ b/pkg/kubectl/history.go @@ -23,6 +23,7 @@ import ( "strconv" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/apis/extensions" clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" @@ -128,9 +129,9 @@ func PrintRolloutHistory(historyInfo HistoryInfo, resource, name string) (string // getChangeCause returns the change-cause annotation of the input object func getChangeCause(obj runtime.Object) string { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return "" } - return meta.Annotations[ChangeCauseAnnotation] + return accessor.GetAnnotations()[ChangeCauseAnnotation] } diff --git a/pkg/registry/generic/etcd/etcd.go b/pkg/registry/generic/etcd/etcd.go index 86cf40ed7036e..16114b738f0ab 100644 --- a/pkg/registry/generic/etcd/etcd.go +++ b/pkg/registry/generic/etcd/etcd.go @@ -643,17 +643,17 @@ func (e *Etcd) calculateTTL(obj runtime.Object, defaultTTL int64, update bool) ( return ttl, err } -func exportObjectMeta(objMeta *api.ObjectMeta, exact bool) { - objMeta.UID = "" +func exportObjectMeta(accessor meta.Object, exact bool) { + accessor.SetUID("") if !exact { - objMeta.Namespace = "" + accessor.SetNamespace("") } - objMeta.CreationTimestamp = unversioned.Time{} - objMeta.DeletionTimestamp = nil - objMeta.ResourceVersion = "" - objMeta.SelfLink = "" - if len(objMeta.GenerateName) > 0 && !exact { - objMeta.Name = "" + accessor.SetCreationTimestamp(unversioned.Time{}) + accessor.SetDeletionTimestamp(nil) + accessor.SetResourceVersion("") + accessor.SetSelfLink("") + if len(accessor.GetGenerateName()) > 0 && !exact { + accessor.SetName("") } } @@ -663,8 +663,8 @@ func (e *Etcd) Export(ctx api.Context, name string, opts unversioned.ExportOptio if err != nil { return nil, err } - if meta, err := api.ObjectMetaFor(obj); err == nil { - exportObjectMeta(meta, opts.Exact) + if accessor, err := meta.Accessor(obj); err == nil { + exportObjectMeta(accessor, opts.Exact) } else { glog.V(4).Infof("Object of type %v does not have ObjectMeta: %v", reflect.TypeOf(obj), err) } diff --git a/pkg/registry/registrytest/etcd.go b/pkg/registry/registrytest/etcd.go index 772e6cde822a5..84ae6670796d5 100644 --- a/pkg/registry/registrytest/etcd.go +++ b/pkg/registry/registrytest/etcd.go @@ -22,6 +22,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/rest/resttest" "k8s.io/kubernetes/pkg/api/testapi" "k8s.io/kubernetes/pkg/fields" @@ -168,12 +169,12 @@ func getCodec(obj runtime.Object) (runtime.Codec, error) { // Helper functions func (t *Tester) getObject(ctx api.Context, obj runtime.Object) (runtime.Object, error) { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return nil, err } - result, err := t.storage.Get(ctx, meta.Name) + result, err := t.storage.Get(ctx, accessor.GetName()) if err != nil { return nil, err } @@ -181,11 +182,11 @@ func (t *Tester) getObject(ctx api.Context, obj runtime.Object) (runtime.Object, } func (t *Tester) setObject(ctx api.Context, obj runtime.Object) error { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return err } - key, err := t.storage.KeyFunc(ctx, meta.Name) + key, err := t.storage.KeyFunc(ctx, accessor.GetName()) if err != nil { return err } @@ -209,11 +210,11 @@ func (t *Tester) emitObject(obj runtime.Object, action string) error { case etcdstorage.EtcdCreate: err = t.setObject(ctx, obj) case etcdstorage.EtcdDelete: - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return err } - _, err = t.storage.Delete(ctx, meta.Name, nil) + _, err = t.storage.Delete(ctx, accessor.GetName(), nil) default: err = fmt.Errorf("unexpected action: %v", action) } diff --git a/pkg/storage/etcd/api_object_versioner.go b/pkg/storage/etcd/api_object_versioner.go index 41875fc558717..b7be1720af743 100644 --- a/pkg/storage/etcd/api_object_versioner.go +++ b/pkg/storage/etcd/api_object_versioner.go @@ -21,6 +21,7 @@ import ( "time" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/storage" @@ -32,18 +33,18 @@ type APIObjectVersioner struct{} // UpdateObject implements Versioner func (a APIObjectVersioner) UpdateObject(obj runtime.Object, expiration *time.Time, resourceVersion uint64) error { - objectMeta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return err } if expiration != nil { - objectMeta.DeletionTimestamp = &unversioned.Time{Time: *expiration} + accessor.SetDeletionTimestamp(&unversioned.Time{Time: *expiration}) } versionString := "" if resourceVersion != 0 { versionString = strconv.FormatUint(resourceVersion, 10) } - objectMeta.ResourceVersion = versionString + accessor.SetResourceVersion(versionString) return nil } @@ -63,11 +64,11 @@ func (a APIObjectVersioner) UpdateList(obj runtime.Object, resourceVersion uint6 // ObjectResourceVersion implements Versioner func (a APIObjectVersioner) ObjectResourceVersion(obj runtime.Object) (uint64, error) { - meta, err := api.ObjectMetaFor(obj) + accessor, err := meta.Accessor(obj) if err != nil { return 0, err } - version := meta.ResourceVersion + version := accessor.GetResourceVersion() if len(version) == 0 { return 0, nil } diff --git a/plugin/pkg/admission/resourcequota/admission.go b/plugin/pkg/admission/resourcequota/admission.go index 17fca8bacd623..2ae3c3c232f60 100644 --- a/plugin/pkg/admission/resourcequota/admission.go +++ b/plugin/pkg/admission/resourcequota/admission.go @@ -30,6 +30,7 @@ import ( "k8s.io/kubernetes/pkg/admission" "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" + "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/client/cache" "k8s.io/kubernetes/pkg/quota" "k8s.io/kubernetes/pkg/quota/install" @@ -186,9 +187,9 @@ func (q *quotaAdmission) Admit(a admission.Attributes) (err error) { // the resource represents a number of unique references to external // resource. In such a case an evaluator needs to process other objects in // the same namespace which needs to be known. - if om, err := api.ObjectMetaFor(inputObject); namespace != "" && err == nil { - if om.Namespace == "" { - om.Namespace = namespace + if accessor, err := meta.Accessor(inputObject); namespace != "" && err == nil { + if accessor.GetNamespace() == "" { + accessor.SetNamespace(namespace) } }