Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
New etcd package
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lipovetsky authored and dlipovetsky committed Sep 4, 2018
1 parent c172481 commit 8174674
Show file tree
Hide file tree
Showing 22 changed files with 3,105 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@
[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/google/go-cmp"
version = "0.2.0"
77 changes: 77 additions & 0 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package etcd

import (
"fmt"
"time"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/pkg/transport"
"github.com/coreos/etcd/snapshot"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/platform9/etcdadm/apis"
)

// ClientForEndpoint returns an etcd client that will use the given etcd endpoint.
func ClientForEndpoint(endpoint string, cfg *apis.EtcdAdmConfig) (*clientv3.Client, error) {
tlsInfo := transport.TLSInfo{
CertFile: cfg.EtcdctlCertFile,
KeyFile: cfg.EtcdctlKeyFile,
TrustedCAFile: cfg.TrustedCAFile,
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return nil, fmt.Errorf("unable to create TLS client: %v", err)
}

cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{endpoint},
DialTimeout: 5 * time.Second,
TLS: tlsConfig,
})
return cli, err
}

// MemberForPeerURLs searches the list for a member with matching peerURLs.
func MemberForPeerURLs(members []*etcdserverpb.Member, peerURLs []string) (bool, *etcdserverpb.Member) {
for _, m := range members {
if cmp.Equal(m.PeerURLs, peerURLs, cmpopts.EquateEmpty()) {
return true, m
}
}
return false, nil
}

// MemberForID searches the list for a member with a matching ID.
func MemberForID(members []*etcdserverpb.Member, id uint64) (bool, *etcdserverpb.Member) {
for _, m := range members {
if m.ID == id {
return true, m
}
}
return false, nil
}

// Started checks whether the member has started.
func Started(member etcdserverpb.Member) bool {
unstarted := (member.Name == "" && len(member.ClientURLs) == 0)
return !unstarted
}

// RestoreSnapshot initializes the etcd data directory from a snapshot
func RestoreSnapshot(cfg *apis.EtcdAdmConfig) error {
sp := snapshot.NewV3(nil)

return sp.Restore(snapshot.RestoreConfig{
SnapshotPath: cfg.Snapshot,
Name: cfg.Name,
OutputDataDir: cfg.DataDir,
PeerURLs: cfg.InitialAdvertisePeerURLs.StringSlice(),
InitialCluster: cfg.InitialCluster,
InitialClusterToken: cfg.InitialClusterToken,
SkipHashCheck: cfg.SkipHashCheck,
})
}
27 changes: 27 additions & 0 deletions vendor/github.com/google/go-cmp/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions vendor/github.com/google/go-cmp/cmp/cmpopts/equate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/google/go-cmp/cmp/cmpopts/ignore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8174674

Please sign in to comment.