node.go 3.0 KB

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