Skip to content

Commit

Permalink
MCAPI-56 Fix network filter for fixed_network support (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
okozachenko1203 authored Jan 10, 2025
1 parent 84e2dd2 commit 71ff876
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist
site
*.orig
*.rej
.tox
42 changes: 21 additions & 21 deletions magnum_cluster_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def get_object(self) -> objects.ClusterClass:
},
},
{
"name": "fixedNetworkName",
"name": "fixedNetworkId",
"required": True,
"schema": {
"openAPIV3Schema": {
Expand Down Expand Up @@ -1888,17 +1888,6 @@ def get_object(self) -> objects.ClusterClass:
"variable": "disableAPIServerFloatingIP"
},
},
{
"op": "add",
"path": "/spec/template/spec/managedSubnets",
"valueFrom": {
"template": textwrap.dedent(
"""\
- dnsNameservers: {{ .dnsNameservers }}
"""
),
},
},
{
"op": "add",
"path": "/spec/template/spec/externalNetwork",
Expand Down Expand Up @@ -1940,7 +1929,7 @@ def get_object(self) -> objects.ClusterClass:
},
{
"name": "newNetworkConfig",
"enabledIf": '{{ if eq .fixedNetworkName "" }}true{{end}}',
"enabledIf": '{{ if eq .fixedNetworkId "" }}true{{end}}',
"definitions": [
{
"selector": {
Expand All @@ -1953,16 +1942,23 @@ def get_object(self) -> objects.ClusterClass:
"jsonPatches": [
{
"op": "add",
"path": "/spec/template/spec/managedSubnets/0/cidr",
"valueFrom": {"variable": "nodeCidr"},
"path": "/spec/template/spec/managedSubnets",
"valueFrom": {
"template": textwrap.dedent(
"""\
- cidr: {{ .nodeCidr }}
dnsNameservers: {{ .dnsNameservers }}
"""
),
},
},
],
},
],
},
{
"name": "existingFixedNetworkNameConfig",
"enabledIf": '{{ if ne .fixedNetworkName "" }}true{{end}}',
"name": "existingFixedNetworkIdConfig",
"enabledIf": '{{ if ne .fixedNetworkId "" }}true{{end}}',
"definitions": [
{
"selector": {
Expand All @@ -1975,9 +1971,13 @@ def get_object(self) -> objects.ClusterClass:
"jsonPatches": [
{
"op": "add",
"path": "/spec/template/spec/network/name",
"path": "/spec/template/spec/network",
"valueFrom": {
"variable": "fixedNetworkName"
"template": textwrap.dedent(
"""\
id: {{ .fixedNetworkId }}
"""
),
},
},
],
Expand Down Expand Up @@ -2766,8 +2766,8 @@ def get_object(self) -> objects.Cluster:
),
},
{
"name": "fixedNetworkName",
"value": neutron.get_fixed_network_name(
"name": "fixedNetworkId",
"value": utils.get_fixed_network_id(
self.context, self.cluster.fixed_network
)
or "",
Expand Down
75 changes: 75 additions & 0 deletions magnum_cluster_api/tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
# under the License.

import textwrap
from unittest import mock

import pykube
import pytest
import responses
from magnum.common import exception
from magnum.tests.unit.objects import utils as magnum_test_utils # type: ignore
from oslo_serialization import base64, jsonutils
from oslo_utils import uuidutils
from oslotest import base

from magnum_cluster_api import exceptions, utils

Expand Down Expand Up @@ -240,3 +244,74 @@ def test_generate_cloud_controller_manager_config_for_ovn_with_invalid_algorithm
utils.generate_cloud_controller_manager_config(
self.context, self.pykube_api, self.cluster
)


class TestUtils(base.BaseTestCase):
"""Test case for utils."""

@mock.patch("magnum.common.neutron.get_network")
def test_get_fixed_network_id_with_uuid(self, mock_get_network):
context = mock.Mock()
fixed_network = uuidutils.generate_uuid()

network = utils.get_fixed_network_id(context, fixed_network)

mock_get_network.assert_not_called()
self.assertEqual(fixed_network, network)

@mock.patch("magnum.common.neutron.get_network")
def test_get_fixed_network_id_with_name(self, mock_get_network):
context = mock.Mock()
fixed_network = "fake-network"

network_id = uuidutils.generate_uuid()
mock_get_network.return_value = network_id

network = utils.get_fixed_network_id(context, fixed_network)

mock_get_network.assert_called_once_with(
context, fixed_network, source="name", target="id", external=False
)
self.assertEqual(network_id, network)

@mock.patch("magnum.common.neutron.get_network")
def test_get_fixed_network_id_with_no_fixed_network(self, mock_get_network):
context = mock.Mock()

network = utils.get_fixed_network_id(context, None)

mock_get_network.assert_not_called()
self.assertEqual(None, network)

@mock.patch("magnum.common.neutron.get_network")
def test_get_fixed_network_id_with_missing_network(self, mock_get_network):
context = mock.Mock()
fixed_network = "fake-network"

mock_get_network.side_effect = exception.FixedNetworkNotFound(
network=fixed_network
)

self.assertRaises(
exception.FixedNetworkNotFound,
utils.get_fixed_network_id,
context,
fixed_network,
)

@mock.patch("magnum.common.neutron.get_network")
def test_get_fixed_network_id_with_multiple_networks(self, mock_get_network):
context = mock.Mock()
fixed_network = "fake-network"

mock_get_network.side_effect = exception.Conflict(
"Multiple networks exist with same name '%s'. Please use the "
"network ID instead." % fixed_network
)

self.assertRaises(
exception.Conflict,
utils.get_fixed_network_id,
context,
fixed_network,
)
9 changes: 9 additions & 0 deletions magnum_cluster_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,3 +673,12 @@ def _delete_server_group(
osc.nova().server_groups.delete(server_group_id)
except nova_exception.NotFound:
return


def get_fixed_network_id(context, network):
if network and not uuidutils.is_uuid_like(network):
return neutron.get_network(
context, network, source="name", target="id", external=False
)
else:
return network

0 comments on commit 71ff876

Please sign in to comment.