Commit 3d491ec8 authored by Geoff Simmons's avatar Geoff Simmons

haproxy controller checks the current offloader config before updates.

Most importantly, this tells us the current version number in use
by the dataplane API, which apparently insists that the number is
always counted up (otherwise there are 409 Conflict responses).
parent a0ca133c
...@@ -30,6 +30,7 @@ package haproxy ...@@ -30,6 +30,7 @@ package haproxy
import ( import (
"bytes" "bytes"
"encoding/json"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -563,3 +564,55 @@ func (client *DataplaneClient) Reloaded(id string) (bool, ReloadState, error) { ...@@ -563,3 +564,55 @@ func (client *DataplaneClient) Reloaded(id string) (bool, ReloadState, error) {
return false, state, dplaneErr return false, state, dplaneErr
} }
} }
type sitesBody struct {
version int `json:"_version,omitempty"`
sites models.Sites `json:"data"`
}
func (client *DataplaneClient) OffldrStatus() (
loaded bool, version int, err error) {
req, err := client.getReq(sitesPath, http.MethodGet, nil, false)
if err != nil {
return
}
resp, err := client.client.Do(req)
if err != nil {
return
}
body, err := getBody(resp)
if err != nil {
return
}
if verStr := resp.Header.Get(versionHdr); verStr != "" {
if version, err = strconv.Atoi(verStr); err != nil {
return
}
}
if resp.StatusCode != http.StatusOK {
err = getError(resp, body)
return
}
var sb sitesBody
if err = json.Unmarshal(body, &sb); err != nil {
return
}
if err = sb.sites.Validate(fmts); err != nil {
return
}
if version == 0 {
version = sb.version
}
// XXX what if version from body & the header don't match?
for _, site := range []*models.Site(sb.sites) {
if site.Name == frontend {
loaded = true
break
}
}
return
}
...@@ -286,6 +286,8 @@ func (hc *Controller) updateInstance(inst *haproxyInst, spec *Spec) error { ...@@ -286,6 +286,8 @@ func (hc *Controller) updateInstance(inst *haproxyInst, spec *Spec) error {
} }
hc.log.Infof("Offloader instance %s: TLS config %s accepted for load", hc.log.Infof("Offloader instance %s: TLS config %s accepted for load",
inst.name, spec) inst.name, spec)
hc.log.Debugf("Offloader instance %s reload state: %+v", inst.name,
state)
inst.status.version = tx.Version inst.status.version = tx.Version
inst.status.dplaneState = state inst.status.dplaneState = state
inst.spec = &Spec{ inst.spec = &Spec{
...@@ -364,6 +366,12 @@ func (hc *Controller) updateOffldSvc(svcKey string) error { ...@@ -364,6 +366,12 @@ func (hc *Controller) updateOffldSvc(svcKey string) error {
continue continue
} }
// XXX metrics // XXX metrics
hc.log.Debugf("offloader svc %s: get current status", inst.name)
if err := hc.getOffldStatus(*inst); err != nil {
offldrErr := inst.mkError(err)
errs = append(errs, offldrErr)
continue
}
if err := hc.updateInstance(inst, svc.spec); err != nil { if err := hc.updateInstance(inst, svc.spec); err != nil {
offldrErr := inst.mkError(err) offldrErr := inst.mkError(err)
errs = append(errs, offldrErr) errs = append(errs, offldrErr)
...@@ -562,6 +570,29 @@ func (hc *Controller) updateOffldrAddrs(key string, addrs []OffldAddr, ...@@ -562,6 +570,29 @@ func (hc *Controller) updateOffldrAddrs(key string, addrs []OffldAddr,
return errs return errs
} }
func (hc *Controller) getOffldStatus(inst haproxyInst) error {
hc.log.Debugf("Offloader instance %s, checking offloader config",
inst.name)
inst.admMtx.Lock()
defer inst.admMtx.Unlock()
hc.wg.Add(1)
defer hc.wg.Done()
loaded, version, err := inst.dplane.OffldrStatus()
if err != nil {
return err
}
inst.status.version = int64(version)
if loaded {
hc.log.Infof("Offloader instance %s: offloader configured, "+
"version=%d", inst.name, version)
} else {
hc.log.Infof("Offloader instance %s: offloader not configured,"+
" current version=%d", inst.name, version)
}
return nil
}
// AddOrUpdateVarnishSvc(key string, addrs []vcl.Address, secrName string, // AddOrUpdateVarnishSvc(key string, addrs []vcl.Address, secrName string,
// loadVCL bool) error // loadVCL bool) error
// AddOrUpdateVarnishSvc causes a sync for the Varnish Service // AddOrUpdateVarnishSvc causes a sync for the Varnish Service
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment