Пример #1
0
    def setup_poll_test(self, mock_driver, mock_openstack_client, cfg,
                        mock_retrieve_cluster_template):
        cfg.CONF.cluster_heat.max_attempts = 10

        worker_ng = mock.MagicMock(uuid='worker_ng', role='worker')
        master_ng = mock.MagicMock(uuid='master_ng', role='master')
        nodegroups = [worker_ng, master_ng]
        cluster = mock.MagicMock(nodegroups=nodegroups,
                                 default_ng_worker=worker_ng,
                                 default_ng_master=master_ng)
        cluster_template_dict = utils.get_test_cluster_template(
            coe='kubernetes')
        mock_heat_stack = mock.MagicMock()
        mock_heat_client = mock.MagicMock()
        mock_heat_client.stacks.get.return_value = mock_heat_stack
        mock_openstack_client.heat.return_value = mock_heat_client
        cluster_template = objects.ClusterTemplate(self.context,
                                                   **cluster_template_dict)
        mock_retrieve_cluster_template.return_value = cluster_template
        mock_driver.return_value = k8s_atomic_dr.Driver()
        poller = heat_driver.HeatPoller(mock_openstack_client,
                                        mock.MagicMock(), cluster,
                                        k8s_atomic_dr.Driver())
        poller.get_version_info = mock.MagicMock()
        return (mock_heat_stack, cluster, poller)
Пример #2
0
    def test_update_stack(self,
                          mock_osc,
                          mock_extract_template_definition,
                          mock_get_template_contents):

        mock_stack_id = 'xx-xx-xx-xx'
        expected_template_contents = 'template_contents'

        mock_tpl_files = {}
        mock_get_template_contents.return_value = [
            mock_tpl_files, expected_template_contents]
        mock_extract_template_definition.return_value = ('template/path',
                                                         {}, [])
        mock_heat_client = mock.MagicMock()
        mock_osc.return_value.heat.return_value = mock_heat_client
        mock_cluster = mock.MagicMock()
        mock_cluster.stack_id = mock_stack_id

        k8s_dr.Driver().update_cluster({}, mock_cluster)

        expected_args = {
            'parameters': {},
            'template': expected_template_contents,
            'files': {},
            'environment_files': [],
            'disable_rollback': True
        }
        mock_heat_client.stacks.update.assert_called_once_with(mock_stack_id,
                                                               **expected_args)
Пример #3
0
    def test_create_stack_no_timeout_specified(
            self, mock_osc, mock_extract_template_definition,
            mock_get_template_contents, mock_generate_id):

        mock_generate_id.return_value = 'xx-xx-xx-xx'
        expected_stack_name = 'expected-stack-name-xx-xx-xx-xx'
        expected_template_contents = 'template_contents'
        dummy_cluster_name = 'expected_stack_name'
        expected_timeout = CONF.cluster_heat.create_timeout

        mock_tpl_files = {}
        mock_get_template_contents.return_value = [
            mock_tpl_files, expected_template_contents
        ]
        mock_extract_template_definition.return_value = ('template/path', {},
                                                         [])
        mock_heat_client = mock.MagicMock()
        mock_osc.return_value.heat.return_value = mock_heat_client
        mock_cluster = mock.MagicMock()
        mock_cluster.name = dummy_cluster_name

        k8s_dr.Driver().create_cluster(self.context, mock_cluster, None)

        expected_args = {
            'stack_name': expected_stack_name,
            'parameters': {},
            'template': expected_template_contents,
            'files': {},
            'environment_files': [],
            'timeout_mins': expected_timeout
        }
        mock_heat_client.stacks.create.assert_called_once_with(**expected_args)
Пример #4
0
    def test_cluster_delete(self, mock_octavia, mock_driver,
                            mock_openstack_client_class, cert_manager):
        mock_octavia.return_value = False
        mock_driver.return_value = k8s_atomic_dr.Driver()
        osc = mock.MagicMock()
        mock_openstack_client_class.return_value = osc
        osc.heat.side_effect = exc.HTTPNotFound
        self.master.create()
        self.worker.create()
        self.assertEqual(2, len(self.cluster.nodegroups))
        self.handler.cluster_delete(self.context, self.cluster.uuid)

        notifications = fake_notifier.NOTIFICATIONS
        self.assertEqual(2, len(notifications))
        self.assertEqual('magnum.cluster.delete', notifications[0].event_type)
        self.assertEqual(taxonomy.OUTCOME_PENDING,
                         notifications[0].payload['outcome'])
        self.assertEqual('magnum.cluster.delete', notifications[1].event_type)
        self.assertEqual(taxonomy.OUTCOME_SUCCESS,
                         notifications[1].payload['outcome'])
        self.assertEqual(
            1, cert_manager.delete_certificates_from_cluster.call_count)
        # Assert that the cluster nodegroups were delete as well
        db_nodegroups = objects.NodeGroup.list(self.context, self.cluster.uuid)
        self.assertEqual([], db_nodegroups)
        # The cluster has been destroyed
        self.assertRaises(exception.ClusterNotFound, objects.Cluster.get,
                          self.context, self.cluster.uuid)
Пример #5
0
    def test_cluster_delete_conflict(self, mock_octavia, mock_driver,
                                     mock_openstack_client_class,
                                     cert_manager):
        mock_octavia.return_value = False
        mock_driver.return_value = k8s_atomic_dr.Driver()
        osc = mock.MagicMock()
        mock_openstack_client_class.return_value = osc
        osc.heat.side_effect = exc.HTTPConflict
        self.assertRaises(exception.OperationInProgress,
                          self.handler.cluster_delete,
                          self.context,
                          self.cluster.uuid)

        notifications = fake_notifier.NOTIFICATIONS
        self.assertEqual(2, len(notifications))
        self.assertEqual(
            'magnum.cluster.delete', notifications[0].event_type)
        self.assertEqual(
            taxonomy.OUTCOME_PENDING, notifications[0].payload['outcome'])
        self.assertEqual(
            'magnum.cluster.delete', notifications[1].event_type)
        self.assertEqual(
            taxonomy.OUTCOME_FAILURE, notifications[1].payload['outcome'])
        self.assertEqual(
            0, cert_manager.delete_certificates_from_cluster.call_count)
