From: Mohamed Abukar Date: Thu, 26 Mar 2020 13:14:33 +0000 (+0200) Subject: Resolve RMR port from K8s services X-Git-Tag: v0.4.3~1 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=45ea51f2a77c4aa22546224b4ded0d3cb03425e6;p=ric-plt%2Fappmgr.git Resolve RMR port from K8s services Change-Id: Ib3f6fef0624f2a69a9bf924ca10ca5a52ccb5745 Signed-off-by: Mohamed Abukar --- diff --git a/container-tag.yaml b/container-tag.yaml index 6fc9ff3..76d2853 100755 --- a/container-tag.yaml +++ b/container-tag.yaml @@ -1,4 +1,4 @@ # The Jenkins job uses this string for the tag in the image name # for example nexus3.o-ran-sc.org:10004/my-image-name:my-tag --- -tag: '0.4.1' +tag: '0.4.3' diff --git a/pkg/appmgr/appmgr.go b/pkg/appmgr/appmgr.go index 26a0453..0e2196d 100755 --- a/pkg/appmgr/appmgr.go +++ b/pkg/appmgr/appmgr.go @@ -68,5 +68,5 @@ func loadConfig() { func Init() { loadConfig() Logger = logger.NewLogger("appmgr") - Logger.SetMdc("xm", "0.4.1") + Logger.SetMdc("xm", "0.4.3") } diff --git a/pkg/helm/helm.go b/pkg/helm/helm.go index 345ea45..157a117 100755 --- a/pkg/helm/helm.go +++ b/pkg/helm/helm.go @@ -214,15 +214,30 @@ func (h *Helm) GetAddress(out string) (ip, port string) { return } -func (h *Helm) GetEndpointInfo(name string) (ip string, port int) { +func (h *Helm) GetEndpointInfo(name string) (svc string, port int) { + port = 4560 // Default ns := h.cm.GetNamespace("") - args := fmt.Sprintf(" get endpoints -o=jsonpath='{.subsets[*].addresses[*].ip}' service-%s-%s-rmr -n %s", ns, name, ns) + args := fmt.Sprintf(" get service -n ricxapp service-%s-%s-rmr -o json", ns, name) out, err := util.KubectlExec(args) if err != nil { - return + return fmt.Sprintf("service-%s-%s-rmr.%s", ns, name, ns), 4560 } - appmgr.Logger.Info("Endpoint IP address of %s: %s", name, string(out)) - return fmt.Sprintf("service-%s-%s-rmr.%s", ns, name, ns), 4560 + appmgr.Logger.Debug("Endpoint IP address of %s: %s", name, string(out)) + + v, err := h.cm.ParseJson(string(out)) + if err != nil { + return fmt.Sprintf("service-%s-%s-rmr.%s", ns, name, ns), 4560 + } + + for _, p := range v.GetArray("spec", "ports") { + if string(p.GetStringBytes("name")) == "rmrdata" { + port = int(p.GetInt("port")) + break + } + } + appmgr.Logger.Info("service-%s-%s-rmr.%s %d", ns, name, ns, port) + + return fmt.Sprintf("service-%s-%s-rmr.%s", ns, name, ns), port } func (h *Helm) GetNames(out string) (names []string, err error) { diff --git a/pkg/helm/helm_test.go b/pkg/helm/helm_test.go index 27f53a7..109103c 100755 --- a/pkg/helm/helm_test.go +++ b/pkg/helm/helm_test.go @@ -73,7 +73,53 @@ Releases: Namespace: default Revision: 1 Status: DEPLOYED - Updated: Sun Mar 24 07:17:00 2019` + Updated: Sun Mar 24 07:17:00 2019 + ` + +var helmServiceOutput = `{ + "apiVersion": "v1", + "kind": "Service", + "metadata": { + "creationTimestamp": "2020-03-31T12:27:12Z", + "labels": { + "app": "ricxapp-dummy-xapp", + "chart": "dummy-xapp-0.0.4", + "heritage": "Tiller", + "release": "dummy-xapp" + }, + "name": "service-ricxapp-dummy-xapp-rmr", + "namespace": "ricxapp", + "resourceVersion": "4423380", + "selfLink": "/api/v1/namespaces/ricxapp/services/service-ricxapp-dummy-xapp-rmr", + "uid": "2254b77d-7dd6-43e0-beff-3e2a7b24c89a" + }, + "spec": { + "clusterIP": "10.98.239.107", + "ports": [ + { + "name": "rmrdata", + "port": 4560, + "protocol": "TCP", + "targetPort": "rmrdata" + }, + { + "name": "rmrroute", + "port": 4561, + "protocol": "TCP", + "targetPort": "rmrroute" + } + ], + "selector": { + "app": "ricxapp-dummy-xapp", + "release": "dummy-xapp" + }, + "sessionAffinity": "None", + "type": "ClusterIP" + }, + "status": { + "loadBalancer": {} + } +}` // Test cases func TestMain(m *testing.M) { @@ -86,7 +132,7 @@ func TestMain(m *testing.M) { func TestHelmStatus(t *testing.T) { util.KubectlExec = func(args string) (out []byte, err error) { - return []byte("10.102.184.212"), nil + return []byte(helmServiceOutput), nil } xapp, err := NewHelm().ParseStatus("dummy-xapp", helmStatusOutput) if err != nil { @@ -104,7 +150,7 @@ func TestHelmStatus(t *testing.T) { } if x.Instances[0].IP != xapp.Instances[0].IP || x.Instances[0].Port != xapp.Instances[0].Port { - t.Errorf("\n1:%v 2:%v", x.Instances[0].IP, xapp.Instances[0].IP) + t.Errorf("\n%v - %v, %v - %v", x.Instances[0].IP, xapp.Instances[0].IP, x.Instances[0].Port, xapp.Instances[0].Port) } } diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index dd3a578..dea70c9 100755 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -51,7 +51,8 @@ func (rh *Resthook) AddSubscription(sr models.SubscriptionRequest) *models.Subsc r := v.Val.(SubscriptionInfo).req if *r.Data.TargetURL == *sr.Data.TargetURL && r.Data.EventType == sr.Data.EventType { appmgr.Logger.Info("Similar subscription already exists!") - return &models.SubscriptionResponse{} + resp := v.Val.(SubscriptionInfo).resp + return &resp } } diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index 14f7016..d1ac790 100755 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -50,7 +50,7 @@ func TestAddSubscriptionSuccess(t *testing.T) { func TestAddSubscriptionExists(t *testing.T) { resp := rh.AddSubscription(CreateSubscription(models.EventTypeCreated, int64(5), int64(10), "http://localhost:8087/xapps_hook")) assert.Equal(t, resp.Version, int64(0)) - assert.Equal(t, resp.EventType, models.EventType("")) + assert.Equal(t, resp.EventType, models.EventTypeCreated) } func TestDeletesubscriptionSuccess(t *testing.T) {