Skip to content

Commit

Permalink
removing wild card support
Browse files Browse the repository at this point in the history
  • Loading branch information
ajones committed Sep 29, 2021
1 parent 7fcd501 commit 95c391f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
18 changes: 6 additions & 12 deletions example.fwdconf.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
baseUnreservedIP: 127.1.27.1
serviceConfigurations:
- # identify a single context that will contain this
# namespace and service or wildcard to match all
context: "*"
# identify a single namespace or match on wildcard
# for each namespace containing this service
# octet 2 will be incremented
namespace: "*"
- # context that will contain this namespace
context: context.name
# namespace expected to hold this service
namespace: foobar
# name of the service you wish to specify the IP for
serviceName: service-name
# attaching to multiple clusters will inc octet 1
ip: 127.1.28.1
- context: "foo"
namespace: "bar"
serviceName: service-name2
# ip address you wish to utilize for this service
ip: 127.1.28.1
36 changes: 11 additions & 25 deletions pkg/fwdIp/fwdIp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,10 @@ func determineIP(regKey string, opts ForwardIPOpts) net.IP {
svcConf := getConfigurationForService(opts)
if svcConf != nil {
if ip, err := ipFromString(svcConf.IP); err == nil {
if svcConf.HasWildcardContext() {
// for each cluster increment octet 1
// if the service could exist in multiple contexts
ip[1] = baseUnreservedIP[1] + byte(opts.ClusterN)
if err := addToRegistry(regKey, opts, ip); err == nil {
return ip
}

if svcConf.HasWildcardNamespace() {
// if the service could exist in multiple namespaces
ip[2] = baseUnreservedIP[2] + byte(opts.NamespaceN)
}

if err := addToRegistry(regKey, opts, ip); err != nil {
panic(fmt.Sprintf("Unable to forward service. %s", err))
}
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 @@ -120,18 +109,21 @@ func determineIP(regKey string, opts ForwardIPOpts) net.IP {
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)
}
return ip
}

func addToRegistry(regKey string, opts ForwardIPOpts, ip net.IP) error {
allocationKey := fmt.Sprintf("%s:%s", ip.String(), opts.Port)
allocationKey := fmt.Sprintf("%s", ip.String())
if _, ok := ipRegistry.allocated[allocationKey]; ok {
// ip/port pair has allready ben allocated
return fmt.Errorf("ip/port pair %s has already been allocated", allocationKey)
return fmt.Errorf("ip %s has already been allocated", allocationKey)
}
ipRegistry.reg[regKey] = ip
ipRegistry.allocated[allocationKey] = true
return nil
}

Expand Down Expand Up @@ -168,9 +160,11 @@ func getBaseUnreservedIP(forwardConfigurationPath string) []byte {

func getConfigurationForService(opts ForwardIPOpts) *ServiceConfiguration {
fwdCfg := getForwardConfiguration(opts.ForwardConfigurationPath)

for _, c := range fwdCfg.ServiceConfigurations {
if c.ServiceName == opts.ServiceName &&
(c.Namespace == "*" || c.Namespace == opts.Namespace) {
c.Namespace == opts.Namespace &&
c.Context == opts.Context {
return &c
}
}
Expand Down Expand Up @@ -208,14 +202,6 @@ func getForwardConfiguration(forwardConfigurationPath string) *ForwardConfigurat
return forwardConfiguration
}

func (c ServiceConfiguration) HasWildcardContext() bool {
return c.Context == "*"
}

func (c ServiceConfiguration) HasWildcardNamespace() bool {
return c.Namespace == "*"
}

func (c ServiceConfiguration) String() string {
return fmt.Sprintf("Ctx: %s Ns:%s Svc:%s IP:%s", c.Context, c.Namespace, c.ServiceName, c.IP)
}

0 comments on commit 95c391f

Please sign in to comment.