Пример #6
0
    def test_get_vm_atomic_kubernetes_definition(self, mock_driver):
        mock_driver.return_value = k8sa_dr.Driver()
        cluster_driver = driver.Driver.get_driver('vm', 'fedora-atomic',
                                                  'kubernetes')
        definition = cluster_driver.get_template_definition()

        self.assertIsInstance(definition,
                              k8sa_tdef.AtomicK8sTemplateDefinition)
 def test_extract_template_definition_without_docker_volume_size(
         self, mock_driver, mock_objects_cluster_template_get_by_uuid,
         mock_get):
     mock_driver.return_value = k8s_dr.Driver()
     self._test_extract_template_definition(
         mock_driver,
         mock_objects_cluster_template_get_by_uuid,
         mock_get,
         missing_attr='docker_volume_size')
 def test_extract_template_definition_without_master_count(
         self, mock_driver, mock_objects_cluster_template_get_by_uuid,
         mock_get):
     mock_driver.return_value = k8s_dr.Driver()
     self._test_extract_template_definition(
         mock_driver,
         mock_objects_cluster_template_get_by_uuid,
         mock_get,
         missing_attr='master_count')
Пример #9
0
    def test_cluster_delete_with_lb(self, mock_delete_lb, mock_octavia,
                                    mock_clients, mock_driver):
        mock_octavia.return_value = True
        mock_driver.return_value = k8s_atomic_dr.Driver()

        self.handler.cluster_delete(self.context, self.cluster.uuid)

        notifications = fake_notifier.NOTIFICATIONS
        self.assertEqual(1, len(notifications))
        self.assertEqual(1, mock_delete_lb.call_count)
Пример #10
0
    def setup_poll_test(self, mock_driver, mock_openstack_client, cfg,
                        mock_retrieve_cluster_template):
        cfg.CONF.cluster_heat.max_attempts = 10

        cluster = mock.MagicMock()
        cluster_template_dict = utils.get_test_cluster_template(
            coe='kubernetes')
        mock_heat_stack = mock.MagicMock()
        mock_heat_client = mock.MagicMock()
        mock_heat_client.stacks.get.return_value = mock_heat_stack
        mock_openstack_client.heat.return_value = mock_heat_client
        cluster_template = objects.ClusterTemplate(self.context,
                                                   **cluster_template_dict)
        mock_retrieve_cluster_template.return_value = cluster_template
        mock_driver.return_value = k8s_atomic_dr.Driver()
        poller = cluster_conductor.HeatPoller(mock_openstack_client, cluster,
                                              k8s_atomic_dr.Driver())
        poller.get_version_info = mock.MagicMock()
        return (mock_heat_stack, cluster, poller)
Пример #11
0
 def test_extract_template_definition_without_node_count(
         self, mock_generate_csr_and_key, mock_sign_node_certificate,
         mock_driver, mock_objects_cluster_template_get_by_uuid, mock_get):
     mock_driver.return_value = k8s_dr.Driver()
     self._test_extract_template_definition(
         mock_generate_csr_and_key,
         mock_sign_node_certificate,
         mock_driver,
         mock_objects_cluster_template_get_by_uuid,
         mock_get,
         missing_attr='node_count')
    def test_add_output_with_mapping_type(self):
        definition = k8sa_dr.Driver().get_template_definition()

        mock_args = [1, 3, 4]
        mock_kwargs = {'test': 'test'}
        mock_mapping_type = mock.MagicMock()
        mock_mapping_type.return_value = mock.MagicMock()
        definition.add_output(mapping_type=mock_mapping_type, *mock_args,
                              **mock_kwargs)

        mock_mapping_type.assert_called_once_with(*mock_args, **mock_kwargs)
        self.assertIn(mock_mapping_type.return_value,
                      definition.output_mappings)
Пример #13
0
    def test_update_stack(self, mock_osc, mock_extract_template_definition,
                          mock_get_template_contents):

        mock_stack_id = 'xx-xx-xx-xx'
        mock_heat_client = mock.MagicMock()
        mock_osc.return_value.heat.return_value = mock_heat_client
        mock_cluster = mock.MagicMock()
        mock_cluster.stack_id = mock_stack_id

        k8s_dr.Driver().update_cluster({}, mock_cluster)

        expected_args = {
            'parameters': {
                'number_of_minions': mock_cluster.node_count
            },
            'existing': True,
            'disable_rollback': True
        }
        mock_heat_client.stacks.update.assert_called_once_with(
            mock_stack_id, **expected_args)
Пример #14
0
 def setUp(self):
     super(K8sFedoraAtomicV1DriverTest, self).setUp()
     self.driver = driver.Driver()
     self.cluster_template = obj_utils.create_test_cluster_template(
         self.context,
         uuid='94889aa4-e686-11e9-81b4-2a2ae2dbcce4',
         name='test_2',
         id=2,
         labels={'kube_tag': 'v1.14.7'},
         image_id='test-image2')
     self.cluster_obj = obj_utils.create_test_cluster(
         self.context, name='cluster_example_A', image_id='test-image1')
     self.cluster_obj.refresh()
     self.nodegroup_obj = obj_utils.create_test_nodegroup(
         self.context,
         name='test_ng',
         cluster_id=self.cluster_obj.uuid,
         uuid='27e3153e-d5bf-4b7e-b517-fb518e17f34c',
         project_id=self.cluster_obj.project_id,
         is_default=False,
         image_id='test-image1')
     self.nodegroup_obj.refresh()
