-
Notifications
You must be signed in to change notification settings - Fork 196
/
reqTrack.go
51 lines (44 loc) · 1.18 KB
/
reqTrack.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
package rules
import (
"fmt"
"github.com/haproxytech/client-native/v5/models"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
)
type ReqTrack struct {
TableName string
TablePeriod *int64
TableSize *int64
TrackKey string
}
func (r ReqTrack) GetType() Type {
return REQ_TRACK
}
func (r ReqTrack) Create(client api.HAProxyClient, frontend *models.Frontend, ingressACL string) error {
if frontend.Mode == "tcp" {
return fmt.Errorf("request Track cannot be configured in TCP mode")
}
// Create tracking table.
if _, err := client.BackendGet(r.TableName); err != nil {
err = client.BackendCreate(models.Backend{
Name: r.TableName,
StickTable: &models.ConfigStickTable{
Peers: "localinstance",
Type: "ip",
Size: r.TableSize,
Store: fmt.Sprintf("http_req_rate(%d)", *r.TablePeriod),
},
})
if err != nil {
return err
}
}
// Create rule
httpRule := models.HTTPRequestRule{
Index: utils.PtrInt64(0),
Type: "track-sc0",
TrackSc0Key: r.TrackKey,
TrackSc0Table: r.TableName,
}
return client.FrontendHTTPRequestRuleCreate(frontend.Name, httpRule, ingressACL)
}