github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/client/unversioned/nodes.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 "fmt" 21 22 "k8s.io/kubernetes/pkg/api" 23 "k8s.io/kubernetes/pkg/fields" 24 "k8s.io/kubernetes/pkg/labels" 25 "k8s.io/kubernetes/pkg/watch" 26 ) 27 28 type NodesInterface interface { 29 Nodes() NodeInterface 30 } 31 32 type NodeInterface interface { 33 Get(name string) (result *api.Node, err error) 34 Create(node *api.Node) (*api.Node, error) 35 List(label labels.Selector, field fields.Selector) (*api.NodeList, error) 36 Delete(name string) error 37 Update(*api.Node) (*api.Node, error) 38 UpdateStatus(*api.Node) (*api.Node, error) 39 Watch(label labels.Selector, field fields.Selector, opts api.ListOptions) (watch.Interface, error) 40 } 41 42 // nodes implements NodesInterface 43 type nodes struct { 44 r *Client 45 } 46 47 // newNodes returns a nodes object. 48 func newNodes(c *Client) *nodes { 49 return &nodes{c} 50 } 51 52 // resourceName returns node's URL resource name. 53 func (c *nodes) resourceName() string { 54 return "nodes" 55 } 56 57 // Create creates a new node. 58 func (c *nodes) Create(node *api.Node) (*api.Node, error) { 59 result := &api.Node{} 60 err := c.r.Post().Resource(c.resourceName()).Body(node).Do().Into(result) 61 return result, err 62 } 63 64 // List takes a selector, and returns the list of nodes that match that selector in the cluster. 65 func (c *nodes) List(label labels.Selector, field fields.Selector) (*api.NodeList, error) { 66 result := &api.NodeList{} 67 err := c.r.Get().Resource(c.resourceName()).LabelsSelectorParam(label).FieldsSelectorParam(field).Do().Into(result) 68 return result, err 69 } 70 71 // Get gets an existing node. 72 func (c *nodes) Get(name string) (*api.Node, error) { 73 result := &api.Node{} 74 err := c.r.Get().Resource(c.resourceName()).Name(name).Do().Into(result) 75 return result, err 76 } 77 78 // Delete deletes an existing node. 79 func (c *nodes) Delete(name string) error { 80 return c.r.Delete().Resource(c.resourceName()).Name(name).Do().Error() 81 } 82 83 // Update updates an existing node. 84 func (c *nodes) Update(node *api.Node) (*api.Node, error) { 85 result := &api.Node{} 86 if len(node.ResourceVersion) == 0 { 87 err := fmt.Errorf("invalid update object, missing resource version: %v", node) 88 return nil, err 89 } 90 err := c.r.Put().Resource(c.resourceName()).Name(node.Name).Body(node).Do().Into(result) 91 return result, err 92 } 93 94 func (c *nodes) UpdateStatus(node *api.Node) (*api.Node, error) { 95 result := &api.Node{} 96 if len(node.ResourceVersion) == 0 { 97 err := fmt.Errorf("invalid update object, missing resource version: %v", node) 98 return nil, err 99 } 100 err := c.r.Put().Resource(c.resourceName()).Name(node.Name).SubResource("status").Body(node).Do().Into(result) 101 return result, err 102 } 103 104 // Watch returns a watch.Interface that watches the requested nodes. 105 func (c *nodes) Watch(label labels.Selector, field fields.Selector, opts api.ListOptions) (watch.Interface, error) { 106 return c.r.Get(). 107 Prefix("watch"). 108 Namespace(api.NamespaceAll). 109 Resource(c.resourceName()). 110 VersionedParams(&opts, api.Scheme). 111 LabelsSelectorParam(label). 112 FieldsSelectorParam(field). 113 Watch() 114 }