Skip to content

Commit

Permalink
Merge pull request kubernetes#23472 from wojtek-t/fix_object_meta_for
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Switch from api.ObjectMetaFor to meta.Accessor in most of places

Fix kubernetes#23278

@smarterclayton @lavalamp
  • Loading branch information
k8s-merge-robot committed Apr 2, 2016
2 parents c7af842 + 2699be2 commit f5c93c8
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 108 deletions.
12 changes: 6 additions & 6 deletions pkg/admission/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
33 changes: 21 additions & 12 deletions pkg/api/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/meta/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 32 additions & 10 deletions pkg/api/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions pkg/client/testing/core/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 4 additions & 5 deletions pkg/client/unversioned/testclient/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -444,22 +445,22 @@ 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)
}
if err := r.KubeClient.Core().Pods(namespace).Delete(podID, nil); err != nil {
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
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/framework/fake_controller_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 12 additions & 9 deletions pkg/kubectl/cmd/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -310,14 +311,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("; ")
}
Expand All @@ -332,29 +333,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
}
Loading

0 comments on commit f5c93c8

Please sign in to comment.