etcdutil

package module
v1.6.7 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 11 Imported by: 19

README

GitHub release CI PkgGoDev Go Report Card

Add-ons for etcd

This package provides utility for using etcd.

Specifications

Programs using this package can implement following common specifications.

YAML/JSON configuration file

The etcd parameters can be embedded in YAML or JSON file.

endpoints:
  - http://10.1.2.3:2379
  - http://10.11.12.13:2379
prefix: "/key-prefix/"
timeout: "2s"

# user/pass authentication
username: "etcd-user-name"
password: "etcd-password"

# etcd server certificates' CA
tls-ca-file: "/etc/ssl/my-ca.crt"

# etcd client certificate authentication
tls-cert-file: "/path/to/my-user.crt"
tls-key-file: "/path/to/my-user.key"

# certificats may be embedded
tls-ca: |
  -----BEGIN CERTIFICATE-----
  MIICAzCCAWwCCQCgYvbe6d0oLzANBgkqhkiG9w0BAQUFADBGMQswCQYDVQQGEwJK
  ....
  -----END CERTIFICATE-----
tls-cert: |
  -----BEGIN CERTIFICATE-----
  MIICAzCCAWwCCQCgYvbe6d0oLzANBgkqhkiG9w0BAQUFADBGMQswCQYDVQQGEwJK
  ....
  -----END CERTIFICATE-----
tls-key: |
  -----BEGIN PRIVATE KEY-----
  MIICAzCCAWwCCQCgYvbe6d0oLzANBgkqhkiG9w0BAQUFADBGMQswCQYDVQQGEwJK
  ....
  -----END PRIVATE KEY-----
Command-line flags
Name Default Description
etcd-endpoints http://127.0.0.1:2379 comma-separated URLs of the backend etcd
etcd-password password for etcd authentication
etcd-prefix prefix for etcd keys
etcd-timeout 2s dial timeout duration to etcd
etcd-tls-ca Path to CA bundle used to verify etcd server certificates.
etcd-tls-cert Path to client certificate file of an etcd user.
etcd-tls-key Path to private key file of an etcd user.
etcd-username username for etcd authentication

Usage

Read the documentation.

License

etcdutil is licensed under the Apache License, Version 2.0.

Documentation

Overview

Package etcdutil helps implementation of the common specification to take etcd connection parameters.

To read parameters from YAML (or JSON):

import (
    "github.com/cybozu-go/etcdutil"
    "sigs.k8s.io/yaml"
)

func main() {
    cfg := etcdutil.NewConfig("/key-prefix/")
    err := yaml.Unmarshal(data, cfg)
    if err != nil {
        panic(err)
    }

    etcd, err := etcdutil.NewClient(cfg)
    if err != nil {
        panic(err)
    }
    defer etcd.Close()

    // use etcd; it is a etcd *clientv3.Client object.
}

To read parameters from command-line flags:

import (
    "flag"

    "github.com/cybozu-go/etcdutil"
)

func main() {
    cfg := etcdutil.NewConfig("/key-prefix/")
    cfg.AddFlags(flag.CommandLine)
    flag.Parse()

    etcd, err := etcdutil.NewClient(cfg)
    if err != nil {
        panic(err)
    }
    defer etcd.Close()

    // use etcd; it is a etcd *clientv3.Client object.
}

Index

Constants

View Source
const (
	// DefaultTimeout is default etcd connection timeout.
	DefaultTimeout = "2s"
)

Variables

View Source
var (
	// DefaultEndpoints is default etcd servers.
	DefaultEndpoints = []string{"http://127.0.0.1:2379"}
)

Functions

func NewClient

func NewClient(c *Config) (*clientv3.Client, error)

NewClient creates etcd client.

Types

type Config

type Config struct {
	// Endpoints are etcd servers.
	Endpoints []string `json:"endpoints" toml:"endpoints"`
	// Prefix is etcd prefix key.
	Prefix string `json:"prefix" toml:"prefix"`
	// Timeout is dial timeout of the etcd client connection.
	Timeout string `json:"timeout" toml:"timeout"`
	// Username is username for loging in to the etcd.
	Username string `json:"username" toml:"username"`
	// Password is password for loging in to the etcd.
	Password string `json:"password" toml:"password"`
	// TLSCAFile is root CA path.
	TLSCAFile string `json:"tls-ca-file" toml:"tls-ca-file"`
	// TLSCA is root CA raw string.
	TLSCA string `json:"tls-ca" toml:"tls-ca"`
	// TLSCertFile is TLS client certificate path.
	TLSCertFile string `json:"tls-cert-file" toml:"tls-cert-file"`
	// TLSCert is TLS client certificate raw string.
	TLSCert string `json:"tls-cert" toml:"tls-cert"`
	// TLSKeyFile is TLS client private key.
	TLSKeyFile string `json:"tls-key-file" toml:"tls-key-file"`
	// TLSKey is TLS client private key raw string.
	TLSKey string `json:"tls-key" toml:"tls-key"`
}

Config represents configuration parameters to access etcd.

func NewConfig

func NewConfig(prefix string) *Config

NewConfig creates Config with default values.

func (*Config) AddFlags added in v1.2.0

func (c *Config) AddFlags(fs *flag.FlagSet)

AddFlags adds common set of command-line flags for etcd.

func (*Config) AddPFlags added in v1.3.0

func (c *Config) AddPFlags(fs *pflag.FlagSet)

AddPFlags is a variation of AddFlags for github.com/spf13/pflag package.

Jump to

Keyboard shortcuts

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