github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/services.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/util"
    24  	"k8s.io/kubernetes/pkg/watch"
    25  )
    26  
    27  // ServicesNamespacer has methods to work with Service resources in a namespace
    28  type ServicesNamespacer interface {
    29  	Services(namespace string) ServiceInterface
    30  }
    31  
    32  // ServiceInterface has methods to work with Service resources.
    33  type ServiceInterface interface {
    34  	List(label labels.Selector, field fields.Selector) (*api.ServiceList, error)
    35  	Get(name string) (*api.Service, error)
    36  	Create(srv *api.Service) (*api.Service, error)
    37  	Update(srv *api.Service) (*api.Service, error)
    38  	Delete(name string) error
    39  	Watch(label labels.Selector, field fields.Selector, opts api.ListOptions) (watch.Interface, error)
    40  	ProxyGet(scheme, name, port, path string, params map[string]string) ResponseWrapper
    41  }
    42  
    43  // services implements ServicesNamespacer interface
    44  type services struct {
    45  	r  *Client
    46  	ns string
    47  }
    48  
    49  // newServices returns a services
    50  func newServices(c *Client, namespace string) *services {
    51  	return &services{c, namespace}
    52  }
    53  
    54  // List takes a selector, and returns the list of services that match that selector
    55  func (c *services) List(label labels.Selector, field fields.Selector) (result *api.ServiceList, err error) {
    56  	result = &api.ServiceList{}
    57  	err = c.r.Get().
    58  		Namespace(c.ns).
    59  		Resource("services").
    60  		LabelsSelectorParam(label).
    61  		FieldsSelectorParam(field).
    62  		Do().
    63  		Into(result)
    64  	return
    65  }
    66  
    67  // Get returns information about a particular service.
    68  func (c *services) Get(name string) (result *api.Service, err error) {
    69  	result = &api.Service{}
    70  	err = c.r.Get().Namespace(c.ns).Resource("services").Name(name).Do().Into(result)
    71  	return
    72  }
    73  
    74  // Create creates a new service.
    75  func (c *services) Create(svc *api.Service) (result *api.Service, err error) {
    76  	result = &api.Service{}
    77  	err = c.r.Post().Namespace(c.ns).Resource("services").Body(svc).Do().Into(result)
    78  	return
    79  }
    80  
    81  // Update updates an existing service.
    82  func (c *services) Update(svc *api.Service) (result *api.Service, err error) {
    83  	result = &api.Service{}
    84  	err = c.r.Put().Namespace(c.ns).Resource("services").Name(svc.Name).Body(svc).Do().Into(result)
    85  	return
    86  }
    87  
    88  // Delete deletes an existing service.
    89  func (c *services) Delete(name string) error {
    90  	return c.r.Delete().Namespace(c.ns).Resource("services").Name(name).Do().Error()
    91  }
    92  
    93  // Watch returns a watch.Interface that watches the requested services.
    94  func (c *services) Watch(label labels.Selector, field fields.Selector, opts api.ListOptions) (watch.Interface, error) {
    95  	return c.r.Get().
    96  		Prefix("watch").
    97  		Namespace(c.ns).
    98  		Resource("services").
    99  		VersionedParams(&opts, api.Scheme).
   100  		LabelsSelectorParam(label).
   101  		FieldsSelectorParam(field).
   102  		Watch()
   103  }
   104  
   105  // ProxyGet returns a response of the service by calling it through the proxy.
   106  func (c *services) ProxyGet(scheme, name, port, path string, params map[string]string) ResponseWrapper {
   107  	request := c.r.Get().
   108  		Prefix("proxy").
   109  		Namespace(c.ns).
   110  		Resource("services").
   111  		Name(util.JoinSchemeNamePort(scheme, name, port)).
   112  		Suffix(path)
   113  	for k, v := range params {
   114  		request = request.Param(k, v)
   115  	}
   116  	return request
   117  }