github.com/fafucoder/cilium@v1.6.11/pkg/endpoint/connector/ipam.go (about)

     1  // Copyright 2016-2018 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 connector
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  
    21  	"github.com/cilium/cilium/api/v1/models"
    22  	"github.com/cilium/cilium/pkg/datapath/linux/route"
    23  	"github.com/cilium/cilium/pkg/defaults"
    24  )
    25  
    26  // IPv6Gateway returns the IPv6 gateway address for endpoints.
    27  func IPv6Gateway(addr *models.NodeAddressing) string {
    28  	// The host's IP is the gateway address
    29  	return addr.IPV6.IP
    30  }
    31  
    32  // IPv4Gateway returns the IPv4 gateway address for endpoints.
    33  func IPv4Gateway(addr *models.NodeAddressing) string {
    34  	// The host's IP is the gateway address
    35  	return addr.IPV4.IP
    36  }
    37  
    38  // IPv6Routes returns IPv6 routes to be installed in endpoint's networking namespace.
    39  func IPv6Routes(addr *models.NodeAddressing, linkMTU int) ([]route.Route, error) {
    40  	ip := net.ParseIP(addr.IPV6.IP)
    41  	if ip == nil {
    42  		return []route.Route{}, fmt.Errorf("Invalid IP address: %s", addr.IPV6.IP)
    43  	}
    44  	return []route.Route{
    45  		{
    46  			Prefix: net.IPNet{
    47  				IP:   ip,
    48  				Mask: defaults.ContainerIPv6Mask,
    49  			},
    50  		},
    51  		{
    52  			Prefix:  defaults.IPv6DefaultRoute,
    53  			Nexthop: &ip,
    54  			MTU:     linkMTU,
    55  		},
    56  	}, nil
    57  }
    58  
    59  // IPv4Routes returns IPv4 routes to be installed in endpoint's networking namespace.
    60  func IPv4Routes(addr *models.NodeAddressing, linkMTU int) ([]route.Route, error) {
    61  	ip := net.ParseIP(addr.IPV4.IP)
    62  	if ip == nil {
    63  		return []route.Route{}, fmt.Errorf("Invalid IP address: %s", addr.IPV4.IP)
    64  	}
    65  	return []route.Route{
    66  		{
    67  			Prefix: net.IPNet{
    68  				IP:   ip,
    69  				Mask: defaults.ContainerIPv4Mask,
    70  			},
    71  		},
    72  		{
    73  			Prefix:  defaults.IPv4DefaultRoute,
    74  			Nexthop: &ip,
    75  			MTU:     linkMTU,
    76  		},
    77  	}, nil
    78  }
    79  
    80  // SufficientAddressing returns an error if the provided NodeAddressing does
    81  // not provide sufficient information to derive all IPAM required settings.
    82  func SufficientAddressing(addr *models.NodeAddressing) error {
    83  	if addr == nil {
    84  		return fmt.Errorf("Cilium daemon did not provide addressing information")
    85  	}
    86  
    87  	if addr.IPV6 != nil && addr.IPV6.IP != "" {
    88  		return nil
    89  	}
    90  
    91  	if addr.IPV4 != nil && addr.IPV4.IP != "" {
    92  		return nil
    93  	}
    94  
    95  	return fmt.Errorf("Either IPv4 or IPv6 addressing must be provided")
    96  }