haproxy

package
v0.0.0-...-f0721d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2019 License: GPL-3.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const (
	UserAgent      string = "^[uU]ser[-.][aA]gent[ ]?([!])?=[ ]?([a-zA-Z0-9]+)$"
	Host           string = "^[hH]ost[ ]?([!])?=[ ]?([a-zA-Z0-9.]+)$"
	CookieContains string = "^[cC]ookie (.*) [Cc]ontains (.*)$"
	HasCookie      string = "^[Hh]as [Cc]ookie (.*)$"
	MissesCookie   string = "^[Mm]isses [Cc]ookie (.*)$"
	HeaderContains string = "^[Hheader] (.*) [Cc]ontains (.*)$"
	HasHeader      string = "^[Hh]as [Hh]eader (.*)$"
	MissesHeader   string = "^[Mm]isses [Hh]eader (.*)$"
)
View Source
const (
	DEFAULT_WEIGHT = 100
)
View Source
const (
	MAX_SOCKET_LENGTH = 103
)
View Source
const (
	SEPARATOR = "::"
)

Variables

This section is empty.

Functions

func BackendName

func BackendName(routeName string, serviceName string) string

func FilterName

func FilterName(routeName string, filterDestination string) string

func FrontendName

func FrontendName(routeName string, serviceName string) string

func RouteName

func RouteName(routeName string, serviceName string) string

func ServerName

func ServerName(routeName string, serviceName string) string

func ServiceName

func ServiceName(routeName string, serviceName string) string

helper function to create a Backend or Frontend name based on a Route and Service

func Validate

func Validate(s interface{}) (bool, error)

simple wrapper function to ease the validation

Types

type Backend

type Backend struct {
	Name      string          `json:"name" binding:"required"`
	Mode      string          `json:"mode" binding:"required"`
	Servers   []*ServerDetail `json:"servers" binding:"required"`
	Options   ProxyOptions    `json:"options"`
	ProxyMode bool            `json:"proxyMode" binding:"required"`
}

Defines a single haproxy "backend".

type Config

type Config struct {
	Frontends     []*Frontend   `json:"frontends" binding:"required"`
	Backends      []*Backend    `json:"backends" binding:"required"`
	Routes        []Route       `json:"routes" binding:"required"`
	PidFile       string        `json:"-"`
	SockFile      string        `json:"-"`
	Mutex         *sync.RWMutex `json:"-"`
	TemplateFile  string        `json:"-"`
	ConfigFile    string        `json:"-"`
	JsonFile      string        `json:"-"`
	WorkingDir    string        `json:"-"`
	ErrorPagesDir string        `json:"-"`
}

Main configuration object for load balancers. This contains all variables and is passed to the templating engine.

func (*Config) AddBackend

func (c *Config) AddBackend(backend *Backend) *Error

adds a frontend

func (*Config) AddFilter

func (c *Config) AddFilter(frontend string, filter *Filter) error

set the filter on a frontend

func (*Config) AddFrontend

func (c *Config) AddFrontend(frontend *Frontend) *Error

adds a frontend

func (*Config) AddRoute

func (c *Config) AddRoute(route Route) *Error

add a route to the configuration

func (*Config) AddRouteServices

func (c *Config) AddRouteServices(routeName string, services []*Service) *Error

func (*Config) AddServer

func (c *Config) AddServer(backendName string, server *ServerDetail) *Error

adds a Server

func (*Config) AddServiceServer

func (c *Config) AddServiceServer(routeName string, serviceName string, server *Server) *Error

func (*Config) BackendExists

func (c *Config) BackendExists(name string) bool

helper function to check if a Backend exists

func (*Config) BackendUsed

func (c *Config) BackendUsed(name string) *Error

Helper function to check if a Backend is used by a Frontend as a default backend or a filter destination

func (*Config) BeginReadTrans

func (c *Config) BeginReadTrans()

func (*Config) BeginWriteTrans

func (c *Config) BeginWriteTrans()

