Skip to content

Instantly share code, notes, and snippets.

@alfredr
Created September 30, 2015 16:09
Show Gist options
  • Save alfredr/3d02ecb54fe0a8b6e8dc to your computer and use it in GitHub Desktop.
Save alfredr/3d02ecb54fe0a8b6e8dc to your computer and use it in GitHub Desktop.
quick hack for finding libnetwork container endpoints by container
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"text/tabwriter"
)
type ConsulKV struct {
CreateIndex int
ModifyIndex int
LockIndex int
Flags int
Key string
Value string
}
func main() {
keys, err := fetch("http://consul:8500/v1/kv/docker/libnetwork/endpoint/52c322c633dd216209922cfa3b2114d628e066444d5e9f5cdd49784c0fd046c9/?recurse")
if err != nil {
panic(err)
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
for i := range keys {
value, err := base64.StdEncoding.DecodeString(keys[i].Value)
if err != nil {
panic(err)
}
var valueData map[string]interface{}
json.Unmarshal(value, &valueData)
fmt.Fprintln(w, valueData["name"].(string)+"\t"+valueData["id"].(string))
}
w.Flush()
}
func fetch(url string) ([]ConsulKV, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var kvs []ConsulKV
json.Unmarshal(body, &kvs)
return kvs, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment