github.com/elfadel/cilium@v1.6.12/pkg/datapath/fake/node_addressing.go (about)

     1  // Copyright 2018-2019 Authors of Cilium
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fake
    16  
    17  import (
    18  	"net"
    19  
    20  	"github.com/cilium/cilium/pkg/cidr"
    21  	"github.com/cilium/cilium/pkg/datapath"
    22  )
    23  
    24  type fakeNodeAddressing struct {
    25  	ipv6 addressFamily
    26  	ipv4 addressFamily
    27  }
    28  
    29  // NewIPv6OnlyNodeAddressing returns a new fake node addressing where IPv4 is
    30  // disabled
    31  func NewIPv6OnlyNodeAddressing() datapath.NodeAddressing {
    32  	return &fakeNodeAddressing{
    33  		ipv4: addressFamily{},
    34  		ipv6: addressFamily{
    35  			router:          net.ParseIP("cafe::2"),
    36  			primaryExternal: net.ParseIP("cafe::1"),
    37  			allocCIDR:       cidr.MustParseCIDR("cafe::/96"),
    38  		},
    39  	}
    40  }
    41  
    42  // NewIPv4OnlyNodeAddressing returns a new fake node addressing where IPv6 is
    43  // disabled
    44  func NewIPv4OnlyNodeAddressing() datapath.NodeAddressing {
    45  	return &fakeNodeAddressing{
    46  		ipv4: addressFamily{
    47  			router:          net.ParseIP("1.1.1.2"),
    48  			primaryExternal: net.ParseIP("1.1.1.1"),
    49  			allocCIDR:       cidr.MustParseCIDR("1.1.1.0/24"),
    50  		},
    51  		ipv6: addressFamily{},
    52  	}
    53  }
    54  
    55  // NewNodeAddressing returns a new fake node addressing
    56  func NewNodeAddressing() datapath.NodeAddressing {
    57  	return &fakeNodeAddressing{
    58  		ipv4: addressFamily{
    59  			router:          net.ParseIP("1.1.1.2"),
    60  			primaryExternal: net.ParseIP("1.1.1.1"),
    61  			allocCIDR:       cidr.MustParseCIDR("1.1.1.0/24"),
    62  			localAddresses: []net.IP{
    63  				net.ParseIP("2.2.2.2"),
    64  				net.ParseIP("3.3.3.3"),
    65  				net.ParseIP("4.4.4.4"),
    66  			},
    67  		},
    68  		ipv6: addressFamily{
    69  			router:          net.ParseIP("cafe::2"),
    70  			primaryExternal: net.ParseIP("cafe::1"),
    71  			allocCIDR:       cidr.MustParseCIDR("cafe::/96"),
    72  			localAddresses: []net.IP{
    73  				net.ParseIP("f00d::1"),
    74  				net.ParseIP("f00d::2"),
    75  				net.ParseIP("f00d::3"),
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  type addressFamily struct {
    82  	router          net.IP
    83  	primaryExternal net.IP
    84  	allocCIDR       *cidr.CIDR
    85  	localAddresses  []net.IP
    86  }
    87  
    88  func (a *addressFamily) Router() net.IP {
    89  	return a.router
    90  }
    91  
    92  func (a *addressFamily) PrimaryExternal() net.IP {
    93  	return a.primaryExternal
    94  }
    95  
    96  func (a *addressFamily) AllocationCIDR() *cidr.CIDR {
    97  	return a.allocCIDR
    98  }
    99  
   100  func (a *addressFamily) LocalAddresses() ([]net.IP, error) {
   101  	return a.localAddresses, nil
   102  }
   103  
   104  func (n *fakeNodeAddressing) IPv6() datapath.NodeAddressingFamily {
   105  	return &n.ipv6
   106  }
   107  
   108  func (n *fakeNodeAddressing) IPv4() datapath.NodeAddressingFamily {
   109  	return &n.ipv4
   110  }