Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow json and yaml for file backend #25

Merged
merged 2 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ jobs:

- name: Test File
run: |
integration/file/test.sh
integration/file/test_yaml.sh
integration/expect/check.sh
integration/file/test_json.sh
integration/expect/check.sh

- name: Install Rancher
Expand Down
29 changes: 24 additions & 5 deletions backends/file/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package file

import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -31,18 +33,30 @@ func NewFileClient(filepath []string, filter string) (*Client, error) {
}

func readFile(path string, vars map[string]string) error {
yamlMap := make(map[interface{}]interface{})
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}

err = yaml.Unmarshal(data, &yamlMap)
if err != nil {
return err
switch filepath.Ext(path) {
case ".json":
fileMap := make(map[string]interface{})
err = json.Unmarshal(data, &fileMap)
if err != nil {
return err
}
err = nodeWalk(fileMap, "/", vars)
case ".yml", ".yaml":
fileMap := make(map[interface{}]interface{})
err = yaml.Unmarshal(data, &fileMap)
if err != nil {
return err
}
err = nodeWalk(fileMap, "/", vars)
default:
err = fmt.Errorf("Invalid file extentsion. YAML or JSON only.")
}

err = nodeWalk(yamlMap, "/", vars)
if err != nil {
return err
}
Expand Down Expand Up @@ -93,6 +107,11 @@ func nodeWalk(node interface{}, key string, vars map[string]string) error {
key := path.Join(key, k.(string))
nodeWalk(v, key, vars)
}
case map[string]interface{}:
for k, v := range node.(map[string]interface{}) {
key := path.Join(key, k)
nodeWalk(v, key, vars)
}
case string:
vars[key] = node.(string)
case int:
Expand Down
56 changes: 56 additions & 0 deletions integration/file/test_json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

export HOSTNAME="localhost"
mkdir backends1 backends2
cat <<EOT >> backends1/1.json
{
"key": "foobar",
"database": {
"host": "127.0.0.1",
"password": "p@sSw0rd",
"port": "3306",
"username": "confd"
}
}
EOT

cat <<EOT >> backends1/2.json
{
"upstream": {
"app1": "10.0.1.10:8080",
"app2": "10.0.1.11:8080"
}
}
EOT

cat <<EOT >> backends2/1.json
{
"nested": {
"app1": "10.0.1.10:8080",
"app2": "10.0.1.11:8080"
}
}
EOT

cat <<EOT >> backends2/2.json
{
"prefix": {
"database": {
"host": "127.0.0.1",
"password": "p@sSw0rd",
"port": "3306",
"username": "confd"
},
"upstream": {
"app1": "10.0.1.10:8080",
"app2": "10.0.1.11:8080"
}
}
}
EOT

# Run confd
confd --onetime --log-level debug --confdir ./integration/confdir --backend file --file backends1/ --file backends2/ --watch

# Clean up after
rm -rf backends1 backends2
5 changes: 4 additions & 1 deletion integration/file/test.sh → integration/file/test_yaml.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ prefix:
EOT

# Run confd
confd --onetime --log-level debug --confdir ./integration/confdir --backend file --file backends1/ --file backends2/ --watch
confd --onetime --log-level debug --confdir ./integration/confdir --backend file --file backends1/ --file backends2/ --watch

# Clean up after
rm -rf backends1 backends2