Skip to content

Commit 9c48840

Browse files
committed
BUG/MINOR: cluster: add better cluster config cleanup
1 parent 9f88249 commit 9c48840

1 file changed

Lines changed: 18 additions & 75 deletions

File tree

handlers/cluster.go

Lines changed: 18 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/v3"
2727
"github.com/haproxytech/client-native/v3/models"
2828

@@ -173,83 +173,12 @@ func (h *DeleteClusterHandlerImpl) Handle(params cluster.DeleteClusterParams, pr
173173
// silently fallback to 1
174174
version = 1
175175
}
176-
transaction, err := configuration.StartTransaction(version)
177-
if err != nil {
178-
return h.err500(err, transaction)
179-
}
180-
// delete backends
181-
_, backends, err := configuration.GetBackends(transaction.ID)
182-
if err != nil {
183-
return h.err500(err, transaction)
184-
}
185-
for _, backend := range backends {
186-
err = configuration.DeleteBackend(backend.Name, transaction.ID, 0)
187-
if err != nil {
188-
return h.err500(err, transaction)
189-
}
190-
}
191-
// delete all frontends
192-
_, frontends, err := configuration.GetFrontends(transaction.ID)
193-
if err != nil {
194-
return h.err500(err, transaction)
195-
}
196-
for _, frontend := range frontends {
197-
err = configuration.DeleteFrontend(frontend.Name, transaction.ID, 0)
198-
if err != nil {
199-
return h.err500(err, transaction)
200-
}
201-
}
202176

203-
// now create dummy frontend so haproxy does not complain
204-
err = configuration.CreateFrontend(&models.Frontend{Name: "disabled"}, transaction.ID, 0)
205-
if err != nil {
206-
return h.err500(err, transaction)
207-
}
208-
err = configuration.CreateBind("disabled", &models.Bind{
209-
BindParams: models.BindParams{
210-
Name: "tmp",
211-
},
212-
Address: fmt.Sprintf("/tmp/dataplaneapi-%s.sock", h.Config.Name.Load()),
213-
}, transaction.ID, 0)
214-
if err != nil {
215-
return h.err500(err, transaction)
216-
}
217-
// now reset peer-id
218-
if h.Config.HAProxy.NodeIDFile != "" {
219-
err = renameio.WriteFile(h.Config.HAProxy.NodeIDFile, []byte("localhost"), 0644)
220-
if err != nil {
221-
return h.err500(err, transaction)
222-
}
223-
_, peerSections, errPeers := configuration.GetPeerSections(transaction.ID)
224-
if errPeers != nil {
225-
return h.err500(errPeers, transaction)
226-
}
227-
peerFound := false
228-
dataplaneID := h.Config.Cluster.ID.Load()
229-
for _, section := range peerSections {
230-
_, peerEntries, errPeersEntries := configuration.GetPeerEntries(section.Name, transaction.ID)
231-
if errPeersEntries != nil {
232-
return h.err500(errPeersEntries, transaction)
233-
}
234-
for _, peer := range peerEntries {
235-
if peer.Name == dataplaneID {
236-
peerFound = true
237-
peer.Name = "localhost"
238-
errPeerEntry := configuration.EditPeerEntry(dataplaneID, section.Name, peer, transaction.ID, 0)
239-
if errPeerEntry != nil {
240-
return h.err500(errPeerEntry, transaction)
241-
}
242-
}
243-
}
244-
}
245-
if !peerFound && dataplaneID != "" {
246-
return h.err500(fmt.Errorf("peer [%s] not found in HAProxy config", dataplaneID), transaction)
247-
}
248-
}
249-
_, err = configuration.CommitTransaction(transaction.ID)
250-
if err != nil {
177+
config := fmt.Sprintf(DummyConfig, time.Now().Format("01-02-2006 15:04:05 MST"), h.Config.Name.Load())
178+
if err = configuration.PostRawConfiguration(&config, version, true); err != nil {
251179
return h.err500(err, nil)
252180
}
181+
253182
// we need to restart haproxy
254183
err = h.ReloadAgent.Restart()
255184
if err != nil {
@@ -363,3 +292,17 @@ func clusterLogTargetsChanged(old []*models.ClusterLogTarget, new []*models.Clus
363292
}
364293
return true
365294
}
295+
296+
const DummyConfig = `# NOTE: This configuration file was managed by the Fusion Control Plane.
297+
# Fusion released the control at %s
298+
299+
defaults
300+
mode http
301+
timeout connect 5000
302+
timeout client 30000
303+
timeout server 10000
304+
305+
frontend disabled
306+
bind /tmp/dataplaneapi-%s.sock name tmp
307+
308+
`

0 commit comments

Comments
 (0)