fix(files): including file path in the config / secret keys

pull/4067/head
Afshin Mehrabani 7 years ago
parent bb19aea597
commit cf559be542

1
.gitignore vendored

@ -10,3 +10,4 @@ rootfs/rudder
vendor/
*.exe
.idea/
*.iml

@ -24,6 +24,7 @@ import (
"github.com/ghodss/yaml"
"fmt"
"github.com/BurntSushi/toml"
"github.com/gobwas/glob"
"github.com/golang/protobuf/ptypes/any"
@ -116,12 +117,31 @@ func (f Files) AsConfig() string {
// Explicitly convert to strings, and file names
for k, v := range f {
m[path.Base(k)] = string(v)
m[AsConfigKey(k)] = string(v)
}
return ToYaml(m)
}
// AsConfigKey accepts a string, which is the path of a file then converts
// it to Kubernetes friendly key to be used in ConfigMap and Secret keys.
//
// Used in AsSecrets() and AsConfig() methods.
func AsConfigKey(k string) string {
dir := path.Dir(k)
prefix := ""
if dir != "." {
prefix = strings.Replace(dir, "/", "_", -1)
prefix += "_"
}
base := path.Base(k)
return fmt.Sprintf("%s%s", prefix, base)
}
// AsSecrets returns the base64-encoded value of a Files object suitable for
// including in the 'data' section of a Kubernetes Secret definition.
// Duplicate keys will be overwritten, so be aware that your file names
@ -144,7 +164,7 @@ func (f Files) AsSecrets() string {
m := map[string]string{}
for k, v := range f {
m[path.Base(k)] = base64.StdEncoding.EncodeToString(v)
m[AsConfigKey(k)] = base64.StdEncoding.EncodeToString(v)
}
return ToYaml(m)

@ -27,6 +27,7 @@ var cases = []struct {
}{
{"ship/captain.txt", "The Captain"},
{"ship/stowaway.txt", "Legatt"},
{"ship/cabin/helm.txt", "Helm"},
{"story/name.txt", "The Secret Sharer"},
{"story/author.txt", "Joseph Conrad"},
{"multiline/test.txt", "bar\nfoo"},
@ -72,10 +73,10 @@ func TestToConfig(t *testing.T) {
f := NewFiles(getTestFiles())
out := f.Glob("**/captain.txt").AsConfig()
as.Equal("captain.txt: The Captain\n", out)
as.Equal("ship_captain.txt: The Captain\n", out)
out = f.Glob("ship/**").AsConfig()
as.Equal("captain.txt: The Captain\nstowaway.txt: Legatt\n", out)
as.Equal("ship_cabin_helm.txt: Helm\nship_captain.txt: The Captain\nship_stowaway.txt: Legatt\n", out)
}
func TestToSecret(t *testing.T) {
@ -84,7 +85,7 @@ func TestToSecret(t *testing.T) {
f := NewFiles(getTestFiles())
out := f.Glob("ship/**").AsSecrets()
as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0\n", out)
as.Equal("ship_cabin_helm.txt: SGVsbQ==\nship_captain.txt: VGhlIENhcHRhaW4=\nship_stowaway.txt: TGVnYXR0\n", out)
}
func TestLines(t *testing.T) {

Loading…
Cancel
Save