本文整理汇总了Golang中github.com/coreos/go-etcd/etcd.Client.Get方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Get方法的具体用法?Golang Client.Get怎么用?Golang Client.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/coreos/go-etcd/etcd.Client
的用法示例。
在下文中一共展示了Client.Get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CanConnect
// CanConnect checks a given SSH key against the list of authorized users in etcd
// to check if a user with a given key is allowed to connect. It takes in an active
// etcd.Client struct pointer and the ssh key to test and returns a username
// and boolean representing if they are allowed to connect.
func CanConnect(e *etcd.Client, sshkey string) (user string, allowed bool) {
reply, err := e.Get("/flitter/builder/users/", true, true)
if err != nil {
log.Printf("etcd: %s", err)
return "", false
}
keybit := strings.Split(sshkey, " ")[1]
fp := GetFingerprint(keybit)
for _, userdir := range reply.Node.Nodes {
for _, fpnode := range userdir.Nodes {
thisFpSplit := strings.Split(fpnode.Key, "/")
thisFp := thisFpSplit[len(thisFpSplit)-1]
if fp == thisFp {
userpath := strings.Split(userdir.Key, "/")
user := userpath[len(userpath)-1]
return user, true
}
}
}
return
}
示例2: acquireOrRenewLease
// acquireOrRenewLease either races to acquire a new master lease, or update the existing master's lease
// returns true if we have the lease, and an error if one occurs.
// TODO: use the master election utility once it is merged in.
func (c *Config) acquireOrRenewLease(etcdClient *etcd.Client) (bool, error) {
result, err := etcdClient.Get(c.key, false, false)
if err != nil {
if tools.IsEtcdNotFound(err) {
// there is no current master, try to become master, create will fail if the key already exists
_, err := etcdClient.Create(c.key, c.whoami, c.ttl)
if err != nil {
return false, err
}
c.lastLease = time.Now()
return true, nil
}
return false, err
}
if result.Node.Value == c.whoami {
glog.Infof("key already exists, we are the master (%s)", result.Node.Value)
// we extend our lease @ 1/2 of the existing TTL, this ensures the master doesn't flap around
if result.Node.Expiration.Sub(time.Now()) < time.Duration(c.ttl/2)*time.Second {
_, err := etcdClient.CompareAndSwap(c.key, c.whoami, c.ttl, c.whoami, result.Node.ModifiedIndex)
if err != nil {
return false, err
}
}
c.lastLease = time.Now()
return true, nil
}
glog.Infof("key already exists, the master is %s, sleeping.", result.Node.Value)
return false, nil
}
示例3: GetIndex
func GetIndex(client *etcd.Client, basedir string, jobname string, ip string) (int, error) {
jobdir := "/jobs" + "/" + jobname
response, err := client.Get(jobdir, true, true)
if err == nil {
for i := 0; i < response.Node.Nodes.Len(); i++ {
if response.Node.Nodes[i].Value == ip {
return i, nil
}
}
}
response, err = client.AddChild(jobdir, ip, 0)
if err != nil {
fmt.Printf("use etcd to get index error: %v\n", err)
return 0, err
}
mykey := response.Node.Key
response, err = client.Get(jobdir, true, true)
if err != nil {
fmt.Printf("get etcd jobdir error: %v\n", err)
return 0, err
}
for i := 0; i < response.Node.Nodes.Len(); i++ {
if response.Node.Nodes[i].Key == mykey {
return i, nil
}
}
// this line would never reach.
return 0, errors.New("etcd add child error!")
}
示例4: GetAddress
// getAddress will return the host:port address of the service taking care of
// the task that we want to talk to.
// Currently we grab the information from etcd every time. Local cache could be used.
// If it failed, e.g. network failure, it should return error.
func GetAddress(client *etcd.Client, name string, id uint64) (string, error) {
resp, err := client.Get(TaskMasterPath(name, id), false, false)
if err != nil {
return "", err
}
return resp.Node.Value, nil
}
示例5: latestRunningVersion
// latestRunningVersion retrieves the highest version of the application published
// to etcd. If no app has been published, returns 0.
func latestRunningVersion(client *etcd.Client, appName string) int {
r := regexp.MustCompile(appNameRegex)
if client == nil {
// FIXME: client should only be nil during tests. This should be properly refactored.
if appName == "ceci-nest-pas-une-app" {
return 3
}
return 0
}
resp, err := client.Get(fmt.Sprintf("/deis/services/%s", appName), false, true)
if err != nil {
// no app has been published here (key not found) or there was an error
return 0
}
var versions []int
for _, node := range resp.Node.Nodes {
match := r.FindStringSubmatch(node.Key)
// account for keys that may not be an application container
if match == nil {
continue
}
version, err := strconv.Atoi(match[2])
if err != nil {
log.Println(err)
return 0
}
versions = append(versions, version)
}
return max(versions)
}
示例6: GetAndWatchEpoch
func GetAndWatchEpoch(client *etcd.Client, appname string, epochC chan uint64, stop chan bool) (uint64, error) {
resp, err := client.Get(EpochPath(appname), false, false)
if err != nil {
log.Fatal("etcdutil: can not get epoch from etcd")
}
ep, err := strconv.ParseUint(resp.Node.Value, 10, 64)
if err != nil {
return 0, err
}
receiver := make(chan *etcd.Response, 1)
go client.Watch(EpochPath(appname), resp.EtcdIndex+1, false, receiver, stop)
go func() {
for resp := range receiver {
if resp.Action != "compareAndSwap" && resp.Action != "set" {
continue
}
epoch, err := strconv.ParseUint(resp.Node.Value, 10, 64)
if err != nil {
log.Fatal("etcdutil: can't parse epoch from etcd")
}
epochC <- epoch
}
}()
return ep, nil
}
示例7: getRuntimeConfFromEtcd
func getRuntimeConfFromEtcd(client *etcd.Client, etcd_path string) (*RuntimeConfig, error) {
resp, err := client.Get(etcd_path, false, false)
if err != nil {
return nil, fmt.Errorf("failed to get RuntimeConfig:%v", err)
}
r := bytes.NewReader([]byte(resp.Node.Value))
return ReadRuntimeConfig(r)
}
示例8: handleChange
// On any change we just refresh everything
func handleChange(etcdClient *etcd.Client) {
resp, err := etcdClient.Get(etcdWatchKey, false, true)
if err != nil {
fmt.Println(err)
return
}
// Go and fetch all the services in REDIS
redisClient := clients.RedisClient()
if (redisClient == nil) {
fmt.Println("Couldn't connect to redis")
return
}
defer redisClient.Close()
keys, err := redis.Strings(redisClient.Do("KEYS", "frontend:*"))
if err != nil {
fmt.Println(err)
return
}
// Now we delete everything in redis and add everything back in from etcd
redisClient.Send("MULTI")
if len(keys) > 0 {
redisClient.Send("DEL", redis.Args{}.AddFlat(keys)...)
}
for _, node := range resp.Node.Nodes {
// This first level is a frontend
split := strings.Split(node.Key, "/")
domain := split[len(split) - 1]
frontendKey := "frontend:" + domain
// Belt and braces - delete this key just in case its not already (but it should be)
redisClient.Send("DEL", frontendKey)
redisClient.Send("RPUSH", frontendKey, domain)
for _, instNode := range node.Nodes {
instsplit := strings.Split(instNode.Key, "/")
inst := instsplit[len(instsplit) - 1]
host := "http://" + inst
redisClient.Send("RPUSH", frontendKey, host)
}
}
// Should be everything
_, err = redisClient.Do("EXEC")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Resynced hipache from etcd")
}
示例9: getEtcdRoute
func getEtcdRoute(client *etcd.Client) *Route {
hostResp, err := client.Get("/deis/logs/host", false, false)
assert(err, "url")
portResp, err := client.Get("/deis/logs/port", false, false)
assert(err, "url")
protocol := getEtcdValueOrDefault(client, "/deis/logs/protocol", "udp")
host := fmt.Sprintf("%s:%s", hostResp.Node.Value, portResp.Node.Value)
log.Printf("routing all to %s://%s", protocol, host)
return &Route{ID: "etcd", Target: Target{Type: "syslog", Addr: host, Protocol: protocol}}
}
示例10: getEtcdValueOrDefault
func getEtcdValueOrDefault(c *etcd.Client, key string, defaultValue string) string {
resp, err := c.Get(key, false, false)
if err != nil {
if strings.Contains(fmt.Sprintf("%v", err), "Key not found") {
return defaultValue
}
assert(err, "url")
}
return resp.Node.Value
}
示例11: get
func get(e *etcd.Client, q string, t uint16) ([]dns.RR, error) {
path := questionToPath(q, t)
r, err := e.Get(path, false, false)
if err != nil {
return nil, err
}
h := dns.RR_Header{Name: q, Rrtype: t, Class: dns.ClassINET, Ttl: 60} // Ttl is overridden
rr := parseValue(t, r.Node.Value, h)
return []dns.RR{rr}, nil
}
示例12: getCurrentVersion
func getCurrentVersion(client *etcd.Client, key string) *etcd.Response {
for {
if resp, err := client.Get(key, false, false); err == nil {
// failed to fetch first value
return resp
} else {
time.Sleep(time.Second)
}
}
}
示例13: loadConfig
func loadConfig(client *etcd.Client, config *server.Config) error {
// Override what isn't set yet from the command line.
n, err := client.Get("/"+msg.PathPrefix+"/config", false, false)
if err != nil {
log.Printf("skydns: falling back to default configuration, could not read from etcd: %s", err)
return nil
}
if err := json.Unmarshal([]byte(n.Node.Value), config); err != nil {
return fmt.Errorf("failed to unmarshal config: %s", err.Error())
}
return nil
}
示例14: WaitFreeTask
// WaitFreeTask blocks until it gets a hint of free task
func WaitFreeTask(client *etcd.Client, name string, logger *log.Logger) (uint64, error) {
slots, err := client.Get(FreeTaskDir(name), false, true)
if err != nil {
return 0, err
}
if total := len(slots.Node.Nodes); total > 0 {
ri := rand.Intn(total)
s := slots.Node.Nodes[ri]
idStr := path.Base(s.Key)
id, err := strconv.ParseUint(idStr, 0, 64)
if err != nil {
return 0, err
}
logger.Printf("got free task %v, randomly choose %d to try...", ListKeys(slots.Node.Nodes), ri)
return id, nil
}
watchIndex := slots.EtcdIndex + 1
respChan := make(chan *etcd.Response, 1)
go func() {
for {
logger.Printf("start to wait failure at index %d", watchIndex)
resp, err := client.Watch(FreeTaskDir(name), watchIndex, true, nil, nil)
if err != nil {
logger.Printf("WARN: WaitFailure watch failed: %v", err)
return
}
if resp.Action == "set" {
respChan <- resp
return
}
watchIndex = resp.EtcdIndex + 1
}
}()
var resp *etcd.Response
var waitTime uint64 = 0
for {
select {
case resp = <-respChan:
idStr := path.Base(resp.Node.Key)
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
return 0, err
}
return id, nil
case <-time.After(10 * time.Second):
waitTime++
logger.Printf("Node already wait failure for %d0s", waitTime)
}
}
}
示例15: TestEtcdClient
// TestEtcdClient verifies a client is functional. It will attempt to
// connect to the etcd server and block until the server responds at least once, or return an
// error if the server never responded.
func TestEtcdClient(etcdClient *etcdclient.Client) error {
for i := 0; ; i++ {
_, err := etcdClient.Get("/", false, false)
if err == nil || etcdutil.IsEtcdNotFound(err) {
break
}
if i > 100 {
return fmt.Errorf("could not reach etcd: %v", err)
}
time.Sleep(50 * time.Millisecond)
}
return nil
}