the transactions methods are kept separate so we can chain an arbitrary set of operations on the Config object within one transaction. Alas, this burdons the developer with extra housekeeping but gives you more control over the flow of mutations and reads without risking deadlocks or duplicating locks and unlocks inside of methods.

func (*Config) DeleteBackend

func (c *Config) DeleteBackend(name string) *Error
Deleting a backend is tricky. Frontends have a default backend. Removing that backend and then reloading

the configuration will crash Haproxy. This means some extra protection is put into this method to check if this backend is still used. If not, it can be deleted.

func (*Config) DeleteFilter

func (c *Config) DeleteFilter(frontendName string, filterName string) *Error

delete a Filter from a frontend

func (*Config) DeleteFrontend

func (c *Config) DeleteFrontend(name string) *Error

deletes a frontend

func (*Config) DeleteRoute

func (c *Config) DeleteRoute(name string) *Error

deletes a route, cascading down the structure and remove all underpinning frontends, backends and servers.

func (*Config) DeleteRouteService

func (c *Config) DeleteRouteService(routeName string, serviceName string) *Error

func (*Config) DeleteServer

func (c *Config) DeleteServer(backendName string, serverName string) *Error

func (*Config) DeleteServiceServer

func (c *Config) DeleteServiceServer(routeName string, serviceName string, serverName string) *Error

func (*Config) EndReadTrans

func (c *Config) EndReadTrans()

func (*Config) EndWriteTrans

func (c *Config) EndWriteTrans()

func (*Config) FrontendExists

func (c *Config) FrontendExists(name string) bool

helper function to check if a Frontend exists

func (*Config) GetBackend

func (c *Config) GetBackend(backend string) (*Backend, *Error)

gets a backend

func (*Config) GetBackends

func (c *Config) GetBackends() []*Backend

gets all backends

func (*Config) GetConfigFromDisk

func (c *Config) GetConfigFromDisk() error

Load a config from disk

func (*Config) GetFilters

func (c *Config) GetFilters(frontend string) []*Filter

get the filters from a frontend

func (*Config) GetFrontend

func (c *Config) GetFrontend(name string) (*Frontend, *Error)

gets a frontend

func (*Config) GetFrontends

func (c *Config) GetFrontends() []*Frontend

gets all frontends

func (*Config) GetRoute

func (c *Config) GetRoute(name string) (Route, *Error)

gets a route

func (*Config) GetRouteService

func (c *Config) GetRouteService(routeName string, serviceName string) (*Service, *Error)

func (*Config) GetRouteServices

func (c *Config) GetRouteServices(name string) ([]*Service, *Error)

func (*Config) GetRoutes

func (c *Config) GetRoutes() []Route

gets all routes

func (*Config) GetServer

func (c *Config) GetServer(backendName string, serverName string) (*ServerDetail, *Error)

func (*Config) GetServers

func (c *Config) GetServers(backendName string) ([]*ServerDetail, *Error)

gets all servers of a specific backend

func (*Config) GetServiceServer

func (c *Config) GetServiceServer(routeName string, serviceName string, serverName string) (*Server, *Error)

func (*Config) GetServiceServers

func (c *Config) GetServiceServers(routeName string, serviceName string) ([]*Server, *Error)

func (*Config) InitializeConfig

func (c *Config) InitializeConfig()

func (*Config) Persist

func (c *Config) Persist() error

save the JSON config to disk

func (*Config) Render

func (c *Config) Render() error

Render a config object to a HAproxy config file

func (*Config) RenderAndPersist

func (c *Config) RenderAndPersist() error

func (*Config) RouteExists

func (c *Config) RouteExists(name string) bool

helper function to check if a Route exists

func (*Config) ServerExists

func (c *Config) ServerExists(routeName string, serviceName string, serverName string) bool

helper function to check if a Server exists in a specific Service

func (*Config) ServiceExists

func (c *Config) ServiceExists(routeName string, serviceName string) bool

helper function to check if a Service exists

func (*Config) SetWeight

func (c *Config) SetWeight(backend string, server string, weight int) *Error

updates the weight of a server of a specific backend with a new weight

func (*Config) UpdateConfig

func (c *Config) UpdateConfig(config *Config) *Error

updates the whole config in one go

func (*Config) UpdateRoute

func (c *Config) UpdateRoute(name string, route *Route) *Error

just a convenience functions for a delete and a create

func (*Config) UpdateRouteService

func (c *Config) UpdateRouteService(routeName string, serviceName string, service *Service) *Error

just a convenience functions for a delete and a create

func (*Config) UpdateRouteServices

func (c *Config) UpdateRouteServices(routeName string, services []*Service) *Error

func (*Config) UpdateServiceServer

func (c *Config) UpdateServiceServer(routeName string, serviceName string, serverName string, server *Server) *Error

just a convenience functions for a delete and a create

type Error

type Error struct {
	Code int
	Err  error
}

custom error that allows us to define the HTTP return code that should be used in different error situations

func (*Error) Error

func (e *Error) Error() string

type Filter

type Filter struct {
	Name        string `json:"name" binding:"required" valid:"filterName"`
	Condition   string `json:"condition" binding:"required"`
	Destination string `json:"destination" binding:"required"`
	Negate      bool   `json:"negate,omitempty"`
}

type Frontend

type Frontend struct {
	Name           string       `json:"name" binding:"required"`
	Mode           string       `json:"mode" binding:"required"`
	BindPort       int          `json:"bindPort"`
	BindIp         string       `json:"bindIp"`
	UnixSock       string       `json:"unixSock"`
	SockProtocol   string       `json:"sockProtocol"`
	Options        ProxyOptions `json:"options"`
	DefaultBackend string       `json:"defaultBackend" binding:"required"`
	Filters        []*Filter    `json:"filters,omitempty"`
	HttpQuota      Quota        `json:"httpQuota,omitempty"`
	TcpQuota       Quota        `json:"tcpQuota,omitempty"`
}

Defines a single haproxy "frontend".

type Info

type Info struct {
	Name                        string `json:"Name"`
	Version                     string `json:"Version"`
	Release_date                string `json:"Release_date"`
	Nbproc                      string `json:"Nbproc"`
	Process_num                 string `json:"Process_num"`
	Pid                         string `json:"Pid"`
	Uptime                      string `json:"Uptime"`
	Uptime_sec                  string `json:"Uptime_sec"`
	Memmax_MB                   string `json:"Memmax_MB"`
	Ulimitn                     string `json:"Ulimit-n"`
	Maxsock                     string `json:"Maxsock"`
	Maxconn                     string `json:"Maxconn"`
	Hard_maxconn                string `json:"Hard_maxconn"`
	CurrConns                   string `json:"CurrConns"`
	CumConns                    string `json:"CumConns"`
	CumReq                      string `json:"CumReq"`
	MaxSslConns                 string `json:"MaxSslConns"`
	CurrSslConns                string `json:"CurrSslConns"`
	CumSslConns                 string `json:"CumSslConns"`
	Maxpipes                    string `json:"Maxpipes"`
	PipesUsed                   string `json:"PipesUsed"`
	PipesFree                   string `json:"PipesFree"`
	ConnRate                    string `json:"ConnRate"`
	ConnRateLimit               string `json:"ConnRateLimit"`
	MaxConnRate                 string `json:"MaxConnRate"`
	SessRate                    string `json:"SessRate"`
	SessRateLimit               string `json:"SessRateLimit"`
	MaxSessRate                 string `json:"MaxSessRate"`
	SslRate                     string `json:"SslRate"`
	SslRateLimit                string `json:"SslRateLimit"`
	MaxSslRate                  string `json:"MaxSslRate"`
	SslFrontendKeyRate          string `json:"SslFrontendKeyRate"`
	SslFrontendMaxKeyRate       string `json:"SslFrontendMaxKeyRate"`
	SslFrontendSessionReuse_pct string `json:"SslFrontendSessionReuse_pct"`
	SslBackendKeyRate           string `json:"SslBackendKeyRate"`
	SslBackendMaxKeyRate        string `json:"SslBackendMaxKeyRate"`
	SslCacheLookups             string `json:"SslCacheLookups"`
	SslCacheMisses              string `json:"SslCacheMisses"`
	CompressBpsIn               string `json:"CompressBpsIn"`
	CompressBpsOut              string `json:"CompressBpsOut"`
	CompressBpsRateLim          string `json:"CompressBpsRateLim"`
	ZlibMemUsage                string `json:"ZlibMemUsage"`
	MaxZlibMemUsage             string `json:"MaxZlibMemUsage"`
	Tasks                       string `json:"Tasks"`
	Run_queue                   string `json:"Run_queue"`
	Idle_pct                    string `json:"Idle_pct"`
	// contains filtered or unexported fields
}

