pods.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "k8s.io/kubernetes/pkg/api"
  16. "k8s.io/kubernetes/pkg/client/restclient"
  17. "k8s.io/kubernetes/pkg/watch"
  18. )
  19. // PodsNamespacer has methods to work with Pod resources in a namespace
  20. type PodsNamespacer interface {
  21. Pods(namespace string) PodInterface
  22. }
  23. // PodInterface has methods to work with Pod resources.
  24. type PodInterface interface {
  25. List(opts api.ListOptions) (*api.PodList, error)
  26. Get(name string) (*api.Pod, error)
  27. Delete(name string, options *api.DeleteOptions) error
  28. Create(pod *api.Pod) (*api.Pod, error)
  29. Update(pod *api.Pod) (*api.Pod, error)
  30. Watch(opts api.ListOptions) (watch.Interface, error)
  31. Bind(binding *api.Binding) error
  32. UpdateStatus(pod *api.Pod) (*api.Pod, error)
  33. GetLogs(name string, opts *api.PodLogOptions) *restclient.Request
  34. }
  35. // pods implements PodsNamespacer interface
  36. type pods struct {
  37. r *Client
  38. ns string
  39. }
  40. // newPods returns a pods
  41. func newPods(c *Client, namespace string) *pods {
  42. return &pods{
  43. r: c,
  44. ns: namespace,
  45. }
  46. }
  47. // List takes label and field selectors, and returns the list of pods that match those selectors.
  48. func (c *pods) List(opts api.ListOptions) (result *api.PodList, err error) {
  49. result = &api.PodList{}
  50. err = c.r.Get().Namespace(c.ns).Resource("pods").VersionedParams(&opts, api.ParameterCodec).Do().Into(result)
  51. return
  52. }
  53. // Get takes the name of the pod, and returns the corresponding Pod object, and an error if it occurs
  54. func (c *pods) Get(name string) (result *api.Pod, err error) {
  55. result = &api.Pod{}
  56. err = c.r.Get().Namespace(c.ns).Resource("pods").Name(name).Do().Into(result)
  57. return
  58. }
  59. // Delete takes the name of the pod, and returns an error if one occurs
  60. func (c *pods) Delete(name string, options *api.DeleteOptions) error {
  61. return c.r.Delete().Namespace(c.ns).Resource("pods").Name(name).Body(options).Do().Error()
  62. }
  63. // Create takes the representation of a pod. Returns the server's representation of the pod, and an error, if it occurs.
  64. func (c *pods) Create(pod *api.Pod) (result *api.Pod, err error) {
  65. result = &api.Pod{}
  66. err = c.r.Post().Namespace(c.ns).Resource("pods").Body(pod).Do().Into(result)
  67. return
  68. }
  69. // Update takes the representation of a pod to update. Returns the server's representation of the pod, and an error, if it occurs.
  70. func (c *pods) Update(pod *api.Pod) (result *api.Pod, err error) {
  71. result = &api.Pod{}
  72. err = c.r.Put().Namespace(c.ns).Resource("pods").Name(pod.Name).Body(pod).Do().Into(result)
  73. return
  74. }
  75. // Watch returns a watch.Interface that watches the requested pods.
  76. func (c *pods) Watch(opts api.ListOptions) (watch.Interface, error) {
  77. return c.r.Get().
  78. Prefix("watch").
  79. Namespace(c.ns).
  80. Resource("pods").
  81. VersionedParams(&opts, api.ParameterCodec).
  82. Watch()
  83. }
  84. // Bind applies the provided binding to the named pod in the current namespace (binding.Namespace is ignored).
  85. func (c *pods) Bind(binding *api.Binding) error {
  86. return c.r.Post().Namespace(c.ns).Resource("pods").Name(binding.Name).SubResource("binding").Body(binding).Do().Error()
  87. }
  88. // UpdateStatus takes the name of the pod and the new status. Returns the server's representation of the pod, and an error, if it occurs.
  89. func (c *pods) UpdateStatus(pod *api.Pod) (result *api.Pod, err error) {
  90. result = &api.Pod{}
  91. err = c.r.Put().Namespace(c.ns).Resource("pods").Name(pod.Name).SubResource("status").Body(pod).Do().Into(result)
  92. return
  93. }
  94. // Get constructs a request for getting the logs for a pod
  95. func (c *pods) GetLogs(name string, opts *api.PodLogOptions) *restclient.Request {
  96. return c.r.Get().Namespace(c.ns).Name(name).Resource("pods").SubResource("log").VersionedParams(opts, api.ParameterCodec)
  97. }