Skip to content

Commit

Permalink
addressing PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ajones committed Oct 26, 2021
1 parent 56b4981 commit 2a5a84c
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions pkg/fwdIp/fwdIp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fwdIp

import (
"errors"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -81,7 +82,6 @@ func determineIP(regKey string, opts ForwardIPOpts) net.IP {
if err := addToRegistry(regKey, opts, ip); err == nil {
return ip
}
log.Errorf("Unable to forward service %s to requested IP %s due to collision", opts.ServiceName, svcConf.IP)
} else {
log.Errorf("Invalid service IP format %s %s", svcConf.String(), err)
}
Expand All @@ -106,8 +106,6 @@ func determineIP(regKey string, opts ForwardIPOpts) net.IP {

ipRegistry.inc[opts.ClusterN][opts.NamespaceN]++
if err := addToRegistry(regKey, opts, ip); err != nil {
// failure to allocate on ip
log.Error(err)
// this recursive call will continue to inc the ip offset until
// an open slot is found or we go out of bounds
return determineIP(regKey, opts)
Expand All @@ -119,14 +117,17 @@ func addToRegistry(regKey string, opts ForwardIPOpts, ip net.IP) error {
allocationKey := fmt.Sprintf("%s", ip.String())
if _, ok := ipRegistry.allocated[allocationKey]; ok {
// ip/port pair has allready ben allocated
return fmt.Errorf("IP %s has already been allocated when placing %s. will allocate next available",
allocationKey, opts.ServiceName)
msg := fmt.Sprintf("Unable to forward service %s to requested IP %s due to collision. Will allocate next available", opts.ServiceName, allocationKey)
log.Error(msg)
return errors.New(msg)
}

// check for conflicting reservation
if conflicting := hasConflictingReservations(opts, ip.String()); conflicting != nil {
return fmt.Errorf("conflicting reservation for %s on %s when placing %s. will allocate next available",
msg := fmt.Sprintf("Conflicting reservation for %s on %s when placing %s. Will allocate next available",
conflicting.Name, allocationKey, opts.ServiceName)
log.Debug(msg)
return errors.New(msg)
}

ipRegistry.reg[regKey] = ip
Expand Down Expand Up @@ -199,6 +200,22 @@ func blockNonLoopbackIPs(f *ForwardConfiguration) {
}
}

func notifyOfDuplicateIPReservations(f *ForwardConfiguration) {
// Alerts the user
requestedIPs := map[string]bool{}
for _, svcCfg := range f.ServiceConfigurations {
if _, ok := requestedIPs[svcCfg.IP]; ok {
log.Warn(fmt.Sprintf("IP %s cannot be used as a reservation for multiple services", svcCfg.IP))
}
requestedIPs[svcCfg.IP] = true
}
}

func validateForwardConfiguration(f *ForwardConfiguration) {
blockNonLoopbackIPs(f)
notifyOfDuplicateIPReservations(f)
}

func applyCLIPassedReservations(opts ForwardIPOpts, f *ForwardConfiguration) *ForwardConfiguration {
for _, resStr := range opts.ForwardIPReservations {
res := ServiceConfigurationFromReservation(resStr)
Expand All @@ -208,14 +225,14 @@ func applyCLIPassedReservations(opts ForwardIPOpts, f *ForwardConfiguration) *Fo
if svcCfg.MatchesName(res) {
svcCfg.IP = res.IP
overridden = true
log.Infof("cli reservation flag overriding config for %s now %s", svcCfg.Name, svcCfg.IP)
log.Infof("Cli reservation flag overriding config for %s now %s", svcCfg.Name, svcCfg.IP)
}
}
if !overridden {
f.ServiceConfigurations = append(f.ServiceConfigurations, res)
}
}
blockNonLoopbackIPs(f)
validateForwardConfiguration(f)
return f
}

Expand Down

0 comments on commit 2a5a84c

Please sign in to comment.