-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathproxy.go
112 lines (87 loc) · 2.73 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// License: OpenFaaS Community Edition (CE) EULA
// Copyright (c) 2017,2019-2024 OpenFaaS Author(s)
// Copyright (c) Alex Ellis 2017. All rights reserved\.
// Copyright 2020 OpenFaaS Author(s)
package k8s
import (
"fmt"
"math/rand"
"net/url"
"strings"
"sync"
corelister "k8s.io/client-go/listers/core/v1"
)
// watchdogPort for the OpenFaaS function watchdog
const watchdogPort = 8080
func NewFunctionLookup(ns string, lister corelister.EndpointsLister) *FunctionLookup {
return &FunctionLookup{
DefaultNamespace: ns,
EndpointLister: lister,
Listers: map[string]corelister.EndpointsNamespaceLister{},
lock: sync.RWMutex{},
}
}
type FunctionLookup struct {
DefaultNamespace string
EndpointLister corelister.EndpointsLister
Listers map[string]corelister.EndpointsNamespaceLister
lock sync.RWMutex
}
func (f *FunctionLookup) GetLister(ns string) corelister.EndpointsNamespaceLister {
f.lock.RLock()
defer f.lock.RUnlock()
return f.Listers[ns]
}
func (f *FunctionLookup) SetLister(ns string, lister corelister.EndpointsNamespaceLister) {
f.lock.Lock()
defer f.lock.Unlock()
f.Listers[ns] = lister
}
func getNamespace(name, defaultNamespace string) string {
namespace := defaultNamespace
if strings.Contains(name, ".") {
namespace = name[strings.LastIndexAny(name, ".")+1:]
}
return namespace
}
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 {
l.SetLister(namespace, l.EndpointLister.Endpoints(namespace))
nsEndpointLister = l.GetLister(namespace)
}
svc, err := nsEndpointLister.Get(functionName)
if err != nil {
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\"", 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\"", functionName, namespace)
}
target := rand.Intn(all)
serviceIP := svc.Subsets[0].Addresses[target].IP
urlStr := fmt.Sprintf("http://%s:%d", serviceIP, watchdogPort)
urlRes, err := url.Parse(urlStr)
if err != nil {
return url.URL{}, err
}
return *urlRes, nil
}
func (l *FunctionLookup) verifyNamespace(name string) error {
if name != "kube-system" {
return nil
}
// ToDo use global namepace parse and validation
return fmt.Errorf("namespace not allowed")
}