namespaces.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2014 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 unversioned
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/watch"
  18. )
  19. type NamespacesInterface interface {
  20. Namespaces() NamespaceInterface
  21. }
  22. type NamespaceInterface interface {
  23. Create(item *api.Namespace) (*api.Namespace, error)
  24. Get(name string) (result *api.Namespace, err error)
  25. List(opts api.ListOptions) (*api.NamespaceList, error)
  26. Delete(name string) error
  27. Update(item *api.Namespace) (*api.Namespace, error)
  28. Watch(opts api.ListOptions) (watch.Interface, error)
  29. Finalize(item *api.Namespace) (*api.Namespace, error)
  30. Status(item *api.Namespace) (*api.Namespace, error)
  31. }
  32. // namespaces implements NamespacesInterface
  33. type namespaces struct {
  34. r *Client
  35. }
  36. // newNamespaces returns a namespaces object.
  37. func newNamespaces(c *Client) *namespaces {
  38. return &namespaces{r: c}
  39. }
  40. // Create creates a new namespace.
  41. func (c *namespaces) Create(namespace *api.Namespace) (*api.Namespace, error) {
  42. result := &api.Namespace{}
  43. err := c.r.Post().Resource("namespaces").Body(namespace).Do().Into(result)
  44. return result, err
  45. }
  46. // List lists all the namespaces in the cluster.
  47. func (c *namespaces) List(opts api.ListOptions) (*api.NamespaceList, error) {
  48. result := &api.NamespaceList{}
  49. err := c.r.Get().
  50. Resource("namespaces").
  51. VersionedParams(&opts, api.ParameterCodec).
  52. Do().Into(result)
  53. return result, err
  54. }
  55. // Update takes the representation of a namespace to update. Returns the server's representation of the namespace, and an error, if it occurs.
  56. func (c *namespaces) Update(namespace *api.Namespace) (result *api.Namespace, err error) {
  57. result = &api.Namespace{}
  58. err = c.r.Put().Resource("namespaces").Name(namespace.Name).Body(namespace).Do().Into(result)
  59. return
  60. }
  61. // Finalize takes the representation of a namespace to update. Returns the server's representation of the namespace, and an error, if it occurs.
  62. func (c *namespaces) Finalize(namespace *api.Namespace) (result *api.Namespace, err error) {
  63. result = &api.Namespace{}
  64. if len(namespace.ResourceVersion) == 0 {
  65. err = fmt.Errorf("invalid update object, missing resource version: %v", namespace)
  66. return
  67. }
  68. err = c.r.Put().Resource("namespaces").Name(namespace.Name).SubResource("finalize").Body(namespace).Do().Into(result)
  69. return
  70. }
  71. // Status takes the representation of a namespace to update. Returns the server's representation of the namespace, and an error, if it occurs.
  72. func (c *namespaces) Status(namespace *api.Namespace) (result *api.Namespace, err error) {
  73. result = &api.Namespace{}
  74. if len(namespace.ResourceVersion) == 0 {
  75. err = fmt.Errorf("invalid update object, missing resource version: %v", namespace)
  76. return
  77. }
  78. err = c.r.Put().Resource("namespaces").Name(namespace.Name).SubResource("status").Body(namespace).Do().Into(result)
  79. return
  80. }
  81. // Get gets an existing namespace
  82. func (c *namespaces) Get(name string) (*api.Namespace, error) {
  83. result := &api.Namespace{}
  84. err := c.r.Get().Resource("namespaces").Name(name).Do().Into(result)
  85. return result, err
  86. }
  87. // Delete deletes an existing namespace.
  88. func (c *namespaces) Delete(name string) error {
  89. return c.r.Delete().Resource("namespaces").Name(name).Do().Error()
  90. }
  91. // Watch returns a watch.Interface that watches the requested namespaces.
  92. func (c *namespaces) Watch(opts api.ListOptions) (watch.Interface, error) {
  93. return c.r.Get().
  94. Prefix("watch").
  95. Resource("namespaces").
  96. VersionedParams(&opts, api.ParameterCodec).
  97. Watch()
  98. }