Browse Source

Convert network UUID to name required for OCCM

Sometimes, the fixed_network value gets rendered as UUID. However OCCM's
internal-network-name requires the network name, it does not support
UUID. This patch introduces a new parameter called fixed_network_name
which converts fixed_network UUID to name if it is UUID-like.

Story: 2005333
Task: 36313

Change-Id: I3453bc0dbea285687d39c9782685cb1f2a3ecd39
changes/67/678067/6
Bharat Kunwar 3 years ago
parent
commit
e84cc4c975
  1. 7
      magnum/common/exception.py
  2. 32
      magnum/common/neutron.py
  3. 1
      magnum/drivers/common/templates/kubernetes/fragments/write-heat-params-master.sh
  4. 2
      magnum/drivers/common/templates/kubernetes/fragments/write-kube-os-config.sh
  5. 19
      magnum/drivers/heat/k8s_template_def.py
  6. 8
      magnum/drivers/k8s_fedora_atomic_v1/templates/kubecluster.yaml
  7. 5
      magnum/drivers/k8s_fedora_atomic_v1/templates/kubemaster.yaml
  8. 62
      magnum/tests/unit/common/test_neutron.py
  9. 8
      magnum/tests/unit/conductor/handlers/test_k8s_cluster_conductor.py
  10. 31
      magnum/tests/unit/drivers/test_template_definition.py

7
magnum/common/exception.py

@ -337,6 +337,13 @@ class FlavorNotFound(ResourceNotFound):
code = 400
class FixedNetworkNotFound(ResourceNotFound):
"""The code here changed to 400 according to the latest document."""
""""Ensure the network is private."""
message = _("Unable to find fixed network %(network)s.")
code = 400
class ExternalNetworkNotFound(ResourceNotFound):
"""The code here changed to 400 according to the latest document."""
""""Ensure the network is not private."""

32
magnum/common/neutron.py

@ -14,6 +14,7 @@
import re
from oslo_log import log as logging
from oslo_utils import uuidutils
from magnum.common import clients
from magnum.common import exception
@ -54,23 +55,42 @@ def delete_floatingip(context, fix_port_id, cluster):
msg=str(e))
def get_network_id(context, network_name):
def get_network(context, network, source, target, external):
nets = []
n_client = clients.OpenStackClients(context).neutron()
ext_filter = {'router:external': True}
ext_filter = {'router:external': external}
networks = n_client.list_networks(**ext_filter)
for net in networks.get('networks'):
if net.get('name') == network_name:
if net.get(source) == network:
nets.append(net)
if len(nets) == 0:
raise exception.ExternalNetworkNotFound(network=network_name)
if external:
raise exception.ExternalNetworkNotFound(network=network)
else:
raise exception.FixedNetworkNotFound(network=network)
if len(nets) > 1:
raise exception.Conflict(
"Multiple networks exist with same name '%s'. Please use the "
"network ID instead." % network_name
"network ID instead." % network
)
return nets[0]["id"]
return nets[0][target]
def get_external_network_id(context, network):
if uuidutils.is_uuid_like(network):
return network
else:
return get_network(context, network, source='name',
target='id', external=True)
def get_fixed_network_name(context, network):
if uuidutils.is_uuid_like(network):
return get_network(context, network, source='id',
target='name', external=False)
else:
return network

1
magnum/drivers/common/templates/kubernetes/fragments/write-heat-params-master.sh

@ -34,6 +34,7 @@ HEAT_PARAMS=/etc/sysconfig/heat-params
USERNAME="$USERNAME"
PASSWORD="$PASSWORD"
CLUSTER_NETWORK="$CLUSTER_NETWORK"
CLUSTER_NETWORK_NAME="$CLUSTER_NETWORK_NAME"
CLUSTER_SUBNET="$CLUSTER_SUBNET"
TLS_DISABLED="$TLS_DISABLED"
KUBE_DASHBOARD_ENABLED="$KUBE_DASHBOARD_ENABLED"

2
magnum/drivers/common/templates/kubernetes/fragments/write-kube-os-config.sh

@ -52,5 +52,5 @@ $ssh_cmd cp ${KUBE_OS_CLOUD_CONFIG} /etc/kubernetes/kube_openstack_config
cat > ${KUBE_OS_CLOUD_CONFIG}-occm <<EOF
$CLOUD_CONFIG
[Networking]
internal-network-name=$CLUSTER_NETWORK
internal-network-name=$CLUSTER_NETWORK_NAME
EOF

19
magnum/drivers/heat/k8s_template_def.py

