Skip to content

Commit 267a161

Browse files
committed
remove slot using patch
1 parent 5a4bad4 commit 267a161

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
@@ -541,45 +541,6 @@ 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-
583544
// compare effective and desired Patroni config options
584545
if desiredPatroniConfig.LoopWait > 0 && desiredPatroniConfig.LoopWait != effectivePatroniConfig.LoopWait {
585546
configToSet["loop_wait"] = desiredPatroniConfig.LoopWait
@@ -618,8 +579,8 @@ func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration(pod *v1.Pod, effectiv
618579
}
619580
}
620581

582+
slotsToSet := make(map[string]interface{})
621583
// check if specified slots exist in config and if they differ
622-
slotsToSet := make(map[string]map[string]string)
623584
for slotName, desiredSlot := range desiredPatroniConfig.Slots {
624585
if effectiveSlot, exists := effectivePatroniConfig.Slots[slotName]; exists {
625586
if reflect.DeepEqual(desiredSlot, effectiveSlot) {
@@ -628,6 +589,15 @@ func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration(pod *v1.Pod, effectiv
628589
}
629590
slotsToSet[slotName] = desiredSlot
630591
}
592+
// check if there is any slot deletion
593+
for slotName, effectiveSlot := range effectivePatroniConfig.Slots {
594+
if desiredSlot, exists := desiredPatroniConfig.Slots[slotName]; exists {
595+
if reflect.DeepEqual(effectiveSlot, desiredSlot) {
596+
continue
597+
}
598+
}
599+
slotsToSet[slotName] = nil
600+
}
631601
if len(slotsToSet) > 0 {
632602
configToSet["slots"] = slotsToSet
633603
}

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)