Skip to content

Commit

Permalink
Upgrade to dataplaneapi v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ShimmerGlass committed Sep 7, 2020
1 parent 19573c8 commit 508acc4
Show file tree
Hide file tree
Showing 28 changed files with 220 additions and 129 deletions.
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@ module github.com/haproxytech/haproxy-consul-connect
go 1.13

require (
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
github.com/criteo/haproxy-spoe-go v0.0.0-20190925130734-97891c13d324
github.com/d4l3k/messagediff v1.2.1 // indirect
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9
github.com/go-openapi/analysis v0.19.0 // indirect
github.com/go-openapi/jsonpointer v0.19.0 // indirect
github.com/go-openapi/jsonreference v0.19.0 // indirect
github.com/go-openapi/loads v0.19.0 // indirect
github.com/go-openapi/runtime v0.19.0 // indirect
github.com/go-openapi/spec v0.19.0 // indirect
github.com/haproxytech/models v1.2.4
github.com/haproxytech/models/v2 v2.1.0
github.com/hashicorp/consul v1.7.2
github.com/hashicorp/consul/api v1.4.0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 // indirect
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2
github.com/sirupsen/logrus v1.4.2
Expand Down
81 changes: 63 additions & 18 deletions go.sum

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions haproxy/dataplane/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/haproxytech/models"
"github.com/haproxytech/models/v2"
)

func (c *Dataplane) Backends() ([]models.Backend, error) {
Expand All @@ -14,7 +14,7 @@ func (c *Dataplane) Backends() ([]models.Backend, error) {

var res resT

err := c.makeReq(http.MethodGet, "/v1/services/haproxy/configuration/backends", nil, &res)
err := c.makeReq(http.MethodGet, "/v2/services/haproxy/configuration/backends", nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func (c *Dataplane) Servers(beName string) ([]models.Server, error) {

var res resT

err := c.makeReq(http.MethodGet, fmt.Sprintf("/v1/services/haproxy/configuration/servers?backend=%s", beName), nil, &res)
err := c.makeReq(http.MethodGet, fmt.Sprintf("/v2/services/haproxy/configuration/servers?backend=%s", beName), nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -41,21 +41,21 @@ func (t *tnx) CreateBackend(be models.Backend) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/backends?transaction_id=%s", t.txID), be, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/backends?transaction_id=%s", t.txID), be, nil)
}

func (t *tnx) DeleteBackend(name string) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v1/services/haproxy/configuration/backends/%s?transaction_id=%s", name, t.txID), nil, nil)
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v2/services/haproxy/configuration/backends/%s?transaction_id=%s", name, t.txID), nil, nil)
}

func (t *tnx) CreateServer(beName string, srv models.Server) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/servers?backend=%s&transaction_id=%s", beName, t.txID), srv, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/servers?backend=%s&transaction_id=%s", beName, t.txID), srv, nil)
}

func (t *tnx) ReplaceServer(beName string, srv models.Server) error {
Expand All @@ -71,7 +71,7 @@ func (c *Dataplane) ReplaceServer(beName string, srv models.Server) error {
return err
}

err = c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", srv.Name, beName, v), srv, nil)
err = c.makeReq(http.MethodPut, fmt.Sprintf("/v2/services/haproxy/configuration/servers/%s?backend=%s&version=%d", srv.Name, beName, v), srv, nil)
if err != nil {
return err
}
Expand All @@ -83,5 +83,5 @@ func (t *tnx) DeleteServer(beName string, name string) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&transaction_id=%s", name, beName, t.txID), nil, nil)
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v2/services/haproxy/configuration/servers/%s?backend=%s&transaction_id=%s", name, beName, t.txID), nil, nil)
}
12 changes: 6 additions & 6 deletions haproxy/dataplane/dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"net/http"
"sync"

"github.com/haproxytech/models"
"github.com/haproxytech/models/v2"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (t *tnx) ensureTnx() error {
}

