From fcd48c112501d22311b93c967bd7d5d91d17b647 Mon Sep 17 00:00:00 2001 From: Vinicius Ruoso Date: Wed, 14 Oct 2020 17:34:12 -0300 Subject: [PATCH] add support for binary secret This allows the engine to be used to watch and retrieve data from binary secrets. The current secret implementation assumes that the value is a string and hence does not work with binary data. Besides the fact that this is a template engine and binary data make very little sense, this engine can be used as a generic k8s watcher. Related to CyCoreSystems/asterisk-config#8. Signed-off-by: Vinicius Ruoso --- engine.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/engine.go b/engine.go index 293c037..bedb997 100644 --- a/engine.go +++ b/engine.go @@ -260,6 +260,40 @@ func (e *Engine) Secret(name string, namespace string, key string) (out string, return } +// SecretBinary returns a kubernetes Secret +func (e *Engine) SecretBinary(name string, namespace string, key string) (out []byte, err error) { + s := new(v1.Secret) + + if err = e.connectKube(); err != nil { + return + } + + namespace, err = getNamespace(namespace) + if err != nil { + return + } + + ctx, cancel := boundedContext() + defer cancel() + + if err = e.kc.Get(ctx, namespace, name, s); err != nil { + return + } + + v, ok := s.GetData()[key] + if !ok { + return nil, fmt.Errorf("key %s not found in Secret %s[%s]", key, name, namespace) + } + out = v + + if e.firstRenderCompleted { + return + } + + err = e.AddWatched(context.Background(), "Secret", namespace, name) + return +} + // Env returns the value of an environment variable func (e *Engine) Env(name string) string { return os.Getenv(name)