Skip to content

Commit

Permalink
Refactor path for configuration file, option(s) and Docker image (#150)
Browse files Browse the repository at this point in the history
* Refactor path for configuration file, opton(s) and Docker image

Signed-off-by: Arrobo, Gabriel <[email protected]>

* Address issue with unit test

Signed-off-by: Arrobo, Gabriel <[email protected]>

---------

Signed-off-by: Arrobo, Gabriel <[email protected]>
  • Loading branch information
gab-arrobo authored Nov 1, 2024
1 parent 592fe28 commit cef0179
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 88 deletions.
10 changes: 3 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

FROM golang:1.23.2-bookworm AS builder

LABEL maintainer="Aether SD-Core <[email protected]>"

RUN apt-get update && \
apt-get -y install --no-install-recommends \
apt-transport-https \
Expand All @@ -28,7 +26,8 @@ RUN make all

FROM alpine:3.20 AS nrf

LABEL description="ONF open source 5G Core Network" \
LABEL maintainer="Aether SD-Core <[email protected]>" \
description="ONF open source 5G Core Network" \
version="Stage 3"

ARG DEBUG_TOOLS
Expand All @@ -38,8 +37,5 @@ RUN if [ "$DEBUG_TOOLS" = "true" ]; then \
apk update && apk add --no-cache -U vim strace net-tools curl netcat-openbsd bind-tools bash; \
fi

# Set working dir
WORKDIR /free5gc/nrf

# Copy executable and default certs
COPY --from=builder /go/src/nrf/bin/* .
COPY --from=builder /go/src/nrf/bin/* /usr/local/bin/.
7 changes: 3 additions & 4 deletions accesstoken/api_access_token_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (
"github.com/omec-project/nrf/dbadapter"
"github.com/omec-project/nrf/factory"
"github.com/omec-project/nrf/logger"
"github.com/omec-project/nrf/util"
"github.com/omec-project/openapi/Nnrf_AccessToken"
"github.com/omec-project/openapi/models"
)

func TestAccessTokenRequest(t *testing.T) {
// run accesstoken Server Routine
go func() {
kl, _ := os.OpenFile(util.NrfLogPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
kl, _ := os.OpenFile("/home/sslkey.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
router := accesstoken.NewRouter()

server := http.Server{
Expand All @@ -38,12 +37,12 @@ func TestAccessTokenRequest(t *testing.T) {

Handler: router,
}
_ = server.ListenAndServeTLS(util.NrfPemPath, util.NrfKeyPath)
_ = server.ListenAndServeTLS("/var/run/certs/tls.crt", "/var/run/certs/tls.key")
}()
time.Sleep(time.Duration(2) * time.Second)

// connect to mongoDB
dbadapter.ConnectToDBClient("free5gc", "mongodb://140.113.214.205:30030", false, false)
dbadapter.ConnectToDBClient("aether", "mongodb://140.113.214.205:30030", false, false)

// Set client and set url
configuration := Nnrf_AccessToken.NewConfiguration()
Expand Down
1 change: 1 addition & 0 deletions factory/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Config struct {
Info *Info `yaml:"info"`
Configuration *Configuration `yaml:"configuration"`
Logger *utilLogger.Logger `yaml:"logger"`
CfgLocation string
}

type Info struct {
Expand Down
5 changes: 3 additions & 2 deletions nrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ func main() {
app := cli.NewApp()
app.Name = "nrf"
logger.InitLog.Infoln(app.Name)
app.Usage = "-free5gccfg common configuration file -nrfcfg nrf configuration file"
app.Usage = "Network Repository Function"
app.UsageText = "nrf -cfg <nrf_config_file.conf>"
app.Action = action
app.Flags = NRF.GetCliCmd()

if err := app.Run(os.Args); err != nil {
logger.AppLog.Errorf("NRF Run Error: %v", err)
logger.AppLog.Fatalf("NRF run error: %v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion nrfTest/nrfcfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ info:
description: NRF initial local configuration

configuration:
MongoDBName: free5gc # database name in MongoDB
MongoDBName: aether # database name in MongoDB
MongoDBUrl: mongodb://127.0.0.1:27017 # a valid URL of the mongodb
sbi: # Service-based interface information
scheme: http # the protocol for sbi (http or https)
Expand Down
49 changes: 21 additions & 28 deletions service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
Expand All @@ -29,32 +30,27 @@ import (
"github.com/omec-project/nrf/logger"
"github.com/omec-project/nrf/management"
"github.com/omec-project/nrf/metrics"
"github.com/omec-project/nrf/util"
openapiLogger "github.com/omec-project/openapi/logger"
"github.com/omec-project/util/http2_util"
utilLogger "github.com/omec-project/util/logger"
"github.com/omec-project/util/path_util"
)

type NRF struct{}

type (
// Config information.
Config struct {
nrfcfg string
cfg string
}
)

var config Config

var nrfCLi = []cli.Flag{
cli.StringFlag{
Name: "free5gccfg",
Usage: "common config file",
},
cli.StringFlag{
Name: "nrfcfg",
Usage: "config file",
Name: "cfg",
Usage: "nrf config file",
Required: true,
},
}

Expand All @@ -70,18 +66,17 @@ func (*NRF) GetCliCmd() (flags []cli.Flag) {

func (nrf *NRF) Initialize(c *cli.Context) error {
config = Config{
nrfcfg: c.String("nrfcfg"),
cfg: c.String("cfg"),
}

if config.nrfcfg != "" {
if err := factory.InitConfigFactory(config.nrfcfg); err != nil {
return err
}
} else {
DefaultNrfConfigPath := path_util.Free5gcPath("free5gc/config/nrfcfg.yaml")
if err := factory.InitConfigFactory(DefaultNrfConfigPath); err != nil {
return err
}
absPath, err := filepath.Abs(config.cfg)
if err != nil {
logger.CfgLog.Errorln(err)
return err
}

if err := factory.InitConfigFactory(absPath); err != nil {
return err
}

nrf.setLogLevel()
Expand All @@ -90,6 +85,8 @@ func (nrf *NRF) Initialize(c *cli.Context) error {
return err
}

factory.NrfConfig.CfgLocation = absPath

if os.Getenv("MANAGED_BY_CONFIG_POD") == "true" {
logger.InitLog.Infoln("MANAGED_BY_CONFIG_POD is true")
go manageGrpcClient(factory.NrfConfig.Configuration.WebuiUri)
Expand Down Expand Up @@ -240,14 +237,10 @@ func (nrf *NRF) Start() {
os.Exit(0)
}()

if os.Getenv("MANAGED_BY_CONFIG_POD") == "true" {
initLog.Infoln("MANAGED_BY_CONFIG_POD is true")
} else {
initLog.Infoln("use helm chart config")
}
bindAddr := factory.NrfConfig.GetSbiBindingAddr()
initLog.Infof("binding addr: [%s]", bindAddr)
server, err := http2_util.NewServer(bindAddr, util.NrfLogPath, router)
sslLog := filepath.Dir(factory.NrfConfig.CfgLocation) + "/sslkey.log"
server, err := http2_util.NewServer(bindAddr, sslLog, router)

if server == nil {
initLog.Errorf("initialize HTTP server failed: %+v", err)
Expand All @@ -271,10 +264,10 @@ func (nrf *NRF) Start() {
}

func (nrf *NRF) Exec(c *cli.Context) error {
initLog.Debugln("args:", c.String("nrfcfg"))
initLog.Debugln("args:", c.String("cfg"))
args := nrf.FilterCli(c)
initLog.Debugln("filter:", args)
command := exec.Command("./nrf", args...)
command := exec.Command("nrf", args...)

if err := nrf.Initialize(c); err != nil {
return err
Expand Down Expand Up @@ -310,7 +303,7 @@ func (nrf *NRF) Exec(c *cli.Context) error {
go func() {
initLog.Infoln("NRF start")
if err = command.Start(); err != nil {
initLog.Infof("NRF Start error: %v", err)
initLog.Infof("NRF start error: %v", err)
}
initLog.Infoln("NRF end")
wg.Done()
Expand Down
17 changes: 2 additions & 15 deletions test/test_server2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,12 @@ import (
"github.com/omec-project/openapi/models"
"github.com/omec-project/util/http2_util"
utilLogger "github.com/omec-project/util/logger"
"github.com/omec-project/util/path_util"
)

var (
NrfLogPath = path_util.Free5gcPath("github.com/free5gc/nrf/management/sslkeylog.log")
NrfPemPath = path_util.Free5gcPath("free5gc/support/TLS/nrf.pem")
NrfKeyPath = path_util.Free5gcPath("free5gc/support/TLS/nrf.key")
)

func main() {
router := utilLogger.NewGinWithZap(logger.GinLog)

router.POST("", func(c *gin.Context) {
/*buf, err := c.GetRawData()
if err != nil {
t.Errorf(err.Error())
}
// Remove NL line feed, new line character
//requestBody = string(buf[:len(b uf)-1])*/
var ND models.NotificationData

if err := c.ShouldBindJSON(&ND); err != nil {
Expand All @@ -42,12 +29,12 @@ func main() {
c.JSON(http.StatusNoContent, gin.H{})
})

srv, err := http2_util.NewServer(":30678", NrfLogPath, router)
srv, err := http2_util.NewServer(":30678", "/opt/sslkey", router)
if err != nil {
logger.UtilLog.Panicln(err.Error())
}

err2 := srv.ListenAndServeTLS(NrfPemPath, NrfKeyPath)
err2 := srv.ListenAndServeTLS("/var/run/certs/tls.crt", "/var/run/certs/tls.key")
if err2 != nil && err2 != http.ErrServerClosed {
logger.UtilLog.Panicln(err2.Error())
}
Expand Down
13 changes: 2 additions & 11 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ import (
"github.com/mitchellh/mapstructure"
"github.com/omec-project/nrf/logger"
"github.com/omec-project/openapi/models"
"github.com/omec-project/util/path_util"
)

// Path of HTTP2 key and log file

var (
NrfLogPath = path_util.Free5gcPath("free5gc/nrfsslkey.log")
NrfPemPath = path_util.Free5gcPath("free5gc/support/TLS/nrf.pem")
NrfKeyPath = path_util.Free5gcPath("free5gc/support/TLS/nrf.key")
)

func MarshToJsonString(v interface{}) (result []string) {
Expand All @@ -34,15 +25,15 @@ func MarshToJsonString(v interface{}) (result []string) {
for i := 0; i < val.Len(); i++ {
tmp, err := json.Marshal(val.Index(i).Interface())
if err != nil {
logger.UtilLog.Errorf("Marshal error: %+v", err)
logger.UtilLog.Errorf("marshal error: %+v", err)
}

result = append(result, string(tmp))
}
} else {
tmp, err := json.Marshal(v)
if err != nil {
logger.UtilLog.Errorf("Marshal error: %+v", err)
logger.UtilLog.Errorf("marshal error: %+v", err)
}

result = append(result, string(tmp))
Expand Down
20 changes: 0 additions & 20 deletions util/util_debug.go

This file was deleted.

0 comments on commit cef0179

Please sign in to comment.