node.go 2.7 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 registrytest
  14. import (
  15. "sync"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/errors"
  18. "k8s.io/kubernetes/pkg/watch"
  19. )
  20. // NodeRegistry implements node.Registry interface.
  21. type NodeRegistry struct {
  22. Err error
  23. Node string
  24. Nodes api.NodeList
  25. sync.Mutex
  26. }
  27. // MakeNodeList constructs api.NodeList from list of node names and a NodeResource.
  28. func MakeNodeList(nodes []string, nodeResources api.NodeResources) *api.NodeList {
  29. list := api.NodeList{
  30. Items: make([]api.Node, len(nodes)),
  31. }
  32. for i := range nodes {
  33. list.Items[i].Name = nodes[i]
  34. list.Items[i].Status.Capacity = nodeResources.Capacity
  35. }
  36. return &list
  37. }
  38. func NewNodeRegistry(nodes []string, nodeResources api.NodeResources) *NodeRegistry {
  39. return &NodeRegistry{
  40. Nodes: *MakeNodeList(nodes, nodeResources),
  41. }
  42. }
  43. func (r *NodeRegistry) SetError(err error) {
  44. r.Lock()
  45. defer r.Unlock()
  46. r.Err = err
  47. }
  48. func (r *NodeRegistry) ListNodes(ctx api.Context, options *api.ListOptions) (*api.NodeList, error) {
  49. r.Lock()
  50. defer r.Unlock()
  51. return &r.Nodes, r.Err
  52. }
  53. func (r *NodeRegistry) CreateNode(ctx api.Context, node *api.Node) error {
  54. r.Lock()
  55. defer r.Unlock()
  56. r.Node = node.Name
  57. r.Nodes.Items = append(r.Nodes.Items, *node)
  58. return r.Err
  59. }
  60. func (r *NodeRegistry) UpdateNode(ctx api.Context, node *api.Node) error {
  61. r.Lock()
  62. defer r.Unlock()
  63. for i, item := range r.Nodes.Items {
  64. if item.Name == node.Name {
  65. r.Nodes.Items[i] = *node
  66. return r.Err
  67. }
  68. }
  69. return r.Err
  70. }
  71. func (r *NodeRegistry) GetNode(ctx api.Context, nodeID string) (*api.Node, error) {
  72. r.Lock()
  73. defer r.Unlock()
  74. if r.Err != nil {
  75. return nil, r.Err
  76. }
  77. for _, node := range r.Nodes.Items {
  78. if node.Name == nodeID {
  79. return &node, nil
  80. }
  81. }
  82. return nil, errors.NewNotFound(api.Resource("nodes"), nodeID)
  83. }
  84. func (r *NodeRegistry) DeleteNode(ctx api.Context, nodeID string) error {
  85. r.Lock()
  86. defer r.Unlock()
  87. var newList []api.Node
  88. for _, node := range r.Nodes.Items {
  89. if node.Name != nodeID {
  90. newList = append(newList, api.Node{ObjectMeta: api.ObjectMeta{Name: node.Name}})
  91. }
  92. }
  93. r.Nodes.Items = newList
  94. return r.Err
  95. }
  96. func (r *NodeRegistry) WatchNodes(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
  97. return nil, r.Err
  98. }