Пример #15
0
    def setup_poll_test(self,
                        mock_driver,
                        mock_openstack_client,
                        cfg,
                        mock_retrieve_cluster_template,
                        default_stack_status=None,
                        status_reason=None,
                        stack_params=None,
                        stack_missing=False):
        cfg.CONF.cluster_heat.max_attempts = 10

        if default_stack_status is None:
            default_stack_status = cluster_status.CREATE_COMPLETE

        cluster = mock.MagicMock(nodegroups=list())

        def_worker = self._create_nodegroup(cluster,
                                            'worker_ng',
                                            'stack1',
                                            name='worker_ng',
                                            role='worker',
                                            is_default=True,
                                            stack_status=default_stack_status,
                                            status_reason=status_reason,
                                            stack_params=stack_params,
                                            stack_missing=stack_missing)
        def_master = self._create_nodegroup(cluster,
                                            'master_ng',
                                            'stack1',
                                            name='master_ng',
                                            role='master',
                                            is_default=True,
                                            stack_status=default_stack_status,
                                            status_reason=status_reason,
                                            stack_params=stack_params,
                                            stack_missing=stack_missing)

        cluster.default_ng_worker = def_worker
        cluster.default_ng_master = def_master

        self.def_ngs = [def_worker, def_master]

        def get_ng_stack(stack_id, resolve_outputs=False):
            try:
                return self.mock_stacks[stack_id]
            except KeyError:
                # In this case we intentionally didn't add the stack
                # to the mock_stacks dict to simulte a not found error.
                # For this reason raise heat NotFound exception.
                raise heatexc.NotFound("stack not found")

        cluster_template_dict = utils.get_test_cluster_template(
            coe='kubernetes')
        mock_heat_client = mock.MagicMock()
        mock_heat_client.stacks.get = get_ng_stack
        mock_openstack_client.heat.return_value = mock_heat_client
        cluster_template = objects.ClusterTemplate(self.context,
                                                   **cluster_template_dict)
        mock_retrieve_cluster_template.return_value = cluster_template
        mock_driver.return_value = k8s_atomic_dr.Driver()
        poller = heat_driver.HeatPoller(mock_openstack_client,
                                        mock.MagicMock(), cluster,
                                        k8s_atomic_dr.Driver())
        poller.get_version_info = mock.MagicMock()
        return (cluster, poller)
