Documentation
¶
Overview ¶
Package confd is a pure-Go (no cgo) Ruby-facing adapter over the confd configuration-templating tool github.com/abtreece/confd — the maintained pure-Go fork of kelseyhightower/confd.
confd renders configuration files from Go text/templates whose data comes from a backend key/value store (env, file, etcd, consul, vault, redis, …). It is already pure Go and is the reference implementation, so this package does not reimplement any of it: it imports confd's own packages (pkg/backends, pkg/template, pkg/memkv, pkg/util) and exposes a small, importable Go API plus the seams a Ruby interpreter (go-embedded-ruby / rbgo) will later wire. 100% compatibility here means running confd's actual code, not a hand-port.
What this package adds ¶
Backend: a thin wrapper selecting a confd backend via backends.New. EnvBackend and FileBackend need no external service and are the always-available, fully-tested path; NewBackend reaches the rest (etcd/consul/vault/redis/… — those need a live service, so they are exercised through the MemoryClient seam or skipped, never mocked into a false pass).
MemoryClient: an in-memory implementation of confd's backends.StoreClient interface, backed by a Go map. It lets tests and the rbgo binding feed key/value data without any network, while confd's real template engine does the rendering.
Processor: runs confd's template processing over a config directory (conf.d/*.toml resources + templates/*.tmpl) against a Backend, in one-shot (Processor.Once) and interval (Processor.RunInterval) modes. The interval/watch mode sits behind a seam so tests do not block.
RenderString / RenderWithBackend: render a single template body to a string, without the caller touching disk or the network. confd's byte level renderer and default function map are unexported, so these drive confd's real template.Process against an ephemeral, internally-managed temp directory and return the produced bytes; the network is never touched (the data comes from an in-memory backend). All of confd's template functions (getv, getvs, gets, exists, base64Encode, json, toUpper, …) are reachable through this path because it is confd's own function map doing the work.
License ¶
This package is BSD-3-Clause ("the go-ruby-confd/confd authors"). The imported dependency github.com/abtreece/confd is MIT-licensed (Copyright (c) 2013 Kelsey Hightower); it is imported and built from source, not vendored or forked, so there is no license mixing of this project's own code.
Index ¶
- func RenderString(templateBody string, data map[string]string, opts ...RenderOption) (string, error)
- func RenderWithBackend(templateBody string, b *Backend, opts ...RenderOption) (string, error)
- func SetLogLevel(level string) error
- type Backend
- type MemoryClient
- type Processor
- type ProcessorConfig
- type RenderOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderString ¶
func RenderString(templateBody string, data map[string]string, opts ...RenderOption) (string, error)
RenderString renders templateBody — a Go text/template using confd's template functions (getv, getvs, gets, exists, base64Encode, json, toUpper, …) — against the in-memory key/value data, returning the produced text. It touches no network. confd's byte-level renderer is unexported, so this drives confd's real template.Process against an ephemeral, internally-managed temp directory (cleaned up before returning); the caller manages no files.
func RenderWithBackend ¶
func RenderWithBackend(templateBody string, b *Backend, opts ...RenderOption) (string, error)
RenderWithBackend is like RenderString but fetches the template's data from an arbitrary Backend (env, file, …) instead of an in-memory map. Only the backend touches the outside world; the render itself is hermetic.
func SetLogLevel ¶
SetLogLevel sets the verbosity of confd's internal logger. Valid levels are debug, info, warn/warning, error, fatal and panic. Rendering is noisy at info; callers typically want "error". An unknown level returns an error (confd itself would os.Exit on a bad level).
Types ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend wraps a confd backends.StoreClient together with the config used to build it. It is the connection seam the rbgo binding wires: the key/value source can be a real service (env/file/etcd/…) or the in-memory MemoryClient.
func EnvBackend ¶
EnvBackend returns a Backend reading confd keys from OS environment variables. It needs no external service and is always available.
func FileBackend ¶
FileBackend returns a Backend reading confd keys from the given YAML/JSON files (or directories, walked recursively). filter is an optional glob applied to filenames ("" matches all). It needs no external service.
func MemoryBackend ¶
MemoryBackend returns a Backend backed by an in-memory MemoryClient seeded with the given key/value data. It touches neither disk nor network and is the backend used by RenderString.
func NewBackend ¶
NewBackend builds a Backend from a confd backend configuration. It selects the backend by cfg.Backend ("env", "file", "etcd", "consul", "vault", "redis", …) via confd's own backends.New. Backends other than env and file require a reachable service; construction of some (e.g. vault) validates eagerly and may fail here, while others fail later on GetValues.
func (*Backend) Client ¶
func (b *Backend) Client() backends.StoreClient
Client returns the underlying confd StoreClient. This is the seam a caller (or the rbgo binding) uses to reach confd's backend directly.
func (*Backend) GetValues ¶
GetValues fetches the values under the given key prefixes from the backend, delegating to confd's StoreClient.
func (*Backend) HealthCheck ¶
HealthCheck reports whether the backend connection is healthy.
type MemoryClient ¶
type MemoryClient struct {
types.NoopWatcher // supplies a no-op WatchPrefix
types.NoopCloser // supplies a no-op Close
// contains filtered or unexported fields
}
MemoryClient is an in-memory implementation of confd's backends.StoreClient, backed by a Go map. It exists so tests and the rbgo binding can supply key/value data without a live backend service; confd's real template engine still performs the rendering. Keys are matched by prefix, mirroring the semantics of confd's real backends. It is safe for concurrent use.
func NewMemoryClient ¶
func NewMemoryClient(kv map[string]string) *MemoryClient
NewMemoryClient returns a MemoryClient seeded with a copy of kv (nil is treated as empty).
func (*MemoryClient) GetValues ¶
GetValues returns every stored key that is prefixed by one of the requested keys, matching confd backend semantics.
func (*MemoryClient) HealthCheck ¶
func (c *MemoryClient) HealthCheck(ctx context.Context) error
HealthCheck always succeeds; an in-memory store is always reachable.
func (*MemoryClient) Set ¶
func (c *MemoryClient) Set(key, value string)
Set stores or overwrites a single key.
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor renders the config files described by a confd config directory (its conf.d/*.toml resources and templates/*.tmpl templates) using a Backend. It wraps confd's own template package.
func NewProcessor ¶
func NewProcessor(cfg ProcessorConfig) (*Processor, error)
NewProcessor builds a Processor from cfg. It returns an error if no backend is supplied or if no config directory can be determined.
func (*Processor) Once ¶
Once loads and renders every template resource exactly once, writing the target config files. It runs confd's template.Process.
func (*Processor) RunInterval ¶
RunInterval polls the backend every intervalSeconds and re-renders on change. It blocks until stop is closed (or the Processor context is cancelled), then closes done; backend/processing errors are delivered on errCh. Run it in its own goroutine. This is confd's IntervalProcessor, behind a seam.
type ProcessorConfig ¶
type ProcessorConfig struct {
// ConfDir is the confd root directory. By default its conf.d subdirectory
// holds the *.toml resources and its templates subdirectory holds the
// *.tmpl templates (confd's standard layout).
ConfDir string
// ConfigDir overrides the resource directory (default: ConfDir/conf.d).
ConfigDir string
// TemplateDir overrides the template directory (default: ConfDir/templates).
TemplateDir string
// Backend supplies the key/value data. Required.
Backend *Backend
// Prefix is prepended to every resource's key prefix (confd -prefix).
Prefix string
// Noop, when true, logs what would change without writing target files.
Noop bool
// SyncOnly, when true, skips check/reload commands.
SyncOnly bool
// KeepStageFile keeps the intermediate staged files for debugging.
KeepStageFile bool
// Ctx is used for cancellation/timeouts (default: context.Background()).
Ctx context.Context
}
ProcessorConfig configures a Processor.
type RenderOption ¶
type RenderOption func(*renderOptions)
RenderOption customises a render call.
func WithKeys ¶
func WithKeys(keys ...string) RenderOption
WithKeys sets the key prefixes the template resource requests from the backend (default: []string{"/"}, i.e. everything).
func WithOutputFormat ¶
func WithOutputFormat(format string) RenderOption
WithOutputFormat enables confd's output-format validation (json, yaml, toml or xml) on the rendered result.
func WithPrefix ¶
func WithPrefix(prefix string) RenderOption
WithPrefix sets a global key prefix for the render (confd -prefix).