confd

package module
v0.0.0-...-1e2c7e6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

README

go-ruby-confd/confd

A pure-Go (CGO=0) Ruby-facing adapter over 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, …). confd 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 adds a small, importable Go API plus the seams a Ruby interpreter (go-embedded-ruby / rbgo) will later wire. 100% compatibility means running confd's actual code, not a hand-port.

Install

go get github.com/go-ruby-confd/confd

Requires Go 1.26+.

Usage

Render a template to a string (no disk or network for the caller)
out, err := confd.RenderString(
    `host={{getv "/db/host"}} up={{toUpper (getv "/db/host")}}`,
    map[string]string{"/db/host": "pg1"},
    confd.WithKeys("/db"),
)
// out == "host=pg1 up=PG1"

All of confd's template functions — getv, getvs, gets, exists, base64Encode, json, toUpper, map, seq, … — are reachable, because it is confd's own function map doing the rendering.

Render from a real backend
b, _ := confd.EnvBackend()          // reads OS environment variables
defer b.Close()
out, _ := confd.RenderWithBackend(`v={{getv "/app/host"}}`, b, confd.WithKeys("/app"))

f, _ := confd.FileBackend([]string{"data.yaml"}, "*.yaml")  // reads YAML/JSON
Process a confd config directory
p, _ := confd.NewProcessor(confd.ProcessorConfig{
    ConfDir: "/etc/confd",           // conf.d/*.toml + templates/*.tmpl
    Backend: b,
})
_ = p.Once()                         // render every resource once, write target files

// interval mode (blocking; run in a goroutine)
stop, done, errCh := make(chan bool), make(chan bool), make(chan error, 1)
go p.RunInterval(30, stop, done, errCh)

Backends

Backend Constructor External service? Test status
env EnvBackend() no fully tested
file FileBackend(files, filter) no fully tested
memory MemoryBackend(kv) / NewMemoryClient no fully tested (adapter seam)
etcd / consul / vault / redis / dynamodb / ssm / … NewBackend(backends.Config{Backend: …}) yes reachable, exercised via the in-memory seam; not tested against a live service

The env and file backends need nothing external and are the always-available path. The remote backends are constructed through confd's own backends.New; they are not mocked into a false pass — supply a real endpoint, or use the in-memory MemoryClient seam for tests and for the interpreter.

Design

  • Backend — selects a confd backend via backends.New.
  • MemoryClient — an in-memory backends.StoreClient (the one piece of StoreClient logic this package owns, so tests/rbgo can inject KV data).
  • Processor — runs confd's template processing over a config directory (Once / RunInterval); watch/interval sits behind a seam so tests do not block.
  • RenderString / RenderWithBackend — render a single template body to a string. 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.

Licensing

This package is BSD-3-Clause (the go-ruby-confd/confd authors; see LICENSE, with SPDX headers on every source file).

The dependency github.com/abtreece/confd is MIT-licensed (Copyright © 2013 Kelsey Hightower). It is imported and built from source, not vendored or forked into this repository, so this project's own code does not mix licenses. Consumers of a compiled binary that links confd must observe confd's MIT terms for that portion.

Benchmarks

See BENCHMARKS.md. Because this package imports confd rather than reimplementing it, confd itself is the performance reference and the adapter overhead is negligible.

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

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

func SetLogLevel(level string) error

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

func EnvBackend() (*Backend, error)

EnvBackend returns a Backend reading confd keys from OS environment variables. It needs no external service and is always available.

func FileBackend

func FileBackend(files []string, filter string) (*Backend, error)

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

func MemoryBackend(kv map[string]string) *Backend

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

func NewBackend(cfg backends.Config) (*Backend, error)

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) Close

func (b *Backend) Close() error

Close releases any resources held by the backend.

func (*Backend) GetValues

func (b *Backend) GetValues(ctx context.Context, keys []string) (map[string]string, error)

GetValues fetches the values under the given key prefixes from the backend, delegating to confd's StoreClient.

func (*Backend) HealthCheck

func (b *Backend) HealthCheck(ctx context.Context) error

HealthCheck reports whether the backend connection is healthy.

func (*Backend) Name

func (b *Backend) Name() string

Name reports the backend kind ("env", "file", "memory", …).

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

func (c *MemoryClient) GetValues(ctx context.Context, keys []string) (map[string]string, error)

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

func (p *Processor) Once() error

Once loads and renders every template resource exactly once, writing the target config files. It runs confd's template.Process.

func (*Processor) RunInterval

func (p *Processor) RunInterval(intervalSeconds int, stop, done chan bool, errCh chan error)

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL