From e97a00243313385c1532d184b4ed1b8de7a1a518 Mon Sep 17 00:00:00 2001 From: Satoshi Fujii Date: Mon, 2 Nov 2020 07:45:57 +0000 Subject: Fix registration failure due to improper handling braces 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 Change-Id: Icc8a1f00fba5248218d45e4d9476042c5c331004 --- .gitignore | 1 + src/kube2msb/kube2msb_test.go | 4 ++-- src/kube2msb/msb_client.go | 10 ++++++---- src/kube2msb/msb_client_test.go | 11 +++++------ src/kube2msb/msb_work.go | 19 ++++--------------- src/kube2msb/msb_work_test.go | 17 ----------------- 6 files changed, 18 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 2a97bf1..96f8427 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ pkg/ *.zip *.tar *.gz +src/kube2msb/kube2msb 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) - } - } -} -- cgit 1.2.3-korg