...

Source file src/edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/plugins/networking/netplan/routes.go

Documentation: edge-infra.dev/pkg/sds/ien/k8s/controllers/nodeagent/plugins/networking/netplan

     1  package netplan
     2  
     3  import (
     4  	"net"
     5  
     6  	v1ien "edge-infra.dev/pkg/sds/ien/k8s/apis/v1"
     7  )
     8  
     9  // staticRoutes returns the routes for the provided interface
    10  // from a node with a static network configuration.
    11  func staticRoutes(iface v1ien.Network) ([]Route, error) {
    12  	gwRoute, err := defaultGatewayRoute(iface)
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  	return []Route{gwRoute}, nil
    17  }
    18  
    19  // defaultGatewayRoute returns the default gateway route for the provided interface and node.
    20  // Assumes Gateway4 is not an empty string
    21  func defaultGatewayRoute(iface v1ien.Network) (Route, error) {
    22  	route := Route{To: "default", Via: iface.Gateway4}
    23  	gateway4IP := net.ParseIP(iface.Gateway4)
    24  	// check if gateway4 IP is in the same subnet as any of the interface's addresses
    25  	match := false
    26  	for _, address := range iface.Addresses {
    27  		_, addressCIDR, err := net.ParseCIDR(address)
    28  		if err != nil {
    29  			return Route{}, err
    30  		}
    31  		if addressCIDR.Contains(gateway4IP) {
    32  			match = true
    33  			break
    34  		}
    35  	}
    36  	// gateway4 IP not in the same subnet as any of the interface's addresses,
    37  	// so set OnLink to true
    38  	if !match {
    39  		route.OnLink = true
    40  	}
    41  	return route, nil
    42  }
    43  

View as plain text