1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package main
import ( "context" "crypto/tls" "crypto/x509" "io/ioutil" "log" "net" "net/http" "time"
"go.etcd.io/etcd/client" )
func main() {
etcdCA, err := ioutil.ReadFile("/Users/mritd/tmp/etcd_ssl/etcd-root-ca.pem") if err != nil { log.Fatal(err) }
etcdClientCert, err := tls.LoadX509KeyPair("/Users/mritd/tmp/etcd_ssl/etcd.pem", "/Users/mritd/tmp/etcd_ssl/etcd-key.pem") if err != nil { log.Fatal(err) }
rootCertPool := x509.NewCertPool() rootCertPool.AppendCertsFromPEM(etcdCA)
cfg := client.Config{ Endpoints: []string{"https://172.16.14.114:2379"}, Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{ KeepAlive: 30 * time.Second, Timeout: 30 * time.Second, }).DialContext, TLSClientConfig: &tls.Config{ RootCAs: rootCertPool, Certificates: []tls.Certificate{etcdClientCert}, }, TLSHandshakeTimeout: 10 * time.Second, }, HeaderTimeoutPerRequest: time.Second, } c, err := client.New(cfg) if err != nil { log.Fatal(err) } kapi := client.NewKeysAPI(c) log.Print("Setting '/foo' key with 'bar' value") resp, err := kapi.Set(context.Background(), "/foo", "bar", nil) if err != nil { log.Fatal(err) } else { log.Printf("Set is done. Metadata is %q\n", resp) } log.Print("Getting '/foo' key value") resp, err = kapi.Get(context.Background(), "/foo", nil) if err != nil { log.Fatal(err) } else { log.Printf("Get is done. Metadata is %q\n", resp) log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value) } }
|