-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
policy: add new file source for loading policies from disk.
The MVP cluster scaling includes the ability to load scaling policies from a specified directory. This functionality is off by default but is turned on when the policy-dir flag or corresponding config file parameter is set to a non-empty string. The file source implements the source interface. It is also responsible for generating UUIDs for the policies, a task which has before been performed by Nomad. The source stores the ID mapped to the file path md5sum. This allows the policy contents to change without the need to update the policyID. Funcationality is in place to allow reloading of the policies dir by sending the agent a SIGHUP signal. This still requires plumbing in from the agent and will be addressed in a follow up PR.
- Loading branch information
Showing
20 changed files
with
939 additions
and
190 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
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,65 @@ | ||
package file | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"path/filepath" | ||
) | ||
|
||
func GetFileListFromDir(dir string, suffixes ...string) ([]string, error) { | ||
|
||
f, err := os.Open(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
fi, err := f.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !fi.IsDir() { | ||
return nil, fmt.Errorf("configuration path must be a directory: %s", dir) | ||
} | ||
|
||
var files []string | ||
err = nil | ||
for err != io.EOF { | ||
var fis []os.FileInfo | ||
fis, err = f.Readdir(128) | ||
if err != nil && err != io.EOF { | ||
return nil, err | ||
} | ||
|
||
for _, fi := range fis { | ||
// Ignore directories | ||
if fi.IsDir() { | ||
continue | ||
} | ||
|
||
// Only care about files that are valid to load. | ||
name := fi.Name() | ||
|
||
if suffixes != nil { | ||
if !fileHasSuffix(name, suffixes) || IsTemporaryFile(name) { | ||
continue | ||
} | ||
path := filepath.Join(dir, name) | ||
files = append(files, path) | ||
} | ||
} | ||
} | ||
return files, nil | ||
} | ||
|
||
func fileHasSuffix(file string, suffixes []string) bool { | ||
for _, suffix := range suffixes { | ||
if strings.HasSuffix(file, suffix) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,11 @@ | ||
package file | ||
|
||
import "strings" | ||
|
||
// IsTemporaryFile returns true or false depending on whether the provided file | ||
// name is a temporary file for the following editors: emacs or vim. | ||
func IsTemporaryFile(name string) bool { | ||
return strings.HasSuffix(name, "~") || // vim | ||
strings.HasPrefix(name, ".#") || // emacs | ||
(strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#")) // emacs | ||
} |
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,46 @@ | ||
package file | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_IsTemporaryFile(t *testing.T) { | ||
testCases := []struct { | ||
testName string | ||
inputName string | ||
expectedReturn bool | ||
}{ | ||
{ | ||
testName: "vim temp input file", | ||
inputName: "config.hcl~", | ||
expectedReturn: true, | ||
}, | ||
{ | ||
testName: "emacs temp input file 1", | ||
inputName: ".#config.hcl", | ||
expectedReturn: true, | ||
}, | ||
{ | ||
testName: "emacs temp input file 2", | ||
inputName: "#config.hcl#", | ||
expectedReturn: true, | ||
}, | ||
{ | ||
testName: "non-temp HCL config input file", | ||
inputName: "config.hcl", | ||
expectedReturn: false, | ||
}, | ||
{ | ||
testName: "non-temp JSON config input file", | ||
inputName: "config.json", | ||
expectedReturn: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
actualOutput := IsTemporaryFile(tc.inputName) | ||
assert.Equal(t, tc.expectedReturn, actualOutput, tc.testName) | ||
} | ||
} |
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,21 @@ | ||
package uuid | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"fmt" | ||
) | ||
|
||
// Generate is used to generate a random UUID. | ||
func Generate() string { | ||
buf := make([]byte, 16) | ||
if _, err := crand.Read(buf); err != nil { | ||
panic(fmt.Errorf("failed to read random bytes: %v", err)) | ||
} | ||
|
||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x", | ||
buf[0:4], | ||
buf[4:6], | ||
buf[6:8], | ||
buf[8:10], | ||
buf[10:16]) | ||
} |
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,41 @@ | ||
package uuid | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerate(t *testing.T) { | ||
testCases := []struct { | ||
count int | ||
name string | ||
}{ | ||
{count: 100, name: "generate 100 unique uuids"}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
stored := make(map[string]interface{}) | ||
|
||
for i := 0; i < 100; i++ { | ||
|
||
newUUID := Generate() | ||
|
||
// Check the UUID matches the expected regex. | ||
matched, err := regexp.MatchString("[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", newUUID) | ||
assert.True(t, matched, tc.name) | ||
assert.Nil(t, err, tc.name) | ||
|
||
// Ensure we have not seen the UUID before. Then store the new | ||
// UUID. | ||
val, ok := stored[newUUID] | ||
assert.False(t, ok, tc.name) | ||
assert.Nil(t, val, tc.name) | ||
stored[newUUID] = nil | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.