etcd

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package etcd provides a confkit Source for etcd v3 configuration storage.

Import and use with Load:

import "github.com/MimoJanra/confkit/etcd"

cfg, err := confkit.Load[Config](
    etcd.FromEtcd([]string{"etcd1.example.com:2379", "etcd2.example.com:2379"}),
)

Configuration

Create an etcd source with optional key prefix:

// Basic usage (no prefix)
src := etcd.FromEtcd([]string{
    "etcd1.example.com:2379",
    "etcd2.example.com:2379",
})

// With key prefix
src := etcd.FromEtcdWithPrefix(
    []string{"etcd.example.com:2379"},
    "/myapp",
)

Usage

Config fields are mapped to etcd keys using dot notation:

type Config struct {
    Host     string `validate:"required"`
    Port     int    `validate:"min=1,max=65535"`
    APIKey   string `secret:"true" validate:"required"`
}

cfg, err := confkit.Load[Config](
    etcd.FromEtcdWithPrefix(
        []string{"etcd.example.com:2379"},
        "/myapp",
    ),
)

With prefix "/myapp", keys are: /myapp/host → Config.Host /myapp/port → Config.Port /myapp/api_key → Config.APIKey

Endpoints

etcd sources require at least one endpoint. Multiple endpoints enable: • Load balancing • High availability • Automatic failover

Endpoints should be in the format: hostname:2379 (default etcd port is 2379)

Secrets

Always mark sensitive fields with secret:"true":

type Config struct {
    APIKey   string `secret:"true"`
    Token    string `secret:"true"`
}

Secrets are automatically redacted in error messages and logs.

High Availability

For production, use multiple etcd endpoints:

endpoints := []string{
    "etcd1.example.com:2379",
    "etcd2.example.com:2379",
    "etcd3.example.com:2379",
}
cfg, err := confkit.Load[Config](etcd.FromEtcd(endpoints))

The client will automatically handle failover between endpoints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromEtcd

func FromEtcd(endpoints []string) confkit.Source

FromEtcd reads keys under the default "/myapp/" prefix. Use FromEtcdWithPrefix to choose your own.

func FromEtcdWithPrefix

func FromEtcdWithPrefix(endpoints []string, prefix string) confkit.Source

FromEtcdWithPrefix reads keys under prefix, appending a trailing slash if absent.

A connection failure is not reported here: the returned Source fails every lookup, so the problem appears in the load's ErrorReport. Note that the returned Source cannot be closed; use NewEtcdSource when you need to release the client.

Types

type EtcdSource

type EtcdSource struct {
	// contains filtered or unexported fields
}

EtcdSource reads configuration values from an etcd v3 key-value store.

Keys are the field's dotted path lower-cased and joined to the prefix, so DB.Host under prefix "/myapp/" reads "/myapp/db.host".

func NewEtcdSource

func NewEtcdSource(endpoints []string, prefix string) (*EtcdSource, error)

NewEtcdSource dials endpoints and returns a source reading keys under prefix. The per-lookup timeout defaults to five seconds; change it with SetTimeout. Close the source when finished to release the client.

func (*EtcdSource) Close

func (e *EtcdSource) Close() error

Close releases the underlying etcd client.

func (*EtcdSource) Lookup

func (e *EtcdSource) Lookup(ctx context.Context, field *confkit.FieldInfo) (any, bool, error)

Lookup fetches the key for field, bounded by the configured timeout. A key that does not exist means not found rather than an error.

func (*EtcdSource) Name

func (e *EtcdSource) Name() string

Name returns "etcd".

func (*EtcdSource) SetTimeout

func (e *EtcdSource) SetTimeout(seconds int)

SetTimeout sets the per-lookup timeout in seconds. Call it before loading, as it is not safe to change concurrently with Lookup.

Jump to

Keyboard shortcuts

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