github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/machine/network.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package machine
    18  
    19  import (
    20  	"net"
    21  
    22  	"k8s.io/apimachinery/pkg/util/sets"
    23  )
    24  
    25  const (
    26  	DefaultNICNamespace = ""
    27  
    28  	IPVersionV4 = 4
    29  	IPVersionV6 = 6
    30  )
    31  
    32  type ExtraNetworkInfo struct {
    33  	// Interface info list of all network interface.
    34  	Interface []InterfaceInfo
    35  }
    36  
    37  type InterfaceInfo struct {
    38  	// Iface name of this interface.
    39  	Iface string
    40  	// Speed of this interface.
    41  	Speed int
    42  	// NumaNode numa node of this interface belongs to.
    43  	NumaNode int
    44  	// Enable whether enable this interface.
    45  	Enable bool
    46  	// Addr address of this interface, which includes ipv4 and ipv6.
    47  	Addr *IfaceAddr
    48  
    49  	// NSName indicates the namespace for this interface
    50  	NSName string
    51  	// NSAbsolutePath indicates the namespace path for this interface
    52  	NSAbsolutePath string
    53  }
    54  
    55  type IfaceAddr struct {
    56  	IPV4 []*net.IP
    57  	IPV6 []*net.IP
    58  }
    59  
    60  func (nic *InterfaceInfo) GetNICIPs(ipVersion int) []string {
    61  	res := sets.NewString()
    62  
    63  	var targetIPs []net.IP
    64  	switch ipVersion {
    65  	case IPVersionV4:
    66  		for _, ip := range nic.Addr.IPV4 {
    67  			if ip != nil {
    68  				targetIPs = append(targetIPs, *ip)
    69  			}
    70  		}
    71  	case IPVersionV6:
    72  		for _, ip := range nic.Addr.IPV6 {
    73  			if ip != nil {
    74  				targetIPs = append(targetIPs, *ip)
    75  			}
    76  		}
    77  	}
    78  
    79  	for _, ip := range targetIPs {
    80  		res.Insert(ip.String())
    81  	}
    82  
    83  	return res.List()
    84  }