...

Source file src/k8s.io/kubernetes/pkg/registry/registrytest/node.go

Documentation: k8s.io/kubernetes/pkg/registry/registrytest

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

View as plain text