...

Source file src/k8s.io/apimachinery/pkg/util/net/util.go

Documentation: k8s.io/apimachinery/pkg/util/net

     1  /*
     2  Copyright 2016 The Kubernetes 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 net
    18  
    19  import (
    20  	"errors"
    21  	"net"
    22  	"reflect"
    23  	"strings"
    24  	"syscall"
    25  )
    26  
    27  // IPNetEqual checks if the two input IPNets are representing the same subnet.
    28  // For example,
    29  //
    30  //	10.0.0.1/24 and 10.0.0.0/24 are the same subnet.
    31  //	10.0.0.1/24 and 10.0.0.0/25 are not the same subnet.
    32  func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool {
    33  	if ipnet1 == nil || ipnet2 == nil {
    34  		return false
    35  	}
    36  	if reflect.DeepEqual(ipnet1.Mask, ipnet2.Mask) && ipnet1.Contains(ipnet2.IP) && ipnet2.Contains(ipnet1.IP) {
    37  		return true
    38  	}
    39  	return false
    40  }
    41  
    42  // Returns if the given err is "connection reset by peer" error.
    43  func IsConnectionReset(err error) bool {
    44  	var errno syscall.Errno
    45  	if errors.As(err, &errno) {
    46  		return errno == syscall.ECONNRESET
    47  	}
    48  	return false
    49  }
    50  
    51  // Returns if the given err is "http2: client connection lost" error.
    52  func IsHTTP2ConnectionLost(err error) bool {
    53  	return err != nil && strings.Contains(err.Error(), "http2: client connection lost")
    54  }
    55  
    56  // Returns if the given err is "connection refused" error
    57  func IsConnectionRefused(err error) bool {
    58  	var errno syscall.Errno
    59  	if errors.As(err, &errno) {
    60  		return errno == syscall.ECONNREFUSED
    61  	}
    62  	return false
    63  }
    64  

View as plain text