Skip to content

Commit f9eaca3

Browse files
committed
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)
1 parent 34c4206 commit f9eaca3

5 files changed

Lines changed: 37 additions & 7 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/free5gc/util v1.2.0
1010
github.com/gin-gonic/gin v1.10.0
1111
github.com/google/gopacket v1.1.19
12-
github.com/google/uuid v1.3.0
12+
github.com/google/uuid v1.6.0
1313
github.com/pkg/errors v0.9.1
1414
github.com/prometheus/client_golang v1.21.0
1515
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
5959
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
6060
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
6161
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
62-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
63-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
62+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
63+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6464
github.com/h2non/gock v1.2.0 h1:K6ol8rfrRkUOefooBC8elXoaNGYkpp7y2qcxGG6BzUE=
6565
github.com/h2non/gock v1.2.0/go.mod h1:tNhoxHYW2W42cYkYb1WqzdbYIieALC99kpYr7rH/BQk=
6666
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=

internal/context/ausf_context_init.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"os"
66
"strconv"
77

8-
"github.com/google/uuid"
9-
108
"github.com/free5gc/ausf/internal/logger"
119
"github.com/free5gc/ausf/pkg/factory"
1210
"github.com/free5gc/openapi/models"
@@ -19,7 +17,7 @@ func InitAusfContext(context *AUSFContext) {
1917
configuration := config.Configuration
2018
sbi := configuration.Sbi
2119

22-
context.NfId = uuid.New().String()
20+
context.NfId = config.GetNfInstanceId()
2321
context.GroupID = configuration.GroupId
2422
context.NrfUri = configuration.NrfUri
2523
context.NrfCertPem = configuration.NrfCertPem

pkg/factory/config.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313

1414
"github.com/asaskevich/govalidator"
15+
"github.com/google/uuid"
1516

1617
"github.com/free5gc/ausf/internal/logger"
1718
"github.com/free5gc/openapi/models"
@@ -22,6 +23,7 @@ const (
2223
AusfDefaultCertPemPath = "./cert/ausf.pem"
2324
AusfDefaultPrivateKeyPath = "./cert/ausf.key"
2425
AusfDefaultConfigPath = "./config/ausfcfg.yaml"
26+
AusfDefaultNfInstanceIdEnvVar = "AUSF_NF_INSTANCE_ID"
2527
AusfSbiDefaultIPv4 = "127.0.0.9"
2628
AusfSbiDefaultPort = 8000
2729
AusfSbiDefaultScheme = "https"
@@ -63,6 +65,7 @@ type Info struct {
6365
}
6466

6567
type Configuration struct {
68+
NfInstanceId string `yaml:"nfInstanceId,omitempty" valid:"optional,uuid"`
6669
Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"`
6770
Metrics *Metrics `yaml:"metrics,omitempty" valid:"optional"`
6871
ServiceNameList []string `yaml:"serviceNameList,omitempty" valid:"required"`
@@ -80,6 +83,10 @@ type Logger struct {
8083
}
8184

8285
func (c *Configuration) validate() (bool, error) {
86+
if c.NfInstanceId == "" {
87+
c.NfInstanceId = uuid.New().String()
88+
}
89+
8390
if sbi := c.Sbi; sbi != nil {
8491
if result, err := sbi.validate(); err != nil {
8592
return result, err
@@ -128,6 +135,31 @@ func (c *Configuration) validate() (bool, error) {
128135
return result, appendInvalid(err)
129136
}
130137

138+
func (c *Config) GetNfInstanceId() string {
139+
c.RLock()
140+
defer c.RUnlock()
141+
142+
var nfInstanceId string
143+
144+
logger.CfgLog.Debugf("Fetching nfInstanceId from env var \"%s\"", AusfDefaultNfInstanceIdEnvVar)
145+
146+
if nfInstanceId = os.Getenv(AusfDefaultNfInstanceIdEnvVar); nfInstanceId == "" {
147+
logger.CfgLog.Debugf("No value found for \"%s\" env, fallback on config nfInstanceId : %s",
148+
AusfDefaultNfInstanceIdEnvVar, c.Configuration.NfInstanceId)
149+
return c.Configuration.NfInstanceId
150+
}
151+
152+
if err := uuid.Validate(nfInstanceId); err != nil {
153+
logger.CfgLog.Errorf("Env var \"%s\" is not a valid uuid, "+
154+
"fallback on configuration nfInstanceId : %s", AusfDefaultNfInstanceIdEnvVar, c.Configuration.NfInstanceId)
155+
return c.Configuration.NfInstanceId
156+
}
157+
158+
logger.CfgLog.Debugf("nfInstanceId from %s : %s", AusfDefaultNfInstanceIdEnvVar, nfInstanceId)
159+
160+
return nfInstanceId
161+
}
162+
131163
type Sbi struct {
132164
Scheme string `yaml:"scheme" valid:"scheme"`
133165
RegisterIPv4 string `yaml:"registerIPv4,omitempty" valid:"host,required"` // IP that is registered at NRF.

pkg/factory/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ReadConfig(cfgPath string) (*Config, error) {
4646
logger.CfgLog.Errorf("%+v", validErr)
4747
}
4848
logger.CfgLog.Errorf("[-- PLEASE REFER TO SAMPLE CONFIG FILE COMMENTS --]")
49-
return nil, fmt.Errorf("Config validate Error")
49+
return nil, fmt.Errorf("config validate Error")
5050
}
5151

5252
return cfg, nil

0 commit comments

Comments
 (0)