go-rancher-metadata update

This commit is contained in:
Alena Prokharchyk
2017-01-27 13:44:40 -08:00
parent 5580566aee
commit a4ccfcc613
3 changed files with 52 additions and 15 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ github.com/juju/ratelimit 77ed1c8
github.com/kolo/xmlrpc 0826b98
github.com/miekg/dns 48ab660
github.com/prasmussen/gandi-api 13205dc
github.com/rancher/go-rancher-metadata/metadata cd79328
github.com/rancher/go-rancher-metadata/metadata 2ba6d4e
github.com/rancher/go-rancher/client 37c17e4
github.com/tent/http-link-go ac974c6
github.com/valyala/fasttemplate 3b87495
+26 -9
View File
@@ -9,8 +9,10 @@ import (
)
func (m *client) OnChangeWithError(intervalSeconds int, do func(string)) error {
version := "init"
return m.onChangeFromVersionWithError("init", intervalSeconds, do)
}
func (m *client) onChangeFromVersionWithError(version string, intervalSeconds int, do func(string)) error {
for {
newVersion, err := m.waitVersion(intervalSeconds, version)
if err != nil {
@@ -28,20 +30,35 @@ func (m *client) OnChangeWithError(intervalSeconds int, do func(string)) error {
}
func (m *client) OnChange(intervalSeconds int, do func(string)) {
version := "init"
updateVersionAndDo := func(v string) {
version = v
do(version)
}
interval := time.Duration(intervalSeconds)
for {
if err := m.OnChangeWithError(intervalSeconds, do); err != nil {
if err := m.onChangeFromVersionWithError(version, intervalSeconds, updateVersionAndDo); err != nil {
logrus.Errorf("Error reading metadata version: %v", err)
}
time.Sleep(interval * time.Second)
}
}
func (m *client) waitVersion(maxWait int, version string) (string, error) {
resp, err := m.SendRequest(fmt.Sprintf("/version?wait=true&value=%s&maxWait=%d", version, maxWait))
if err != nil {
return "", err
}
err = json.Unmarshal(resp, &version)
return version, err
type timeout interface {
Timeout() bool
}
func (m *client) waitVersion(maxWait int, version string) (string, error) {
for {
resp, err := m.SendRequest(fmt.Sprintf("/version?wait=true&value=%s&maxWait=%d", version, maxWait))
if err != nil {
t, ok := err.(timeout)
if ok && t.Timeout() {
continue
}
return "", err
}
err = json.Unmarshal(resp, &version)
return version, err
}
}
+25 -5
View File
@@ -74,11 +74,13 @@ type Container struct {
}
type Network struct {
Name string `json:"name"`
UUID string `json:"uuid"`
Metadata map[string]interface{} `json:"metadata"`
HostPorts bool `json:"host_ports"`
Default bool `json:"is_default"`
Name string `json:"name"`
UUID string `json:"uuid"`
Metadata map[string]interface{} `json:"metadata"`
HostPorts bool `json:"host_ports"`
Default bool `json:"is_default"`
Policy []NetworkPolicyRule `json:"policy,omitempty"`
DefaultPolicyAction string `json:"default_policy_action"`
}
type Host struct {
@@ -123,3 +125,21 @@ type LBStickinessPolicy struct {
Postonly bool `json:"postonly"`
Mode string `json:"mode"`
}
type NetworkPolicyRuleBetween struct {
Selector string `yaml:"selector,omitempty"`
GroupBy string `yaml:"groupBy,omitempty"`
}
type NetworkPolicyRuleMember struct {
Selector string `yaml:"selector,omitempty"`
}
type NetworkPolicyRule struct {
From *NetworkPolicyRuleMember `yaml:"from"`
To *NetworkPolicyRuleMember `yaml:"to"`
Ports []string `yaml:"ports"`
Within string `yaml:"within"`
Between *NetworkPolicyRuleBetween `yaml:"between"`
Action string `yaml:"action"`
}