From a4fa3102074d45b0d2849c5488f97bba014256ea Mon Sep 17 00:00:00 2001 From: Gulsum Atici Date: Mon, 28 Oct 2024 18:40:56 +0300 Subject: [PATCH] chore: use manageGrpcClient method in Service/init.go (#145) * Use manageGrpcClient method in Service/init.go Signed-off-by: gatici * chore: update config5g version Signed-off-by: gatici * removing spots in the logs Signed-off-by: gatici * remove MANAGED_BY_CONFIG_POD check if clause in factory Signed-off-by: gatici --------- Signed-off-by: gatici --- VERSION | 2 +- factory/config.go | 2 +- factory/factory.go | 50 --------------------------------------- go.mod | 2 +- go.sum | 4 ++-- service/init.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 55 deletions(-) diff --git a/VERSION b/VERSION index 4bb2004..4cda8f1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.5.2-dev +1.5.2 diff --git a/factory/config.go b/factory/config.go index bd9c987..3dfc764 100644 --- a/factory/config.go +++ b/factory/config.go @@ -133,7 +133,7 @@ func (c *Config) GetSbiUri() string { return c.GetSbiScheme() + "://" + c.GetSbiRegisterAddr() } -func (c *Config) updateConfig(commChannel chan *protos.NetworkSliceResponse) bool { +func (c *Config) UpdateConfig(commChannel chan *protos.NetworkSliceResponse) bool { for rsp := range commChannel { logger.GrpcLog.Infoln("Received updateConfig in the nrf app : ", rsp) for _, ns := range rsp.NetworkSlice { diff --git a/factory/factory.go b/factory/factory.go index cc5e6da..b78c85e 100644 --- a/factory/factory.go +++ b/factory/factory.go @@ -12,10 +12,7 @@ package factory import ( "fmt" "os" - "time" - grpcClient "github.com/omec-project/config5g/proto/client" - protos "github.com/omec-project/config5g/proto/sdcoreConfig" "github.com/omec-project/nrf/logger" "gopkg.in/yaml.v2" ) @@ -43,57 +40,10 @@ func InitConfigFactory(f string) error { NrfConfig.Configuration.WebuiUri = "webui:9876" } logger.InitLog.Infof("DefaultPlmnId Mnc %v, Mcc %v", NrfConfig.Configuration.DefaultPlmnId.Mnc, NrfConfig.Configuration.DefaultPlmnId.Mcc) - if os.Getenv("MANAGED_BY_CONFIG_POD") == "true" { - logger.InitLog.Infoln("MANAGED_BY_CONFIG_POD is true") - client, err := grpcClient.ConnectToConfigServer(NrfConfig.Configuration.WebuiUri) - if err != nil { - go updateConfig(client) - } - return err - } } return nil } -// updateConfig connects the config pod GRPC server and subscribes the config changes -// then updates NRF configuration -func updateConfig(client grpcClient.ConfClient) { - var stream protos.ConfigService_NetworkSliceSubscribeClient - var err error - var configChannel chan *protos.NetworkSliceResponse - for { - if client != nil { - stream, err = client.CheckGrpcConnectivity() - if err != nil { - logger.InitLog.Errorf("%v", err) - if stream != nil { - time.Sleep(time.Second * 30) - continue - } else { - err = client.GetConfigClientConn().Close() - if err != nil { - logger.InitLog.Debugf("failing ConfigClient is not closed properly: %+v", err) - } - client = nil - continue - } - } - if configChannel == nil { - configChannel = client.PublishOnConfigChange(true, stream) - ManagedByConfigPod = true - go NrfConfig.updateConfig(configChannel) - } - - } else { - client, err = grpcClient.ConnectToConfigServer(NrfConfig.Configuration.WebuiUri) - if err != nil { - logger.InitLog.Errorf("%+v", err) - } - continue - } - } -} - func CheckConfigVersion() error { currentVersion := NrfConfig.GetVersion() diff --git a/go.mod b/go.mod index ce1e68c..c960d0f 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/uuid v1.6.0 github.com/mitchellh/mapstructure v1.5.0 - github.com/omec-project/config5g v1.5.3 + github.com/omec-project/config5g v1.5.4 github.com/omec-project/openapi v1.3.1 github.com/omec-project/util v1.2.3 github.com/prometheus/client_golang v1.20.5 diff --git a/go.sum b/go.sum index 82945b5..2b98089 100644 --- a/go.sum +++ b/go.sum @@ -80,8 +80,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4= github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms= -github.com/omec-project/config5g v1.5.3 h1:FPetMFU1/BXGzihcoRHJm0q1vU1AnYyR4Tq1T1gyvUQ= -github.com/omec-project/config5g v1.5.3/go.mod h1:HOvQtmi79f8cw35AiFHWHDoCTuZbXfMjeFJWgtPbwaI= +github.com/omec-project/config5g v1.5.4 h1:5JMw5Fsr5qyLZpQi3IZQaQPj78QJMFQXDVS3QDMOY9Y= +github.com/omec-project/config5g v1.5.4/go.mod h1:HOvQtmi79f8cw35AiFHWHDoCTuZbXfMjeFJWgtPbwaI= github.com/omec-project/openapi v1.3.1 h1:NCteMRdMtWnMhf1CXYduuLgeu8fEhc/7XO1CiE7fN3Y= github.com/omec-project/openapi v1.3.1/go.mod h1:cR6Iharp2TLOzEmskQ/EdCVFZnpKh0zTvUSSuyXAYLE= github.com/omec-project/util v1.2.3 h1:h32ZYFT99+fB9VPp1CQUIKwrSP6RtX+PbFDcqmEHmn0= diff --git a/service/init.go b/service/init.go index a6e78fc..a421535 100644 --- a/service/init.go +++ b/service/init.go @@ -15,6 +15,8 @@ import ( "syscall" "time" + grpcClient "github.com/omec-project/config5g/proto/client" + protos "github.com/omec-project/config5g/proto/sdcoreConfig" "github.com/urfave/cli" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -88,9 +90,66 @@ func (nrf *NRF) Initialize(c *cli.Context) error { return err } + if os.Getenv("MANAGED_BY_CONFIG_POD") == "true" { + logger.InitLog.Infoln("MANAGED_BY_CONFIG_POD is true") + go manageGrpcClient(factory.NrfConfig.Configuration.WebuiUri) + } + return nil } +// manageGrpcClient connects the config pod GRPC server and subscribes the config changes. +// Then it updates NRF configuration. +func manageGrpcClient(webuiUri string) { + var configChannel chan *protos.NetworkSliceResponse + var client grpcClient.ConfClient + var stream protos.ConfigService_NetworkSliceSubscribeClient + var err error + count := 0 + for { + if client != nil { + if client.CheckGrpcConnectivity() != "ready" { + time.Sleep(time.Second * 30) + count++ + if count > 5 { + err = client.GetConfigClientConn().Close() + if err != nil { + logger.InitLog.Infof("failing ConfigClient is not closed properly: %+v", err) + } + client = nil + count = 0 + } + logger.InitLog.Infoln("checking the connectivity readiness") + continue + } + + if stream == nil { + stream, err = client.SubscribeToConfigServer() + if err != nil { + logger.InitLog.Infof("failing SubscribeToConfigServer: %+v", err) + continue + } + } + + if configChannel == nil { + configChannel = client.PublishOnConfigChange(true, stream) + logger.InitLog.Infoln("PublishOnConfigChange is triggered") + go factory.NrfConfig.UpdateConfig(configChannel) + logger.InitLog.Infoln("NRF updateConfig is triggered") + } + } else { + client, err = grpcClient.ConnectToConfigServer(webuiUri) + stream = nil + configChannel = nil + logger.InitLog.Infoln("connecting to config server") + if err != nil { + logger.InitLog.Errorf("%+v", err) + } + continue + } + } +} + func (nrf *NRF) setLogLevel() { if factory.NrfConfig.Logger == nil { initLog.Warnln("NRF config without log level setting!!!")