...

Source file src/edge-infra.dev/pkg/k8s/net/calico/calico.go

Documentation: edge-infra.dev/pkg/k8s/net/calico

     1  package net
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  
     7  	v1 "k8s.io/api/core/v1"
     8  )
     9  
    10  const (
    11  	IPAnnotation = "projectcalico.org/IPv4Address"
    12  )
    13  
    14  // Parses list of nodes and returns their ips
    15  func ParseNodeIPs(nodes ...v1.Node) ([]*net.IP, error) {
    16  	ips := make([]*net.IP, 0, len(nodes))
    17  	for _, node := range nodes {
    18  		ip, _, err := getNodeCIDRFromAnnotation(node)
    19  		if err != nil {
    20  			return nil, err
    21  		}
    22  		ips = append(ips, &ip)
    23  	}
    24  	return ips, nil
    25  }
    26  
    27  // Parses list of nodes and returns their ips in cidr notation
    28  func ParseNodeCIDRs(nodes ...v1.Node) ([]*net.IPNet, error) {
    29  	networksToIgnore := make([]*net.IPNet, 0, len(nodes))
    30  	for _, node := range nodes {
    31  		_, ipNet, err := getNodeCIDRFromAnnotation(node)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		networksToIgnore = append(networksToIgnore, ipNet)
    36  	}
    37  	return networksToIgnore, nil
    38  }
    39  
    40  func getNodeCIDRFromAnnotation(node v1.Node) (net.IP, *net.IPNet, error) {
    41  	nodeCIDR, ok := node.ObjectMeta.Annotations[IPAnnotation]
    42  	if !ok {
    43  		return nil, nil, fmt.Errorf("could not find node ip address via %s annotation for node %s", IPAnnotation, node.ObjectMeta.Name)
    44  	}
    45  	return net.ParseCIDR(nodeCIDR)
    46  }
    47  

View as plain text