Пример #16
0
    def test_create_with_environment(
            self, mock_short_id, mock_openstack_client_class, mock_driver,
            mock_extract_tmpl_def, mock_cert_manager, mock_trust_manager,
            mock_get_template_contents, mock_process_mult,
            mock_heat_poller_class):
        timeout = 15
        mock_poller = mock.MagicMock()
        mock_poller.poll_and_check.return_value = loopingcall.LoopingCallDone()
        mock_heat_poller_class.return_value = mock_poller
        mock_driver.return_value = k8s_atomic_dr.Driver()
        mock_short_id.return_value = 'short_id'

        mock_extract_tmpl_def.return_value = ('the/template/path.yaml', {
            'heat_param_1': 'foo',
            'heat_param_2': 'bar'
        }, ['env_file_1', 'env_file_2'])

        mock_get_template_contents.return_value = ({
            'tmpl_file_1':
            'some content',
            'tmpl_file_2':
            'some more content'
        }, 'some template yaml')

        def do_mock_process_mult(env_paths=None, env_list_tracker=None):
            self.assertEqual(env_list_tracker, [])
            for f in env_paths:
                env_list_tracker.append('file:///' + f)
            env_map = {path: 'content of ' + path for path in env_list_tracker}
            return (env_map, None)

        mock_process_mult.side_effect = do_mock_process_mult

        mock_hc = mock.Mock()
        mock_hc.stacks.create.return_value = {'stack': {'id': 'stack-id'}}

        osc = mock.Mock()
        osc.heat.return_value = mock_hc
        mock_openstack_client_class.return_value = osc

        # NOTE(ttsiouts): self.cluster is already created so it's
        # a bad idea to use it and try to create it again... Instead
        # get a new object and use it.
        cluster_dict = utils.get_test_cluster(
            node_count=1, uuid='f6a99187-6f42-4fbb-aa6f-18407c0ee50e')
        del cluster_dict['id']
        cluster = objects.Cluster(self.context, **cluster_dict)
        node_count = cluster.node_count
        master_count = cluster.master_count

        self.handler.cluster_create(self.context, cluster, master_count,
                                    node_count, timeout)

        mock_extract_tmpl_def.assert_called_once_with(self.context,
                                                      cluster,
                                                      nodegroups=None)
        mock_get_template_contents.assert_called_once_with(
            'the/template/path.yaml')
        mock_process_mult.assert_called_once_with(
            env_paths=['the/template/env_file_1', 'the/template/env_file_2'],
            env_list_tracker=mock.ANY)
        mock_hc.stacks.create.assert_called_once_with(
            environment_files=[
                'file:///the/template/env_file_1',
                'file:///the/template/env_file_2'
            ],
            files={
                'tmpl_file_1':
                'some content',
                'tmpl_file_2':
                'some more content',
                'file:///the/template/env_file_1':
                'content of file:///the/template/env_file_1',
                'file:///the/template/env_file_2':
                'content of file:///the/template/env_file_2'
            },
            parameters={
                'is_cluster_stack': True,
                'heat_param_1': 'foo',
                'heat_param_2': 'bar'
            },
            stack_name=('%s-short_id' % cluster.name),
            template='some template yaml',
            timeout_mins=timeout)
        self.assertEqual(node_count, cluster.node_count)
        self.assertEqual(node_count, cluster.default_ng_worker.node_count)
        self.assertEqual(master_count, cluster.master_count)
        self.assertEqual(master_count, cluster.default_ng_master.node_count)
    def test_extract_template_definition_with_registry(
            self, mock_driver, mock_objects_cluster_template_get_by_uuid,
            mock_get):
        self.cluster_template_dict['registry_enabled'] = True
        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        expected_result = str('{"action":"get","node":{"key":"test","value":'
                              '"1","modifiedIndex":10,"createdIndex":10}}')
        mock_resp = mock.MagicMock()
        mock_resp.text = expected_result
        mock_get.return_value = mock_resp
        cluster = objects.Cluster(self.context, **self.cluster_dict)
        mock_driver.return_value = k8s_dr.Driver()

        CONF.set_override('swift_region', 'RegionOne', group='docker_registry')

        CONF.set_override('cluster_user_trust', True, group='trust')

        (template_path, definition,
         env_files) = mock_driver()._extract_template_definition(
             self.context, cluster)

        expected = {
            'auth_url': 'http://192.168.10.10:5000/v3',
            'cluster_uuid': '5d12f6fd-a196-4bf0-ae4c-1f639a523a52',
            'discovery_url': 'https://discovery.etcd.io/test',
            'dns_nameserver': 'dns_nameserver',
            'docker_storage_driver': 'devicemapper',
            'docker_volume_size': 20,
            'external_network': 'external_network_id',
            'fixed_network': 'fixed_network',
            'fixed_subnet': 'fixed_subnet',
            'flannel_backend': 'vxlan',
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'system_pods_initial_delay': '15',
            'system_pods_timeout': '1',
            'admission_control_list': 'fake_list',
            'prometheus_monitoring': 'False',
            'grafana_admin_passwd': 'fake_pwd',
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'magnum_url': 'http://127.0.0.1:9511/v1',
            'master_flavor': 'master_flavor_id',
            'minion_flavor': 'flavor_id',
            'network_driver': 'network_driver',
            'no_proxy': 'no_proxy',
            'number_of_masters': 1,
            'number_of_minions': 1,
            'region_name': 'RegionOne',
            'registry_container': 'docker_registry',
            'registry_enabled': True,
            'server_image': 'image_id',
            'ssh_key_name': 'keypair_id',
            'swift_region': 'RegionOne',
            'tenant_name': 'fake_tenant',
            'tls_disabled': False,
            'trust_id': 'bd11efc5-d4e2-4dac-bbce-25e348ddf7de',
            'trustee_domain_id': self.mock_keystone.trustee_domain_id,
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trustee_username': '******',
            'username': '******',
            'volume_driver': 'volume_driver',
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
        }

        self.assertEqual(expected, definition)
        self.assertEqual([
            '../../common/templates/environments/no_private_network.yaml',
            '../../common/templates/environments/with_volume.yaml',
            '../../common/templates/environments/no_master_lb.yaml',
            '../../common/templates/environments/disable_floating_ip.yaml',
        ], env_files)
Пример #18
0
    def test_extract_template_definition_without_discovery_url(
            self, mock_generate_csr_and_key, mock_sign_node_certificate,
            mock_driver, mock_objects_cluster_template_get_by_uuid, reqget):
        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_generate_csr_and_key.return_value = {
            'csr': 'csr',
            'private_key': 'private_key',
            'public_key': 'public_key'
        }
        mock_sign_node_certificate.return_value = 'signed_cert'
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        cluster_dict = self.cluster_dict
        cluster_dict['discovery_url'] = None
        cluster = objects.Cluster(self.context, **cluster_dict)
        mock_driver.return_value = k8s_dr.Driver()

        CONF.set_override('etcd_discovery_service_endpoint_format',
                          'http://etcd/test?size=%(size)d',
                          group='cluster')
        mock_req = mock.MagicMock(text='https://address/token',
                                  status_code=200)
        reqget.return_value = mock_req

        (template_path, definition,
         env_files) = mock_driver()._extract_template_definition(
             self.context, cluster)

        expected = {
            'ssh_key_name': 'keypair_id',
            'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
            'fixed_network': 'fixed_network',
            'fixed_subnet': 'fixed_subnet',
            'dns_nameserver': 'dns_nameserver',
            'server_image': 'image_id',
            'master_flavor': 'master_flavor_id',
            'minion_flavor': 'flavor_id',
            'number_of_minions': 1,
            'number_of_masters': 1,
            'network_driver': 'network_driver',
            'volume_driver': 'volume_driver',
            'docker_volume_size': 20,
            'docker_volume_type': 'lvmdriver-1',
            'docker_storage_driver': 'devicemapper',
            'discovery_url': 'https://address/token',
            'etcd_volume_size': None,
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'no_proxy': 'no_proxy',
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'flannel_backend': 'vxlan',
            'system_pods_initial_delay': '15',
            'system_pods_timeout': '1',
            'admission_control_list': 'fake_list',
            'prometheus_monitoring': 'False',
            'grafana_admin_passwd': 'fake_pwd',
            'kube_dashboard_enabled': 'True',
            'influx_grafana_dashboard_enabled': 'True',
            'username': '******',
            'cluster_uuid': self.cluster_dict['uuid'],
            'magnum_url': self.mock_osc.magnum_url.return_value,
            'region_name': self.mock_osc.cinder_region_name.return_value,
            'tls_disabled': False,
            'registry_enabled': False,
            'trustee_domain_id': self.mock_keystone.trustee_domain_id,
            'trustee_username': '******',
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trust_id': '',
            'auth_url': 'http://192.168.10.10:5000/v3',
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
            'verify_ca': True,
            'openstack_ca': '',
            "nodes_affinity_policy": "soft-anti-affinity",
            'availability_zone': 'az_1',
            'cert_manager_api': 'False',
            'ingress_controller': 'i-controller',
            'ingress_controller_role': 'i-controller-role',
            'octavia_ingress_controller_tag': None,
            'kubelet_options': '--kubelet',
            'kubeapi_options': '--kubeapi',
            'kubecontroller_options': '--kubecontroller',
            'kubescheduler_options': '--kubescheduler',
            'kubeproxy_options': '--kubeproxy',
            'octavia_enabled': False,
            'kube_service_account_key': 'public_key',
            'kube_service_account_private_key': 'private_key',
            'portal_network_cidr': '10.254.0.0/16',
            'project_id': 'project_id'
        }
        self.assertEqual(expected, definition)
        self.assertEqual([
            '../../common/templates/environments/no_private_network.yaml',
            '../../common/templates/environments/no_etcd_volume.yaml',
            '../../common/templates/environments/with_volume.yaml',
            '../../common/templates/environments/no_master_lb.yaml',
            '../../common/templates/environments/disable_floating_ip.yaml',
        ], env_files)
        reqget.assert_called_once_with('http://etcd/test?size=1',
                                       proxies={
                                           'http': 'http_proxy',
                                           'https': 'https_proxy'
                                       })
Пример #19
0
    def test_extract_template_definition_only_required(
            self, mock_generate_csr_and_key, mock_sign_node_certificate,
            mock_driver, mock_objects_cluster_template_get_by_uuid, mock_get):

        not_required = [
            'image_id', 'flavor_id', 'dns_nameserver', 'docker_volume_size',
            'fixed_network', 'http_proxy', 'https_proxy', 'no_proxy',
            'network_driver', 'master_flavor_id', 'docker_storage_driver',
            'volume_driver', 'fixed_subnet'
        ]
        for key in not_required:
            self.cluster_template_dict[key] = None
        self.cluster_dict['discovery_url'] = 'https://discovery.etcd.io/test'

        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_generate_csr_and_key.return_value = {
            'csr': 'csr',
            'private_key': 'private_key',
            'public_key': 'public_key'
        }
        mock_sign_node_certificate.return_value = 'signed_cert'
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        expected_result = str('{"action":"get","node":{"key":"test","value":'
                              '"1","modifiedIndex":10,"createdIndex":10}}')
        mock_resp = mock.MagicMock()
        mock_resp.text = expected_result
        mock_get.return_value = mock_resp
        mock_driver.return_value = k8s_dr.Driver()
        cluster = objects.Cluster(self.context, **self.cluster_dict)

        (template_path, definition,
         env_files) = mock_driver()._extract_template_definition(
             self.context, cluster)

        expected = {
            'auth_url': 'http://192.168.10.10:5000/v3',
            'cluster_uuid': '5d12f6fd-a196-4bf0-ae4c-1f639a523a52',
            'discovery_url': 'https://discovery.etcd.io/test',
            'docker_volume_size': 20,
            'master_flavor': 'master_flavor_id',
            'minion_flavor': 'flavor_id',
            'external_network': 'e2a6c8b0-a3c2-42a3-b3f4-01400a30896e',
            'flannel_backend': 'vxlan',
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'system_pods_initial_delay': '15',
            'system_pods_timeout': '1',
            'admission_control_list': 'fake_list',
            'prometheus_monitoring': 'False',
            'grafana_admin_passwd': 'fake_pwd',
            'kube_dashboard_enabled': 'True',
            'influx_grafana_dashboard_enabled': 'True',
            'docker_volume_type': 'lvmdriver-1',
            'etcd_volume_size': None,
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
            'magnum_url': 'http://127.0.0.1:9511/v1',
            'number_of_masters': 1,
            'number_of_minions': 1,
            'region_name': 'RegionOne',
            'registry_enabled': False,
            'ssh_key_name': 'keypair_id',
            'tls_disabled': False,
            'trust_id': '',
            'trustee_domain_id': 'trustee_domain_id',
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trustee_username': '******',
            'username': '******',
            'verify_ca': True,
            'openstack_ca': '',
            "nodes_affinity_policy": "soft-anti-affinity",
            'availability_zone': 'az_1',
            'cert_manager_api': 'False',
            'ingress_controller': 'i-controller',
            'ingress_controller_role': 'i-controller-role',
            'octavia_ingress_controller_tag': None,
            'kubelet_options': '--kubelet',
            'kubeapi_options': '--kubeapi',
            'kubecontroller_options': '--kubecontroller',
            'kubescheduler_options': '--kubescheduler',
            'kubeproxy_options': '--kubeproxy',
            'octavia_enabled': False,
            'kube_service_account_key': 'public_key',
            'kube_service_account_private_key': 'private_key',
            'portal_network_cidr': '10.254.0.0/16',
            'project_id': 'project_id'
        }
        self.assertEqual(expected, definition)
        self.assertEqual([
            '../../common/templates/environments/with_private_network.yaml',
            '../../common/templates/environments/no_etcd_volume.yaml',
            '../../common/templates/environments/with_volume.yaml',
            '../../common/templates/environments/no_master_lb.yaml',
            '../../common/templates/environments/disable_floating_ip.yaml',
        ], env_files)
