...

Source file src/k8s.io/kubernetes/pkg/registry/registrytest/endpoint.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  	"fmt"
    22  	"sync"
    23  
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	runtime "k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/apimachinery/pkg/watch"
    29  	"k8s.io/apiserver/pkg/registry/rest"
    30  	api "k8s.io/kubernetes/pkg/apis/core"
    31  )
    32  
    33  // Registry is an interface for things that know how to store endpoints.
    34  type EndpointRegistry struct {
    35  	Endpoints *api.EndpointsList
    36  	Updates   []api.Endpoints
    37  	Err       error
    38  
    39  	lock sync.Mutex
    40  }
    41  
    42  func (e *EndpointRegistry) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
    43  	// TODO: support namespaces in this mock
    44  	e.lock.Lock()
    45  	defer e.lock.Unlock()
    46  
    47  	return e.Endpoints, e.Err
    48  }
    49  
    50  func (e *EndpointRegistry) New() runtime.Object {
    51  	return &api.Endpoints{}
    52  }
    53  func (e *EndpointRegistry) NewList() runtime.Object {
    54  	return &api.EndpointsList{}
    55  }
    56  
    57  func (e *EndpointRegistry) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
    58  	// TODO: support namespaces in this mock
    59  	e.lock.Lock()
    60  	defer e.lock.Unlock()
    61  	if e.Err != nil {
    62  		return nil, e.Err
    63  	}
    64  	if e.Endpoints != nil {
    65  		for _, endpoint := range e.Endpoints.Items {
    66  			if endpoint.Name == name {
    67  				return &endpoint, nil
    68  			}
    69  		}
    70  	}
    71  	return nil, errors.NewNotFound(api.Resource("endpoints"), name)
    72  }
    73  
    74  func (e *EndpointRegistry) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
    75  	return nil, fmt.Errorf("unimplemented!")
    76  }
    77  
    78  func (e *EndpointRegistry) Create(ctx context.Context, endpoints runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
    79  	return nil, fmt.Errorf("unimplemented!")
    80  }
    81  
    82  func (e *EndpointRegistry) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreateOnUpdate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
    83  	obj, err := objInfo.UpdatedObject(ctx, nil)
    84  	if err != nil {
    85  		return nil, false, err
    86  	}
    87  	endpoints := obj.(*api.Endpoints)
    88  	// TODO: support namespaces in this mock
    89  	e.lock.Lock()
    90  	defer e.lock.Unlock()
    91  
    92  	e.Updates = append(e.Updates, *endpoints)
    93  
    94  	if e.Err != nil {
    95  		return nil, false, e.Err
    96  	}
    97  	if e.Endpoints == nil {
    98  		e.Endpoints = &api.EndpointsList{
    99  			Items: []api.Endpoints{
   100  				*endpoints,
   101  			},
   102  		}
   103  		return endpoints, false, nil
   104  	}
   105  	for ix := range e.Endpoints.Items {
   106  		if e.Endpoints.Items[ix].Name == endpoints.Name {
   107  			e.Endpoints.Items[ix] = *endpoints
   108  		}
   109  	}
   110  	e.Endpoints.Items = append(e.Endpoints.Items, *endpoints)
   111  	return endpoints, false, nil
   112  }
   113  
   114  func (e *EndpointRegistry) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
   115  	// TODO: support namespaces in this mock
   116  	e.lock.Lock()
   117  	defer e.lock.Unlock()
   118  	if e.Err != nil {
   119  		return nil, false, e.Err
   120  	}
   121  	if e.Endpoints != nil {
   122  		var newList []api.Endpoints
   123  		for _, endpoint := range e.Endpoints.Items {
   124  			if endpoint.Name != name {
   125  				newList = append(newList, endpoint)
   126  			}
   127  		}
   128  		e.Endpoints.Items = newList
   129  	}
   130  	return nil, true, nil
   131  }
   132  
   133  func (e *EndpointRegistry) DeleteCollection(ctx context.Context, _ rest.ValidateObjectFunc, _ *metav1.DeleteOptions, _ *metainternalversion.ListOptions) (runtime.Object, error) {
   134  	return nil, fmt.Errorf("unimplemented!")
   135  }
   136  

View as plain text