2 Copyright 2014 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
26 "k8s.io/apimachinery/pkg/api/meta"
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 "k8s.io/apimachinery/pkg/runtime"
32 // Errors that could be returned by GetReference.
33 ErrNilObject = errors.New("can't reference a nil object")
34 ErrNoSelfLink = errors.New("selfLink was empty, can't make reference")
37 // GetReference returns an ObjectReference which refers to the given
38 // object, or an error if the object doesn't follow the conventions
39 // that would allow this.
40 // TODO: should take a meta.Interface see http://issue.k8s.io/7127
41 func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*v1.ObjectReference, error) {
43 return nil, ErrNilObject
45 if ref, ok := obj.(*v1.ObjectReference); ok {
46 // Don't make a reference to a reference.
50 gvk := obj.GetObjectKind().GroupVersionKind()
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
56 // TODO: this is wrong
57 gvks, _, err := scheme.ObjectKinds(obj)
64 // An object that implements only List has enough metadata to build a reference
65 var listMeta metav1.Common
66 objectMeta, err := meta.Accessor(obj)
68 listMeta, err = meta.CommonAccessor(obj)
76 // if the object referenced is actually persisted, we can also get version from meta
77 version := gvk.GroupVersion().String()
78 if len(version) == 0 {
79 selfLink := listMeta.GetSelfLink()
80 if len(selfLink) == 0 {
81 return nil, ErrNoSelfLink
83 selfLinkUrl, err := url.Parse(selfLink)
87 // example paths: /<prefix>/<version>/*
88 parts := strings.Split(selfLinkUrl.Path, "/")
90 return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", selfLink, version)
92 if parts[1] == "api" {
95 version = parts[2] + "/" + parts[3]
99 // only has list metadata
100 if objectMeta == nil {
101 return &v1.ObjectReference{
104 ResourceVersion: listMeta.GetResourceVersion(),
108 return &v1.ObjectReference{
111 Name: objectMeta.GetName(),
112 Namespace: objectMeta.GetNamespace(),
113 UID: objectMeta.GetUID(),
114 ResourceVersion: objectMeta.GetResourceVersion(),
118 // GetPartialReference is exactly like GetReference, but allows you to set the FieldPath.
119 func GetPartialReference(scheme *runtime.Scheme, obj runtime.Object, fieldPath string) (*v1.ObjectReference, error) {
120 ref, err := GetReference(scheme, obj)
124 ref.FieldPath = fieldPath