Skip to content

Commit 4995cc3

Browse files
authored
Different way to retrieve platform information (#12)
1 parent 8b7f036 commit 4995cc3

File tree

15 files changed

+140
-83
lines changed

15 files changed

+140
-83
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test-generate: docker-build
3535

3636
.PHONY: lint
3737
lint:
38-
golangci-lint run --build-tags client -p bugs -p unused
38+
golangci-lint run -p bugs -p unused
3939

4040
.PHONY: docker-build
4141
docker-build:

README.md

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ Then run
1111
sonic-confidb-utils generate
1212
```
1313

14+
Following files and directories are expected to be in place:
15+
16+
1. The `/usr/share/sonic/device` directory holds all device specific information. `sonic-configdb-utils` needs to read `usr/share/sonic/device/<platform-identifier>/platform.json` to set and validate the ports' breakout configurations.
17+
2. To retrieve the platform identifier and HWSKU the `/etc/sonic/sonic-environment` file is read which looks like this:
18+
19+
```bash
20+
SONIC_VERSION=sonic-123
21+
PLATFORM=x86_64-accton_as7726_32x-r0
22+
HWSKU=Accton-AS7726-32X
23+
DEVICE_TYPE=LeafRouter
24+
ASIC_TYPE=broadcom
25+
```
26+
1427
## Configuration Parameters
1528

1629
### bgp_ports
@@ -65,31 +78,6 @@ Result:
6578

6679
For each breakout also the correspondig ports entries are added.
6780

68-
### device_metadata
69-
70-
Example:
71-
72-
```yaml
73-
device_metadata:
74-
hwsku: Accton-AS7726-32X
75-
mac: aa:aa:aa:aa:aa:aa
76-
platform: x86_64-accton_as7726_32x-r0
77-
```
78-
79-
Result:
80-
81-
```json
82-
{
83-
"DEVICE_METADATA": {
84-
"localhost": {
85-
"hwsku": "Accton-AS7726-32X",
86-
"mac": "aa:aa:aa:aa:aa:aa",
87-
"platform": "x86_64-accton_as7726_32x-r0"
88-
}
89-
}
90-
}
91-
```
92-
9381
### docker_routing_config_mode
9482

9583
Can be one of `separated`, `split`, `split-unified`, `unified`.
@@ -630,16 +618,17 @@ Result:
630618
}
631619
```
632620

633-
### vteps
621+
### vtep
634622

635623
Example:
636624

637625
```yaml
638626
loopback_address: 10.7.7.7
639627
640-
vteps:
641-
- vni: 103999
642-
vlan: Vlan3999
628+
vtep:
629+
vxlan_tunnel_maps:
630+
- vni: 103999
631+
vlan: Vlan3999
643632
```
644633

645634
Result:
@@ -664,3 +653,29 @@ Result:
664653
}
665654
}
666655
```
656+
657+
If only `VXLAN_EVPN_NVO` and `VXLAN_TUNNEL` are needed with no tunnel maps:
658+
659+
```yaml
660+
loopback_address: 10.7.7.7
661+
662+
vtep:
663+
enabled: true
664+
```
665+
666+
Result:
667+
668+
```json
669+
{
670+
"VXLAN_EVPN_NVO": {
671+
"nvo": {
672+
"source_vtep": "vtep"
673+
}
674+
},
675+
"VXLAN_TUNNEL": {
676+
"vtep": {
677+
"src_ip": "10.7.7.7"
678+
}
679+
}
680+
}
681+
```

cmd/generate.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ import (
1414
var generateCmd = &cobra.Command{
1515
Use: "generate",
1616
Short: "Generate a config_db.json",
17-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
sonicEnvFile, _ := cmd.Flags().GetString("env-file")
19+
env, err := p.GetEnvironment(sonicEnvFile)
20+
if err != nil {
21+
return fmt.Errorf("failed to get environment information:%w", err)
22+
}
23+
24+
platformIdentifier := env.Platform
1825
inputFile, _ := cmd.Flags().GetString("input-file")
1926
outputFile, _ := cmd.Flags().GetString("output-file")
2027
deviceDir, _ := cmd.Flags().GetString("device-dir")
@@ -31,38 +38,34 @@ var generateCmd = &cobra.Command{
3138
os.Exit(1)
3239
}
3340

34-
platformIdentifier := values.DeviceMetadata.Platform
3541
platformFile := fmt.Sprintf("%s/%s/platform.json", deviceDir, platformIdentifier)
3642

3743
platformBytes, err := os.ReadFile(platformFile)
3844
if err != nil {
39-
fmt.Printf("failed to read platform.json file: %v\n", err)
40-
os.Exit(1)
45+
return fmt.Errorf("failed to read platform.json file:%w", err)
4146
}
4247

4348
platform, err := p.UnmarshalPlatformJSON(platformBytes)
4449
if err != nil {
45-
fmt.Printf("failed to parse platform.json: %v\n", err)
46-
os.Exit(1)
50+
return fmt.Errorf("failed to parse platform.json:%w", err)
4751
}
4852

49-
configDB, err := configdb.GenerateConfigDB(values, platform, values.DeviceMetadata)
53+
configDB, err := configdb.GenerateConfigDB(values, platform, env)
5054
if err != nil {
51-
fmt.Printf("failed to generate config, %v\n", err)
52-
os.Exit(1)
55+
return fmt.Errorf("failed to generate config:%w", err)
5356
}
5457

5558
configDBBytes, err := json.MarshalIndent(configDB, "", " ")
5659
if err != nil {
57-
fmt.Printf("failed to serialize json, %v\n", err)
58-
os.Exit(1)
60+
return fmt.Errorf("failed to serialize json:%w", err)
5961
}
6062

6163
err = os.WriteFile(outputFile, configDBBytes, 0644) //nolint:gosec
6264
if err != nil {
63-
fmt.Printf("failed to write file, %v", err)
64-
os.Exit(1)
65+
return fmt.Errorf("failed to write file:%w", err)
6566
}
67+
68+
return nil
6669
},
6770
}
6871

@@ -72,4 +75,5 @@ func init() {
7275
generateCmd.Flags().StringP("input-file", "i", "sonic-config.yaml", "path to input file to generate the config_db.json from")
7376
generateCmd.Flags().StringP("output-file", "o", "config_db.json", "path to output file")
7477
generateCmd.Flags().String("device-dir", "/usr/share/sonic/device", "directory which holds all device-specific files")
78+
generateCmd.Flags().StringP("env-file", "e", "/etc/sonic/sonic-environment", "sonic-environment file holding platform information")
7579
}

configdb/configdb.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"slices"
88
"strconv"
99

10+
"github.com/metal-stack/sonic-configdb-utils/platform"
1011
p "github.com/metal-stack/sonic-configdb-utils/platform"
1112
"github.com/metal-stack/sonic-configdb-utils/values"
1213
)
@@ -43,7 +44,7 @@ type ConfigDB struct {
4344
VXLANTunnelMap VXLANTunnelMap `json:"VXLAN_TUNNEL_MAP,omitempty"`
4445
}
4546

46-
func GenerateConfigDB(input *values.Values, platform *p.Platform, currentDeviceMetadata values.DeviceMetadata) (*ConfigDB, error) {
47+
func GenerateConfigDB(input *values.Values, platform *p.Platform, environment *platform.Environment) (*ConfigDB, error) {
4748
if input == nil {
4849
return nil, fmt.Errorf("no input values provided")
4950
}
@@ -56,7 +57,7 @@ func GenerateConfigDB(input *values.Values, platform *p.Platform, currentDeviceM
5657
return nil, err
5758
}
5859

59-
deviceMetadata, err := getDeviceMetadata(input, currentDeviceMetadata)
60+
deviceMetadata, err := getDeviceMetadata(input, environment)
6061
if err != nil {
6162
return nil, err
6263
}
@@ -168,27 +169,27 @@ func getACLRulesAndTables(sourceRanges []string) (map[string]ACLRule, map[string
168169
return rules, tables
169170
}
170171

171-
func getDeviceMetadata(input *values.Values, currentMetadata values.DeviceMetadata) (*DeviceMetadata, error) {
172-
if currentMetadata.Platform == "" {
173-
return nil, fmt.Errorf("missing platform from current device metadata")
172+
func getDeviceMetadata(input *values.Values, environment *platform.Environment) (*DeviceMetadata, error) {
173+
if environment.Platform == "" {
174+
return nil, fmt.Errorf("no platform identifiert found in environment file")
174175
}
175176

176-
if currentMetadata.HWSKU == "" {
177-
return nil, fmt.Errorf("missing hwsku from current device metadata")
177+
if environment.HWSKU == "" {
178+
return nil, fmt.Errorf("no hwsku found in environment file")
178179
}
179180

180-
if currentMetadata.MAC == "" {
181-
return nil, fmt.Errorf("missing mac from current device metadata")
181+
if environment.MAC == "" {
182+
return nil, fmt.Errorf("failed to retrieve system mac address")
182183
}
183184

184185
return &DeviceMetadata{
185186
Localhost: Metadata{
186187
DockerRoutingConfigMode: DockerRoutingConfigMode(input.DockerRoutingConfigMode),
187188
FRRMgmtFrameworkConfig: strconv.FormatBool(input.FRRMgmtFrameworkConfig),
188189
Hostname: input.Hostname,
189-
HWSKU: currentMetadata.HWSKU,
190-
MAC: currentMetadata.MAC,
191-
Platform: currentMetadata.Platform,
190+
HWSKU: environment.HWSKU,
191+
MAC: environment.MAC,
192+
Platform: environment.Platform,
192193
RouterType: "LeafRouter",
193194
},
194195
}, nil

platform/environment.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package platform
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
type Environment struct {
11+
HWSKU string
12+
MAC string
13+
Platform string
14+
}
15+
16+
func GetEnvironment(envFile string) (*Environment, error) {
17+
var (
18+
hwsku string
19+
mac string
20+
platform string
21+
)
22+
23+
file, err := os.Open(envFile)
24+
if err != nil {
25+
return nil, err
26+
}
27+
defer func() {
28+
err := file.Close()
29+
if err != nil {
30+
fmt.Printf("failed to close file:%v", err)
31+
}
32+
}()
33+
34+
scanner := bufio.NewScanner(file)
35+
for scanner.Scan() {
36+
line := scanner.Text()
37+
if p, ok := strings.CutPrefix(line, "PLATFORM="); ok {
38+
platform = p
39+
}
40+
if h, ok := strings.CutPrefix(line, "HWSKU="); ok {
41+
hwsku = h
42+
}
43+
}
44+
45+
addrBytes, err := os.ReadFile("/sys/class/net/eth0/address")
46+
if err != nil {
47+
return nil, fmt.Errorf("failed to get mac address:%w", err)
48+
}
49+
mac = strings.TrimSpace(string(addrBytes))
50+
51+
return &Environment{
52+
HWSKU: hwsku,
53+
MAC: mac,
54+
Platform: platform,
55+
}, nil
56+
}

tests/1/sonic-config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ bgp_ports:
55
breakouts:
66
Ethernet0: 4x25G
77

8-
device_metadata:
9-
hwsku: Accton-AS7726-32X
10-
mac: aa:aa:aa:aa:aa:aa
11-
platform: x86_64-accton_as7726_32x-r0
12-
138
docker_routing_config_mode: split
149

1510
features:

tests/1/sonic-environment

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PLATFORM=x86_64-accton_as7726_32x-r0
2+
HWSKU=Accton-AS7726-32X

tests/2/sonic-config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ bgp_ports:
66
breakouts:
77
Ethernet0: 4x25G
88

9-
device_metadata:
10-
hwsku: Accton-AS7726-32X
11-
mac: aa:aa:aa:aa:aa:aa
12-
platform: x86_64-accton_as7726_32x-r0
13-
149
docker_routing_config_mode: split
1510
features: {}
1611
frr_mgmt_framework_config: false

tests/2/sonic-environment

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PLATFORM=x86_64-accton_as7726_32x-r0
2+
HWSKU=Accton-AS7726-32X

tests/3/sonic-config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ bgp_ports:
22
- Ethernet0
33
- Ethernet1
44

5-
device_metadata:
6-
hwsku: Accton-AS4630-54TE
7-
mac: aa:aa:aa:aa:aa:aa
8-
platform: x86_64-accton_as4630_54te-r0
9-
105
docker_routing_config_mode: split
116
frr_mgmt_framework_config: true
127
hostname: mgmtspine

0 commit comments

Comments
 (0)