etcd

package module
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EtcdContainer

type EtcdContainer struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

EtcdContainer represents the etcd container type used in the module. It can be used to create a single-node instance or a cluster. For the cluster, the first node creates the cluster and the other nodes join it as child nodes.

func Run

Run creates an instance of the etcd container type

Example
// runetcdContainer {
ctx := context.Background()

etcdContainer, err := etcd.Run(ctx, "gcr.io/etcd-development/etcd:v3.5.14")
defer func() {
	if err := testcontainers.TerminateContainer(etcdContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
// }

state, err := etcdContainer.State(ctx)
if err != nil {
	log.Printf("failed to get container state: %s", err)
	return
}

fmt.Println(state.Running)
Output:

true
Example (Cluster)
ctx := context.Background()

ctr, err := etcd.Run(ctx, "gcr.io/etcd-development/etcd:v3.5.14", etcd.WithNodes("etcd-1", "etcd-2", "etcd-3"))
if err != nil {
	log.Printf("failed to start container: %s", err)
	return
}
defer func() {
	if err := testcontainers.TerminateContainer(ctr); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()

clientEndpoints, err := ctr.ClientEndpoints(ctx)
if err != nil {
	log.Printf("failed to get client endpoints: %s", err)
	return
}

// we have 3 nodes, 1 cluster node and 2 child nodes
fmt.Println(len(clientEndpoints))

cli, err := clientv3.New(clientv3.Config{
	Endpoints:   clientEndpoints,
	DialTimeout: 5 * time.Second,
})
if err != nil {
	log.Printf("failed to create etcd client: %s", err)
	return
}
defer cli.Close()

ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
_, err = cli.Put(ctx, "sample_key", "sample_value")
if err != nil {
	log.Printf("failed to put key: %s", err)
	return
}

resp, err := cli.Get(ctx, "sample_key")
if err != nil {
	log.Printf("failed to get key: %s", err)
	return
}

fmt.Println(len(resp.Kvs))
fmt.Println(string(resp.Kvs[0].Value))
Output:

3
1
sample_value

func (*EtcdContainer) ClientEndpoint

func (c *EtcdContainer) ClientEndpoint(ctx context.Context) (string, error)

ClientEndpoint returns the client endpoint for the etcd container, and an error if any. For a cluster, it returns the client endpoint of the first node.

func (*EtcdContainer) ClientEndpoints

func (c *EtcdContainer) ClientEndpoints(ctx context.Context) ([]string, error)

ClientEndpoints returns the client endpoints for the etcd cluster.

func (*EtcdContainer) PeerEndpoint

func (c *EtcdContainer) PeerEndpoint(ctx context.Context) (string, error)

PeerEndpoint returns the peer endpoint for the etcd container, and an error if any. For a cluster, it returns the peer endpoint of the first node.

func (*EtcdContainer) PeerEndpoints

func (c *EtcdContainer) PeerEndpoints(ctx context.Context) ([]string, error)

PeerEndpoints returns the peer endpoints for the etcd cluster.

func (*EtcdContainer) Terminate

func (c *EtcdContainer) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error

Terminate terminates the etcd container, its child nodes, and the network in which the cluster is running to communicate between the nodes.

type Option

type Option func(*options) error

Option is an option for the Etcd container.

func WithAdditionalArgs

func WithAdditionalArgs(args ...string) Option

WithAdditionalArgs is an option to pass additional arguments to the etcd container. They will be appended last to the command line.

func WithClusterToken

func WithClusterToken(token string) Option

WithClusterToken is an option to set the cluster token.

func WithDataDir

func WithDataDir() Option

WithDataDir is an option to mount the data directory, which is located at /data.etcd. The option will add a lifecycle hook to the container to change the permissions of the data directory.

func WithNodes

func WithNodes(node1 string, node2 string, nodes ...string) Option

WithNodes is an option to set the nodes of the etcd cluster. It should be used to create a cluster with more than one node.

func (Option) Customize

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

Jump to

Keyboard shortcuts

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