Skip to content

Commit e11c3d6

Browse files
authored
Merge pull request #10 from numiralofe/delete-strategy
added delete strategy changes
2 parents 69cde10 + 98b505f commit e11c3d6

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Below are all available command line flags for starting **Gonsul**
9999
--allow-deletes=
100100
--poll-interval=
101101
--input-ext=
102+
--KeepFileExt=
102103
```
103104
Below is the full description for each individual command line flag
104105

@@ -290,18 +291,21 @@ no secrets are written to disk.
290291
> `default:` **false**
291292
> `example:` **`--allow-deletes=true`**
292293
293-
This instructs Gonsul how it should proceed in case some Consul deletes are to be made. If the value for this flag is
294-
`true`, Gonsul will proceed with the delete operations, but if instead is `false`, Gonsul will not proceed with the
295-
deletes, and depending on the `--strategy` it is running at, it respondes with some different behaviors, such as:
296-
- **`ONCE`** When running in once mode, Gonsul will terminate with __error code 10__ and output to console all the
297-
Consul KV paths that are supposed to be deleted.
298-
- **`HOOK`** Gonsul will repond to the HTTP request with error 503 and will also return the following headers and values:
299-
- `X-Gonsul-Error:10`
300-
- `X-Gonsul-Delete-Paths:path1/to/be/deleted,path2/to/be/deleted`
301-
- **`POLL`** Gonsul will log all the paths to be deleted as ERRORS and carry on, over and over. In this mode you should
302-
monitor Gonsul logs to detect any found errors, and react appropriately. The errors will follow the syntax:
303-
- `[ERROR] [28-01-2018 17:38:25 1234] error-10 path1/to/be/deleted`
304-
- `[ERROR] [28-01-2018 17:38:25 5678] error-10 path2/to/be/deleted`
294+
This option sets Gonsul behaviour on how it should proceed in case some Consul deletes are to be made.
295+
296+
- **`true`** With this mode deletes are allowed and Gonsul will proceed with the delete operations, removed files from the repo and/or files added directly on consul k/v will be deleted.
297+
- **`skip`** In this mode, Gonsul will skip the deletion check and operations and will just sync/push files to consul k/v path (can lead to inconsistencies at the consul k/v path since removed files from the repo and/or files added directly on consul k/v will stay in the consul path because we are skipping deletions).
298+
- **`false`** _this is the default mode_ and Gonsul will not proceed with the deletes, and depending on the `--strategy` it is running at, will respond with some different behaviors, such as:
299+
300+
1. **`ONCE`** When running in once mode, Gonsul will terminate with __error code 10__ and output to console all the Consul KV paths that are supposed to be deleted.
301+
302+
2. **`HOOK`** Gonsul will repond to the HTTP request with error 503 and will also return the following headers and values:
303+
- `X-Gonsul-Error:10`
304+
- `X-Gonsul-Delete-Paths:path1/to/be/deleted,path2/to/be/deleted`
305+
306+
3. **`POLL`** Gonsul will log all the paths to be deleted as ERRORS and carry on, over and over. In this mode you should monitor Gonsul logs to detect any found errors, and react appropriately. The errors will follow the syntax:
307+
- `[ERROR] [28-01-2018 17:38:25 1234] error-10 path1/to/be/deleted`
308+
- `[ERROR] [28-01-2018 17:38:25 5678] error-10 path2/to/be/deleted`
305309

306310

307311
### `--poll-interval`
@@ -321,6 +325,12 @@ This is the number of seconds you want Gonsul to wait between checks on the repo
321325
This is the file extensions that Gonsul should consider as inputs to populate our Consul. Please set each extension
322326
without the dot, and separate each extension with a comma.
323327

328+
### `--keep-FileExt`
329+
> `require:` **no**
330+
> `default:` **false**
331+
> `example:` **`--keep-FileExt=true`**
332+
333+
Gonsul default behavior is to remove/trim the filextension from the filename when pushing files to consul k/v path, if this option is set to true gonsul will keep the file extension.
324334

325335
### `--timeout`
326336
> `require:` **no**

internal/config/config.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type config struct {
3434
expandJSON bool
3535
doSecrets bool
3636
secretsMap map[string]string
37-
allowDeletes bool
37+
allowDeletes string
3838
pollInterval int
3939
Working chan bool
4040
validExtensions []string
@@ -62,7 +62,7 @@ type IConfig interface {
6262
ShouldExpandJSON() bool
6363
DoSecrets() bool
6464
GetSecretsMap() map[string]string
65-
AllowDeletes() bool
65+
AllowDeletes() string
6666
GetPollInterval() int
6767
WorkingChan() chan bool
6868
GetValidExtensions() []string
@@ -111,6 +111,12 @@ func buildConfig(flags ConfigFlags) (*config, error) {
111111
return nil, errors.New(fmt.Sprintf("strategy invalid, must be one of: %s, %s, %s, %s", StrategyDry, StrategyOnce, StrategyPoll, StrategyHook))
112112
}
113113

114+
// Make sure delete method is properly given
115+
allowDeletes := strings.ToLower(*flags.AllowDeletes)
116+
if allowDeletes != "true" && allowDeletes != "false" && allowDeletes != "skip" {
117+
return nil, errors.New(fmt.Sprintf("AllowDelete method is invalid, please define one of the following valid options as argument: true, false, skip"))
118+
}
119+
114120
// Shall we use a local copy of the repository instead of cloning ourselves
115121
// This should be useful if we use Gonsul on a CI stack (such as Bamboo)
116122
// And the repo is checked out already, alleviating Gonsul work
@@ -224,8 +230,8 @@ func (config *config) GetSecretsMap() map[string]string {
224230
return config.secretsMap
225231
}
226232

227-
func (config *config) AllowDeletes() bool {
228-
return config.allowDeletes
233+
func (config *config) AllowDeletes() string {
234+
return strings.ToLower(config.allowDeletes)
229235
}
230236

231237
func (config *config) GetPollInterval() int {

internal/config/flags_parser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ type ConfigFlags struct {
2121
ConsulBasePath *string
2222
ExpandJSON *bool
2323
SecretsFile *string
24-
AllowDeletes *bool
24+
AllowDeletes *string
2525
PollInterval *int
2626
ValidExtensions *string
27-
KeepFileExt *bool
27+
KeepFileExt *bool
2828
Timeout *int
2929
Version *bool
3030
}
@@ -46,7 +46,7 @@ func parseFlags() ConfigFlags {
4646
flags.ConsulBasePath = flag.String("consul-base-path", "", "The base KV path will be prefixed to dir path")
4747
flags.ExpandJSON = flag.Bool("expand-json", false, "Expand and parse JSON files as full paths? (Default false)")
4848
flags.SecretsFile = flag.String("secrets-file", "", "A key value json file with placeholders->secrets mapping, in order to do on the fly replace")
49-
flags.AllowDeletes = flag.Bool("allow-deletes", false, "Show Gonsul issue deletes? (If not, nothing will be done and a report on conflicting deletes will be shown) (Default false)")
49+
flags.AllowDeletes = flag.String("allow-deletes", "false", "false, nothing will be done and a report on conflicting deletes will be shown; true: deletes reported conflitcs and proceeds; skip: reportes conflitcs, does not performe any deletes and proceeds syncing remaining files.) (Default false)")
5050
flags.PollInterval = flag.Int("poll-interval", 60, "The number of seconds for the repository polling interval")
5151
flags.ValidExtensions = flag.String("input-ext", "json,txt,ini", "A comma separated list of file extensions valid as input")
5252
flags.KeepFileExt = flag.Bool("keep-FileExt", false, "Do we want to keep file name extensions ? (If not set to true defaults by ommiting the file name extension.) (Default false)")

internal/importer/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (i *importer) createOperationMatrix(liveData map[string]string, localData m
5959
// Now check for deletes
6060
// Check for deletes
6161
for liveKey := range liveData {
62-
if _, ok := localData[liveKey]; !ok {
62+
if _, ok := localData[liveKey]; !ok && i.config.AllowDeletes() != "skip" {
6363
// Not found in local - DELETE
6464
operations.AddDelete(entities.Entry{KVPath: liveKey, Value: ""})
6565
}

internal/importer/importer.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package importer
22

33
import (
4-
"internal/config"
5-
"internal/entities"
6-
"internal/util"
74
"encoding/json"
85
"errors"
96
"fmt"
7+
"internal/config"
8+
"internal/entities"
9+
"internal/util"
1010
"net/http"
1111
)
1212

@@ -57,7 +57,7 @@ func (i *importer) Start(localData map[string]string) {
5757

5858
func (i *importer) processOperations(matrix entities.OperationMatrix) {
5959
// Did we got any deletes and are we allowed to delete them?
60-
if !i.config.AllowDeletes() && matrix.HasDeletes() {
60+
if i.config.AllowDeletes() == "false" && matrix.HasDeletes() {
6161
// We're not supposed to trigger Consul deletes, output report and exit with error
6262
i.logger.PrintError("We're stopping as there are deletes and Gonsul is running without delete permission")
6363
i.logger.PrintError("Below is all the Consul KV paths that would be deleted")
@@ -83,14 +83,13 @@ func (i *importer) processOperations(matrix entities.OperationMatrix) {
8383
verb := op.GetVerb()
8484
path := op.GetPath()
8585

86-
8786
currentPayload, err := json.Marshal(transactions)
8887
if err != nil {
8988
util.ExitError(errors.New("Marshal: "+err.Error()), util.ErrorFailedJsonEncode, i.logger)
9089
}
91-
currentPayloadSize := len(currentPayload);
90+
currentPayloadSize := len(currentPayload)
9291

93-
var TxnKV entities.ConsulTxnKV;
92+
var TxnKV entities.ConsulTxnKV
9493

9594
if op.GetType() == entities.OperationDelete {
9695
TxnKV = entities.ConsulTxnKV{Verb: &verb, Key: &path}
@@ -105,7 +104,7 @@ func (i *importer) processOperations(matrix entities.OperationMatrix) {
105104
util.ExitError(errors.New("Marshal: "+err.Error()), util.ErrorFailedJsonEncode, i.logger)
106105
}
107106

108-
if currentPayloadSize + len(nextOpPayload) > maximumPayloadSize {
107+
if currentPayloadSize+len(nextOpPayload) > maximumPayloadSize {
109108
i.processConsulTransaction(transactions)
110109
transactions = []entities.ConsulTxn{}
111110
}

0 commit comments

Comments
 (0)