Пример #20
0
    def _test_extract_template_definition(
            self,
            mock_generate_csr_and_key,
            mock_sign_node_certificate,
            mock_driver,
            mock_objects_cluster_template_get_by_uuid,
            mock_get,
            missing_attr=None):
        if missing_attr in self.cluster_template_dict:
            self.cluster_template_dict[missing_attr] = None
        elif missing_attr in self.cluster_dict:
            self.cluster_dict[missing_attr] = None
        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_generate_csr_and_key.return_value = {
            'csr': 'csr',
            'private_key': 'private_key',
            'public_key': 'public_key'
        }
        mock_sign_node_certificate.return_value = 'signed_cert'
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        expected_result = str('{"action":"get","node":{"key":"test","value":'
                              '"1","modifiedIndex":10,"createdIndex":10}}')
        mock_resp = mock.MagicMock()
        mock_resp.text = expected_result
        mock_resp.status_code = 200
        mock_get.return_value = mock_resp
        cluster = objects.Cluster(self.context, **self.cluster_dict)
        mock_driver.return_value = k8s_dr.Driver()

        (template_path, definition,
         env_files) = mock_driver()._extract_template_definition(
             self.context, cluster)

        mapping = {
            'dns_nameserver': 'dns_nameserver',
            'image_id': 'server_image',
            'flavor_id': 'minion_flavor',
            'docker_volume_size': 'docker_volume_size',
            'docker_storage_driver': 'docker_storage_driver',
            'network_driver': 'network_driver',
            'volume_driver': 'volume_driver',
            'master_flavor_id': 'master_flavor',
            'apiserver_port': '',
            'node_count': 'number_of_minions',
            'master_count': 'number_of_masters',
            'discovery_url': 'discovery_url',
            'labels': {
                'flannel_network_cidr': '10.101.0.0/16',
                'flannel_network_subnetlen': '26',
                'flannel_backend': 'vxlan',
                'system_pods_initial_delay': '15',
                'system_pods_timeout': '1',
                'admission_control_list': 'fake_list',
                'prometheus_monitoring': 'False',
                'grafana_admin_passwd': 'fake_pwd',
                'kube_dashboard_enabled': 'True',
                'influx_grafana_dashboard_enabled': 'True',
                'docker_volume_type': 'lvmdriver-1',
                'etcd_volume_size': None,
                'availability_zone': 'az_1',
                'cert_manager_api': 'False',
                'ingress_controller': 'i-controller',
                'ingress_controller_role': 'i-controller-role',
                'kubelet_options': '--kubelet',
                'kubeapi_options': '--kubeapi',
                'kubecontroller_options': '--kubecontroller',
                'kubescheduler_options': '--kubescheduler',
                'kubeproxy_options': '--kubeproxy',
            },
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'no_proxy': 'no_proxy',
            'cluster_uuid': self.cluster_dict['uuid'],
            'magnum_url': self.mock_osc.magnum_url.return_value,
            'tls_disabled': False,
            'insecure_registry': '10.0.0.1:5000',
        }
        expected = {
            'ssh_key_name': 'keypair_id',
            'external_network': 'external_network_id',
            'fixed_network': 'fixed_network',
            'fixed_subnet': 'fixed_subnet',
            'network_driver': 'network_driver',
            'volume_driver': 'volume_driver',
            'dns_nameserver': 'dns_nameserver',
            'server_image': 'image_id',
            'minion_flavor': 'flavor_id',
            'master_flavor': 'master_flavor_id',
            'number_of_minions': 1,
            'number_of_masters': 1,
            'docker_volume_size': 20,
            'docker_volume_type': 'lvmdriver-1',
            'docker_storage_driver': 'devicemapper',
            'discovery_url': 'https://discovery.etcd.io/test',
            'etcd_volume_size': None,
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'flannel_backend': 'vxlan',
            'system_pods_initial_delay': '15',
            'system_pods_timeout': '1',
            'admission_control_list': 'fake_list',
            'prometheus_monitoring': 'False',
            'grafana_admin_passwd': 'fake_pwd',
            'kube_dashboard_enabled': 'True',
            'influx_grafana_dashboard_enabled': 'True',
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'no_proxy': 'no_proxy',
            'username': '******',
            'cluster_uuid': self.cluster_dict['uuid'],
            'magnum_url': self.mock_osc.magnum_url.return_value,
            'region_name': self.mock_osc.cinder_region_name.return_value,
            'tls_disabled': False,
            'registry_enabled': False,
            'trustee_domain_id': self.mock_keystone.trustee_domain_id,
            'trustee_username': '******',
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trust_id': '',
            'auth_url': 'http://192.168.10.10:5000/v3',
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
            'verify_ca': True,
            'openstack_ca': '',
            "nodes_affinity_policy": "soft-anti-affinity",
            'availability_zone': 'az_1',
            'cert_manager_api': 'False',
            'ingress_controller': 'i-controller',
            'ingress_controller_role': 'i-controller-role',
            'kubelet_options': '--kubelet',
            'kubeapi_options': '--kubeapi',
            'kubecontroller_options': '--kubecontroller',
            'kubescheduler_options': '--kubescheduler',
            'kubeproxy_options': '--kubeproxy',
            'octavia_enabled': False,
            'kube_service_account_key': 'public_key',
            'kube_service_account_private_key': 'private_key',
        }
        if missing_attr is not None:
            expected.pop(mapping[missing_attr], None)

        self.assertEqual(expected, definition)
        self.assertEqual([
            '../../common/templates/environments/no_private_network.yaml',
            '../../common/templates/environments/no_etcd_volume.yaml',
            '../../common/templates/environments/with_volume.yaml',
            '../../common/templates/environments/no_master_lb.yaml',
            '../../common/templates/environments/disable_floating_ip.yaml',
        ], env_files)
