def test_add_label_to_node(cl, client, has_conf):
    fake_node_name = "fake_node.com"

    has_conf.return_value = False
    v1 = MagicMock()

    condition = k8sClient.V1NodeCondition(type="Ready", status="True")
    status = k8sClient.V1NodeStatus(conditions=[condition])
    spec = k8sClient.V1NodeSpec(unschedulable=False)
    metadata = k8sClient.V1ObjectMeta(name=fake_node_name,
                                      labels={"label1": "True"})
    node = k8sClient.V1Node(status=status, spec=spec, metadata=metadata)
    response = k8sClient.V1NodeList(items=[node])

    v1.list_node_with_http_info.return_value = response
    v1.patch_node.return_value = node
    client.CoreV1Api.return_value = v1

    label_selector = 'label_default=true'

    add_label_to_node(label_selector=label_selector,
                      label_name="label1",
                      label_value="value1")

    v1.list_node_with_http_info.assert_called_with(
        label_selector=label_selector,
        _preload_content=True,
        _return_http_data_only=True)
    v1.patch_node.assert_called_with(
        fake_node_name, {'metadata': {
            'labels': {
                'label1': "value1"
            }
        }})
示例#2
0
def set_condition(node, type, status, reason, message):
    """set condition of condition_name for the node"""
    v1 = client.CoreV1Api()
    node_name = node.metadata.name
    
    original_condition = None
    original_conditions = [condition for condition in node.status.conditions if condition.type == type]
    if original_conditions:
        assert len(original_conditions) == 1
        original_condition, = original_conditions

    now = datetime.now(tz=tzutc())
    last_transition_time = now
    if original_condition and original_condition.status == status:
        last_transition_time = original_condition.last_transition_time

    condition = client.V1NodeCondition(
        type=type,
        status=status,
        reason=reason,
        message=message,
        last_heartbeat_time=now,
        last_transition_time=last_transition_time,
    )

    patch = {'status': {'conditions': [condition]}}
    rtn = v1.patch_node_status(node_name, patch)
    return rtn
示例#3
0
def fake_node_false():
    return client.V1Node(
        api_version='v1',
        kind='Node',
        metadata=client.V1ObjectMeta(name='curry-node-test',
                                     labels={'name': 'curry-node-test'}),
        status=client.V1NodeStatus(
            conditions=[client.V1NodeCondition(status='False', type='Ready')]))
def create_node_object(name: str = "default",
                       labels: {} = None) -> k8sClient.V1Node:
    condition = k8sClient.V1NodeCondition(type="Ready", status="True")
    status = k8sClient.V1NodeStatus(conditions=[condition])
    spec = k8sClient.V1NodeSpec(unschedulable=False)
    metadata = k8sClient.V1ObjectMeta(name=name, labels=labels)
    node = k8sClient.V1Node(status=status, spec=spec, metadata=metadata)
    return node
示例#5
0
def test_can_select_nodes_by_label(cl, client, has_conf):
    has_conf.return_value = False
    v1 = MagicMock()


    condition = k8sClient.V1NodeCondition(type="Ready", status="True")
    status = k8sClient.V1NodeStatus(conditions=[condition])

    node = k8sClient.V1Node(status=status)

    response = k8sClient.V1NodeList(items=[node])
    v1.list_node.return_value = response
    client.CoreV1Api.return_value = v1

    label_selector = 'beta.kubernetes.io/instance-type=m5.large'
    resp = all_nodes_are_ok(label_selector=label_selector)
    v1.list_node.assert_called_with(
        label_selector=label_selector, _preload_content=False)
    assert resp is True
def test_taint_nodes_by_label(gks, cl, client, has_conf):
    fake_node_name = "fake_node.com"
    gks.return_value = {
        'url': 'fake_url.com',
        'token': 'fake_token_towhatever',
        'SLACK_CHANNEL': 'chaos_fanout',
        'SLACK_TOKEN': 'sometoken'
    }
    has_conf.return_value = False
    v1 = MagicMock()

    condition = k8sClient.V1NodeCondition(type="Ready", status="True")
    status = k8sClient.V1NodeStatus(conditions=[condition])
    spec = k8sClient.V1NodeSpec(unschedulable=False)
    metadata = k8sClient.V1ObjectMeta(name=fake_node_name,
                                      labels={"label1": "True"})
    node = k8sClient.V1Node(status=status, spec=spec, metadata=metadata)

    response = k8sClient.V1NodeList(items=[node])

    v1.list_node_with_http_info.return_value = response
    v1.patch_node.return_value = node
    client.CoreV1Api.return_value = v1
    client.V1Taint.return_value = k8sClient.V1Taint(key="",
                                                    value="",
                                                    effect="")

    label_selector = 'label_default=true, label1=True'

    taint_nodes_by_label(label_selector=label_selector,
                         key="key1",
                         value="Apps",
                         effect="NoExec")
    assert v1.patch_node.call_count == 1
    args = v1.patch_node.call_args[0]
    assert args[0] == fake_node_name
    assert args[1]['spec']['taints'][0].key == "key1"
    assert args[1]['spec']['taints'][0].effect == "NoExec"
    assert args[1]['spec']['taints'][0].value == "Apps"
def test_remove_label_from_node(gks, cl, client, has_conf):
    fake_node_name = "fake_node.com"
    gks.return_value = {
        'url': 'fake_url.com',
        'token': 'fake_token_towhatever',
        'SLACK_CHANNEL': 'chaos_fanout',
        'SLACK_TOKEN': 'sometoken'
    }
    has_conf.return_value = False
    v1 = MagicMock()

    condition = k8sClient.V1NodeCondition(type="Ready", status="True")
    status = k8sClient.V1NodeStatus(conditions=[condition])
    spec = k8sClient.V1NodeSpec(unschedulable=False)
    metadata = k8sClient.V1ObjectMeta(name=fake_node_name,
                                      labels={"label1": "True"})
    node = k8sClient.V1Node(status=status, spec=spec, metadata=metadata)
    response = k8sClient.V1NodeList(items=[node])

    v1.list_node_with_http_info.return_value = response
    v1.patch_node.return_value = node
    client.CoreV1Api.return_value = v1
    client.V1NodeList.return_value = k8sClient.V1NodeList(items=[])

    label_selector = 'label_default=true, label1=True'

    remove_label_from_node(label_selector, "label1")

    v1.list_node_with_http_info.assert_called_with(
        label_selector=label_selector,
        _preload_content=True,
        _return_http_data_only=True)
    v1.patch_node.assert_called_with(
        fake_node_name, {'metadata': {
            'labels': {
                'label1': None
            }
        }})