Skip to content

Commit

Permalink
Add new probe user agent (#863)
Browse files Browse the repository at this point in the history
* Add new user agent probe

Signed-off-by: Max Moeschinger <[email protected]>

* Add fix information to changelog

Signed-off-by: Max Moeschinger <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Jorge Turrado Ferrero <[email protected]>
Signed-off-by: Max Moeschinger <[email protected]>

---------

Signed-off-by: Max Moeschinger <[email protected]>
Signed-off-by: Max Moeschinger <[email protected]>
Co-authored-by: Jorge Turrado Ferrero <[email protected]>
  • Loading branch information
maxmoeschinger and JorTurFer authored Dec 22, 2023
1 parent 2144312 commit 814f333
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This changelog keeps track of work items that have been completed and are ready

### Fixes

- **General**: Add new user agent probe ([#862](https://github.com/kedacore/http-add-on/issues/862))
- **General**: Increase ScaledObject polling interval to 15 seconds ([#799](https://github.com/kedacore/http-add-on/issues/799))

### Deprecations
Expand Down
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 814f333

Please sign in to comment.