res := models.Transaction{}
err = t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/transactions?version=%d", v), nil, &res)
err = t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/transactions?version=%d", v), nil, &res)
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func (c *Dataplane) ConfigVersion() (int, error) {
var res struct {
Version int `json:"_version"`
}
err := c.makeReq(http.MethodGet, "/v1/services/haproxy/configuration/frontends", nil, &res)
err := c.makeReq(http.MethodGet, "/v2/services/haproxy/configuration/frontends", nil, &res)
if err != nil {
return 0, err
}
Expand All @@ -86,17 +86,17 @@ func (c *Dataplane) ConfigVersion() (int, error) {
}

func (c *Dataplane) Ping() error {
return c.makeReq(http.MethodGet, "/v1/specification", nil, nil)
return c.makeReq(http.MethodGet, "/v2/specification", nil, nil)
}

func (c *Dataplane) Stats() (models.NativeStats, error) {
res := models.NativeStats{}
return res, c.makeReq(http.MethodGet, "/v1/services/haproxy/stats/native", nil, &res)
return res, c.makeReq(http.MethodGet, "/v2/services/haproxy/stats/native", nil, &res)
}

func (t *tnx) Commit() error {
if t.txID != "" {
err := t.client.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/transactions/%s", t.txID), nil, nil)
err := t.client.makeReq(http.MethodPut, fmt.Sprintf("/v2/services/haproxy/transactions/%s", t.txID), nil, nil)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions haproxy/dataplane/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/haproxytech/models"
"github.com/haproxytech/models/v2"
)

func (c *Dataplane) Filters(parentType, parentName string) ([]models.Filter, error) {
Expand All @@ -14,7 +14,7 @@ func (c *Dataplane) Filters(parentType, parentName string) ([]models.Filter, err

var res resT

err := c.makeReq(http.MethodGet, fmt.Sprintf("/v1/services/haproxy/configuration/filters?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
err := c.makeReq(http.MethodGet, fmt.Sprintf("/v2/services/haproxy/configuration/filters?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func (c *Dataplane) TCPRequestRules(parentType, parentName string) ([]models.TCP

var res resT

err := c.makeReq(http.MethodGet, fmt.Sprintf("/v1/services/haproxy/configuration/tcp_request_rules?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
err := c.makeReq(http.MethodGet, fmt.Sprintf("/v2/services/haproxy/configuration/tcp_request_rules?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -41,12 +41,12 @@ func (t *tnx) CreateFilter(parentType, parentName string, filter models.Filter)
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/filters?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), filter, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/filters?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), filter, nil)
}

func (t *tnx) CreateTCPRequestRule(parentType, parentName string, rule models.TCPRequestRule) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/tcp_request_rules?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/tcp_request_rules?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
}
12 changes: 6 additions & 6 deletions haproxy/dataplane/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/haproxytech/models"
"github.com/haproxytech/models/v2"
)

func (c *Dataplane) Frontends() ([]models.Frontend, error) {
Expand All @@ -14,7 +14,7 @@ func (c *Dataplane) Frontends() ([]models.Frontend, error) {

var res resT

err := c.makeReq(http.MethodGet, "/v1/services/haproxy/configuration/frontends", nil, &res)
err := c.makeReq(http.MethodGet, "/v2/services/haproxy/configuration/frontends", nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -29,7 +29,7 @@ func (c *Dataplane) Binds(feName string) ([]models.Bind, error) {

var res resT

err := c.makeReq(http.MethodGet, fmt.Sprintf("/v1/services/haproxy/configuration/binds?frontend=%s", feName), nil, &res)
err := c.makeReq(http.MethodGet, fmt.Sprintf("/v2/services/haproxy/configuration/binds?frontend=%s", feName), nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -41,19 +41,19 @@ func (t *tnx) CreateFrontend(fe models.Frontend) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/frontends?transaction_id=%s", t.txID), fe, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/frontends?transaction_id=%s", t.txID), fe, nil)
}

func (t *tnx) DeleteFrontend(name string) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v1/services/haproxy/configuration/frontends/%s?transaction_id=%s", name, t.txID), nil, nil)
return t.client.makeReq(http.MethodDelete, fmt.Sprintf("/v2/services/haproxy/configuration/frontends/%s?transaction_id=%s", name, t.txID), nil, nil)
}

func (t *tnx) CreateBind(feName string, bind models.Bind) error {
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/binds?frontend=%s&transaction_id=%s", feName, t.txID), bind, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/binds?frontend=%s&transaction_id=%s", feName, t.txID), bind, nil)
}
6 changes: 3 additions & 3 deletions haproxy/dataplane/http_request_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/haproxytech/models"
"github.com/haproxytech/models/v2"
)

func (c *Dataplane) HTTPRequestRules(parentType, parentName string) ([]models.HTTPRequestRule, error) {
Expand All @@ -14,7 +14,7 @@ func (c *Dataplane) HTTPRequestRules(parentType, parentName string) ([]models.HT

var res resT

err := c.makeReq(http.MethodGet, fmt.Sprintf("/v1/services/haproxy/configuration/http_request_rules?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
err := c.makeReq(http.MethodGet, fmt.Sprintf("/v2/services/haproxy/configuration/http_request_rules?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -26,5 +26,5 @@ func (t *tnx) CreateHTTPRequestRule(parentType, parentName string, rule models.H
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/http_request_rules?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/http_request_rules?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
}
6 changes: 3 additions & 3 deletions haproxy/dataplane/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/haproxytech/models"
"github.com/haproxytech/models/v2"
)

func (c *Dataplane) LogTargets(parentType, parentName string) ([]models.LogTarget, error) {
Expand All @@ -14,7 +14,7 @@ func (c *Dataplane) LogTargets(parentType, parentName string) ([]models.LogTarge

var res resT

err := c.makeReq(http.MethodGet, fmt.Sprintf("/v1/services/haproxy/configuration/log_targets?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
err := c.makeReq(http.MethodGet, fmt.Sprintf("/v2/services/haproxy/configuration/log_targets?parent_type=%s&parent_name=%s", parentType, parentName), nil, &res)
if err != nil {
return nil, err
}
Expand All @@ -26,5 +26,5 @@ func (t *tnx) CreateLogTargets(parentType, parentName string, rule models.LogTar
if err := t.ensureTnx(); err != nil {
return err
}
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/log_targets?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v2/services/haproxy/configuration/log_targets?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
}
63 changes: 63 additions & 0 deletions haproxy/dataplanelog/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dataplanelog

import (
"bufio"
"bytes"
"encoding/json"
"io"
"strings"

log "github.com/sirupsen/logrus"
)

func New(r io.Reader) {
scan := bufio.NewScanner(r)
go func() {
for scan.Scan() {
logLine(scan.Bytes())
}
}()
}

func logLine(line []byte) {
// dataplane starts by logging stuff in text before switching to json
if bytes.HasPrefix(line, []byte("time=\"")) {
return
}

f := log.Fields{}
err := json.Unmarshal(line, &f)
if err != nil {
log.Errorf("dataplane: failed to parse log line: %s", string(line))
return
}

msg := f["msg"].(string)
level := f["level"].(string)

delete(f, "msg")
delete(f, "level")
delete(f, "time")

e := log.WithFields(log.Fields(f))
fn := e.Errorf

switch strings.ToLower(level) {
case "panic":
fn = e.Panicf
case "fatal":
fn = e.Fatalf
case "error":
fn = e.Errorf
case "warn", "warning":
fn = e.Warnf
case "info":
fn = e.Infof
case "debug":
fn = e.Debugf
case "trace":
fn = e.Tracef
}

fn("dataplane: %s", msg)
}
23 changes: 4 additions & 19 deletions haproxy/halog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,28 @@ package halog
import (
"bufio"
"io"
"os/exec"
"strings"

log "github.com/sirupsen/logrus"
)

func New(prefix string, r io.Reader) {
func New(r io.Reader) {
scan := bufio.NewScanner(r)
go func() {
for scan.Scan() {
haproxyLog(prefix, scan.Text())
haproxyLog(scan.Text())
}
}()
}

func Cmd(prefix string, cmd *exec.Cmd) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
New(prefix, stdout)
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
New(prefix, stderr)
return nil
}

func haproxyLog(prefix, l string) {
func haproxyLog(l string) {
if len(l) == 0 {
return
}

f := log.Errorf
defer func() {
f("%s: %s", prefix, strings.TrimSpace(l))
f("%s: %s", "haproxy", strings.TrimSpace(l))
}()

if l[0] != '[' {
Expand Down
Loading

0 comments on commit 508acc4

Please sign in to comment.