server add gives another machine this llmgw, a certificate, the configuration and store address, lets it into the store through Patroni, and starts it; --vip keeps a floating address on the leader with keepalived, following a new /leader endpoint. Nodes learn every control plane from the leader and notice a dead one by HTTP/2 ping instead of after the kernel's fifteen minutes. The setup, console, node and init screens are drawn full screen in one frame: title bar, sidebar, bordered panel, key bar, dialogs. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
92 lines
2.7 KiB
Go
92 lines
2.7 KiB
Go
package pgha
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"slices"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// ClientLine is the access rule that lets a control plane at ip in.
|
|
func ClientLine(ip string) string {
|
|
return fmt.Sprintf("host %s %s %s/32 scram-sha-256", Database, Role, ip)
|
|
}
|
|
|
|
// AllowClient lets a control plane at ip into the cluster's database: one
|
|
// change to the cluster's shared configuration through Patroni's API, which
|
|
// every member applies. It says whether anything changed.
|
|
//
|
|
// A cluster set up before the rules moved into the shared configuration
|
|
// keeps them in each member's own file, which this cannot reach; that is
|
|
// said, with what to add, rather than half done.
|
|
func AllowClient(ctx context.Context, hosts []string, ip string) (bool, error) {
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
var lastErr error
|
|
for _, h := range hosts {
|
|
base := "http://" + net.JoinHostPort(h, strconv.Itoa(restPort))
|
|
cfg, err := getConfig(ctx, client, base)
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
var hba []string
|
|
if pg, ok := cfg["postgresql"].(map[string]any); ok {
|
|
if list, ok := pg["pg_hba"].([]any); ok {
|
|
for _, l := range list {
|
|
if s, ok := l.(string); ok {
|
|
hba = append(hba, s)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(hba) == 0 {
|
|
return false, fmt.Errorf("this cluster keeps its access rules in each member's "+
|
|
"/etc/patroni/config.yml, not in its shared configuration: add %q to pg_hba there "+
|
|
"on every member, and run `patronictl reload llmgw`", ClientLine(ip))
|
|
}
|
|
line := ClientLine(ip)
|
|
if slices.Contains(hba, line) {
|
|
return false, nil
|
|
}
|
|
body, _ := json.Marshal(map[string]any{"postgresql": map[string]any{"pg_hba": append(hba, line)}})
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, base+"/config", bytes.NewReader(body))
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
lastErr = err
|
|
continue
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return false, fmt.Errorf("Patroni on %s refused the change: %s", h, resp.Status)
|
|
}
|
|
return true, nil
|
|
}
|
|
return false, fmt.Errorf("no member's Patroni answered: %w", lastErr)
|
|
}
|
|
|
|
func getConfig(ctx context.Context, client *http.Client, base string) (map[string]any, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, base+"/config", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
var cfg map[string]any
|
|
if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil {
|
|
return nil, fmt.Errorf("%s/config: %w", base, err)
|
|
}
|
|
return cfg, nil
|
|
}
|