github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/pods.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     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
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    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.
    15  */
    16  
    17  package unversioned
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/api"
    21  	"k8s.io/kubernetes/pkg/fields"
    22  	"k8s.io/kubernetes/pkg/labels"
    23  	"k8s.io/kubernetes/pkg/watch"
    24  )
    25  
    26  // PodsNamespacer has methods to work with Pod resources in a namespace
    27  type PodsNamespacer interface {
    28  	Pods(namespace string) PodInterface
    29  }
    30  
    31  // PodInterface has methods to work with Pod resources.
    32  type PodInterface interface {
    33  	List(label labels.Selector, field fields.Selector) (*api.PodList, error)
    34  	Get(name string) (*api.Pod, error)
    35  	Delete(name string, options *api.DeleteOptions) error
    36  	Create(pod *api.Pod) (*api.Pod, error)
    37  	Update(pod *api.Pod) (*api.Pod, error)
    38  	Watch(label labels.Selector, field fields.Selector, opts api.ListOptions) (watch.Interface, error)
    39  	Bind(binding *api.Binding) error
    40  	UpdateStatus(pod *api.Pod) (*api.Pod, error)
    41  	GetLogs(name string, opts *api.PodLogOptions) *Request
    42  }
    43  
    44  // pods implements PodsNamespacer interface
    45  type pods struct {
    46  	r  *Client
    47  	ns string
    48  }
    49  
    50  // newPods returns a pods
    51  func newPods(c *Client, namespace string) *pods {
    52  	return &pods{
    53  		r:  c,
    54  		ns: namespace,
    55  	}
    56  }
    57  
    58  // List takes label and field selectors, and returns the list of pods that match those selectors.
    59  func (c *pods) List(label labels.Selector, field fields.Selector) (result *api.PodList, err error) {
    60  	result = &api.PodList{}
    61  	err = c.r.Get().Namespace(c.ns).Resource("pods").LabelsSelectorParam(label).FieldsSelectorParam(field).Do().Into(result)
    62  	return
    63  }
    64  
    65  // Get takes the name of the pod, and returns the corresponding Pod object, and an error if it occurs
    66  func (c *pods) Get(name string) (result *api.Pod, err error) {
    67  	result = &api.Pod{}
    68  	err = c.r.Get().Namespace(c.ns).Resource("pods").Name(name).Do().Into(result)
    69  	return
    70  }
    71  
    72  // Delete takes the name of the pod, and returns an error if one occurs
    73  func (c *pods) Delete(name string, options *api.DeleteOptions) error {
    74  	// TODO: to make this reusable in other client libraries
    75  	if options == nil {
    76  		return c.r.Delete().Namespace(c.ns).Resource("pods").Name(name).Do().Error()
    77  	}
    78  	body, err := api.Scheme.EncodeToVersion(options, c.r.APIVersion())
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return c.r.Delete().Namespace(c.ns).Resource("pods").Name(name).Body(body).Do().Error()
    83  }
    84  
    85  // Create takes the representation of a pod.  Returns the server's representation of the pod, and an error, if it occurs.
    86  func (c *pods) Create(pod *api.Pod) (result *api.Pod, err error) {
    87  	result = &api.Pod{}
    88  	err = c.r.Post().Namespace(c.ns).Resource("pods").Body(pod).Do().Into(result)
    89  	return
    90  }
    91  
    92  // Update takes the representation of a pod to update.  Returns the server's representation of the pod, and an error, if it occurs.
    93  func (c *pods) Update(pod *api.Pod) (result *api.Pod, err error) {
    94  	result = &api.Pod{}
    95  	err = c.r.Put().Namespace(c.ns).Resource("pods").Name(pod.Name).Body(pod).Do().Into(result)
    96  	return
    97  }
    98  
    99  // Watch returns a watch.Interface that watches the requested pods.
   100  func (c *pods) Watch(label labels.Selector, field fields.Selector, opts api.ListOptions) (watch.Interface, error) {
   101  	return c.r.Get().
   102  		Prefix("watch").
   103  		Namespace(c.ns).
   104  		Resource("pods").
   105  		VersionedParams(&opts, api.Scheme).
   106  		LabelsSelectorParam(label).
   107  		FieldsSelectorParam(field).
   108  		Watch()
   109  }
   110  
   111  // Bind applies the provided binding to the named pod in the current namespace (binding.Namespace is ignored).
   112  func (c *pods) Bind(binding *api.Binding) error {
   113  	return c.r.Post().Namespace(c.ns).Resource("pods").Name(binding.Name).SubResource("binding").Body(binding).Do().Error()
   114  }
   115  
   116  // 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.
   117  func (c *pods) UpdateStatus(pod *api.Pod) (result *api.Pod, err error) {
   118  	result = &api.Pod{}
   119  	err = c.r.Put().Namespace(c.ns).Resource("pods").Name(pod.Name).SubResource("status").Body(pod).Do().Into(result)
   120  	return
   121  }
   122  
   123  // Get constructs a request for getting the logs for a pod
   124  func (c *pods) GetLogs(name string, opts *api.PodLogOptions) *Request {
   125  	return c.r.Get().Namespace(c.ns).Name(name).Resource("pods").SubResource("log").VersionedParams(opts, api.Scheme)
   126  }