def test_k8s_get_compute_nodes(): fake_node_list = [{ "metadata": { "name": "fakenode1" }, "spec": { "unschedulable": True } }, { "metadata": { "name": "fakenode2" }, "spec": { "unschedulable": False } }, { "metadata": { "name": "fakenode3" }, "spec": {} }] with patch('intel.k8s.get_node_list', MagicMock(return_value=fake_node_list)): node_list = k8s.get_compute_nodes(None) resv_list = list(map(lambda node: node["metadata"]["name"], node_list)) assert resv_list == ["fakenode2", "fakenode3"]
def get_cmk_node_list(host_list, all_hosts): cmk_node_list = [] if host_list: cmk_node_list = [host.strip() for host in host_list.split(',')] if all_hosts: try: node_list_resp = k8s.get_compute_nodes(None) for node in node_list_resp: cmk_node_list.append(node["metadata"]["name"]) except K8sApiException as err: logging.error("Exception when getting the node list: {}" .format(err)) logging.error("Aborting cluster-init ...") sys.exit(1) return cmk_node_list
def __init__(self, num_nodes=1, timeout=60, config_file_location=None, host='https://localhost:443', api_cert_file='/etc/kubernetes/ssl/ca.pem', user_cert_file='/etc/kubernetes/ssl/admin.pem', user_key_file='/etc/kubernetes/ssl/admin-key.pem'): """ Driver constructor is reserving nodes for test(s) and creates namespace. # noqa: E501 :param num_nodes: Requested number of nodes from cluster(int; default=1) # noqa: E501 :param timeout: Timeout on waiting for resource arability in seconds(int; default=60) # noqa: E501 :param config_file_location: Path to kubernetes client configuration(string, default=None) # noqa: E501 :param host: (if config_file_location is not set) URL to API-server(string, default="https://localhost:443") # noqa: E501 :param api_cert_file: (if config_file_location is not set) Path to API server cert(string, default="/etc/kubernetes/ssl/ca.pem") # noqa: E501 :param user_cert_file: (if config_file_location is not set) Path to user cert(string, default="/etc/kubernetes/ssl/admin.pem") # noqa: E501 :param user_key_file: (if config_file_location is not set) Path to user key(string, default="/etc/kubernetes/ssl/admin-key.pem") # noqa: E501 """ self.num_nodes = num_nodes self.timeout = timeout if config_file_location is None: self.configuration = client.Configuration() self.configuration.host = host self.configuration.ssl_ca_cert = api_cert_file self.configuration.cert_file = user_cert_file self.configuration.key_file = user_key_file else: self.configuration = config.load_kube_config( config_file=config_file_location) # check if cluster is able to reserve requested number of nodes. cluster_size = len(k8s.get_compute_nodes(config=self.configuration)) if cluster_size < self.num_nodes: raise Exception("Cluster doesn't have requested number of nodes." "Requested {}. Available {}.".format( self.num_nodes, cluster_size)) # generate random namespace name. self.ns_name = "namespace-%s" % str(uuid.uuid4())[:5] # wait for nodes until timeout. timeout_destination = int(time.time()) + self.timeout while len(self._get_list_unassigned_nodes( self.configuration)) < self.num_nodes: if int(time.time()) >= timeout_destination: raise Exception("Timeout after {} ".format(self.timeout) + "seconds on waiting for resources avability") logging.debug("Waiting for resources...") time.sleep(1) # add the label to the node. unassigned_nodes = self._get_list_unassigned_nodes(self.configuration) self.nodes = unassigned_nodes[:self.num_nodes] for node in self.nodes: k8s.set_node_label(self.configuration, node, "namespace", self.ns_name) # create namespace k8s.create_namespace(self.configuration, self.ns_name) self.active = True
def _get_list_unassigned_nodes(self, config): compute_nodes = k8s.get_compute_nodes(config, label_selector='!namespace') return list( map(lambda node: node["spec"]["external_id"], compute_nodes))