Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ This branch contains the compatibility runtime used by PastureStack Server to op

## Runtime image

The maintained runtime release for PastureStack HAProxy load-balancer services is `v0.9.37`. It uses HAProxy 3.5.0 with AWS-LC 5.15.0 and Lua 5.4.8.
The maintained runtime release for PastureStack HAProxy load-balancer services is `v0.9.38`. It uses HAProxy 3.5.0 with AWS-LC 5.15.0 and Lua 5.4.8.

```text
ghcr.io/pasturestack/load-balancer-service:v0.9.37
ghcr.io/pasturestack/load-balancer-service:v0.9.38
```

The image is intended to be managed by PastureStack Server, not started by itself:
the control plane supplies metadata, API credentials, service links, port rules,
and health-check configuration. Selecting this release does not itself upgrade
an existing Server installation or its load-balancer services.

The [release page](https://github.com/PastureStack/load-balancer-controller/releases/tag/v0.9.37)
The [release page](https://github.com/PastureStack/load-balancer-controller/releases/tag/v0.9.38)
provides the manifest digest, controller archive, SBOMs, checksums, and build
provenance. HAProxy links AWS-LC statically. Runtime tools such as curl
use Alpine's OpenSSL packages.
Expand Down Expand Up @@ -48,6 +48,18 @@ It also accepts the generic `CATTLE_ACCESS_KEY` and `CATTLE_SECRET_KEY` pair use

The HAProxy logging helper keeps its configuration under the standard `/etc/rsyslog.d` path and does not require a daemon PID file. Logging initialization is validated independently before HAProxy configuration is applied.

### Certificate renewal

For API-managed certificates, the controller checks only certificates referenced
by its load balancer every 30 seconds (or the configured certificate polling
interval). Changes to the certificate, private key, or chain schedule the normal
configuration apply queue, without requiring a service edit or restart.
Failed, incomplete, or ambiguous reads retain the running configuration and are
retried; failed configuration application uses the existing retry queue.
Certificate material stays in memory and is not logged. Mounted certificate
directories retain their existing polling behavior. The watcher stops with the
controller.

### Drain weight recovery

The controller records each backend's runtime weight before setting it to zero
Expand Down
124 changes: 102 additions & 22 deletions controller/rancher/certificate_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
type CertificateFetcher interface {
FetchCertificates(lbMeta *LBMetadata, isDefaultCert bool) ([]*config.Certificate, error)
UpdateEndpoints(lbSvc *metadata.Service, eps []client.PublicEndpoint) error
LookForCertUpdates(do func(string))
LookForCertUpdates(do func(string), stop <-chan struct{})
}

type RCertificateFetcher struct {
Expand All @@ -46,6 +46,9 @@ type RCertificateFetcher struct {

initPollDone bool
initPollMu *sync.RWMutex

apiCertMu sync.Mutex
apiCerts map[string]*config.Certificate
}

func (fetcher *RCertificateFetcher) checkIfInitPollDone() bool {
Expand Down Expand Up @@ -95,6 +98,7 @@ func (fetcher *RCertificateFetcher) FetchCertificates(lbMeta *LBMetadata, isDefa
}
}
} else {
fetcher.trackRancherCertificateNames(lbMeta)
if !isDefaultCert {
for _, certName := range lbMeta.Certs {
cert, err := fetcher.FetchRancherCertificate(certName)
Expand All @@ -121,6 +125,19 @@ func (fetcher *RCertificateFetcher) FetchCertificates(lbMeta *LBMetadata, isDefa
}

func (fetcher *RCertificateFetcher) FetchRancherCertificate(certName string) (*config.Certificate, error) {
cert, err := fetcher.fetchRancherCertificate(certName)
if err == nil && cert != nil {
fetcher.apiCertMu.Lock()
if _, tracked := fetcher.apiCerts[certName]; tracked {
copy := *cert
fetcher.apiCerts[certName] = &copy
}
fetcher.apiCertMu.Unlock()
}
return cert, err
}

func (fetcher *RCertificateFetcher) fetchRancherCertificate(certName string) (*config.Certificate, error) {
if certName == "" {
return nil, nil
}
Expand All @@ -132,19 +149,58 @@ func (fetcher *RCertificateFetcher) FetchRancherCertificate(certName string) (*c
if err != nil {
return nil, fmt.Errorf("Coudln't get certificate by name [%s]. Error: %#v", certName, err)
}
var cert client.Certificate
var certWithChain string
if len(certs.Data) >= 1 {
cert = certs.Data[0]
certWithChain = fmt.Sprintf("%s\n%s", cert.Cert, cert.CertChain)
if len(certs.Data) != 1 {
return nil, fmt.Errorf("expected one active certificate named %q", certName)
}
cert := certs.Data[0]
if cert.Name != certName || strings.TrimSpace(cert.Cert) == "" || strings.TrimSpace(cert.Key) == "" {
return nil, fmt.Errorf("certificate %q is incomplete", certName)
}
certWithChain := fmt.Sprintf("%s\n%s", cert.Cert, cert.CertChain)
return &config.Certificate{
Name: cert.Name,
Key: cert.Key,
Cert: certWithChain,
}, nil
}

// Track only the certificates referenced by this LB, never the environment's
// complete secret inventory. Values stay in memory and are never logged.
func (fetcher *RCertificateFetcher) trackRancherCertificateNames(meta *LBMetadata) {
fetcher.apiCertMu.Lock()
defer fetcher.apiCertMu.Unlock()
next := make(map[string]*config.Certificate)
names := append([]string{meta.DefaultCert}, meta.Certs...)
for _, name := range names {
if name != "" {
next[name] = fetcher.apiCerts[name]
}
}
fetcher.apiCerts = next
}

// A failed/incomplete read cannot replace a usable certificate or acknowledge a
// change. FetchCertificates refreshes the baseline on the normal apply path.
func (fetcher *RCertificateFetcher) pollRancherCertificates(read func(string) (*config.Certificate, error)) bool {
fetcher.apiCertMu.Lock()
previous := make(map[string]config.Certificate)
for name, cert := range fetcher.apiCerts {
if cert != nil {
previous[name] = *cert
}
}
fetcher.apiCertMu.Unlock()
changed := false
for name, before := range previous {
after, err := read(name)
if err != nil || after == nil || after.Name != name || after.Key == "" || after.Cert == "" {
return false
}
changed = changed || before != *after
}
return changed
}

func (fetcher *RCertificateFetcher) UpdateEndpoints(lbSvc *metadata.Service, eps []client.PublicEndpoint) error {
opts := client.NewListOpts()
opts.Filters["uuid"] = lbSvc.UUID
Expand Down Expand Up @@ -206,26 +262,50 @@ func (fetcher *RCertificateFetcher) ReadDefaultCertificate(defaultCertDir string
return currentDefCert
}

func (fetcher *RCertificateFetcher) LookForCertUpdates(doOnUpdate func(string)) {
if fetcher.CertDir != "" || fetcher.DefaultCertDir != "" {
lastUpdated := time.Now()
for {
log.Debugf("Start --- LookForCertUpdates polling cert dir %v and default cert dir %v", fetcher.CertDir, fetcher.DefaultCertDir)
log.Debugf("lastUpdated %v", lastUpdated)
func (fetcher *RCertificateFetcher) LookForCertUpdates(doOnUpdate func(string), stop <-chan struct{}) {
interval := fetcher.updateCheckInterval
if interval <= 0 {
interval = 30
}
ticker := time.NewTicker(time.Duration(interval) * time.Second)
defer ticker.Stop()
lastUpdated := time.Now()
for {
select {
case <-stop:
return
default:
}
log.Debugf("Start --- LookForCertUpdates polling cert dir %v and default cert dir %v", fetcher.CertDir, fetcher.DefaultCertDir)
log.Debugf("lastUpdated %v", lastUpdated)

forceUpdate := time.Since(lastUpdated).Seconds() >= fetcher.forceUpdateInterval
if forceUpdate {
log.Infof("LookForCertUpdates: Executing force update as certs in cache have not been updated in: %v seconds", fetcher.forceUpdateInterval)
}
forceUpdate := time.Since(lastUpdated).Seconds() >= fetcher.forceUpdateInterval
if forceUpdate {
log.Infof("LookForCertUpdates: Executing force update as certs in cache have not been updated in: %v seconds", fetcher.forceUpdateInterval)
}

if fetcher.pollCertificateDirectories(forceUpdate) {
//scheduleApplyConfig
doOnUpdate("")
lastUpdated = time.Now()
changed := false
if fetcher.CertDir != "" || fetcher.DefaultCertDir != "" {
changed = fetcher.pollCertificateDirectories(forceUpdate)
} else {
changed = fetcher.pollRancherCertificates(fetcher.fetchRancherCertificate)
}
if changed {
select {
case <-stop:
return
default:
}
//scheduleApplyConfig
doOnUpdate("")
lastUpdated = time.Now()
}

log.Debug("Done --- LookForCertUpdates poll")
time.Sleep(time.Duration(fetcher.updateCheckInterval) * time.Second)
log.Debug("Done --- LookForCertUpdates poll")
select {
case <-stop:
return
case <-ticker.C:
}
}
}
Expand Down
Loading