Ejemplo n.º 1
0
    def test_cluster_host_config(self):

        expected_error = "'noderole' list must have two values [ node, role ]"
        try:
            K8sClusterHostConfig.create_from_list(noderole=[1, 2, 3])
        except AssertionError as e:
            self.assertEquals(
                e.args[0],
                expected_error,
            )

        conf = K8sClusterHostConfig.create_from_list(
            noderole=["/api/v2/worker/k8shost/1", "master"])
        self.assertIsInstance(conf, K8sClusterHostConfig)
Ejemplo n.º 2
0
    def test_create_with_APIException(self, mock_post):

        with self.assertRaises(APIException):
            get_client().k8s_cluster.create(
                name="a",
                k8shosts_config=[
                    K8sClusterHostConfig("/api/v2/worker/k8shost/1", "master")
                ],
            )
Ejemplo n.º 3
0
    def create(
        self,
        name,
        k8shosts_config,
        description=None,
        k8s_version=None,
        pod_network_range="10.192.0.0/12",
        service_network_range="10.96.0.0/12",
        pod_dns_domain="cluster.local",
        persistent_storage_local=False,
        persistent_storage_nimble_csi=False,
    ):
        """Create a K8s Cluster

        :param name: the cluster name
        :param k8shosts_config: k8s host ids and roles 'id1:master|worker,id2:master|worker,...'
        :param description: the cluster descripton
        :param k8s_version: e.g. 1.17.0
        :param pod_network_range: the pod network range, default='10.192.0.0/12'
        :param service_network_range: the service network range, default='10.96.0.0/12'
        :param pod_dns_domain: the pod dns domain, default='cluster.local'
        :param persistent_storage_local: True/False
        :param persistent_storage_nimble_csi: True/False
        """

        host_config = [
            K8sClusterHostConfig.create_from_list(h.split(":"))
            for h in k8shosts_config.split(",")
        ]

        print(
            get_client().k8s_cluster.create(
                name=name,
                description=description,
                k8s_version=k8s_version,
                pod_network_range=pod_network_range,
                service_network_range=service_network_range,
                pod_dns_domain=pod_dns_domain,
                persistent_storage_local=persistent_storage_local,
                persistent_storage_nimble_csi=persistent_storage_nimble_csi,
                k8shosts_config=host_config,
            )
        )
    def create(
        self,
        name,
        k8shosts_config,
        description=None,
        k8s_version=None,
        pod_network_range="10.192.0.0/12",
        service_network_range="10.96.0.0/12",
        pod_dns_domain="cluster.local",
        persistent_storage_local=False,
        persistent_storage_nimble_csi=False,
        addons=[],
        external_identity_server={},
        external_groups=[],
    ):
        """Create a K8s Cluster.

        :param name: the cluster name
        :param k8shosts_config: k8s host ids and roles 'id1:master|worker,id2:
            master|worker,...'
        :param description: the cluster descripton
        :param k8s_version: e.g. 1.17.0
        :param pod_network_range: the pod network range,
            default='10.192.0.0/12'
        :param service_network_range: the service network range,
            default='10.96.0.0/12'
        :param pod_dns_domain: the pod dns domain, default='cluster.local'
        :param persistent_storage_local: True/False
        :param persistent_storage_nimble_csi: True/False
        :param addons: list of required addons. See:
            `hpecp k8scluster get-available-addons`
        :param external_identity_server: dict
           Example {
                "bind_pwd":"password",
                "user_attribute":"CN",
                "bind_type":"search_bind",
                "bind_dn":"cn=Administrator,CN=Users,DC=samdom,DC=example,DC=com",
                "host":"10.1.0.15",
                "group_attribute":"member",
                "security_protocol":"ldaps",
                "base_dn":"CN=Users,DC=samdom,DC=example,DC=com",
                "verify_peer":false,
                "type":"Active Directory",
                "port":636}
        """
        host_config = [
            K8sClusterHostConfig.create_from_list(h.split(":"))
            for h in k8shosts_config.split(",")
        ]

        print(base.get_client().k8s_cluster.create(
            name=name,
            description=description,
            k8s_version=k8s_version,
            pod_network_range=pod_network_range,
            service_network_range=service_network_range,
            pod_dns_domain=pod_dns_domain,
            persistent_storage_local=persistent_storage_local,
            persistent_storage_nimble_csi=persistent_storage_nimble_csi,
            k8shosts_config=host_config,
            addons=addons,
            external_identity_server=external_identity_server,
            external_groups=external_groups,
        ))
Ejemplo n.º 5
0
    def test_create(self, mock_post):

        with self.assertRaisesRegexp(
                AssertionError,
                "'name' must be provided and must be a string"):
            get_client().k8s_cluster.create()

        with self.assertRaisesRegexp(
                AssertionError, "'description' if provided, must be a string"):
            get_client().k8s_cluster.create(name="a", description=1)

        with self.assertRaisesRegexp(
                AssertionError, "'k8s_version' if provided, must be a string"):
            get_client().k8s_cluster.create(name="a", k8s_version=1)

        with self.assertRaisesRegexp(AssertionError,
                                     "'pod_network_range' must be a string"):
            get_client().k8s_cluster.create(name="a", pod_network_range=1)

        with self.assertRaisesRegexp(
                AssertionError, "'service_network_range' must be a string"):
            get_client().k8s_cluster.create(name="a", service_network_range=1)

        with self.assertRaisesRegexp(AssertionError,
                                     "'pod_dns_domain' must be a string"):
            get_client().k8s_cluster.create(name="a", pod_dns_domain=1)

        with self.assertRaisesRegexp(
                AssertionError,
                "'persistent_storage_local' must be True or False"):
            get_client().k8s_cluster.create(name="a",
                                            persistent_storage_local=1)

        with self.assertRaisesRegexp(
                AssertionError,
                "'persistent_storage_nimble_csi' must be True or False",
        ):
            get_client().k8s_cluster.create(name="a",
                                            persistent_storage_nimble_csi=1)

        with self.assertRaisesRegexp(AssertionError,
                                     "'k8shosts_config' must be a list"):
            get_client().k8s_cluster.create(name="a", k8shosts_config=1)

        with self.assertRaisesRegexp(
                AssertionError,
                "'k8shosts_config' must have at least one item"):
            get_client().k8s_cluster.create(name="a", k8shosts_config=[])

        with self.assertRaisesRegexp(
                AssertionError,
                "'k8shosts_config' item '0' is not of type K8sClusterHostConfig",
        ):
            get_client().k8s_cluster.create(name="a", k8shosts_config=[1, 2])

        # pylint: disable=anomalous-backslash-in-string
        with self.assertRaisesRegexp(
                AssertionError,
            (
                "'node' must have format"
                " '\/api\/v2\/worker\/k8shost\/\[0-9\]\+'"  # noqa: W605
            ),
        ):
            get_client().k8s_cluster.create(
                name="a", k8shosts_config=[K8sClusterHostConfig("a", "b")])

        with self.assertRaisesRegexp(
                AssertionError,
                "'role' must one of \['master, worker'\]",  # noqa: W605
        ):
            get_client().k8s_cluster.create(
                name="a",
                k8shosts_config=[
                    K8sClusterHostConfig("/api/v2/worker/k8shost/1", "b")
                ],
            )

        # Finally we can create a cluster
        id = get_client().k8s_cluster.create(
            name="a",
            k8shosts_config=[
                K8sClusterHostConfig("/api/v2/worker/k8shost/1", "master")
            ],
        )
        self.assertEqual(id, "/api/v2/k8scluster/99")

        # now with a description
        id = get_client().k8s_cluster.create(
            name="a",
            k8shosts_config=[
                K8sClusterHostConfig("/api/v2/worker/k8shost/1", "master")
            ],
            description="Cluster Description",
        )
        self.assertEqual(id, "/api/v2/k8scluster/99")

        # now with a k8s version
        id = get_client().k8s_cluster.create(
            name="a",
            k8shosts_config=[
                K8sClusterHostConfig("/api/v2/worker/k8shost/1", "master")
            ],
            k8s_version="1.18.0",
        )
        self.assertEqual(id, "/api/v2/k8scluster/99")

        # now with a k8s version
        id = get_client().k8s_cluster.create(
            name="a",
            k8shosts_config=[
                K8sClusterHostConfig("/api/v2/worker/k8shost/1", "master")
            ],
            k8s_version="1.18.0",
            addons=["picasso"],
        )
        self.assertEqual(id, "/api/v2/k8scluster/99")
Ejemplo n.º 6
0
import textwrap

client = ContainerPlatformClient(username='******',
                                 password='******',
                                 api_host='127.0.0.1',
                                 api_port=8080,
                                 use_ssl=True,
                                 verify_ssl='/certs/hpecp-ca-cert.pem')

client.create_session()

print(client.k8s_worker.get_k8shosts().tabulate())

try:
    k8shosts_config = [
        K8sClusterHostConfig(4, 'worker'),
        K8sClusterHostConfig(5, 'master')
    ]
    k8s_cluster_id = client.k8s_cluster.create(name='def',
                                               description='my cluster',
                                               k8s_version='1.17.0',
                                               k8shosts_config=k8shosts_config)
    print('creating cluster id: ' + k8s_cluster_id)
