...

Source file src/github.com/prometheus/alertmanager/cluster/advertise.go

Documentation: github.com/prometheus/alertmanager/cluster

     1  // Copyright 2018 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cluster
    15  
    16  import (
    17  	"net"
    18  
    19  	"github.com/hashicorp/go-sockaddr"
    20  	"github.com/pkg/errors"
    21  )
    22  
    23  type getIPFunc func() (string, error)
    24  
    25  // These are overridden in unit tests to mock the sockaddr functions.
    26  var getPrivateAddress getIPFunc = sockaddr.GetPrivateIP
    27  var getPublicAddress getIPFunc = sockaddr.GetPublicIP
    28  
    29  // calculateAdvertiseAddress attempts to clone logic from deep within memberlist
    30  // (NetTransport.FinalAdvertiseAddr) in order to surface its conclusions to the
    31  // application, so we can provide more actionable error messages if the user has
    32  // inadvertently misconfigured their cluster.
    33  //
    34  // https://github.com/hashicorp/memberlist/blob/022f081/net_transport.go#L126
    35  func calculateAdvertiseAddress(bindAddr, advertiseAddr string, allowInsecureAdvertise bool) (net.IP, error) {
    36  	if advertiseAddr != "" {
    37  		ip := net.ParseIP(advertiseAddr)
    38  		if ip == nil {
    39  			return nil, errors.Errorf("failed to parse advertise addr '%s'", advertiseAddr)
    40  		}
    41  		if ip4 := ip.To4(); ip4 != nil {
    42  			ip = ip4
    43  		}
    44  		return ip, nil
    45  	}
    46  
    47  	if isAny(bindAddr) {
    48  		return discoverAdvertiseAddress(allowInsecureAdvertise)
    49  	}
    50  
    51  	ip := net.ParseIP(bindAddr)
    52  	if ip == nil {
    53  		return nil, errors.Errorf("failed to parse bind addr '%s'", bindAddr)
    54  	}
    55  	return ip, nil
    56  }
    57  
    58  // discoverAdvertiseAddress will attempt to get a single IP address to use as
    59  // the advertise address when one is not explicitly provided. It defaults to
    60  // using a private IP address, and if not found then using a public IP if
    61  // insecure advertising is allowed.
    62  func discoverAdvertiseAddress(allowInsecureAdvertise bool) (net.IP, error) {
    63  	addr, err := getPrivateAddress()
    64  	if err != nil {
    65  		return nil, errors.Wrap(err, "failed to get private IP")
    66  	}
    67  	if addr == "" && !allowInsecureAdvertise {
    68  		return nil, errors.New("no private IP found, explicit advertise addr not provided")
    69  	}
    70  
    71  	if addr == "" {
    72  		addr, err = getPublicAddress()
    73  		if err != nil {
    74  			return nil, errors.Wrap(err, "failed to get public IP")
    75  		}
    76  		if addr == "" {
    77  			return nil, errors.New("no private/public IP found, explicit advertise addr not provided")
    78  		}
    79  	}
    80  
    81  	ip := net.ParseIP(addr)
    82  	if ip == nil {
    83  		return nil, errors.Errorf("failed to parse discovered IP '%s'", addr)
    84  	}
    85  	return ip, nil
    86  }
    87  

View as plain text