From d036a19d8ebe4cce3cba658b8d1901c57dd38644 Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Wed, 13 Jan 2016 11:04:07 -0800 Subject: [PATCH 1/3] Godeps update for go-rancher-metadata bindings --- Godeps/Godeps.json | 2 +- .../go-rancher-metadata/metadata/metadata.go | 21 +++++++++++++++++++ .../go-rancher-metadata/metadata/types.go | 4 +++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 54f9bc1..8ec4dbf 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -38,7 +38,7 @@ }, { "ImportPath": "github.com/rancher/go-rancher-metadata/metadata", - "Rev": "3235034fa02c64bf78855c2673386260b5c43cc7" + "Rev": "9f075a094b0c421ae795af18dba1c8ee45d81ff7" }, { "ImportPath": "github.com/rancher/go-rancher/client", diff --git a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go index 28b93e2..97047a0 100644 --- a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go +++ b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/metadata.go @@ -33,6 +33,11 @@ func (m *Client) SendRequest(path string) ([]byte, error) { if err != nil { return nil, err } + + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Error %v accessing %v path", resp.StatusCode, path) + } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -117,6 +122,22 @@ func (m *Client) GetContainers() ([]Container, error) { return containers, nil } +func (m *Client) GetServiceContainers(serviceName string, stackName string) ([]Container, error) { + var serviceContainers = []Container{} + containers, err := m.GetContainers() + if err != nil { + return serviceContainers, err + } + + for _, container := range containers { + if container.StackName == stackName && container.ServiceName == serviceName { + serviceContainers = append(serviceContainers, container) + } + } + + return serviceContainers, nil +} + func (m *Client) GetHosts() ([]Host, error) { resp, err := m.SendRequest("/hosts") var hosts []Host diff --git a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go index 7d63401..92ec4a7 100644 --- a/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go +++ b/Godeps/_workspace/src/github.com/rancher/go-rancher-metadata/metadata/types.go @@ -7,12 +7,14 @@ type Stack struct { } type Service struct { + Scale int `json:"scale"` Name string `json:"name"` StackName string `json:"stack_name"` Kind string `json:"kind"` Hostname string `json:"hostname"` Vip string `json:"vip"` - CreateIndex string `json:"create_index"` + CreateIndex int `json:"create_index"` + UUID string `json:"uuid"` ExternalIps []string `json:"external_ips"` Sidekicks []string `json:"sidekicks"` Containers []string `json:"containers"` From 1343dc5bc43e3a4636ca2a355b8b346e808769e3 Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Wed, 13 Jan 2016 11:21:50 -0800 Subject: [PATCH 2/3] Explicit version for rancher-metadata --- metadata/metadata.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index 00910a3..8f10f89 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -7,7 +7,7 @@ import ( ) const ( - metadataUrl = "http://rancher-metadata/latest" + metadataUrl = "http://rancher-metadata/2015-07-25" ) type MetadataClient struct { From 450d47d29b9d026134bbfbd5ec2e55e847fd657d Mon Sep 17 00:00:00 2001 From: Alena Prokharchyk Date: Wed, 13 Jan 2016 14:23:42 -0800 Subject: [PATCH 3/3] Don't start rancher metadata till selfstack info is returned --- metadata/metadata.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/metadata/metadata.go b/metadata/metadata.go index 8f10f89..b2b22ef 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -1,9 +1,11 @@ package metadata import ( + "fmt" "github.com/Sirupsen/logrus" "github.com/rancher/external-dns/dns" "github.com/rancher/go-rancher-metadata/metadata" + "time" ) const ( @@ -15,19 +17,36 @@ type MetadataClient struct { EnvironmentName string } +func getEnvironmentName(m *metadata.Client) (string, error) { + timeout := 30 * time.Second + var err error + var stack metadata.Stack + for i := 1 * time.Second; i < timeout; i *= time.Duration(2) { + stack, err = m.GetSelfStack() + if err != nil { + logrus.Errorf("Error reading stack info: %v...will retry", err) + time.Sleep(i) + } else { + return stack.EnvironmentName, nil + } + } + return "", fmt.Errorf("Error reading stack info: %v", err) +} + func NewMetadataClient() (*MetadataClient, error) { m, err := metadata.NewClientAndWait(metadataUrl) if err != nil { logrus.Fatalf("Failed to configure rancher-metadata: %v", err) } - selfStack, err := m.GetSelfStack() + + envName, err := getEnvironmentName(m) if err != nil { logrus.Fatalf("Error reading stack info: %v", err) } return &MetadataClient{ MetadataClient: m, - EnvironmentName: selfStack.EnvironmentName, + EnvironmentName: envName, }, nil }