Пример #21
0
    def _test_extract_template_definition(
            self,
            mock_driver,
            mock_objects_cluster_template_get_by_uuid,
            mock_get,
            missing_attr=None):
        if missing_attr in self.cluster_template_dict:
            self.cluster_template_dict[missing_attr] = None
        elif missing_attr in self.cluster_dict:
            self.cluster_dict[missing_attr] = None
        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        expected_result = str('{"action":"get","node":{"key":"test","value":'
                              '"1","modifiedIndex":10,"createdIndex":10}}')
        mock_resp = mock.MagicMock()
        mock_resp.text = expected_result
        mock_get.return_value = mock_resp
        cluster = objects.Cluster(self.context, **self.cluster_dict)
        mock_driver.return_value = k8s_dr.Driver()

        (template_path,
         definition,
         env_files) = mock_driver()._extract_template_definition(self.context,
                                                                 cluster)

        mapping = {
            'dns_nameserver': 'dns_nameserver',
            'image_id': 'server_image',
            'flavor_id': 'minion_flavor',
            'docker_volume_size': 'docker_volume_size',
            'docker_storage_driver': 'docker_storage_driver',
            'network_driver': 'network_driver',
            'volume_driver': 'volume_driver',
            'master_flavor_id': 'master_flavor',
            'apiserver_port': '',
            'node_count': 'number_of_minions',
            'master_count': 'number_of_masters',
            'discovery_url': 'discovery_url',
            'labels': {'flannel_network_cidr': '10.101.0.0/16',
                       'flannel_network_subnetlen': '26',
                       'flannel_backend': 'vxlan'},
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'no_proxy': 'no_proxy',
            'cluster_uuid': self.cluster_dict['uuid'],
            'magnum_url': self.mock_osc.magnum_url.return_value,
            'tls_disabled': False,
            'insecure_registry': '10.0.0.1:5000',
        }
        expected = {
            'ssh_key_name': 'keypair_id',
            'external_network': 'external_network_id',
            'network_driver': 'network_driver',
            'volume_driver': 'volume_driver',
            'dns_nameserver': 'dns_nameserver',
            'server_image': 'image_id',
            'minion_flavor': 'flavor_id',
            'master_flavor': 'master_flavor_id',
            'number_of_minions': 1,
            'number_of_masters': 1,
            'docker_volume_size': 20,
            'docker_storage_driver': 'devicemapper',
            'discovery_url': 'https://discovery.etcd.io/test',
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'flannel_backend': 'vxlan',
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'no_proxy': 'no_proxy',
            'tenant_name': 'fake_tenant',
            'username': '******',
            'cluster_uuid': self.cluster_dict['uuid'],
            'magnum_url': self.mock_osc.magnum_url.return_value,
            'region_name': self.mock_osc.cinder_region_name.return_value,
            'tls_disabled': False,
            'registry_enabled': False,
            'trustee_domain_id': self.mock_keystone.trustee_domain_id,
            'trustee_username': '******',
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trust_id': 'bd11efc5-d4e2-4dac-bbce-25e348ddf7de',
            'auth_url': 'http://192.168.10.10:5000/v3',
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
        }
        if missing_attr is not None:
            expected.pop(mapping[missing_attr], None)

        self.assertEqual(expected, definition)
        self.assertEqual(
            ['../../common/templates/environments/with_volume.yaml',
             '../../common/templates/environments/no_master_lb.yaml',
             '../../common/templates/environments/disable_floating_ip.yaml'],
            env_files)
Пример #22
0
    def test_extract_template_definition_without_discovery_url(
            self,
            mock_driver,
            mock_objects_cluster_template_get_by_uuid,
            reqget):
        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        cluster_dict = self.cluster_dict
        cluster_dict['discovery_url'] = None
        cluster = objects.Cluster(self.context, **cluster_dict)
        mock_driver.return_value = k8s_dr.Driver()

        CONF.set_override('etcd_discovery_service_endpoint_format',
                          'http://etcd/test?size=%(size)d',
                          group='cluster')
        mock_req = mock.MagicMock(text='https://address/token')
        reqget.return_value = mock_req

        (template_path,
         definition,
         env_files) = mock_driver()._extract_template_definition(self.context,
                                                                 cluster)

        expected = {
            'ssh_key_name': 'keypair_id',
            'external_network': 'external_network_id',
            'dns_nameserver': 'dns_nameserver',
            'server_image': 'image_id',
            'master_flavor': 'master_flavor_id',
            'minion_flavor': 'flavor_id',
            'number_of_minions': 1,
            'number_of_masters': 1,
            'network_driver': 'network_driver',
            'volume_driver': 'volume_driver',
            'docker_volume_size': 20,
            'docker_storage_driver': 'devicemapper',
            'discovery_url': 'https://address/token',
            'http_proxy': 'http_proxy',
            'https_proxy': 'https_proxy',
            'no_proxy': 'no_proxy',
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'flannel_backend': 'vxlan',
            'tenant_name': 'fake_tenant',
            'username': '******',
            'cluster_uuid': self.cluster_dict['uuid'],
            'magnum_url': self.mock_osc.magnum_url.return_value,
            'region_name': self.mock_osc.cinder_region_name.return_value,
            'tls_disabled': False,
            'registry_enabled': False,
            'trustee_domain_id': self.mock_keystone.trustee_domain_id,
            'trustee_username': '******',
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trust_id': 'bd11efc5-d4e2-4dac-bbce-25e348ddf7de',
            'auth_url': 'http://192.168.10.10:5000/v3',
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
        }
        self.assertEqual(expected, definition)
        self.assertEqual(
            ['../../common/templates/environments/with_volume.yaml',
             '../../common/templates/environments/no_master_lb.yaml',
             '../../common/templates/environments/disable_floating_ip.yaml'],
            env_files)
        reqget.assert_called_once_with('http://etcd/test?size=1')
