Skip to content

Commit 6692384

Browse files
committed
BUG/MINOR: cluster: add better cluster config cleanup
1 parent 1d03a80 commit 6692384

File tree

1 file changed

+19
-75
lines changed

1 file changed

+19
-75
lines changed

handlers/cluster.go

Lines changed: 19 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ package handlers
1818
import (
1919
"fmt"
2020
"reflect"
21+
"time"
2122

2223
log "github.com/sirupsen/logrus"
2324

2425
"github.com/go-openapi/runtime/middleware"
25-
"github.com/google/renameio"
2626
client_native "github.com/haproxytech/client-native/v2"
2727
"github.com/haproxytech/client-native/v2/models"
2828

@@ -159,84 +159,14 @@ func (h *DeleteClusterHandlerImpl) Handle(params cluster.DeleteClusterParams, pr
159159
// silently fallback to 1
160160
version = 1
161161
}
162-
transaction, err := h.Client.Configuration.StartTransaction(version)
163-
if err != nil {
164-
return h.err500(err, transaction)
165-
}
166-
// delete backends
167-
_, backends, err := h.Client.Configuration.GetBackends(transaction.ID)
168-
if err != nil {
169-
return h.err500(err, transaction)
170-
}
171-
for _, backend := range backends {
172-
err = h.Client.Configuration.DeleteBackend(backend.Name, transaction.ID, 0)
173-
if err != nil {
174-
return h.err500(err, transaction)
175-
}
176-
}
177-
// delete all frontends
178-
_, frontends, err := h.Client.Configuration.GetFrontends(transaction.ID)
179-
if err != nil {
180-
return h.err500(err, transaction)
181-
}
182-
for _, frontend := range frontends {
183-
err = h.Client.Configuration.DeleteFrontend(frontend.Name, transaction.ID, 0)
184-
if err != nil {
185-
return h.err500(err, transaction)
186-
}
187-
}
188162

189-
// now create dummy frontend so haproxy does not complain
190-
err = h.Client.Configuration.CreateFrontend(&models.Frontend{Name: "disabled"}, transaction.ID, 0)
191-
if err != nil {
192-
return h.err500(err, transaction)
193-
}
194-
err = h.Client.Configuration.CreateBind("disabled", &models.Bind{
195-
Name: "tmp",
196-
Address: fmt.Sprintf("/tmp/dataplaneapi-%s.sock", h.Config.Name.Load()),
197-
}, transaction.ID, 0)
198-
if err != nil {
199-
return h.err500(err, transaction)
200-
}
201-
// now reset peer-id
202-
if h.Config.HAProxy.NodeIDFile != "" {
203-
err = renameio.WriteFile(h.Config.HAProxy.NodeIDFile, []byte("localhost"), 0644)
204-
if err != nil {
205-
return h.err500(err, transaction)
206-
}
207-
_, peerSections, errPeers := h.Client.Configuration.GetPeerSections(transaction.ID)
208-
if errPeers != nil {
209-
return h.err500(errPeers, transaction)
210-
}
211-
peerFound := false
212-
dataplaneID := h.Config.Cluster.ID.Load()
213-
for _, section := range peerSections {
214-
_, peerEntries, errPeersEntries := h.Client.Configuration.GetPeerEntries(section.Name, transaction.ID)
215-
if errPeersEntries != nil {
216-
return h.err500(errPeersEntries, transaction)
217-
}
218-
for _, peer := range peerEntries {
219-
if peer.Name == dataplaneID {
220-
peerFound = true
221-
peer.Name = "localhost"
222-
errPeerEntry := h.Client.Configuration.EditPeerEntry(dataplaneID, section.Name, peer, transaction.ID, 0)
223-
if errPeerEntry != nil {
224-
return h.err500(errPeerEntry, transaction)
225-
}
226-
}
227-
}
228-
}
229-
if !peerFound && dataplaneID != "" {
230-
return h.err500(fmt.Errorf("peer [%s] not found in HAProxy config", dataplaneID), transaction)
231-
}
232-
}
233-
_, err = h.Client.Configuration.CommitTransaction(transaction.ID)
234-
if err != nil {
163+
config := fmt.Sprintf(DummyConfig, time.Now().Format("01-02-2006 15:04:05 MST"), h.Config.Name.Load())
164+
if err := configuration.PostRawConfiguration(&config, version, true); err != nil {
235165
return h.err500(err, nil)
236166
}
167+
237168
// we need to restart haproxy
238-
err = h.ReloadAgent.Restart()
239-
if err != nil {
169+
if err := h.ReloadAgent.Restart(); err != nil {
240170
return h.err500(err, nil)
241171
}
242172
}
@@ -344,3 +274,17 @@ func clusterLogTargetsChanged(old []*models.ClusterLogTarget, new []*models.Clus
344274
}
345275
return true
346276
}
277+
278+
const DummyConfig = `# NOTE: This configuration file was managed by the Fusion Control Plane.
279+
# Fusion released the control at %s
280+
281+
defaults
282+
mode http
283+
timeout connect 5000
284+
timeout client 30000
285+
timeout server 10000
286+
287+
frontend disabled
288+
bind /tmp/dataplaneapi-%s.sock name tmp
289+
290+
`

0 commit comments

Comments
 (0)