Skip to content

Commit

Permalink
Add new user agent probe
Browse files Browse the repository at this point in the history
Signed-off-by: Max Moeschinger <[email protected]>
  • Loading branch information
maxmoeschinger committed Dec 21, 2023
1 parent 2144312 commit 639fb30
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 5 additions & 3 deletions interceptor/middleware/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

var (
kpUserAgent = regexp.MustCompile(`(^|\s)kube-probe/`)
ghUserAgent = regexp.MustCompile(`(^|\s)GoogleHC/`)
)

type Routing struct {
Expand All @@ -37,7 +38,7 @@ func (rm *Routing) ServeHTTP(w http.ResponseWriter, r *http.Request) {

httpso := rm.routingTable.Route(r)
if httpso == nil {
if rm.isKubeProbe(r) {
if rm.isProbe(r) {
rm.probeHandler.ServeHTTP(w, r)
return
}
Expand Down Expand Up @@ -71,7 +72,8 @@ func (rm *Routing) streamFromHTTPSO(httpso *httpv1alpha1.HTTPScaledObject) (*url
))
}

func (rm *Routing) isKubeProbe(r *http.Request) bool {
func (rm *Routing) isProbe(r *http.Request) bool {
ua := r.UserAgent()
return kpUserAgent.Match([]byte(ua))

return kpUserAgent.Match([]byte(ua)) || ghUserAgent.Match([]byte(ua))
}
18 changes: 15 additions & 3 deletions interceptor/middleware/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,31 @@ var _ = Describe("RoutingMiddleware", func() {
r.Header.Set(uaKey, uaVal)

var rm Routing
b := rm.isKubeProbe(r)
b := rm.isProbe(r)
Expect(b).To(BeTrue())
})

It("returns false if the request is not from kube-probe", func() {
It("returns true if the request is from GoogleHC", func() {
const (
uaVal = "Go-http-client/1.1 GoogleHC/1.0 (linux/amd64) kubernetes/4c94112"
)

r.Header.Set(uaKey, uaVal)

var rm Routing
b := rm.isProbe(r)
Expect(b).To(BeTrue())
})

It("returns false if the request is not from kube-probe or GoogleHC", func() {
const (
uaVal = "Go-http-client/1.1 kubectl/v1.27.1 (linux/amd64) kubernetes/4c94112"
)

r.Header.Set(uaKey, uaVal)

var rm Routing
b := rm.isKubeProbe(r)
b := rm.isProbe(r)
Expect(b).To(BeFalse())
})
})
Expand Down

0 comments on commit 639fb30

Please sign in to comment.