mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 15:26:40 +00:00
[management] Add support for disabling resources and routing peers in networks (#3154)
* sync openapi changes Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * add option to disable network resource(s) Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * add network resource enabled state from api Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * fix tests Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * add option to disable network router(s) Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * fix tests Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * Add tests Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> * migrate old network resources and routers Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com> --------- Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
@@ -1288,6 +1288,10 @@ func (a *Account) getNetworkResourceGroups(resourceID string) []*Group {
|
||||
func (a *Account) GetResourcePoliciesMap() map[string][]*Policy {
|
||||
resourcePolicies := make(map[string][]*Policy)
|
||||
for _, resource := range a.NetworkResources {
|
||||
if !resource.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
resourceAppliedPolicies := a.GetPoliciesForNetworkResource(resource.ID)
|
||||
resourcePolicies[resource.ID] = resourceAppliedPolicies
|
||||
}
|
||||
@@ -1301,6 +1305,10 @@ func (a *Account) GetNetworkResourcesRoutesToSync(ctx context.Context, peerID st
|
||||
allSourcePeers := make(map[string]struct{}, len(a.Peers))
|
||||
|
||||
for _, resource := range a.NetworkResources {
|
||||
if !resource.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
var addSourcePeers bool
|
||||
|
||||
networkRoutingPeers, exists := routers[resource.NetworkID]
|
||||
@@ -1455,6 +1463,10 @@ func (a *Account) GetResourceRoutersMap() map[string]map[string]*routerTypes.Net
|
||||
routers := make(map[string]map[string]*routerTypes.NetworkRouter)
|
||||
|
||||
for _, router := range a.NetworkRouters {
|
||||
if !router.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
if routers[router.NetworkID] == nil {
|
||||
routers[router.NetworkID] = make(map[string]*routerTypes.NetworkRouter)
|
||||
}
|
||||
|
||||
@@ -139,6 +139,11 @@ func setupTestAccount() *Account {
|
||||
AccountID: "accountID",
|
||||
Name: "network2",
|
||||
},
|
||||
{
|
||||
ID: "network3ID",
|
||||
AccountID: "accountID",
|
||||
Name: "network3",
|
||||
},
|
||||
},
|
||||
NetworkRouters: []*routerTypes.NetworkRouter{
|
||||
{
|
||||
@@ -149,6 +154,7 @@ func setupTestAccount() *Account {
|
||||
PeerGroups: []string{},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "router2ID",
|
||||
@@ -158,6 +164,7 @@ func setupTestAccount() *Account {
|
||||
PeerGroups: []string{},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "router3ID",
|
||||
@@ -167,6 +174,7 @@ func setupTestAccount() *Account {
|
||||
PeerGroups: []string{},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "router4ID",
|
||||
@@ -176,6 +184,7 @@ func setupTestAccount() *Account {
|
||||
PeerGroups: []string{"group1"},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "router5ID",
|
||||
@@ -185,6 +194,7 @@ func setupTestAccount() *Account {
|
||||
PeerGroups: []string{"group2", "group3"},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "router6ID",
|
||||
@@ -194,6 +204,17 @@ func setupTestAccount() *Account {
|
||||
PeerGroups: []string{"group4"},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "router6ID",
|
||||
NetworkID: "network3ID",
|
||||
AccountID: "accountID",
|
||||
Peer: "",
|
||||
PeerGroups: []string{"group6"},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: false,
|
||||
},
|
||||
},
|
||||
NetworkResources: []*resourceTypes.NetworkResource{
|
||||
@@ -201,21 +222,31 @@ func setupTestAccount() *Account {
|
||||
ID: "resource1ID",
|
||||
AccountID: "accountID",
|
||||
NetworkID: "network1ID",
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "resource2ID",
|
||||
AccountID: "accountID",
|
||||
NetworkID: "network2ID",
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "resource3ID",
|
||||
AccountID: "accountID",
|
||||
NetworkID: "network1ID",
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "resource4ID",
|
||||
AccountID: "accountID",
|
||||
NetworkID: "network1ID",
|
||||
Enabled: true,
|
||||
},
|
||||
{
|
||||
ID: "resource5ID",
|
||||
AccountID: "accountID",
|
||||
NetworkID: "network3ID",
|
||||
Enabled: false,
|
||||
},
|
||||
},
|
||||
Policies: []*Policy{
|
||||
@@ -281,6 +312,17 @@ func setupTestAccount() *Account {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ID: "policy6ID",
|
||||
AccountID: "accountID",
|
||||
Enabled: true,
|
||||
Rules: []*PolicyRule{
|
||||
{
|
||||
ID: "rule6ID",
|
||||
Enabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -302,6 +344,8 @@ func Test_GetResourceRoutersMap(t *testing.T) {
|
||||
require.Equal(t, 2, len(routers["network2ID"]))
|
||||
require.NotNil(t, routers["network2ID"]["peer2"])
|
||||
require.NotNil(t, routers["network2ID"]["peer41"])
|
||||
|
||||
require.Equal(t, 0, len(routers["network3ID"]))
|
||||
}
|
||||
|
||||
func Test_GetResourcePoliciesMap(t *testing.T) {
|
||||
@@ -312,6 +356,7 @@ func Test_GetResourcePoliciesMap(t *testing.T) {
|
||||
require.Equal(t, 1, len(policies["resource2ID"]))
|
||||
require.Equal(t, 2, len(policies["resource3ID"]))
|
||||
require.Equal(t, 1, len(policies["resource4ID"]))
|
||||
require.Equal(t, 0, len(policies["resource5ID"]))
|
||||
}
|
||||
|
||||
func Test_AddNetworksRoutingPeersAddsMissingPeers(t *testing.T) {
|
||||
@@ -476,6 +521,7 @@ func getBasicAccountsWithResource() *Account {
|
||||
PeerGroups: []string{},
|
||||
Masquerade: false,
|
||||
Metric: 100,
|
||||
Enabled: true,
|
||||
},
|
||||
},
|
||||
NetworkResources: []*resourceTypes.NetworkResource{
|
||||
@@ -486,6 +532,7 @@ func getBasicAccountsWithResource() *Account {
|
||||
Address: "10.10.10.0/24",
|
||||
Prefix: netip.MustParsePrefix("10.10.10.0/24"),
|
||||
Type: resourceTypes.NetworkResourceType("subnet"),
|
||||
Enabled: true,
|
||||
},
|
||||
},
|
||||
Policies: []*Policy{
|
||||
|
||||
Reference in New Issue
Block a user