Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: observe GRPC client #135

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ test: .coverage
-v \
./ ./...

fmt:
@go fmt ./...

golint:
@docker run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --config /app/.golangci.yml
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.1-dev
1.5.1
65 changes: 59 additions & 6 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ package factory
import (
"fmt"
"os"
"time"

"github.com/omec-project/config5g/proto/client"
grpcClient "github.com/omec-project/config5g/proto/client"
protos "github.com/omec-project/config5g/proto/sdcoreConfig"
"github.com/omec-project/udr/logger"
"go.uber.org/zap"
"google.golang.org/grpc/connectivity"
"gopkg.in/yaml.v2"
)

Expand All @@ -39,6 +41,11 @@ func init() {
initLog = logger.InitLog
}

// InitConfigFactory gets the UdrConfig and subscribes the config pod.
// This observes the GRPC client availability and connection status in a loop.
// When the GRPC server pod is restarted, GRPC connection status stuck in idle.
// If GRPC client does not exist, creates it. If client exists but GRPC connectivity is not ready,
// then it closes the existing client start a new client.
// TODO: Support configuration update from REST api
func InitConfigFactory(f string) error {
if content, err := os.ReadFile(f); err != nil {
Expand All @@ -59,12 +66,13 @@ func InitConfigFactory(f string) error {
if UdrConfig.Configuration.WebuiUri == "" {
UdrConfig.Configuration.WebuiUri = "webui:9876"
}
roc := os.Getenv("MANAGED_BY_CONFIG_POD")
if roc == "true" {
if os.Getenv("MANAGED_BY_CONFIG_POD") == "true" {
initLog.Infoln("MANAGED_BY_CONFIG_POD is true")
commChannel := client.ConfigWatcher(UdrConfig.Configuration.WebuiUri)
ConfigUpdateDbTrigger = make(chan *UpdateDb, 10)
go UdrConfig.updateConfig(commChannel, ConfigUpdateDbTrigger)
client, err := grpcClient.ConnectToConfigServer(UdrConfig.Configuration.WebuiUri)
if err != nil {
logger.InitLog.Infof("Connect to config server failed: %v", err)
}
go manageGrpcClient(client)
} else {
go func() {
initLog.Infoln("Use helm chart config ")
Expand All @@ -76,6 +84,51 @@ func InitConfigFactory(f string) error {
return nil
}

// manageGrpcClient connects the config pod GRPC server and subscribes the config changes.
// Then it updates UDR configuration.
func manageGrpcClient(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 {
initLog.Infoln("Stream is nil, waiting for 30 secs")
time.Sleep(time.Second * 30)
continue
}
time.Sleep(time.Second * 30)
if client.GetConfigClientConn().GetState() != connectivity.Ready {
initLog.Infoln("GRPC connectivity is not ready, the connection will be closed.")
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)
initLog.Infoln("PublishOnConfigChange is triggered.")
ConfigUpdateDbTrigger = make(chan *UpdateDb, 10)
go UdrConfig.updateConfig(configChannel, ConfigUpdateDbTrigger)
initLog.Infoln("UDR updateConfig is triggered.")
}
} else {
client, err = grpcClient.ConnectToConfigServer(UdrConfig.Configuration.WebuiUri)
initLog.Infoln("Connecting to config server.")
if err != nil {
logger.InitLog.Errorf("%+v", err)
}
continue
}
}
}

func CheckConfigVersion() error {
currentVersion := UdrConfig.GetVersion()

Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ module github.com/omec-project/udr

go 1.21

replace github.com/omec-project/config5g => /home/gatici/omec/config5g/

require (
github.com/evanphx/json-patch v5.9.0+incompatible
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/mitchellh/mapstructure v1.5.0
github.com/omec-project/config5g v1.5.1
github.com/omec-project/config5g v1.5.3
github.com/omec-project/openapi v1.3.1
github.com/omec-project/util v1.2.3
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.9.0
github.com/urfave/cli v1.22.16
go.mongodb.org/mongo-driver v1.17.1
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.67.1
gopkg.in/yaml.v2 v2.4.0
)

Expand Down Expand Up @@ -69,8 +72,7 @@ require (
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/h2non/gock.v1 v1.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ 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.1 h1:JaVgr76tnjJIb7Uoesv5a9GI72NdOXtCvfukj0/ONio=
github.com/omec-project/config5g v1.5.1/go.mod h1:o04ZdwGcM7tbGjuT5t/WzYSKLXOSnFl6vH7b6BGAspU=
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=
Expand Down Expand Up @@ -183,8 +181,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down