-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sonic redis with serialized interface reconfiguration (#81)
- Loading branch information
Showing
44 changed files
with
2,125 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
package switcher | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/metal-stack/metal-core/cmd/internal/switcher/cumulus" | ||
"github.com/metal-stack/metal-core/cmd/internal/switcher/sonic" | ||
"github.com/metal-stack/metal-core/cmd/internal/switcher/types" | ||
"github.com/metal-stack/metal-go/api/models" | ||
) | ||
|
||
type NOS interface { | ||
SanitizeConfig(cfg *types.Conf) | ||
Apply(cfg *types.Conf) error | ||
IsInitialized() (initialized bool, err error) | ||
GetNics(log *zap.SugaredLogger, blacklist []string) ([]*models.V1SwitchNic, error) | ||
GetSwitchPorts() ([]*net.Interface, error) | ||
GetOS() (*models.V1SwitchOS, error) | ||
GetManagement() (ip, user string, err error) | ||
} | ||
|
||
func NewNOS(log *zap.SugaredLogger, frrTplFile, interfacesTplFile string) NOS { | ||
return cumulus.New(log.Named("cumulus"), frrTplFile, interfacesTplFile) | ||
func NewNOS(log *zap.SugaredLogger, frrTplFile, interfacesTplFile string) (NOS, error) { | ||
if _, err := os.Stat(sonic.SonicVersionFile); err == nil { | ||
log.Infow("create sonic NOS") | ||
nos, err := sonic.New(log.Named("sonic"), frrTplFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to initialize SONiC NOS %w", err) | ||
} | ||
return nos, nil | ||
} | ||
log.Infow("create cumulus NOS") | ||
return cumulus.New(log.Named("cumulus"), frrTplFile, interfacesTplFile), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type ApplDB struct { | ||
c *Client | ||
} | ||
|
||
func newApplDB(addr string, id int, sep string) *ApplDB { | ||
return &ApplDB{ | ||
c: NewClient(addr, id, sep), | ||
} | ||
} | ||
|
||
func (d *ApplDB) ExistPortInitDone(ctx context.Context) (bool, error) { | ||
key := Key{"PORT_TABLE", "PortInitDone"} | ||
|
||
return d.c.Exists(ctx, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type AsicDB struct { | ||
c *Client | ||
} | ||
|
||
type OID string | ||
|
||
func newAsicDB(addr string, id int, sep string) *AsicDB { | ||
return &AsicDB{ | ||
c: NewClient(addr, id, sep), | ||
} | ||
} | ||
|
||
func (d *AsicDB) GetPortIdBridgePortMap(ctx context.Context) (map[OID]OID, error) { | ||
t := d.c.GetTable(Key{"ASIC_STATE", "SAI_OBJECT_TYPE_BRIDGE_PORT"}) | ||
|
||
bridges, err := t.GetView(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m := make(map[OID]OID, len(bridges)) | ||
for bridge := range bridges { | ||
port, err := t.HGet(ctx, bridge, "SAI_BRIDGE_PORT_ATTR_PORT_ID") | ||
if err != nil { | ||
if errors.Is(err, redis.Nil) { | ||
continue | ||
} | ||
return nil, err | ||
} | ||
if len(port) == 0 { | ||
continue | ||
} | ||
m[OID(port)] = OID(bridge) | ||
} | ||
return m, nil | ||
} | ||
|
||
func (d *AsicDB) ExistBridgePort(ctx context.Context, bridgePort OID) (bool, error) { | ||
key := Key{"ASIC_STATE", "SAI_OBJECT_TYPE_BRIDGE_PORT", string(bridgePort)} | ||
|
||
return d.c.Exists(ctx, key) | ||
} | ||
|
||
func (d *AsicDB) ExistRouterInterface(ctx context.Context, rif OID) (bool, error) { | ||
key := Key{"ASIC_STATE", "SAI_OBJECT_TYPE_ROUTER_INTERFACE", string(rif)} | ||
|
||
return d.c.Exists(ctx, key) | ||
} | ||
|
||
func (d *AsicDB) InFecModeRs(ctx context.Context, port OID) (bool, error) { | ||
key := Key{"ASIC_STATE", "SAI_OBJECT_TYPE_PORT", string(port)} | ||
|
||
result, err := d.c.HGet(ctx, key, "SAI_PORT_ATTR_FEC_MODE") | ||
if err != nil { | ||
return false, err | ||
} | ||
return result == "SAI_PORT_FEC_MODE_RS", err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestAsicDB_GetPortIdBridgePortMap(t *testing.T) { | ||
c, mock := NewClientMock(":") | ||
asic := &AsicDB{c: c} | ||
bridgePort := "ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a000000001a0a" | ||
want := map[OID]OID{OID("oid:0x1000000001251"): "oid:0x3a000000001a0a"} | ||
|
||
mock.ExpectKeys("ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:*").SetVal([]string{bridgePort}) | ||
mock.ExpectHGet(bridgePort, "SAI_BRIDGE_PORT_ATTR_PORT_ID").SetVal("oid:0x1000000001251") | ||
|
||
got, err := asic.GetPortIdBridgePortMap(context.TODO()) | ||
if err != nil { | ||
t.Errorf("GetPortIdBridgePortMap() error = %v, wantErr %v", err, nil) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("GetPortIdBridgePortMap() got = %v, want %v", got, want) | ||
} | ||
} |
Oops, something went wrong.