github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/ref.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package api
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"net/url"
    23  	"strings"
    24  
    25  	"k8s.io/kubernetes/pkg/api/meta"
    26  	"k8s.io/kubernetes/pkg/runtime"
    27  )
    28  
    29  var (
    30  	// Errors that could be returned by GetReference.
    31  	ErrNilObject  = errors.New("can't reference a nil object")
    32  	ErrNoSelfLink = errors.New("selfLink was empty, can't make reference")
    33  )
    34  
    35  // GetReference returns an ObjectReference which refers to the given
    36  // object, or an error if the object doesn't follow the conventions
    37  // that would allow this.
    38  // TODO: should take a meta.Interface see http://issue.k8s.io/7127
    39  func GetReference(obj runtime.Object) (*ObjectReference, error) {
    40  	if obj == nil {
    41  		return nil, ErrNilObject
    42  	}
    43  	if ref, ok := obj.(*ObjectReference); ok {
    44  		// Don't make a reference to a reference.
    45  		return ref, nil
    46  	}
    47  	meta, err := meta.Accessor(obj)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	// if the object referenced is actually persisted, we can just get kind from meta
    53  	// if we are building an object reference to something not yet persisted, we should fallback to scheme
    54  	kind := meta.Kind()
    55  	if kind == "" {
    56  		_, kind, err = Scheme.ObjectVersionAndKind(obj)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  	}
    61  
    62  	// if the object referenced is actually persisted, we can also get version from meta
    63  	version := meta.APIVersion()
    64  	if version == "" {
    65  		selfLink := meta.SelfLink()
    66  		if selfLink == "" {
    67  			return nil, ErrNoSelfLink
    68  		} else {
    69  			selfLinkUrl, err := url.Parse(selfLink)
    70  			if err != nil {
    71  				return nil, err
    72  			}
    73  			// example paths: /<prefix>/<version>/*
    74  			parts := strings.Split(selfLinkUrl.Path, "/")
    75  			if len(parts) < 3 {
    76  				return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", selfLink, version)
    77  			}
    78  			version = parts[2]
    79  		}
    80  	}
    81  
    82  	return &ObjectReference{
    83  		Kind:            kind,
    84  		APIVersion:      version,
    85  		Name:            meta.Name(),
    86  		Namespace:       meta.Namespace(),
    87  		UID:             meta.UID(),
    88  		ResourceVersion: meta.ResourceVersion(),
    89  	}, nil
    90  }
    91  
    92  // GetPartialReference is exactly like GetReference, but allows you to set the FieldPath.
    93  func GetPartialReference(obj runtime.Object, fieldPath string) (*ObjectReference, error) {
    94  	ref, err := GetReference(obj)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	ref.FieldPath = fieldPath
    99  	return ref, nil
   100  }
   101  
   102  // IsAnAPIObject allows clients to preemptively get a reference to an API object and pass it to places that
   103  // intend only to get a reference to that object. This simplifies the event recording interface.
   104  func (*ObjectReference) IsAnAPIObject() {}