@ -11,7 +11,6 @@
# under the License.
from oslo_config import cfg
from oslo_utils import uuidutils
from magnum.common import exception
from magnum.common import keystone
@ -132,12 +131,18 @@ class K8sTemplateDefinition(template_def.BaseTemplateDefinition):
# field name is confused. If external_network_id is not specified in
# cluster template use 'public' as the default value, which is the same
# with the heat template default value as before.
ext_net = cluster_template.external_network_id or "public"
if not uuidutils.is_uuid_like(ext_net):
ext_net_id = neutron.get_network_id(context, ext_net)
extra_params['external_network'] = ext_net_id
else:
extra_params['external_network'] = ext_net
external_network = cluster_template.external_network_id or "public"
extra_params['external_network'] = \
neutron.get_external_network_id(context, external_network)
# NOTE(brtknr): Convert fixed network UUID to name if the given network
# name is UUID like because OpenStack Cloud Controller Manager only
# accepts a name as an argument to internal-network-name in the
# cloud-config file provided to it. The default fixed network name is
# the same as that defined in the heat template.
fixed_network = cluster_template.fixed_network or "private"
extra_params['fixed_network_name'] = \
neutron.get_fixed_network_name(context, fixed_network)
label_list = ['flannel_network_cidr', 'flannel_backend',
'flannel_network_subnetlen',

8
magnum/drivers/k8s_fedora_atomic_v1/templates/kubecluster.yaml

@ -21,6 +21,11 @@ parameters:
description: uuid/name of an existing network to use to provision machines
default: ""
fixed_network_name:
type: string
description: name of a private network to use to provision machines
default: "private"
fixed_subnet:
type: string
description: uuid/name of an existing subnet to use to provision machines
@ -690,7 +695,7 @@ resources:
private_network_cidr: {get_param: fixed_network_cidr}
dns_nameserver: {get_param: dns_nameserver}
external_network: {get_param: external_network}
private_network_name: private
private_network_name: {get_param: fixed_network_name}
api_lb:
type: ../../common/templates/lb_api.yaml
@ -893,6 +898,7 @@ resources:
volume_driver: {get_param: volume_driver}
region_name: {get_param: region_name}
fixed_network: {get_attr: [network, fixed_network]}
fixed_network_name: {get_param: fixed_network_name}
fixed_subnet: {get_attr: [network, fixed_subnet]}
api_pool_id: {get_attr: [api_lb, pool_id]}
etcd_pool_id: {get_attr: [etcd_lb, pool_id]}

5
magnum/drivers/k8s_fedora_atomic_v1/templates/kubemaster.yaml

@ -174,6 +174,10 @@ parameters:
type: string
description: Network from which to allocate fixed addresses.
fixed_network_name:
type: string
description: Network from which to allocate fixed addresses.
fixed_subnet:
type: string
description: Subnet from which to allocate fixed addresses.
@ -585,6 +589,7 @@ resources:
"$USERNAME": {get_param: username}
"$PASSWORD": {get_param: password}
"$CLUSTER_NETWORK": {get_param: fixed_network}
"$CLUSTER_NETWORK_NAME": {get_param: fixed_network_name}
"$CLUSTER_SUBNET": {get_param: fixed_subnet}
"$TLS_DISABLED": {get_param: tls_disabled}
"$TRAEFIK_INGRESS_CONTROLLER_TAG": {get_param: traefik_ingress_controller_tag}

62
magnum/tests/unit/common/test_neutron.py

@ -143,7 +143,7 @@ class NeutronTest(base.TestCase):
)
@mock.patch('magnum.common.clients.OpenStackClients')
def test_get_network_id(self, mock_clients):
def test_get_external_network_id(self, mock_clients):
fake_name = "fake_network"
fake_id = "24fe5da0-1ac0-11e9-84cd-00224d6b7bc1"
mock_nclient = mock.MagicMock()
@ -161,12 +161,12 @@ class NeutronTest(base.TestCase):
mock_clients.return_value = osc
osc.neutron.return_value = mock_nclient
network_id = neutron.get_network_id(self.context, fake_name)
network_id = neutron.get_external_network_id(self.context, fake_name)
self.assertEqual(fake_id, network_id)
@mock.patch('magnum.common.clients.OpenStackClients')
def test_get_network_id_notfound(self, mock_clients):
def test_get_external_network_id_notfound(self, mock_clients):
fake_name = "fake_network"
fake_id = "24fe5da0-1ac0-11e9-84cd-00224d6b7bc1"
mock_nclient = mock.MagicMock()
@ -186,13 +186,13 @@ class NeutronTest(base.TestCase):
self.assertRaises(
exception.ExternalNetworkNotFound,
neutron.get_network_id,
neutron.get_external_network_id,
self.context,
"another_network"
)
@mock.patch('magnum.common.clients.OpenStackClients')
def test_get_network_id_conflict(self, mock_clients):
def test_get_external_network_id_conflict(self, mock_clients):
fake_name = "fake_network"
fake_id_1 = "24fe5da0-1ac0-11e9-84cd-00224d6b7bc1"
fake_id_2 = "93781f82-1ac0-11e9-84cd-00224d6b7bc1"
@ -218,7 +218,57 @@ class NeutronTest(base.TestCase):
self.assertRaises(
exception.Conflict,
neutron.get_network_id,
neutron.get_external_network_id,
self.context,
fake_name
)
@mock.patch('magnum.common.clients.OpenStackClients')
def test_get_fixed_network_name(self, mock_clients):
fake_name = "fake_network"
fake_id = "24fe5da0-1ac0-11e9-84cd-00224d6b7bc1"
mock_nclient = mock.MagicMock()
mock_nclient.list_networks.return_value = {
'networks': [
{
'id': fake_id,
'name': fake_name,
'router:external': False
}
]
}
osc = mock.MagicMock()
mock_clients.return_value = osc
osc.neutron.return_value = mock_nclient
network_name = neutron.get_fixed_network_name(self.context, fake_id)
self.assertEqual(fake_name, network_name)
@mock.patch('magnum.common.clients.OpenStackClients')
def test_get_fixed_network_name_notfound(self, mock_clients):
fake_name = "fake_network"
fake_id = "24fe5da0-1ac0-11e9-84cd-00224d6b7bc1"
another_fake_id = "34fe5da0-1ac0-11e9-84cd-00224d6b7bc1"
mock_nclient = mock.MagicMock()
mock_nclient.list_networks.return_value = {
'networks': [
{
'id': fake_id,
'name': fake_name,
'router:external': False
}
]
}
osc = mock.MagicMock()
mock_clients.return_value = osc
osc.neutron.return_value = mock_nclient
self.assertRaises(
exception.FixedNetworkNotFound,
neutron.get_fixed_network_name,
self.context,
another_fake_id
)