struct to hold the output from the /info endpoint

type ProxyOptions

type ProxyOptions struct {
	AbortOnClose    bool `json:"abortOnClose"`
	AllBackups      bool `json:"allBackups"`
	CheckCache      bool `json:"checkCache"`
	ForwardFor      bool `json:"forwardFor"`
	HttpClose       bool `json:"httpClose"`
	HttpCheck       bool `json:"httpCheck"`
	SslHelloCheck   bool `json:"sslHelloCheck"`
	TcpKeepAlive    bool `json:"tcpKeepAlive"`
	TcpLog          bool `json:"tcpLog"`
	TcpSmartAccept  bool `json:"tcpSmartAccept"`
	TcpSmartConnect bool `json:"tcpSmartConnect"`
}

type Quota

type Quota struct {
	SampleWindow string `json:"sampleWindow,omitempty" binding:"required"`
	Rate         int    `json:"rate,omitempty" binding:"required"`
	ExpiryTime   string `json:"expiryTime,omitempty" binding:"required"`
}

type Route

type Route struct {
	Name      string     `json:"name" binding:"required" valid:"routeName"`
	Port      int        `json:"port" binding:"required"`
	Protocol  string     `json:"protocol" binding:"required"`
	HttpQuota Quota      `json:"httpQuota"`
	TcpQuota  Quota      `json:"tcpQuota"`
	Filters   []*Filter  `json:"filters"`
	Services  []*Service `json:"services"`
}

A Route is structured set of Haproxy frontends, backends and servers. The Route provides a convenient and higher level way of creating and managing this structure. You could create this structure by hand with separate API calls, but this is faster and easier in 9 out of 10 cases.

The structure of a route is as follows:

                          -> [srv a] -> sock -> [fe a: be a] -> [*srv] -> host:port
                        /
->[fe (fltr)(qts) : be]-
                        \
                          -> [srv b] -> sock -> [fe b: be b] -> [*srv] -> host:port

fe = frontend
be = backend
srv = server
fltr = filter
qts = quotas

The above example has two services, a and b, but a route can have many services. The start of the route (the first frontend) has filters and quotas that influence the way traffic flows in a route, i.e. to which services the traffic goes.

All items in a route map to actual Haproxy types from the vamp-loadbalancer/haproxy package.

type Runtime

type Runtime struct {
	Binary   string
	SockFile string
}

func (*Runtime) GetInfo

func (r *Runtime) GetInfo() (Info, *Error)

Gets basic info on haproxy process

func (*Runtime) GetJsonStats

func (r *Runtime) GetJsonStats(statsType string) ([]Stats, error)

func (*Runtime) GetStats

func (r *Runtime) GetStats(statsType string) (map[string]map[string]string, error)

func (*Runtime) Reload

func (r *Runtime) Reload(c *Config) error

Reload runtime with configuration

func (*Runtime) Reset

func (r *Runtime) Reset() *Error

func (*Runtime) SetPid

func (r *Runtime) SetPid(pidfile string) error

