func NoNamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) { meta, err := meta.Accessor(obj) if err != nil { return "", err } name := meta.Name() if ok, msg := validation.IsValidPathSegmentName(name); !ok { return "", fmt.Errorf("invalid name: %v", msg) } return prefix + "/" + meta.Name(), nil }
// MetaNamespaceKeyFunc is a convenient default KeyFunc which knows how to make // keys for API objects which implement meta.Interface. // The key uses the format <namespace>/<name> unless <namespace> is empty, then // it's just <name>. // // TODO: replace key-as-string with a key-as-struct so that this // packing/unpacking won't be necessary. func MetaNamespaceKeyFunc(obj interface{}) (string, error) { if key, ok := obj.(ExplicitKey); ok { return string(key), nil } meta, err := meta.Accessor(obj) if err != nil { return "", fmt.Errorf("object has no meta: %v", err) } if len(meta.Namespace()) > 0 { return meta.Namespace() + "/" + meta.Name(), nil } return meta.Name(), nil }
// nameIndexFunc is an index function that indexes based on an object's name func nameIndexFunc(obj interface{}) ([]string, error) { meta, err := meta.Accessor(obj) if err != nil { return []string{""}, fmt.Errorf("object has no meta: %v", err) } return []string{meta.Name()}, nil }
func NamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) { meta, err := meta.Accessor(obj) if err != nil { return "", err } return prefix + "/" + meta.Namespace() + "/" + meta.Name(), nil }
// GetReference returns an ObjectReference which refers to the given // object, or an error if the object doesn't follow the conventions // that would allow this. // TODO: should take a meta.Interface see https://github.com/GoogleCloudPlatform/kubernetes/issues/7127 func GetReference(obj runtime.Object) (*ObjectReference, error) { if obj == nil { return nil, ErrNilObject } if ref, ok := obj.(*ObjectReference); ok { // Don't make a reference to a reference. return ref, nil } meta, err := meta.Accessor(obj) if err != nil { return nil, err } // if the object referenced is actually persisted, we can just get kind from meta // if we are building an object reference to something not yet persisted, we should fallback to scheme kind := meta.Kind() if kind == "" { _, kind, err = Scheme.ObjectVersionAndKind(obj) if err != nil { return nil, err } } // if the object referenced is actually persisted, we can also get version from meta version := meta.APIVersion() if version == "" { selfLink := meta.SelfLink() if selfLink == "" { if ForTesting_ReferencesAllowBlankSelfLinks { version = "testing" } else { return nil, ErrNoSelfLink } } else { selfLinkUrl, err := url.Parse(selfLink) if err != nil { return nil, err } // example paths: /<prefix>/<version>/* parts := strings.Split(selfLinkUrl.Path, "/") if len(parts) < 3 { return nil, fmt.Errorf("unexpected self link format: '%v'; got version '%v'", selfLink, version) } version = parts[2] } } return &ObjectReference{ Kind: kind, APIVersion: version, Name: meta.Name(), Namespace: meta.Namespace(), UID: meta.UID(), ResourceVersion: meta.ResourceVersion(), }, nil }
//CreateObjList will create a list from the array of objects func CreateObjList(prefix string, helper storage.Interface, items []runtime.Object) error { for i := range items { obj := items[i] meta, err := meta.Accessor(obj) if err != nil { return err } err = CreateObj(helper, path.Join(prefix, meta.Name()), obj, obj, 0) if err != nil { return err } items[i] = obj } return nil }
// CreateList will properly create a list using the storage interface func CreateList(t *testing.T, prefix string, helper storage.Interface, list runtime.Object) error { items, err := runtime.ExtractList(list) if err != nil { return err } for i := range items { obj := items[i] meta, err := meta.Accessor(obj) if err != nil { return err } err = CreateObj(t, helper, path.Join(prefix, meta.Name()), obj, obj, 0) if err != nil { return err } items[i] = obj } return runtime.SetList(list, items) }