8
magnum/tests/unit/conductor/handlers/test_k8s_cluster_conductor.py

@ -286,6 +286,7 @@ class TestClusterConductorWithK8s(base.TestCase):
'ssh_key_name': 'keypair_id',
'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
'fixed_network': 'fixed_network',
'fixed_network_name': 'fixed_network',
'fixed_subnet': 'fixed_subnet',
'network_driver': 'network_driver',
'volume_driver': 'volume_driver',
@ -430,6 +431,7 @@ class TestClusterConductorWithK8s(base.TestCase):
'etcd_volume_size': None,
'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
'fixed_network': 'fixed_network',
'fixed_network_name': 'fixed_network',
'fixed_subnet': 'fixed_subnet',
'flannel_backend': 'vxlan',
'flannel_network_cidr': '10.101.0.0/16',
@ -518,7 +520,7 @@ class TestClusterConductorWithK8s(base.TestCase):
mock_get):
not_required = ['image_id', 'flavor_id', 'dns_nameserver',
'docker_volume_size', 'fixed_network', 'http_proxy',
'docker_volume_size', 'http_proxy',
'https_proxy', 'no_proxy', 'network_driver',
'master_flavor_id', 'docker_storage_driver',
'volume_driver', 'fixed_subnet']
@ -558,6 +560,7 @@ class TestClusterConductorWithK8s(base.TestCase):
'master_flavor': 'master_flavor_id',
'minion_flavor': 'flavor_id',
'fixed_network': 'fixed_network',
'fixed_network_name': 'fixed_network',
'fixed_subnet': 'fixed_subnet',
'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
'flannel_backend': 'vxlan',
@ -657,6 +660,7 @@ class TestClusterConductorWithK8s(base.TestCase):
'ssh_key_name': 'keypair_id',
'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
'fixed_network': 'fixed_network',
'fixed_network_name': 'fixed_network',
'fixed_subnet': 'fixed_subnet',
'availability_zone': 'az_1',
'nodes_affinity_policy': 'soft-anti-affinity',
@ -761,6 +765,7 @@ class TestClusterConductorWithK8s(base.TestCase):
'availability_zone': 'az_1',
'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
'fixed_network': 'fixed_network',
'fixed_network_name': 'fixed_network',
'fixed_subnet': 'fixed_subnet',
'dns_nameserver': 'dns_nameserver',
'docker_storage_driver': u'devicemapper',
@ -976,6 +981,7 @@ class TestClusterConductorWithK8s(base.TestCase):
'ssh_key_name': 'keypair_id',
'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
'fixed_network': 'fixed_network',
'fixed_network_name': 'fixed_network',
'fixed_subnet': 'fixed_subnet',
'dns_nameserver': 'dns_nameserver',
'server_image': 'image_id',

31
magnum/tests/unit/drivers/test_template_definition.py

