This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Consul state backend storage for Policies and Scale State.
- Loading branch information
Showing
12 changed files
with
511 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package client | ||
|
||
import consulAPI "github.com/hashicorp/consul/api" | ||
|
||
// NewConsulClient is responsible for generating a reusable Consul client using the HashiCorp | ||
// Consul SDK and the default config. This default config pulls Consul client configuration from | ||
// env vars which can therefore be customized by the user. | ||
func NewConsulClient() (*consulAPI.Client, error) { | ||
return consulAPI.NewClient(consulAPI.DefaultConfig()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package server | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
configKeyStorageConsulEnabled = "storage-consul-enabled" | ||
configKeyStorageConsulPath = "storage-consul-path" | ||
configKeyStorageConsulPathDefault = "chemtrail/" | ||
) | ||
|
||
// StorageConfig is the CLI configuration options for the state storage backend. | ||
type StorageConfig struct { | ||
ConsulEnabled bool | ||
ConsulPath string | ||
} | ||
|
||
// GetStorageConfig populates a StorageConfig object with the users CLI parameters. | ||
func GetStorageConfig() *StorageConfig { | ||
|
||
// Check that the path has suffixed with a forward slash, otherwise put this on so we do not | ||
// need to check in a number of other places. | ||
path := viper.GetString(configKeyStorageConsulPath) | ||
if suffix := strings.HasSuffix(path, "/"); !suffix { | ||
path = path + "/" | ||
} | ||
|
||
return &StorageConfig{ | ||
ConsulEnabled: viper.GetBool(configKeyStorageConsulEnabled), | ||
ConsulPath: path, | ||
} | ||
} | ||
|
||
// RegisterStorageConfig register the storage CLI parameters for the state storage backend. | ||
func RegisterStorageConfig(cmd *cobra.Command) { | ||
flags := cmd.PersistentFlags() | ||
|
||
{ | ||
const ( | ||
key = configKeyStorageConsulEnabled | ||
longOpt = "storage-consul-enabled" | ||
defaultValue = false | ||
description = "Enable the Consul state storage backend" | ||
) | ||
|
||
flags.Bool(longOpt, defaultValue, description) | ||
_ = viper.BindPFlag(key, flags.Lookup(longOpt)) | ||
viper.SetDefault(key, defaultValue) | ||
} | ||
{ | ||
const ( | ||
key = configKeyStorageConsulPath | ||
longOpt = "storage-consul-path" | ||
defaultValue = configKeyStorageConsulPathDefault | ||
description = "The Consul KV base path that will be used to store state" | ||
) | ||
|
||
flags.String(longOpt, defaultValue, description) | ||
_ = viper.BindPFlag(key, flags.Lookup(longOpt)) | ||
viper.SetDefault(key, defaultValue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package server | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_StorageConfig(t *testing.T) { | ||
fakeCMD := &cobra.Command{} | ||
RegisterStorageConfig(fakeCMD) | ||
|
||
cfg := GetStorageConfig() | ||
assert.Equal(t, false, cfg.ConsulEnabled) | ||
assert.Equal(t, "chemtrail/", cfg.ConsulPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package server | ||
|
||
import "time" | ||
|
||
// gcEvalPeriod is the time period at which the automatic scaling state garbage collector is run | ||
// at. | ||
var gcEvalPeriod = time.Minute * 10 | ||
|
||
// runGarbageCollectionLoop is responsible for periodically running the scaling state garbage | ||
// collection function. | ||
func (h *HTTPServer) runGarbageCollectionLoop() { | ||
h.logger.Info().Msg("started scaling state garbage collector handler") | ||
|
||
h.gcIsRunning = true | ||
|
||
t := time.NewTicker(gcEvalPeriod) | ||
defer t.Stop() | ||
|
||
for { | ||
select { | ||
case <-h.stopChan: | ||
h.logger.Info().Msg("shutting down state garbage collection handler") | ||
h.gcIsRunning = false | ||
return | ||
case <-t.C: | ||
h.logger.Debug().Msg("triggering internal run of state garbage collection") | ||
h.scaleState.RunStateGarbageCollection() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.