From fbdfc5093a979e045d0af66c582f7745c9885ea0 Mon Sep 17 00:00:00 2001 From: Niahh Date: Mon, 15 Dec 2025 11:51:20 +0100 Subject: [PATCH] feat: making the nfInstanceId configurable Initially the NF created at each boot a new uuid. This makes it difficult to retrieve the information afterwards. This commit propose is to add the nfInstanceId in the configuration as optional so that if needed, it can be tracked more easily. If no nfInstanceId specified, it will still be generated at each boot and give more control to users that might need it (for kube deployment, other checks, better identification) Also using the uuidv4 validator to match spec 3GPP TS 29.510 version 17.6.0 Release 17 5.2.2.2.2 mentioning that UUID should be in the version 4 format --- go.mod | 2 +- go.sum | 4 ++-- internal/context/context.go | 3 +-- pkg/factory/config.go | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 46f0d69..8a78c00 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/free5gc/util v1.2.0 github.com/gin-gonic/gin v1.10.0 github.com/golang-jwt/jwt/v5 v5.2.2 - github.com/google/uuid v1.3.0 + github.com/google/uuid v1.6.0 github.com/mitchellh/mapstructure v1.5.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.21.0 diff --git a/go.sum b/go.sum index 087d31c..cd5f00c 100644 --- a/go.sum +++ b/go.sum @@ -59,8 +59,8 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g= github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k= github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE= diff --git a/internal/context/context.go b/internal/context/context.go index fde7c88..530cbe9 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -9,7 +9,6 @@ import ( "strings" "sync" - "github.com/google/uuid" "github.com/pkg/errors" "github.com/free5gc/nrf/internal/logger" @@ -48,7 +47,7 @@ func InitNrfContext() error { config.Info.Version, config.Info.Description) configuration := config.Configuration - nrfContext.NrfNfProfile.NfInstanceId = uuid.New().String() + nrfContext.NrfNfProfile.NfInstanceId = config.GetNfInstanceId() nrfContext.NrfNfProfile.NfType = models.NrfNfManagementNfType_NRF nrfContext.NrfNfProfile.NfStatus = models.NrfNfManagementNfStatus_REGISTERED nrfContext.NfRegistNum = 0 diff --git a/pkg/factory/config.go b/pkg/factory/config.go index 8b0eca0..a1cb015 100644 --- a/pkg/factory/config.go +++ b/pkg/factory/config.go @@ -13,6 +13,7 @@ import ( "sync" "github.com/asaskevich/govalidator" + "github.com/google/uuid" "github.com/free5gc/nrf/internal/logger" "github.com/free5gc/openapi/models" @@ -25,6 +26,7 @@ const ( NrfDefaultRootCertPemPath = "./cert/root.pem" NrfDefaultRootPrivateKeyPath = "./cert/root.key" NrfDefaultConfigPath = "./config/nrfcfg.yaml" + NrfDefaultNfInstanceIdEnvVar = "NRF_NF_INSTANCE_ID" NrfSbiDefaultIPv4 = "127.0.0.10" NrfSbiDefaultPort = 8000 NrfSbiDefaultScheme = "https" @@ -65,6 +67,7 @@ type Info struct { } type Configuration struct { + NfInstanceId string `yaml:"nfInstanceId,omitempty" valid:"optional,uuidv4"` Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"` Metrics *Metrics `yaml:"metrics,omitempty" valid:"optional"` MongoDBName string `yaml:"MongoDBName" valid:"required"` @@ -80,6 +83,10 @@ type Logger struct { } func (c *Configuration) validate() (bool, error) { + if c.NfInstanceId == "" { + c.NfInstanceId = uuid.New().String() + } + if sbi := c.Sbi; sbi != nil { if result, err := sbi.validate(); err != nil { return result, err @@ -125,6 +132,31 @@ func (c *Configuration) validate() (bool, error) { return result, appendInvalid(err) } +func (c *Config) GetNfInstanceId() string { + c.RLock() + defer c.RUnlock() + + var nfInstanceId string + + logger.CfgLog.Debugf("Fetching nfInstanceId from env var \"%s\"", NrfDefaultNfInstanceIdEnvVar) + + if nfInstanceId = os.Getenv(NrfDefaultNfInstanceIdEnvVar); nfInstanceId == "" { + logger.CfgLog.Debugf("No value found for \"%s\" env, fallback on config nfInstanceId : %s", + NrfDefaultNfInstanceIdEnvVar, c.Configuration.NfInstanceId) + return c.Configuration.NfInstanceId + } + + if err := uuid.Validate(nfInstanceId); err != nil { + logger.CfgLog.Errorf("Env var \"%s\" is not a valid uuid, "+ + "fallback on configuration nfInstanceId : %s", NrfDefaultNfInstanceIdEnvVar, c.Configuration.NfInstanceId) + return c.Configuration.NfInstanceId + } + + logger.CfgLog.Debugf("nfInstanceId from %s : %s", NrfDefaultNfInstanceIdEnvVar, nfInstanceId) + + return nfInstanceId +} + type Sbi struct { Scheme string `yaml:"scheme" valid:"scheme,required"` RegisterIPv4 string `yaml:"registerIPv4,omitempty" valid:"host,optional"`