Skip to content

Commit

Permalink
Support function resolution in multiple namespaces
Browse files Browse the repository at this point in the history
**What**
- Ensure that the pass only the function name to the Endpoint lister,
  because the namespace is already set when constructing the lister.
  The existing code would always return a 404 because it sent the
  "function_name.namespace" as the function name. This can never be
  found since it is not a valid service name
- Update the error messages to make the component parts of the message
  easier to read.  It will now be easier to see which part is the error
  from the k8s api.

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
  • Loading branch information
LucasRoesler authored and alexellis committed Sep 17, 2020
1 parent 54ffc9f commit 4cbd76b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
14 changes: 9 additions & 5 deletions pkg/k8s/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ func getNamespace(name, defaultNamespace string) string {
}

func (l *FunctionLookup) Resolve(name string) (url.URL, error) {

functionName := name
namespace := getNamespace(name, l.DefaultNamespace)
if err := l.verifyNamespace(namespace); err != nil {
return url.URL{}, err
}

if strings.Contains(name, ".") {
functionName = strings.TrimSuffix(name, "."+namespace)
}

nsEndpointLister := l.GetLister(namespace)

if nsEndpointLister == nil {
Expand All @@ -69,18 +73,18 @@ func (l *FunctionLookup) Resolve(name string) (url.URL, error) {
nsEndpointLister = l.GetLister(namespace)
}

svc, err := nsEndpointLister.Get(name)
svc, err := nsEndpointLister.Get(functionName)
if err != nil {
return url.URL{}, fmt.Errorf("error listing %s.%s %s", name, namespace, err.Error())
return url.URL{}, fmt.Errorf("error listing \"%s.%s\": %s", functionName, namespace, err.Error())
}

if len(svc.Subsets) == 0 {
return url.URL{}, fmt.Errorf("no subsets available for %s.%s", name, namespace)
return url.URL{}, fmt.Errorf("no subsets available for \"%s.%s\"", functionName, namespace)
}

all := len(svc.Subsets[0].Addresses)
if len(svc.Subsets[0].Addresses) == 0 {
return url.URL{}, fmt.Errorf("no addresses in subset for %s.%s", name, namespace)
return url.URL{}, fmt.Errorf("no addresses in subset for \"%s.%s\"", functionName, namespace)
}

target := rand.Intn(all)
Expand Down
17 changes: 10 additions & 7 deletions pkg/k8s/proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k8s

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -30,14 +31,16 @@ func (f FakeNSLister) List(selector labels.Selector) (ret []*corev1.Endpoints, e
}

func (f FakeNSLister) Get(name string) (*corev1.Endpoints, error) {

// make sure that we only send the function name to the lister
if strings.Contains(name, ".") {
return nil, fmt.Errorf("can not look up function name with a dot!")
}

ep := corev1.Endpoints{
Subsets: []corev1.EndpointSubset{
corev1.EndpointSubset{
Addresses: []corev1.EndpointAddress{
corev1.EndpointAddress{IP: "127.0.0.1"},
},
},
},
Subsets: []corev1.EndpointSubset{{
Addresses: []corev1.EndpointAddress{{IP: "127.0.0.1"}},
}},
}

return &ep, nil
Expand Down

0 comments on commit 4cbd76b

Please sign in to comment.