diff options
author | Satoshi Fujii <fujii-satoshi@jp.fujitsu.com> | 2020-11-02 07:45:57 +0000 |
---|---|---|
committer | Satoshi Fujii <fujii-satoshi@jp.fujitsu.com> | 2020-11-02 07:45:57 +0000 |
commit | e97a00243313385c1532d184b4ed1b8de7a1a518 (patch) | |
tree | a90dd2731dbe570d7dd56fe8056c6bba56a284d1 /src/kube2msb | |
parent | 66c00e4df2bcb8f6142d6f5573e4db3f429cfeb4 (diff) |
Fix registration failure due to improper handling braceshonolulu
mergeIP function did not handle braces '{' properly
and JSON structure was broken by the improper string replacement.
This change removes mergeIP func and IP address is passed directly
to Register and DeRegister functions.
Issue-ID: MSB-534
Signed-off-by: Satoshi Fujii <fujii-satoshi@jp.fujitsu.com>
Change-Id: Icc8a1f00fba5248218d45e4d9476042c5c331004
Diffstat (limited to 'src/kube2msb')
-rw-r--r-- | src/kube2msb/kube2msb_test.go | 4 | ||||
-rw-r--r-- | src/kube2msb/msb_client.go | 10 | ||||
-rw-r--r-- | src/kube2msb/msb_client_test.go | 11 | ||||
-rw-r--r-- | src/kube2msb/msb_work.go | 19 | ||||
-rw-r--r-- | src/kube2msb/msb_work_test.go | 17 |
5 files changed, 17 insertions, 44 deletions
diff --git a/src/kube2msb/kube2msb_test.go b/src/kube2msb/kube2msb_test.go index a71c035..fe49c2b 100644 --- a/src/kube2msb/kube2msb_test.go +++ b/src/kube2msb/kube2msb_test.go @@ -85,7 +85,7 @@ func TestSendServiceWork(t *testing.T) { got := <-kubeWorkQueue if got.Action != c { - t.Errorf("sendServiceWork(%action, queue, service) got %gotAction", c, got.Action) + t.Errorf("sendServiceWork(%v, queue, service) got %v", c, got.Action) } } } @@ -106,7 +106,7 @@ func TestSendPodWork(t *testing.T) { got := <-kubeWorkQueue if got.Action != c { - t.Errorf("sendPodWork(%action, queue, service) got %gotAction", c, got.Action) + t.Errorf("sendPodWork(%v, queue, service) got %v", c, got.Action) } } } diff --git a/src/kube2msb/msb_client.go b/src/kube2msb/msb_client.go index 9e16d00..0a6d17b 100644 --- a/src/kube2msb/msb_client.go +++ b/src/kube2msb/msb_client.go @@ -28,8 +28,8 @@ const ( ) type Client interface { - Register(string) - DeRegister(string) + Register(string, string) + DeRegister(string, string) } type MSBAgent struct { @@ -51,7 +51,7 @@ func newMSBAgent(s string) (*MSBAgent, error) { return &MSBAgent{url: s}, nil } -func (client *MSBAgent) Register(serviceInfo string) { +func (client *MSBAgent) Register(ip, serviceInfo string) { var ( sas = []*ServiceAnnotation{} ) @@ -62,6 +62,7 @@ func (client *MSBAgent) Register(serviceInfo string) { } for _, sa := range sas { + sa.IP = ip su := ServiceAnnotation2ServiceUnit(sa) body, _ := json.Marshal(su) postURL := client.url + urlPrefix @@ -76,7 +77,7 @@ func (client *MSBAgent) Register(serviceInfo string) { } } -func (client *MSBAgent) DeRegister(serviceInfo string) { +func (client *MSBAgent) DeRegister(ip, serviceInfo string) { var ( sas = []*ServiceAnnotation{} ) @@ -88,6 +89,7 @@ func (client *MSBAgent) DeRegister(serviceInfo string) { } for _, sa := range sas { + sa.IP = ip var deleteURL string if sa.Version == "" { deleteURL = client.url + urlPrefix + "/" + sa.ServiceName + "/version/" + "null" + "/nodes/" + sa.IP + "/" + sa.Port diff --git a/src/kube2msb/msb_client_test.go b/src/kube2msb/msb_client_test.go index 2897085..74e71fe 100644 --- a/src/kube2msb/msb_client_test.go +++ b/src/kube2msb/msb_client_test.go @@ -67,14 +67,14 @@ func TestServiceAnnotation2ServiceUnit(t *testing.T) { } unit := ServiceAnnotation2ServiceUnit(&sa) - if unit.Name != sa.ServiceName || unit.Version != sa.Version || unit.URL != sa.URL || unit.Protocol != sa.Protocol || unit.LBPolicy != sa.LBPolicy || unit.LBPolicy != sa.LBPolicy || unit.Path != sa.Path || unit.EnableSSL != sa.EnableSSL || unit.Instances[0].ServiceIP != sa.IP || unit.Instances[0].ServicePort != sa.Port { + if unit.Name != sa.ServiceName || unit.Version != sa.Version || unit.URL != sa.URL || unit.Protocol != sa.Protocol || unit.LBPolicy != sa.LBPolicy || unit.Path != sa.Path || unit.EnableSSL != sa.EnableSSL || unit.Instances[0].ServiceIP != sa.IP || unit.Instances[0].ServicePort != sa.Port { t.Errorf("ServiceAnnotation2ServiceUnit error") } } func TestRegister(t *testing.T) { + ip := "192.168.1.10" serviceInfo := `[{ - "ip":"192.168.1.10", "port":"8080", "serviceName":"resgisterTest", "version":"v1", @@ -116,15 +116,15 @@ func TestRegister(t *testing.T) { url: server.URL, } - client.Register(serviceInfo) + client.Register(ip, serviceInfo) } func TestDeRegister(t *testing.T) { + ip := "192.168.1.10" cases := []struct{ url, serviceInfo string }{ { // Version is "" urlPrefix + "/resgisterTest1/version/null/nodes/192.168.1.10/8080", `[{ - "ip":"192.168.1.10", "port":"8080", "serviceName":"resgisterTest1", "version":"", @@ -138,7 +138,6 @@ func TestDeRegister(t *testing.T) { }, { // version is not "" urlPrefix + "/resgisterTest2/version/v1/nodes/192.168.1.10/8080", `[{ - "ip":"192.168.1.10", "port":"8080", "serviceName":"resgisterTest2", "version":"v1", @@ -172,7 +171,7 @@ func TestDeRegister(t *testing.T) { url: server.URL, } - client.DeRegister(c.serviceInfo) + client.DeRegister(ip, c.serviceInfo) } } diff --git a/src/kube2msb/msb_work.go b/src/kube2msb/msb_work.go index 5c40bae..a36bec9 100644 --- a/src/kube2msb/msb_work.go +++ b/src/kube2msb/msb_work.go @@ -17,7 +17,6 @@ package main import ( "log" - "strings" "sync" ) @@ -49,7 +48,7 @@ func (client *MSBAgentWorker) AddService(ip, sInfo string) { return } - client.agent.Register(mergeIP(ip, sInfo)) + client.agent.Register(ip, sInfo) } func (client *MSBAgentWorker) RemoveService(ip, sInfo string) { @@ -61,7 +60,7 @@ func (client *MSBAgentWorker) RemoveService(ip, sInfo string) { return } - client.agent.DeRegister(mergeIP(ip, sInfo)) + client.agent.DeRegister(ip, sInfo) } func (client *MSBAgentWorker) AddPod(ip, sInfo string) { @@ -72,7 +71,7 @@ func (client *MSBAgentWorker) AddPod(ip, sInfo string) { return } - client.agent.Register(mergeIP(ip, sInfo)) + client.agent.Register(ip, sInfo) } func (client *MSBAgentWorker) RemovePod(ip, sInfo string) { @@ -83,15 +82,5 @@ func (client *MSBAgentWorker) RemovePod(ip, sInfo string) { return } - client.agent.DeRegister(mergeIP(ip, sInfo)) -} - -func mergeIP(ip, sInfo string) string { - insert := "{\"ip\":\"" + ip + "\"," - parts := strings.Split(sInfo, "{") - out := parts[0] - for i := 1; i < len(parts); i++ { - out += insert + parts[i] - } - return out + client.agent.DeRegister(ip, sInfo) } diff --git a/src/kube2msb/msb_work_test.go b/src/kube2msb/msb_work_test.go index d234a38..a3df907 100644 --- a/src/kube2msb/msb_work_test.go +++ b/src/kube2msb/msb_work_test.go @@ -155,20 +155,3 @@ func TestRemoveServiceMsb(t *testing.T) { func TestRemovePodMsb(t *testing.T) { removeServiceOrPodTest(t, "Pod") } - -func TestMergeIP(t *testing.T) { - cases := []struct{ ip, sInfo, want string }{ - {"127.0.0.1", "{}", "{\"ip\":\"127.0.0.1\",}"}, - {"127.0.0.1", "[{}]", "[{\"ip\":\"127.0.0.1\",}]"}, - {"127.0.0.1", "{\"name\":\"msb\"}", "{\"ip\":\"127.0.0.1\",\"name\":\"msb\"}"}, - {"127.0.0.1", "{\"name\":\"msb\", \"child\":{\"name\":\"childname\"}}", - "{\"ip\":\"127.0.0.1\",\"name\":\"msb\", \"child\":{\"ip\":\"127.0.0.1\",\"name\":\"childname\"}}"}, - } - - for _, c := range cases { - got := mergeIP(c.ip, c.sInfo) - if got != c.want { - t.Errorf("mergeIP(%ip, %sInfo) == %got, want %want", c.ip, c.sInfo, got, c.want) - } - } -} |