returns an error if the file was already there

func (*Runtime) SetWeight

func (r *Runtime) SetWeight(backend string, server string, weight int) (string, error)

Sets the weight of a backend

type Server

type Server struct {
	Name string `json:"name" binding:"required"`
	Host string `json:"host" binding:"required"`
	Port int    `json:"port" binding:"required"`
}

type ServerDetail

type ServerDetail struct {
	Name          string `json:"name" binding:"required"`
	Host          string `json:"host" binding:"required"`
	Port          int    `json:"port" binding:"required"`
	UnixSock      string `json:"unixSock" valid:"socketPath"`
	Weight        int    `json:"weight" binding:"required"`
	MaxConn       int    `json:"maxconn"`
	Check         bool   `json:"check"`
	CheckInterval int    `json:"checkInterval"`
}

type Service

type Service struct {
	Name    string    `json:"name" binding:"required"`
	Weight  int       `json:"weight" binding:"required"`
	Servers []*Server `json:"servers"`
}

type Stats

type Stats struct {
	Pxname   string `json:"pxname"`
	Svname   string `json:"svname"`
	Qcur     string `json:"qcur"`
	Qmax     string `json:"qmax"`
	Scur     string `json:"scur"`
	Smax     string `json:"smax"`
	Slim     string `json:"slim"`
	Stot     string `json:"stot"`
	Bin      string `json:"bin"`
	Bout     string `json:"bout"`
	Dreq     string `json:"dreq"`
	Dresp    string `json:"dresp"`
	Ereq     string `json:"ereq"`
	Econ     string `json:"econ"`
	Eresp    string `json:"eresp"`
	Wretr    string `json:"wretr"`
	Wredis   string `json:"wredis"`
	Status   string `json:"status"`
	Weight   string `json:"weight"`
	Act      string `json:"act"`
	Bck      string `json:"bck"`
	Chkfail  string `json:"chkfail"`
	Chkdown  string `json:"chkdown"`
	Lastchg  string `json:"lastchg"`
	Downtime string `json:"downtime"`
	Qlimit   string `json:"qlimit"`
	Pid      string `json:"pid"`
	Iid      string `json:"iid"`
	Sid      string `json:"sid"`
	Throttle string `json:"throttle"`
	Lbtot    string `json:"lbtot"`
	Tracked  string `json:"tracked"`

	Rate           string `json:"rate"`
	Rate_lim       string `json:"rate_lim"`
	Rate_max       string `json:"rate_max"`
	Check_status   string `json:"check_status"`
	Check_code     string `json:"check_code"`
	Check_duration string `json:"check_duration"`
	Hrsp_1xx       string `json:"hrsp_1xx"`
	Hrsp_2xx       string `json:"hrsp_2xx"`
	Hrsp_3xx       string `json:"hrsp_3xx"`
	Hrsp_4xx       string `json:"hrsp_4xx"`
	Hrsp_5xx       string `json:"hrsp_5xx"`
	Hrsp_other     string `json:"hrsp_other"`
	Hanafail       string `json:"hanafail"`
	Req_rate       string `json:"req_rate"`
	Req_rate_max   string `json:"req_rate_max"`
	Req_tot        string `json:"req_tot"`
	Cli_abrt       string `json:"cli_abrt"`
	Srv_abrt       string `json:"srv_abrt"`
	Comp_in        string `json:"comp_in"`
	Comp_out       string `json:"comp_out"`
	Comp_byp       string `json:"comp_byp"`
	Comp_rsp       string `json:"comp_rsp"`
	Lastsess       string `json:"lastsess"`
	Last_chk       string `json:"last_chk"`
	Last_agt       string `json:"last_agt"`
	Qtime          string `json:"qtime"`
	Ctime          string `json:"ctime"`
	Rtime          string `json:"rtime"`
	Ttime          string `json:"ttime"`
	// contains filtered or unexported fields
}

Struct to hold the output from the /stats endpoint

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL