From 332f6bec409522d887f3f656fe3ea0ddaa25e826 Mon Sep 17 00:00:00 2001 From: Dobes Vandermeer Date: Tue, 22 Sep 2020 14:07:56 -0700 Subject: [PATCH] Fix IP address calculation when there are multiple services in the same namespace There was one top level loop that assigned IP addresses by incrementing the IP address once per namespace, but there was another piece of logic that was incrementing the IP address once per service, adding that to the one from the namespace logic. The result was that a namespace with multiple services could try to bind to an IP address in use by a subsequent namespace, resulting in a conflict and a failed port forward. This changes it so that it does not increment once per namespace, restoring the "old" logic where it increments the IP once per service or additional times if the desired port is not available. --- cmd/kubefwd/services/services.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/kubefwd/services/services.go b/cmd/kubefwd/services/services.go index 5041a85f..8ea21032 100644 --- a/cmd/kubefwd/services/services.go +++ b/cmd/kubefwd/services/services.go @@ -222,7 +222,6 @@ Try: // ipD is the class D for the local IP address // increment this for each service in each cluster ipC := 27 - ipD := 1 stopListenCh := make(chan struct{}) @@ -276,9 +275,11 @@ Try: log.Fatalf("Error creating k8s RestClient: %s\n", err.Error()) } - for ii, namespace := range namespaces { + ipD := 1 + + for _, namespace := range namespaces { nsWatchesDone.Add(1) - go func(ctx string, namespace string, ipC int, ipD int) { + go func(ctx string, namespace string, ipC int) { nameSpaceOpts := NamespaceOpts{ ClientSet: clientSet, Context: ctx, @@ -290,13 +291,13 @@ Try: RESTClient: restClient, ShortName: !useFullName, IpC: byte(ipC), - IpD: ipD, + IpD: &ipD, Domain: domain, ManualStopChannel: stopListenCh, } nameSpaceOpts.watchServiceEvents(stopListenCh) nsWatchesDone.Done() - }(ctx, namespace, ipC+i, ipD+ii) + }(ctx, namespace, ipC+i) } } @@ -320,7 +321,7 @@ type NamespaceOpts struct { RESTClient *restclient.RESTClient ShortName bool IpC byte - IpD int + IpD *int Domain string ManualStopChannel chan struct{} } @@ -386,7 +387,7 @@ func (opts *NamespaceOpts) AddServiceHandler(obj interface{}) { RESTClient: opts.RESTClient, ShortName: opts.ShortName, IpC: opts.IpC, - IpD: &opts.IpD, + IpD: opts.IpD, Domain: opts.Domain, PodLabelSelector: selector, NamespaceIPLock: opts.NamespaceIPLock,