2014-07-26 00:59:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-10-23 02:28:06 +00:00
|
|
|
package meta
|
2014-07-26 00:59:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-10-09 22:42:31 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
2014-10-23 02:28:06 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
2014-07-26 00:59:41 +00:00
|
|
|
)
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
// FindAccessor takes an arbitary api type, returns pointer to its TypeMeta field.
|
2014-10-09 22:42:31 +00:00
|
|
|
// obj must be a pointer to an api type.
|
2014-10-23 02:54:34 +00:00
|
|
|
func FindAccessor(obj runtime.Object) (Accessor, error) {
|
2014-10-09 22:42:31 +00:00
|
|
|
v, err := conversion.EnforcePtr(obj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t := v.Type()
|
|
|
|
name := t.Name()
|
|
|
|
if v.Kind() != reflect.Struct {
|
|
|
|
return nil, fmt.Errorf("expected struct, but got %v: %v (%#v)", v.Kind(), name, v.Interface())
|
|
|
|
}
|
|
|
|
typeMeta := v.FieldByName("TypeMeta")
|
|
|
|
if !typeMeta.IsValid() {
|
2014-10-23 02:54:34 +00:00
|
|
|
return nil, fmt.Errorf("struct %v lacks embedded TypeMeta type", name)
|
2014-10-09 22:42:31 +00:00
|
|
|
}
|
|
|
|
g, err := newGenericTypeMeta(typeMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
// NewResourceVersioner returns a ResourceVersioner that can set or
|
2014-10-07 15:12:16 +00:00
|
|
|
// retrieve ResourceVersion on objects derived from TypeMeta.
|
2014-10-23 02:54:34 +00:00
|
|
|
func NewResourceVersioner() runtime.ResourceVersioner {
|
|
|
|
return typeMetaModifier{}
|
2014-08-06 02:23:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
// typeMetaModifier implements ResourceVersioner and SelfLinker.
|
|
|
|
type typeMetaModifier struct{}
|
2014-08-04 00:23:56 +00:00
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (v typeMetaModifier) ResourceVersion(obj runtime.Object) (string, error) {
|
|
|
|
accessor, err := FindAccessor(obj)
|
2014-08-04 00:23:56 +00:00
|
|
|
if err != nil {
|
2014-10-07 20:51:28 +00:00
|
|
|
return "", err
|
2014-08-04 00:23:56 +00:00
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
return accessor.ResourceVersion(), nil
|
2014-08-04 00:23:56 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (v typeMetaModifier) SetResourceVersion(obj runtime.Object, version string) error {
|
|
|
|
accessor, err := FindAccessor(obj)
|
2014-08-04 00:23:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
accessor.SetResourceVersion(version)
|
2014-08-04 00:23:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (v typeMetaModifier) Name(obj runtime.Object) (string, error) {
|
|
|
|
accessor, err := FindAccessor(obj)
|
2014-09-25 21:57:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
return accessor.Name(), nil
|
2014-09-25 21:57:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (v typeMetaModifier) SelfLink(obj runtime.Object) (string, error) {
|
|
|
|
accessor, err := FindAccessor(obj)
|
2014-09-25 21:57:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
return accessor.SelfLink(), nil
|
2014-09-25 21:57:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (v typeMetaModifier) SetSelfLink(obj runtime.Object, selfLink string) error {
|
|
|
|
accessor, err := FindAccessor(obj)
|
2014-09-25 21:57:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-23 02:54:34 +00:00
|
|
|
accessor.SetSelfLink(selfLink)
|
2014-09-25 21:57:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
// NewSelfLinker returns a SelfLinker that works on all TypeMeta SelfLink fields.
|
|
|
|
func NewSelfLinker() runtime.SelfLinker {
|
|
|
|
return typeMetaModifier{}
|
2014-09-25 21:57:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
// Accessor lets you work with object metadata from any of the versioned or
|
2014-10-23 02:28:06 +00:00
|
|
|
// internal APIruntime.Objects.
|
2014-10-23 02:54:34 +00:00
|
|
|
type Accessor interface {
|
|
|
|
Name() string
|
|
|
|
SetName(name string)
|
2014-10-23 02:59:15 +00:00
|
|
|
UID() string
|
|
|
|
SetUID(uid string)
|
2014-07-26 00:59:41 +00:00
|
|
|
APIVersion() string
|
|
|
|
SetAPIVersion(version string)
|
|
|
|
Kind() string
|
|
|
|
SetKind(kind string)
|
2014-10-07 20:51:28 +00:00
|
|
|
ResourceVersion() string
|
|
|
|
SetResourceVersion(version string)
|
2014-09-25 21:57:41 +00:00
|
|
|
SelfLink() string
|
|
|
|
SetSelfLink(selfLink string)
|
2014-07-26 00:59:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
type genericTypeMeta struct {
|
2014-10-23 02:54:34 +00:00
|
|
|
name *string
|
2014-10-23 02:59:15 +00:00
|
|
|
uid *string
|
2014-07-26 00:59:41 +00:00
|
|
|
apiVersion *string
|
|
|
|
kind *string
|
2014-10-07 20:51:28 +00:00
|
|
|
resourceVersion *string
|
2014-09-25 21:57:41 +00:00
|
|
|
selfLink *string
|
2014-07-26 00:59:41 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (g genericTypeMeta) Name() string {
|
|
|
|
return *g.name
|
2014-08-03 07:01:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:54:34 +00:00
|
|
|
func (g genericTypeMeta) SetName(name string) {
|
|
|
|
*g.name = name
|
2014-08-03 07:01:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 02:59:15 +00:00
|
|
|
func (g genericTypeMeta) UID() string {
|
|
|
|
return *g.uid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g genericTypeMeta) SetUID(uid string) {
|
|
|
|
*g.uid = uid
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
func (g genericTypeMeta) APIVersion() string {
|
2014-07-26 00:59:41 +00:00
|
|
|
return *g.apiVersion
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
func (g genericTypeMeta) SetAPIVersion(version string) {
|
2014-07-26 00:59:41 +00:00
|
|
|
*g.apiVersion = version
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
func (g genericTypeMeta) Kind() string {
|
2014-07-26 00:59:41 +00:00
|
|
|
return *g.kind
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
func (g genericTypeMeta) SetKind(kind string) {
|
2014-07-26 00:59:41 +00:00
|
|
|
*g.kind = kind
|
|
|
|
}
|
|
|
|
|
2014-10-07 20:51:28 +00:00
|
|
|
func (g genericTypeMeta) ResourceVersion() string {
|
2014-07-26 00:59:41 +00:00
|
|
|
return *g.resourceVersion
|
|
|
|
}
|
|
|
|
|
2014-10-07 20:51:28 +00:00
|
|
|
func (g genericTypeMeta) SetResourceVersion(version string) {
|
2014-07-26 00:59:41 +00:00
|
|
|
*g.resourceVersion = version
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
func (g genericTypeMeta) SelfLink() string {
|
2014-09-25 21:57:41 +00:00
|
|
|
return *g.selfLink
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
func (g genericTypeMeta) SetSelfLink(selfLink string) {
|
2014-09-25 21:57:41 +00:00
|
|
|
*g.selfLink = selfLink
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// fieldPtr puts the address of fieldName, which must be a member of v,
|
|
|
|
// into dest, which must be an address of a variable to which this field's
|
|
|
|
// address can be assigned.
|
2014-07-26 00:59:41 +00:00
|
|
|
func fieldPtr(v reflect.Value, fieldName string, dest interface{}) error {
|
|
|
|
field := v.FieldByName(fieldName)
|
|
|
|
if !field.IsValid() {
|
|
|
|
return fmt.Errorf("Couldn't find %v field in %#v", fieldName, v.Interface())
|
|
|
|
}
|
|
|
|
v = reflect.ValueOf(dest)
|
|
|
|
if v.Kind() != reflect.Ptr {
|
|
|
|
return fmt.Errorf("dest should be ptr")
|
|
|
|
}
|
|
|
|
v = v.Elem()
|
|
|
|
field = field.Addr()
|
|
|
|
if field.Type().AssignableTo(v.Type()) {
|
|
|
|
v.Set(field)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if field.Type().ConvertibleTo(v.Type()) {
|
|
|
|
v.Set(field.Convert(v.Type()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Couldn't assign/convert %v to %v", field.Type(), v.Type())
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:12:16 +00:00
|
|
|
// newGenericTypeMeta creates a new generic TypeMeta from v, which must be an
|
|
|
|
// addressable/setable reflect.Value having the same fields as api.TypeMeta.
|
2014-07-26 00:59:41 +00:00
|
|
|
// Returns an error if this isn't the case.
|
2014-10-07 15:12:16 +00:00
|
|
|
func newGenericTypeMeta(v reflect.Value) (genericTypeMeta, error) {
|
|
|
|
g := genericTypeMeta{}
|
2014-10-23 02:54:34 +00:00
|
|
|
if err := fieldPtr(v, "Name", &g.name); err != nil {
|
2014-08-03 07:01:01 +00:00
|
|
|
return g, err
|
|
|
|
}
|
2014-10-23 02:59:15 +00:00
|
|
|
if err := fieldPtr(v, "UID", &g.uid); err != nil {
|
|
|
|
return g, err
|
|
|
|
}
|
2014-08-03 22:36:36 +00:00
|
|
|
if err := fieldPtr(v, "APIVersion", &g.apiVersion); err != nil {
|
2014-07-26 00:59:41 +00:00
|
|
|
return g, err
|
|
|
|
}
|
2014-08-03 22:36:36 +00:00
|
|
|
if err := fieldPtr(v, "Kind", &g.kind); err != nil {
|
2014-07-26 00:59:41 +00:00
|
|
|
return g, err
|
|
|
|
}
|
2014-08-03 22:36:36 +00:00
|
|
|
if err := fieldPtr(v, "ResourceVersion", &g.resourceVersion); err != nil {
|
2014-07-26 00:59:41 +00:00
|
|
|
return g, err
|
|
|
|
}
|
2014-09-25 21:57:41 +00:00
|
|
|
if err := fieldPtr(v, "SelfLink", &g.selfLink); err != nil {
|
|
|
|
return g, err
|
|
|
|
}
|
2014-07-26 00:59:41 +00:00
|
|
|
return g, nil
|
|
|
|
}
|