utils.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testing
  14. import (
  15. "path"
  16. "golang.org/x/net/context"
  17. "k8s.io/kubernetes/pkg/api/meta"
  18. "k8s.io/kubernetes/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/storage"
  20. )
  21. // CreateObj will create a single object using the storage interface
  22. func CreateObj(helper storage.Interface, name string, obj, out runtime.Object, ttl uint64) error {
  23. return helper.Create(context.TODO(), name, obj, out, ttl)
  24. }
  25. //CreateObjList will create a list from the array of objects
  26. func CreateObjList(prefix string, helper storage.Interface, items []runtime.Object) error {
  27. for i := range items {
  28. obj := items[i]
  29. meta, err := meta.Accessor(obj)
  30. if err != nil {
  31. return err
  32. }
  33. err = CreateObj(helper, path.Join(prefix, meta.GetName()), obj, obj, 0)
  34. if err != nil {
  35. return err
  36. }
  37. items[i] = obj
  38. }
  39. return nil
  40. }
  41. // CreateList will properly create a list using the storage interface
  42. func CreateList(prefix string, helper storage.Interface, list runtime.Object) error {
  43. items, err := meta.ExtractList(list)
  44. if err != nil {
  45. return err
  46. }
  47. err = CreateObjList(prefix, helper, items)
  48. if err != nil {
  49. return err
  50. }
  51. return meta.SetList(list, items)
  52. }