@ -388,6 +388,7 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
expected_scale_params = {'minions_to_remove': ['node1', 'node2']}
self.assertEqual(scale_params, expected_scale_params)
@mock.patch('magnum.common.neutron.get_fixed_network_name')
@mock.patch('magnum.common.keystone.is_octavia_enabled')
@mock.patch('magnum.common.clients.OpenStackClients')
@mock.patch('magnum.drivers.k8s_fedora_atomic_v1.template_def'
@ -403,7 +404,8 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
mock_sign_node_certificate,
mock_get_output, mock_get_params,
mock_get_discovery_url, mock_osc_class,
mock_enable_octavia):
mock_enable_octavia,
mock_get_fixed_network_name):
mock_generate_csr_and_key.return_value = {'csr': 'csr',
'private_key': 'private_key',
'public_key': 'public_key'}
@ -417,6 +419,10 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
mock_cluster_template.network_driver = 'flannel'
external_network_id = '17e4e301-b7f3-4996-b3dd-97b3a700174b'
mock_cluster_template.external_network_id = external_network_id
fixed_network_name = 'fixed_network'
mock_get_fixed_network_name.return_value = fixed_network_name
fixed_network = '5d12f6fd-a196-4bf0-ae4c-1f639a523a52'
mock_cluster_template.fixed_network = fixed_network
mock_cluster = mock.MagicMock()
mock_cluster.uuid = '5d12f6fd-a196-4bf0-ae4c-1f639a523a52'
del mock_cluster.stack_id
@ -572,6 +578,7 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
'kube_tag': kube_tag,
'etcd_tag': etcd_tag,
'coredns_tag': coredns_tag,
'fixed_network_name': fixed_network_name,
'flannel_tag': flannel_tag,
'flannel_cni_tag': flannel_cni_tag,
'container_infra_prefix': container_infra_prefix,
@ -636,8 +643,17 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
mock_cluster_template,
mock_cluster,
)
actual_params = mock_get_params.call_args[1]["extra_params"]
self.assertEqual(
fixed_network_name,
actual_params.get("fixed_network_name")
)
mock_get_fixed_network_name.assert_called_once_with(
mock_context,
mock_cluster_template.fixed_network
)
@mock.patch('magnum.common.neutron.get_network_id')
@mock.patch('magnum.common.neutron.get_external_network_id')
@mock.patch('magnum.common.keystone.is_octavia_enabled')
@mock.patch('magnum.common.clients.OpenStackClients')
@mock.patch('magnum.drivers.k8s_fedora_atomic_v1.template_def'
@ -654,14 +670,14 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
mock_get_discovery_url,
mock_osc_class,
mock_enable_octavia,
mock_network_id):
mock_get_external_network_id):
mock_generate_csr_and_key.return_value = {'csr': 'csr',
'private_key': 'private_key',
'public_key': 'public_key'}
mock_enable_octavia.return_value = False
mock_get_discovery_url.return_value = 'fake_discovery_url'
external_network_id = 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e'
mock_network_id.return_value = external_network_id
mock_get_external_network_id.return_value = external_network_id
mock_context = mock.MagicMock()
mock_context.auth_token = 'AUTH_TOKEN'
@ -672,7 +688,7 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
mock_cluster_template.tls_disabled = False
mock_cluster_template.registry_enabled = False
mock_cluster_template.network_driver = 'calico'
mock_cluster_template.external_network_id = "public"
mock_cluster_template.external_network_id = 'public'
mock_cluster = mock.MagicMock()
mock_cluster.labels = {}
@ -692,7 +708,7 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
external_network_id,
actual_params.get("external_network")
)
mock_network_id.assert_called_once_with(
mock_get_external_network_id.assert_called_once_with(
mock_context,
mock_cluster_template.external_network_id
)
@ -833,6 +849,8 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
mock_cluster_template.network_driver = 'calico'
external_network_id = '17e4e301-b7f3-4996-b3dd-97b3a700174b'
mock_cluster_template.external_network_id = external_network_id
fixed_network_name = 'fixed_network'
mock_cluster_template.fixed_network = fixed_network_name
mock_cluster = mock.MagicMock()
mock_cluster.uuid = '5d12f6fd-a196-4bf0-ae4c-1f639a523a52'
del mock_cluster.stack_id
@ -967,6 +985,7 @@ class AtomicK8sTemplateDefinitionTestCase(BaseK8sTemplateDefinitionTestCase):
'flannel_backend': flannel_backend,
'system_pods_initial_delay': system_pods_initial_delay,
'system_pods_timeout': system_pods_timeout,
'fixed_network_name': fixed_network_name,
'admission_control_list': admission_control_list,
'prometheus_monitoring': prometheus_monitoring,
'grafana_admin_passwd': grafana_admin_passwd,

Loading…
Cancel
Save