Skip to content

Commit 5a4bad4

Browse files
committed
Allow drop slots when it gets deleted from the manifest
1 parent 3e148ea commit 5a4bad4

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

pkg/cluster/sync.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,45 @@ func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration(pod *v1.Pod, effectiv
541541
configPatched := false
542542
requiresMasterRestart := false
543543

544+
for slotName, _ := range effectivePatroniConfig.Slots {
545+
if _, exists := desiredPatroniConfig.Slots[slotName]; exists {
546+
continue
547+
}
548+
549+
configToRewrite := make(map[string]interface{})
550+
configToRewrite["loop_wait"] = effectivePatroniConfig.LoopWait
551+
configToRewrite["maximum_lag_on_failover"] = effectivePatroniConfig.MaximumLagOnFailover
552+
configToRewrite["pg_hba"] = effectivePatroniConfig.PgHba
553+
configToRewrite["retry_timeout"] = effectivePatroniConfig.RetryTimeout
554+
configToRewrite["synchronous_mode"] = effectivePatroniConfig.SynchronousMode
555+
configToRewrite["synchronous_mode_strict"] = effectivePatroniConfig.SynchronousModeStrict
556+
configToRewrite["ttl"] = effectivePatroniConfig.TTL
557+
configToRewrite["postgresql"] = map[string]interface{}{constants.PatroniPGParametersParameterName: effectivePgParameters}
558+
559+
slotsToRewrite := make(map[string]map[string]string)
560+
for slotName, desiredSlot := range desiredPatroniConfig.Slots {
561+
slotsToRewrite[slotName] = desiredSlot
562+
}
563+
564+
if len(slotsToRewrite) > 0 {
565+
configToRewrite["slots"] = slotsToRewrite
566+
}
567+
568+
configToRewriteJson, err := json.Marshal(configToRewrite)
569+
if err != nil {
570+
c.logger.Debugf("could not convert config rewrite to JSON: %v", err)
571+
}
572+
573+
podName := util.NameFromMeta(pod.ObjectMeta)
574+
c.logger.Debugf("rewrite Postgres config via Patroni API on pod %s with following options: %s",
575+
podName, configToRewriteJson)
576+
if err = c.patroni.RewriteConfig(pod, configToRewrite); err != nil {
577+
return configPatched, requiresMasterRestart, fmt.Errorf("could not rewrite postgres parameters within pod %s: %v", podName, err)
578+
}
579+
580+
break
581+
}
582+
544583
// compare effective and desired Patroni config options
545584
if desiredPatroniConfig.LoopWait > 0 && desiredPatroniConfig.LoopWait != effectivePatroniConfig.LoopWait {
546585
configToSet["loop_wait"] = desiredPatroniConfig.LoopWait

pkg/util/patroni/patroni.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Interface interface {
3838
Restart(server *v1.Pod) error
3939
GetConfig(server *v1.Pod) (acidv1.Patroni, map[string]string, error)
4040
SetConfig(server *v1.Pod, config map[string]interface{}) error
41+
RewriteConfig(server *v1.Pod, config map[string]interface{}) error
4142
}
4243

4344
// Patroni API client
@@ -113,6 +114,42 @@ func (p *Patroni) httpPostOrPatch(method string, url string, body *bytes.Buffer)
113114
return nil
114115
}
115116

117+
func (p *Patroni) httpPut(method string, url string, body *bytes.Buffer) (err error) {
118+
request, err := http.NewRequest(method, url, body)
119+
if err != nil {
120+
return fmt.Errorf("could not create request: %v", err)
121+
}
122+
123+
if p.logger != nil {
124+
p.logger.Debugf("making %s http request: %s", method, request.URL.String())
125+
}
126+
127+
resp, err := p.httpClient.Do(request)
128+
if err != nil {
129+
return fmt.Errorf("could not make request: %v", err)
130+
}
131+
defer func() {
132+
if err2 := resp.Body.Close(); err2 != nil {
133+
if err != nil {
134+
err = fmt.Errorf("could not close request: %v, prior error: %v", err2, err)
135+
} else {
136+
err = fmt.Errorf("could not close request: %v", err2)
137+
}
138+
return
139+
}
140+
}()
141+
142+
if resp.StatusCode != http.StatusOK {
143+
bodyBytes, err := ioutil.ReadAll(resp.Body)
144+
if err != nil {
145+
return fmt.Errorf("could not read response: %v", err)
146+
}
147+
148+
return fmt.Errorf("patroni returned '%s'", string(bodyBytes))
149+
}
150+
return nil
151+
}
152+
116153
func (p *Patroni) httpGet(url string) (string, error) {
117154
p.logger.Debugf("making GET http request: %s", url)
118155

@@ -178,6 +215,19 @@ func (p *Patroni) SetConfig(server *v1.Pod, config map[string]interface{}) error
178215
return p.httpPostOrPatch(http.MethodPatch, apiURLString+configPath, buf)
179216
}
180217

218+
func (p *Patroni) RewriteConfig(server *v1.Pod, config map[string]interface{}) error {
219+
buf := &bytes.Buffer{}
220+
err := json.NewEncoder(buf).Encode(config)
221+
if err != nil {
222+
return fmt.Errorf("could not encode json: %v", err)
223+
}
224+
apiURLString, err := apiURL(server)
225+
if err != nil {
226+
return err
227+
}
228+
return p.httpPut(http.MethodPut, apiURLString+configPath, buf)
229+
}
230+
181231
// ClusterMembers array of cluster members from Patroni API
182232
type ClusterMembers struct {
183233
Members []ClusterMember `json:"members"`

0 commit comments

Comments
 (0)