Пример #23
0
    def test_extract_template_definition_only_required(
            self,
            mock_driver,
            mock_objects_cluster_template_get_by_uuid,
            mock_get):

        not_required = ['image_id', 'flavor_id', 'dns_nameserver',
                        'docker_volume_size', 'fixed_network', 'http_proxy',
                        'https_proxy', 'no_proxy', 'network_driver',
                        'master_flavor_id', 'docker_storage_driver',
                        'volume_driver']
        for key in not_required:
            self.cluster_template_dict[key] = None
        self.cluster_dict['discovery_url'] = 'https://discovery.etcd.io/test'

        cluster_template = objects.ClusterTemplate(
            self.context, **self.cluster_template_dict)
        mock_objects_cluster_template_get_by_uuid.return_value = \
            cluster_template
        expected_result = str('{"action":"get","node":{"key":"test","value":'
                              '"1","modifiedIndex":10,"createdIndex":10}}')
        mock_resp = mock.MagicMock()
        mock_resp.text = expected_result
        mock_get.return_value = mock_resp
        mock_driver.return_value = k8s_dr.Driver()
        cluster = objects.Cluster(self.context, **self.cluster_dict)

        (template_path,
         definition,
         env_files) = mock_driver()._extract_template_definition(self.context,
                                                                 cluster)

        expected = {
            'auth_url': 'http://192.168.10.10:5000/v3',
            'cluster_uuid': '5d12f6fd-a196-4bf0-ae4c-1f639a523a52',
            'discovery_url': 'https://discovery.etcd.io/test',
            'external_network': 'external_network_id',
            'flannel_backend': 'vxlan',
            'flannel_network_cidr': '10.101.0.0/16',
            'flannel_network_subnetlen': '26',
            'insecure_registry_url': '10.0.0.1:5000',
            'kube_version': 'fake-version',
            'magnum_url': 'http://127.0.0.1:9511/v1',
            'number_of_masters': 1,
            'number_of_minions': 1,
            'region_name': 'RegionOne',
            'registry_enabled': False,
            'ssh_key_name': 'keypair_id',
            'tenant_name': 'fake_tenant',
            'tls_disabled': False,
            'trust_id': 'bd11efc5-d4e2-4dac-bbce-25e348ddf7de',
            'trustee_domain_id': 'trustee_domain_id',
            'trustee_password': '******',
            'trustee_user_id': '7b489f04-b458-4541-8179-6a48a553e656',
            'trustee_username': '******',
            'username': '******'
        }
        self.assertEqual(expected, definition)
        self.assertEqual(
            ['../../common/templates/environments/no_volume.yaml',
             '../../common/templates/environments/no_master_lb.yaml',
             '../../common/templates/environments/disable_floating_ip.yaml'],
            env_files)
 def get_definition(self):
     return k8sa_dr.Driver().get_template_definition()
Пример #25
0
    def test_create_with_environment(self,
                                     mock_short_id,
                                     mock_openstack_client_class,
                                     mock_driver,
                                     mock_extract_tmpl_def,
                                     mock_cert_manager,
                                     mock_trust_manager,
                                     mock_get_template_contents,
                                     mock_process_mult,
                                     mock_heat_poller_class):
        timeout = 15
        self.cluster.cluster_template_id = self.cluster_template.uuid
        cluster_name = self.cluster.name
        mock_poller = mock.MagicMock()
        mock_poller.poll_and_check.return_value = loopingcall.LoopingCallDone()
        mock_heat_poller_class.return_value = mock_poller
        mock_driver.return_value = k8s_atomic_dr.Driver()
        mock_short_id.return_value = 'short_id'

        mock_extract_tmpl_def.return_value = (
            'the/template/path.yaml',
            {'heat_param_1': 'foo', 'heat_param_2': 'bar'},
            ['env_file_1', 'env_file_2'])

        mock_get_template_contents.return_value = (
            {'tmpl_file_1': 'some content',
             'tmpl_file_2': 'some more content'},
            'some template yaml')

        def do_mock_process_mult(env_paths=None, env_list_tracker=None):
            self.assertEqual(env_list_tracker, [])
            for f in env_paths:
                env_list_tracker.append('file:///' + f)
            env_map = {path: 'content of ' + path for path in env_list_tracker}
            return (env_map, None)

        mock_process_mult.side_effect = do_mock_process_mult

        mock_hc = mock.Mock()
        mock_hc.stacks.create.return_value = {'stack': {'id': 'stack-id'}}

        osc = mock.Mock()
        osc.heat.return_value = mock_hc
        mock_openstack_client_class.return_value = osc

        self.handler.cluster_create(self.context, self.cluster, timeout)

        mock_extract_tmpl_def.assert_called_once_with(self.context,
                                                      self.cluster)
        mock_get_template_contents.assert_called_once_with(
            'the/template/path.yaml')
        mock_process_mult.assert_called_once_with(
            env_paths=['the/template/env_file_1', 'the/template/env_file_2'],
            env_list_tracker=mock.ANY)
        mock_hc.stacks.create.assert_called_once_with(
            environment_files=['file:///the/template/env_file_1',
                               'file:///the/template/env_file_2'],
            files={
                'tmpl_file_1': 'some content',
                'tmpl_file_2': 'some more content',
                'file:///the/template/env_file_1':
                    'content of file:///the/template/env_file_1',
                'file:///the/template/env_file_2':
                    'content of file:///the/template/env_file_2'
            },
            parameters={'heat_param_1': 'foo', 'heat_param_2': 'bar'},
            stack_name=('%s-short_id' % cluster_name),
            template='some template yaml',
            timeout_mins=timeout)