Skip to content

Commit

Permalink
Begin to unify ResourceVersioner and SelfLinker
Browse files Browse the repository at this point in the history
Create a new MetadataAccessor interface that combines both
and use it where previously latest.ResourceVersioner and SelfLinker
were being used.

Adds Namespace to the get/set interface. Adds TODO about future
fast path for metadata (as per thockin's comment)
  • Loading branch information
smarterclayton committed Oct 27, 2014
1 parent 52b5822 commit 66ace4c
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 38 deletions.
20 changes: 10 additions & 10 deletions pkg/api/latest/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,24 @@ var Versions = []string{"v1beta1", "v1beta2"}
// This codec can decode any object that Kubernetes is aware of.
var Codec = v1beta1.Codec

// accessor is the shared static metadata accessor for the API.
var accessor = meta.NewAccessor()

// ResourceVersioner describes a default versioner that can handle all types
// of versioning.
// TODO: when versioning changes, make this part of each API definition.
var ResourceVersioner = meta.NewResourceVersioner()
var ResourceVersioner runtime.ResourceVersioner = accessor

// SelfLinker can set or get the SelfLink field of all API types.
// TODO: when versioning changes, make this part of each API definition.
// TODO(lavalamp): Combine SelfLinker & ResourceVersioner interfaces, force all uses
// to go through the InterfacesFor method below.
var SelfLinker = meta.NewSelfLinker()
var SelfLinker runtime.SelfLinker = accessor

// VersionInterfaces contains the interfaces one should use for dealing with types of a particular version.
type VersionInterfaces struct {
runtime.Codec
runtime.ResourceVersioner
runtime.SelfLinker
meta.MetadataAccessor
}

// InterfacesFor returns the default Codec and ResourceVersioner for a given version
Expand All @@ -69,15 +71,13 @@ func InterfacesFor(version string) (*VersionInterfaces, error) {
switch version {
case "v1beta1":
return &VersionInterfaces{
Codec: v1beta1.Codec,
ResourceVersioner: ResourceVersioner,
SelfLinker: SelfLinker,
Codec: v1beta1.Codec,
MetadataAccessor: accessor,
}, nil
case "v1beta2":
return &VersionInterfaces{
Codec: v1beta2.Codec,
ResourceVersioner: ResourceVersioner,
SelfLinker: SelfLinker,
Codec: v1beta2.Codec,
MetadataAccessor: accessor,
}, nil
default:
return nil, fmt.Errorf("unsupported storage version: %s (valid: %s)", version, strings.Join(Versions, ", "))
Expand Down
158 changes: 139 additions & 19 deletions pkg/api/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
)

// Interface lets you work with object metadata from any of the versioned or
// internal API objects.
// Interface lets you work with object and list metadata from any of the versioned or
// internal API objects. Attempting to set or retrieve a field on an object that does
// not support that field (Name, UID, Namespace on lists) will be a no-op and return
// a default value.
type Interface interface {
Namespace() string
SetNamespace(namespace string)
Name() string
SetName(name string)
UID() string
Expand All @@ -45,6 +49,7 @@ type Interface interface {
// obj must be a pointer to an API type. An error is returned if the minimum
// required fields are missing. Fields that are not required return the default
// value and are a no-op if set.
// TODO: add a fast path for *TypeMeta and *ObjectMeta for internal objects
func Accessor(obj interface{}) (Interface, error) {
v, err := conversion.EnforcePtr(obj)
if err != nil {
Expand All @@ -62,76 +67,161 @@ func Accessor(obj interface{}) (Interface, error) {

a := &genericAccessor{}
if err := extractFromTypeMeta(typeMeta, a); err != nil {
return nil, fmt.Errorf("unable to find type fields on %#v", typeMeta)
return nil, fmt.Errorf("unable to find type fields on %#v: %v", typeMeta, err)
}

objectMeta := v.FieldByName("ObjectMeta")
if objectMeta.IsValid() {
// look for the ObjectMeta fields
if err := extractFromObjectMeta(objectMeta, a); err != nil {
return nil, fmt.Errorf("unable to find object fields on %#v", objectMeta)
return nil, fmt.Errorf("unable to find object fields on %#v: %v", objectMeta, err)
}
} else {
listMeta := v.FieldByName("ListMeta")
if listMeta.IsValid() {
// look for the ListMeta fields
if err := extractFromListMeta(listMeta, a); err != nil {
return nil, fmt.Errorf("unable to find list fields on %#v", listMeta)
return nil, fmt.Errorf("unable to find list fields on %#v: %v", listMeta, err)
}
} else {
// look for the older TypeMeta with all metadata
if err := extractFromObjectMeta(typeMeta, a); err != nil {
return nil, fmt.Errorf("unable to find object fields on %#v", typeMeta)
return nil, fmt.Errorf("unable to find object fields on %#v: %v", typeMeta, err)
}
}
}

return a, nil
}

// NewResourceVersioner returns a ResourceVersioner that can set or
// retrieve ResourceVersion on objects derived from TypeMeta.
func NewResourceVersioner() runtime.ResourceVersioner {
// MetadataAccessor lets you work with object metadata from any of the versioned or
// internal API objects.
type MetadataAccessor interface {
APIVersion(obj runtime.Object) (string, error)
SetAPIVersion(obj runtime.Object, version string) error

Kind(obj runtime.Object) (string, error)
SetKind(obj runtime.Object, kind string) error

Namespace(obj runtime.Object) (string, error)
SetNamespace(obj runtime.Object, namespace string) error

Name(obj runtime.Object) (string, error)
SetName(obj runtime.Object, name string) error

UID(obj runtime.Object) (string, error)
SetUID(obj runtime.Object, uid string) error

SelfLink(obj runtime.Object) (string, error)
SetSelfLink(obj runtime.Object, selfLink string) error

runtime.ResourceVersioner
}

// NewAccessor returns a MetadataAccessor that can retrieve
// or manipulate resource version on objects derived from core API
// metadata concepts.
func NewAccessor() MetadataAccessor {
return resourceAccessor{}
}

// resourceAccessor implements ResourceVersioner and SelfLinker.
type resourceAccessor struct{}

func (v resourceAccessor) ResourceVersion(obj runtime.Object) (string, error) {
func (resourceAccessor) Kind(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.ResourceVersion(), nil
return accessor.Kind(), nil
}

func (v resourceAccessor) SetResourceVersion(obj runtime.Object, version string) error {
func (resourceAccessor) SetKind(obj runtime.Object, kind string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
}
accessor.SetResourceVersion(version)
accessor.SetKind(kind)
return nil
}

func (v resourceAccessor) Name(obj runtime.Object) (string, error) {
func (resourceAccessor) APIVersion(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.APIVersion(), nil
}

func (resourceAccessor) SetAPIVersion(obj runtime.Object, version string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
}
accessor.SetAPIVersion(version)
return nil
}

func (resourceAccessor) Namespace(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.Namespace(), nil
}

func (resourceAccessor) SetNamespace(obj runtime.Object, namespace string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
}
accessor.SetNamespace(namespace)
return nil
}

func (resourceAccessor) Name(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.Name(), nil
}

func (v resourceAccessor) SelfLink(obj runtime.Object) (string, error) {
func (resourceAccessor) SetName(obj runtime.Object, name string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
}
accessor.SetName(name)
return nil
}

func (resourceAccessor) UID(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.UID(), nil
}

func (resourceAccessor) SetUID(obj runtime.Object, uid string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
}
accessor.SetUID(uid)
return nil
}

func (resourceAccessor) SelfLink(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.SelfLink(), nil
}

func (v resourceAccessor) SetSelfLink(obj runtime.Object, selfLink string) error {
func (resourceAccessor) SetSelfLink(obj runtime.Object, selfLink string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
Expand All @@ -140,14 +230,27 @@ func (v resourceAccessor) SetSelfLink(obj runtime.Object, selfLink string) error
return nil
}

// NewSelfLinker returns a SelfLinker that works on all TypeMeta SelfLink fields.
func NewSelfLinker() runtime.SelfLinker {
return resourceAccessor{}
func (resourceAccessor) ResourceVersion(obj runtime.Object) (string, error) {
accessor, err := Accessor(obj)
if err != nil {
return "", err
}
return accessor.ResourceVersion(), nil
}

func (resourceAccessor) SetResourceVersion(obj runtime.Object, version string) error {
accessor, err := Accessor(obj)
if err != nil {
return err
}
accessor.SetResourceVersion(version)
return nil
}

// genericAccessor contains pointers to strings that can modify an arbitrary
// struct and implements the Accessor interface.
type genericAccessor struct {
namespace *string
name *string
uid *string
apiVersion *string
Expand All @@ -156,6 +259,20 @@ type genericAccessor struct {
selfLink *string
}

func (a genericAccessor) Namespace() string {
if a.namespace == nil {
return ""
}
return *a.namespace
}

func (a genericAccessor) SetNamespace(namespace string) {
if a.namespace == nil {
return
}
*a.namespace = namespace
}

func (a genericAccessor) Name() string {
if a.name == nil {
return ""
Expand Down Expand Up @@ -254,6 +371,9 @@ func extractFromTypeMeta(v reflect.Value, a *genericAccessor) error {

// extractFromObjectMeta extracts pointers to metadata fields from an object
func extractFromObjectMeta(v reflect.Value, a *genericAccessor) error {
if err := fieldPtr(v, "Namespace", &a.namespace); err != nil {
return err
}
if err := fieldPtr(v, "Name", &a.name); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 66ace4c

Please sign in to comment.