Skip to content

Commit 2930b09

Browse files
committed
remove slot using patch
1 parent d3cae5f commit 2930b09

File tree

2 files changed

+10
-90
lines changed

2 files changed

+10
-90
lines changed

pkg/cluster/sync.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -545,45 +545,6 @@ func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration(pod *v1.Pod, effectiv
545545
configPatched := false
546546
requiresMasterRestart := false
547547

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

571+
slotsToSet := make(map[string]interface{})
610572
// check if specified slots exist in config and if they differ
611-
slotsToSet := make(map[string]map[string]string)
612573
for slotName, desiredSlot := range desiredPatroniConfig.Slots {
613574
if effectiveSlot, exists := effectivePatroniConfig.Slots[slotName]; exists {
614575
if reflect.DeepEqual(desiredSlot, effectiveSlot) {
@@ -617,6 +578,15 @@ func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration(pod *v1.Pod, effectiv
617578
}
618579
slotsToSet[slotName] = desiredSlot
619580
}
581+
// check if there is any slot deletion
582+
for slotName, effectiveSlot := range effectivePatroniConfig.Slots {
583+
if desiredSlot, exists := desiredPatroniConfig.Slots[slotName]; exists {
584+
if reflect.DeepEqual(effectiveSlot, desiredSlot) {
585+
continue
586+
}
587+
}
588+
slotsToSet[slotName] = nil
589+
}
620590
if len(slotsToSet) > 0 {
621591
configToSet["slots"] = slotsToSet
622592
}

pkg/util/patroni/patroni.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ 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
4241
}
4342

4443
// Patroni API client
@@ -114,42 +113,6 @@ func (p *Patroni) httpPostOrPatch(method string, url string, body *bytes.Buffer)
114113
return nil
115114
}
116115

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-
153116
func (p *Patroni) httpGet(url string) (string, error) {
154117
p.logger.Debugf("making GET http request: %s", url)
155118

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

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-
231181
// ClusterMembers array of cluster members from Patroni API
232182
type ClusterMembers struct {
233183
Members []ClusterMember `json:"members"`

0 commit comments

Comments
 (0)