...

Source file src/k8s.io/kubernetes/pkg/proxy/winkernel/testing/hcnutils_mock.go

Documentation: k8s.io/kubernetes/pkg/proxy/winkernel/testing

     1  //go:build windows
     2  // +build windows
     3  
     4  /*
     5  Copyright 2018 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package testing
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  
    26  	"github.com/Microsoft/hcsshim/hcn"
    27  )
    28  
    29  var (
    30  	epIdCounter     int
    31  	lbIdCounter     int
    32  	endpointMap     map[string]*hcn.HostComputeEndpoint
    33  	loadbalancerMap map[string]*hcn.HostComputeLoadBalancer
    34  )
    35  
    36  type HcnMock struct {
    37  	supportedFeatures hcn.SupportedFeatures
    38  	network           *hcn.HostComputeNetwork
    39  }
    40  
    41  func (hcnObj HcnMock) generateEndpointGuid() (endpointId string, endpointName string) {
    42  	epIdCounter++
    43  	endpointId = fmt.Sprintf("EPID-%d", epIdCounter)
    44  	endpointName = fmt.Sprintf("EPName-%d", epIdCounter)
    45  	return
    46  }
    47  
    48  func (hcnObj HcnMock) generateLoadbalancerGuid() (loadbalancerId string) {
    49  	lbIdCounter++
    50  	loadbalancerId = fmt.Sprintf("LBID-%d", lbIdCounter)
    51  	return
    52  }
    53  
    54  func NewHcnMock(hnsNetwork *hcn.HostComputeNetwork) *HcnMock {
    55  	epIdCounter = 0
    56  	lbIdCounter = 0
    57  	endpointMap = make(map[string]*hcn.HostComputeEndpoint)
    58  	loadbalancerMap = make(map[string]*hcn.HostComputeLoadBalancer)
    59  	return &HcnMock{
    60  		supportedFeatures: hcn.SupportedFeatures{
    61  			Api: hcn.ApiSupport{
    62  				V2: true,
    63  			},
    64  			DSR:           true,
    65  			IPv6DualStack: true,
    66  		},
    67  		network: hnsNetwork,
    68  	}
    69  }
    70  
    71  func (hcnObj HcnMock) PopulateQueriedEndpoints(epId, hnsId, ipAddress, mac string, prefixLen uint8) {
    72  	endpoint := &hcn.HostComputeEndpoint{
    73  		Id:                 epId,
    74  		Name:               epId,
    75  		HostComputeNetwork: hnsId,
    76  		IpConfigurations: []hcn.IpConfig{
    77  			{
    78  				IpAddress:    ipAddress,
    79  				PrefixLength: prefixLen,
    80  			},
    81  		},
    82  		MacAddress: mac,
    83  	}
    84  
    85  	endpointMap[endpoint.Id] = endpoint
    86  	endpointMap[endpoint.Name] = endpoint
    87  }
    88  
    89  func (hcnObj HcnMock) GetNetworkByName(networkName string) (*hcn.HostComputeNetwork, error) {
    90  	return hcnObj.network, nil
    91  }
    92  
    93  func (hcnObj HcnMock) GetNetworkByID(networkID string) (*hcn.HostComputeNetwork, error) {
    94  	return hcnObj.network, nil
    95  }
    96  
    97  func (hcnObj HcnMock) ListEndpoints() ([]hcn.HostComputeEndpoint, error) {
    98  	var hcnEPList []hcn.HostComputeEndpoint
    99  	for _, ep := range endpointMap {
   100  		hcnEPList = append(hcnEPList, *ep)
   101  	}
   102  	return hcnEPList, nil
   103  }
   104  
   105  func (hcnObj HcnMock) ListEndpointsOfNetwork(networkId string) ([]hcn.HostComputeEndpoint, error) {
   106  	var hcnEPList []hcn.HostComputeEndpoint
   107  	for _, ep := range endpointMap {
   108  		if ep.HostComputeNetwork == networkId {
   109  			hcnEPList = append(hcnEPList, *ep)
   110  		}
   111  	}
   112  	return hcnEPList, nil
   113  }
   114  
   115  func (hcnObj HcnMock) GetEndpointByID(endpointId string) (*hcn.HostComputeEndpoint, error) {
   116  	if ep, ok := endpointMap[endpointId]; ok {
   117  		return ep, nil
   118  	}
   119  	epNotFoundError := hcn.EndpointNotFoundError{EndpointID: endpointId}
   120  	return nil, epNotFoundError
   121  }
   122  
   123  func (hcnObj HcnMock) GetEndpointByName(endpointName string) (*hcn.HostComputeEndpoint, error) {
   124  	if ep, ok := endpointMap[endpointName]; ok {
   125  		return ep, nil
   126  	}
   127  	epNotFoundError := hcn.EndpointNotFoundError{EndpointName: endpointName}
   128  	return nil, epNotFoundError
   129  }
   130  
   131  func (hcnObj HcnMock) CreateEndpoint(network *hcn.HostComputeNetwork, endpoint *hcn.HostComputeEndpoint) (*hcn.HostComputeEndpoint, error) {
   132  	if _, err := hcnObj.GetNetworkByID(network.Id); err != nil {
   133  		return nil, err
   134  	}
   135  	if _, ok := endpointMap[endpoint.Id]; ok {
   136  		return nil, fmt.Errorf("endpoint id %s already present", endpoint.Id)
   137  	}
   138  	if _, ok := endpointMap[endpoint.Name]; ok {
   139  		return nil, fmt.Errorf("endpoint Name %s already present", endpoint.Name)
   140  	}
   141  	endpoint.Id, endpoint.Name = hcnObj.generateEndpointGuid()
   142  	endpoint.HostComputeNetwork = network.Id
   143  	endpointMap[endpoint.Id] = endpoint
   144  	endpointMap[endpoint.Name] = endpoint
   145  	return endpoint, nil
   146  }
   147  
   148  func (hcnObj HcnMock) CreateRemoteEndpoint(network *hcn.HostComputeNetwork, endpoint *hcn.HostComputeEndpoint) (*hcn.HostComputeEndpoint, error) {
   149  	return hcnObj.CreateEndpoint(network, endpoint)
   150  }
   151  
   152  func (hcnObj HcnMock) DeleteEndpoint(endpoint *hcn.HostComputeEndpoint) error {
   153  	if _, ok := endpointMap[endpoint.Id]; !ok {
   154  		return hcn.EndpointNotFoundError{EndpointID: endpoint.Id}
   155  	}
   156  	delete(endpointMap, endpoint.Id)
   157  	delete(endpointMap, endpoint.Name)
   158  	return nil
   159  }
   160  
   161  func (hcnObj HcnMock) ListLoadBalancers() ([]hcn.HostComputeLoadBalancer, error) {
   162  	var hcnLBList []hcn.HostComputeLoadBalancer
   163  	for _, lb := range loadbalancerMap {
   164  		hcnLBList = append(hcnLBList, *lb)
   165  	}
   166  	return hcnLBList, nil
   167  }
   168  
   169  func (hcnObj HcnMock) GetLoadBalancerByID(loadBalancerId string) (*hcn.HostComputeLoadBalancer, error) {
   170  	if lb, ok := loadbalancerMap[loadBalancerId]; ok {
   171  		return lb, nil
   172  	}
   173  	lbNotFoundError := hcn.LoadBalancerNotFoundError{LoadBalancerId: loadBalancerId}
   174  	return nil, lbNotFoundError
   175  }
   176  
   177  func (hcnObj HcnMock) CreateLoadBalancer(loadBalancer *hcn.HostComputeLoadBalancer) (*hcn.HostComputeLoadBalancer, error) {
   178  	if _, ok := loadbalancerMap[loadBalancer.Id]; ok {
   179  		return nil, fmt.Errorf("LoadBalancer id %s Already Present", loadBalancer.Id)
   180  	}
   181  	loadBalancer.Id = hcnObj.generateLoadbalancerGuid()
   182  	loadbalancerMap[loadBalancer.Id] = loadBalancer
   183  	return loadBalancer, nil
   184  }
   185  
   186  func (hcnObj HcnMock) DeleteLoadBalancer(loadBalancer *hcn.HostComputeLoadBalancer) error {
   187  	if _, ok := loadbalancerMap[loadBalancer.Id]; !ok {
   188  		return hcn.LoadBalancerNotFoundError{LoadBalancerId: loadBalancer.Id}
   189  	}
   190  	delete(loadbalancerMap, loadBalancer.Id)
   191  	return nil
   192  }
   193  
   194  func (hcnObj HcnMock) GetSupportedFeatures() hcn.SupportedFeatures {
   195  	return hcnObj.supportedFeatures
   196  }
   197  
   198  func (hcnObj HcnMock) Ipv6DualStackSupported() error {
   199  	if hcnObj.supportedFeatures.IPv6DualStack {
   200  		return nil
   201  	}
   202  	return errors.New("IPV6 DualStack Not Supported")
   203  }
   204  
   205  func (hcnObj HcnMock) DsrSupported() error {
   206  	if hcnObj.supportedFeatures.DSR {
   207  		return nil
   208  	}
   209  	return errors.New("DSR Not Supported")
   210  }
   211  
   212  func (hcnObj HcnMock) DeleteAllHnsLoadBalancerPolicy() {
   213  	for k := range loadbalancerMap {
   214  		delete(loadbalancerMap, k)
   215  	}
   216  }
   217  

View as plain text