...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/podnetworkchaos/netutils/cidr.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/podnetworkchaos/netutils

     1  // Copyright 2021 Chaos Mesh Authors.
     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  
    16  package netutils
    17  
    18  import (
    19  	"net"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/pkg/errors"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    26  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    27  )
    28  
    29  // IPToCidr converts from an ip to a full mask cidr
    30  func IPToCidr(ip string) string {
    31  	// TODO: support IPv6
    32  	return ip + "/32"
    33  }
    34  
    35  // ResolveCidrs converts multiple cidrs/ips/domains into cidr
    36  func ResolveCidrs(names []string) ([]v1alpha1.CidrAndPort, error) {
    37  	cidrs := []v1alpha1.CidrAndPort{}
    38  	for _, target := range names {
    39  		// TODO: resolve ip on every pods but not in controller, in case the dns server of these pods differ
    40  		cidr, err := ResolveCidr(target)
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  
    45  		cidrs = append(cidrs, cidr...)
    46  	}
    47  
    48  	return cidrs, nil
    49  }
    50  
    51  // ResolveCidr converts cidr/ip/domain into cidr
    52  func ResolveCidr(name string) ([]v1alpha1.CidrAndPort, error) {
    53  	var toResolve string
    54  	var port uint16
    55  
    56  	if host, portStr, err := net.SplitHostPort(name); err == nil {
    57  		toResolve = host
    58  		port64, err := strconv.ParseUint(portStr, 10, 16)
    59  		if err != nil {
    60  			return nil, errors.Errorf("parse port %s", err)
    61  		}
    62  		port = uint16(port64)
    63  	} else {
    64  		toResolve = name
    65  	}
    66  
    67  	_, ipnet, err := net.ParseCIDR(toResolve)
    68  	if err == nil {
    69  		return []v1alpha1.CidrAndPort{{Cidr: ipnet.String(), Port: port}}, nil
    70  	}
    71  
    72  	if net.ParseIP(toResolve) != nil {
    73  		return []v1alpha1.CidrAndPort{{Cidr: IPToCidr(toResolve), Port: port}}, nil
    74  	}
    75  
    76  	addrs, err := LookupIP(toResolve)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	cidrs := []v1alpha1.CidrAndPort{}
    82  	for _, addr := range addrs {
    83  		addr := addr.String()
    84  
    85  		// TODO: support IPv6
    86  		if strings.Contains(addr, ".") {
    87  			cidrs = append(cidrs, v1alpha1.CidrAndPort{Cidr: IPToCidr(addr), Port: port})
    88  		}
    89  	}
    90  	return cidrs, nil
    91  }
    92  
    93  func LookupIP(host string) ([]net.IP, error) {
    94  	if result := mock.On("LookupIP"); result != nil {
    95  		return result.([]net.IP), nil
    96  	}
    97  	return net.LookupIP(host)
    98  }
    99