except APIException as e:
    text = """APIException(
        Backend API Response -> {}
        HTTP Method -> {}
        Request URL -> {}
        Request Data -> [{}]
    )"""
    print(
    def create(
        self,
        name,
        k8shosts_config,
        description=None,
        k8s_version=None,
        pod_network_range="10.192.0.0/12",
        service_network_range="10.96.0.0/12",
        pod_dns_domain="cluster.local",
        persistent_storage_local=False,
        persistent_storage_nimble_csi=False,
        addons=[],
        external_identity_server={},
        ext_id_svr_bind_pwd=None,
        ext_id_svr_user_attribute=None,
        ext_id_svr_bind_type=None,
        ext_id_svr_bind_dn=None,
        ext_id_svr_host=None,
        ext_id_svr_group_attribute=None,
        ext_id_svr_security_protocol=None,
        ext_id_svr_base_dn=None,
        ext_id_svr_verify_peer=None,
        ext_id_svr_type=None,
        ext_id_svr_port=None,
        external_groups=[],
        datafabric=False,
        datafabric_name=None,
    ):
        """Create a K8s Cluster.

        :param name: the cluster name
        :param k8shosts_config: k8s host ids and roles 'id1:master|worker,id2:
            master|worker,...'
        :param description: the cluster descripton
        :param k8s_version: e.g. 1.17.0
        :param pod_network_range: the pod network range,
            default='10.192.0.0/12'
        :param service_network_range: the service network range,
            default='10.96.0.0/12'
        :param pod_dns_domain: the pod dns domain, default='cluster.local'
        :param persistent_storage_local: True/False
        :param persistent_storage_nimble_csi: True/False
        :param addons: list of required addons. See:
            `hpecp k8scluster get-available-addons`
        :param external_identity_server: dict (deprecated)
           Example '{"bind_pwd":"password",
                "user_attribute":"CN",
                "bind_type":"search_bind",
                "bind_dn":"cn=Administrator,CN=Users,DC=samdom,DC=example,DC=com",
                "host":"10.1.0.15",
                "group_attribute":"member",
                "security_protocol":"ldaps",
                "base_dn":"CN=Users,DC=samdom,DC=example,DC=com",
                "verify_peer":false,
                "type":"Active Directory",
                "port":636}'
        :param ext_id_svr_bind_pwd str
        :param ext_id_svr_user_attribute str
        :param ext_id_svr_bind_type str
        :param ext_id_svr_bind_dn str
        :param ext_id_svr_host str
        :param ext_id_svr_group_attribute str
        :param ext_id_svr_security_protocol str
        :param ext_id_svr_base_dn str
        :param ext_id_svr_verify_peer bool
        :param ext_id_svr_type str
        :param ext_id_svr_port int
        """
        host_config = [
            K8sClusterHostConfig.create_from_list(h.split(":"))
            for h in k8shosts_config.split(",")
        ]

        if external_identity_server:
            if not isinstance(external_identity_server, dict):
                print(
                    ("Could not parse 'external_identity_server' parameter"
                     " - is it valid json?\n"
                     "Received: " + external_identity_server + "\n"),
                    file=sys.stderr,
                )
                sys.exit(1)
        else:
            external_identity_server = {}

        if ext_id_svr_bind_pwd is not None:
            external_identity_server["bind_pwd"] = ext_id_svr_bind_pwd

        if ext_id_svr_user_attribute is not None:
            external_identity_server[
                "user_attribute"] = ext_id_svr_user_attribute

        if ext_id_svr_bind_type is not None:
            external_identity_server["bind_type"] = ext_id_svr_bind_type

        if ext_id_svr_bind_dn is not None:
            external_identity_server["bind_dn"] = ext_id_svr_bind_dn

        if ext_id_svr_host is not None:
            external_identity_server["host"] = ext_id_svr_host

        if ext_id_svr_group_attribute is not None:
            external_identity_server[
                "group_attribute"] = ext_id_svr_group_attribute

        if ext_id_svr_security_protocol is not None:
            external_identity_server[
                "security_protocol"] = ext_id_svr_security_protocol

        if ext_id_svr_base_dn is not None:
            external_identity_server["base_dn"] = ext_id_svr_base_dn

        if ext_id_svr_verify_peer is not None:
            external_identity_server["verify_peer"] = json.loads(
                ext_id_svr_verify_peer.lower())

        if ext_id_svr_type is not None:
            external_identity_server["type"] = ext_id_svr_type

        if ext_id_svr_port is not None:
            external_identity_server["port"] = int(ext_id_svr_port)

        print(base.get_client().k8s_cluster.create(
            name=name,
            description=description,
            k8s_version=k8s_version,
            pod_network_range=pod_network_range,
            service_network_range=service_network_range,
            pod_dns_domain=pod_dns_domain,
            persistent_storage_local=persistent_storage_local,
            persistent_storage_nimble_csi=persistent_storage_nimble_csi,
            k8shosts_config=host_config,
            addons=addons,
            external_identity_server=external_identity_server,
            external_groups=external_groups,
            datafabric=datafabric,
            datafabric_name=datafabric_name,
        ))