Skip to content

Commit c43512a

Browse files
committed
MINOR: cluster: removing storage upon cluster removal
1 parent 7b6d352 commit c43512a

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

configuration/cluster_sync.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ import (
3636
"github.com/google/renameio"
3737
client_native "github.com/haproxytech/client-native/v4"
3838
"github.com/haproxytech/config-parser/v4/types"
39-
"github.com/haproxytech/dataplaneapi/log"
4039
jsoniter "github.com/json-iterator/go"
4140

41+
"github.com/haproxytech/dataplaneapi/log"
42+
4243
"github.com/haproxytech/dataplaneapi/haproxy"
4344
"github.com/haproxytech/dataplaneapi/misc"
4445
)
@@ -226,6 +227,10 @@ func (c *ClusterSync) monitorBootstrapKey() {
226227
log.Error(errStorageDir)
227228
continue
228229
}
230+
// Init NOTICE file to inform user that the cluster storage folder is programmatically managed by Fusion API
231+
if errStorageInit := InitStorageNoticeFile(data["storage-dir"]); errStorageInit != nil {
232+
log.Warningf("unable to create notice file, %s: skipping it", errStorageInit.Error())
233+
}
229234
url := fmt.Sprintf("%s://%s", data["schema"], data["address"])
230235
c.cfg.Cluster.URL.Store(url)
231236
c.cfg.Cluster.Port.Store(func() int {

configuration/misc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323
"os"
2424
"path"
2525
"strconv"
26+
"strings"
2627
"syscall"
2728
"time"
2829

2930
"github.com/go-openapi/strfmt"
31+
"github.com/google/renameio"
3032
"github.com/haproxytech/client-native/v4/misc"
3133
"github.com/haproxytech/client-native/v4/storage"
3234
jsoniter "github.com/json-iterator/go"
@@ -82,6 +84,20 @@ func fileExists(filename string) bool {
8284
return !info.IsDir()
8385
}
8486

87+
func RemoveStorageFolder(storageDir string) error {
88+
return os.RemoveAll(storageDir)
89+
}
90+
91+
func InitStorageNoticeFile(storageDir string) error {
92+
content := strings.Builder{}
93+
94+
_, _ = fmt.Fprintf(&content, "# *********************************************************************************\n")
95+
_, _ = fmt.Fprintf(&content, "# NOTE: This storage folder contains files managed by HAProxy Fusion Control Plane:\n")
96+
_, _ = fmt.Fprintf(&content, "# manual edits may cause issues and misconfigurations.\n")
97+
98+
return renameio.WriteFile(path.Join(storageDir, "NOTICE"), []byte(content.String()), os.ModePerm)
99+
}
100+
85101
func CheckIfStorageDirIsOK(storageDir string, config *Configuration) error {
86102
if storageDir == "" {
87103
return errors.New("storage-dir in bootstrap key is empty")

handlers/cluster.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ func (h *CreateClusterHandlerImpl) Handle(params cluster.PostClusterParams, prin
130130
log.Warningf("configured storage dir incompatible with cluster configuration: %s", errStorageDir)
131131
return h.err409(errStorageDir, nil)
132132
}
133-
133+
// Init NOTICE file to inform user that the cluster storage folder is programmatically managed by Fusion API
134+
if errStorageInit := configuration.InitStorageNoticeFile(key["storage-dir"]); errStorageInit != nil {
135+
log.Warningf("unable to create notice file, %s: skipping it", errStorageInit.Error())
136+
}
134137
// enforcing API advertising options
135138
if a := params.AdvertisedAddress; a != nil {
136139
h.Config.APIOptions.APIAddress = *a
@@ -176,18 +179,18 @@ func (h *DeleteClusterHandlerImpl) Handle(params cluster.DeleteClusterParams, pr
176179
if params.Configuration == nil || *params.Configuration != "keep" {
177180
log.Warning("clearing configuration as requested")
178181

179-
configuration, err := h.Client.Configuration()
182+
conf, err := h.Client.Configuration()
180183
if err != nil {
181184
return h.err500(err, nil)
182185
}
183-
version, errVersion := configuration.GetVersion("")
186+
version, errVersion := conf.GetVersion("")
184187
if errVersion != nil || version < 1 {
185188
// silently fallback to 1
186189
version = 1
187190
}
188191

189192
config := fmt.Sprintf(DummyConfig, time.Now().Format("01-02-2006 15:04:05 MST"), h.Config.Name.Load())
190-
if err = configuration.PostRawConfiguration(&config, version, true); err != nil {
193+
if err = conf.PostRawConfiguration(&config, version, true); err != nil {
191194
return h.err500(err, nil)
192195
}
193196

@@ -196,6 +199,13 @@ func (h *DeleteClusterHandlerImpl) Handle(params cluster.DeleteClusterParams, pr
196199
if err != nil {
197200
return h.err500(err, nil)
198201
}
202+
// Deleting the storage directory used by Fusion:
203+
// avoiding at all entering any nil pointer dereference.
204+
if storageData := h.Config.GetStorageData(); storageData != nil && storageData.Cluster != nil && storageData.Cluster.StorageDir != nil {
205+
if storageErr := configuration.RemoveStorageFolder(*storageData.Cluster.StorageDir); storageErr != nil {
206+
log.Warningf("failed to clean-up the cluster storage directory: %s", storageErr.Error())
207+
}
208+
}
199209
}
200210
h.Config.Cluster.BootstrapKey.Store("")
201211
h.Config.Mode.Store(configuration.ModeSingle)

0 commit comments

Comments
 (0)