forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmiddleware.go
136 lines (117 loc) · 3.3 KB
/
middleware.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"fmt"
"log"
"net"
"net/http"
"strings"
"time"
"github.com/go-logr/logr"
"github.com/kedacore/http-add-on/pkg/queue"
)
func getHost(r *http.Request) (string, error) {
remoteIP := r.RemoteAddr
if remoteIP == "" {
return "", fmt.Errorf("remote address not found")
}
// removing port if exists
if i := strings.Index(remoteIP, ":"); i != -1 {
remoteIP = remoteIP[:i]
}
host := r.Host
if host == "" {
return "", fmt.Errorf("host not found")
}
// removing port if exists
if i := strings.Index(host, ":"); i != -1 {
host = host[:i]
}
// ReverseDNS lookup on the remote IP
names, err := net.LookupAddr(remoteIP)
if err != nil {
return "", fmt.Errorf("error looking up address %q: %s", remoteIP, err)
}
if len(names) == 0 {
return "", fmt.Errorf("no names found for address %q", remoteIP)
}
remoteDNS := names[0]
_, _, remoteNs := extractPodInfo(remoteDNS)
if remoteNs == "" {
return "", fmt.Errorf("namespace not found in %q", remoteDNS)
}
// Extracting service name and namespace from the host header
destService, destNs := extractServiceInfo(host)
// If the caller is from user namespace or
// the destination namespace is not provided in the host header
// then the destination namespace is the same as the caller namespace
if strings.HasPrefix(remoteNs, "dp-") || destNs == "" {
host = fmt.Sprintf("%s.%s", destService, remoteNs)
} else {
host = fmt.Sprintf("%s.%s", destService, destNs)
}
return host, nil
}
// $SVC.$NAMESPACE.svc.cluster.local
func extractServiceInfo(serviceURL string) (string, string) {
parts := strings.Split(serviceURL, ".")
if len(parts) >= 2 {
serviceName := parts[0]
namespace := parts[1]
return serviceName, namespace
}
if len(parts) == 1 {
serviceName := parts[0]
return serviceName, ""
}
return "", ""
}
// $POD_IP.$DEPLOYMENT_NAME.$NAMESPACE.svc.cluster.local
func extractPodInfo(podURL string) (string, string, string) {
parts := strings.Split(podURL, ".")
if len(parts) >= 3 {
podIP := parts[0]
deploymentName := parts[1]
namespace := parts[2]
return podIP, deploymentName, namespace
}
if len(parts) == 2 {
podIP := parts[0]
deploymentName := parts[1]
return podIP, deploymentName, ""
}
return "", "", ""
}
// countMiddleware adds 1 to the given queue counter, executes next
// (by calling ServeHTTP on it), then decrements the queue counter
func countMiddleware(
lggr logr.Logger,
q queue.Counter,
next http.Handler,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
host, err := getHost(r)
if err != nil {
lggr.Error(err, "not forwarding request")
w.WriteHeader(400)
if _, err := w.Write([]byte("Host not found, not forwarding request")); err != nil {
lggr.Error(err, "could not write error message to client")
}
return
}
lggr.Info("request received.", "host", host)
if err := q.Resize(host, +1); err != nil {
log.Printf("Error incrementing queue for %q (%s)", r.RequestURI, err)
}
defer func() {
if q.ShouldPostponeResize() && q.Count(host) == 1 {
lggr.Info("postponing resize", "host", host)
q.PostponeResize(host, time.Now().Add(q.PostponeDuration()))
return
}
if err := q.Resize(host, -1); err != nil {
log.Printf("Error decrementing queue for %q (%s)", r.RequestURI, err)
}
}()
next.ServeHTTP(w, r)
})
}