Esempio n. 1
0
def test_is_namespace_excluded(monkeypatch):
    c_is_excluded = mock.Mock(return_value=False)
    monkeypatch.setattr('datadog_checks.kubelet.common.c_is_excluded',
                        c_is_excluded)

    pods = json.loads(mock_from_file('pods.json'))
    pod_list_utils = PodListUtils(pods)

    # Test namespace=None
    c_is_excluded.reset_mock()
    assert pod_list_utils.is_namespace_excluded(None) is False
    c_is_excluded.assert_not_called()

    # Test excluded namespace
    c_is_excluded.reset_mock()
    c_is_excluded.return_value = True
    assert pod_list_utils.is_namespace_excluded(
        'an_excluded_namespace') is True
    c_is_excluded.assert_called_once()
    c_is_excluded.assert_called_with('', '', 'an_excluded_namespace')

    # Test non-excluded namespace
    c_is_excluded.reset_mock()
    c_is_excluded.return_value = False
    assert pod_list_utils.is_namespace_excluded(
        'a_non_excluded_namespace') is False
    c_is_excluded.assert_called_once()
    c_is_excluded.assert_called_with('', '', 'a_non_excluded_namespace')
Esempio n. 2
0
def test_process_stats_summary_not_source_linux(monkeypatch, aggregator,
                                                tagger):
    check = KubeletCheck('kubelet', {}, [{}])
    pod_list_utils = PodListUtils(json.loads(mock_from_file('pods.json')))
    stats = json.loads(mock_from_file('stats_summary.json'))

    tagger.reset()
    tagger.set_tags(COMMON_TAGS)

    tags = ["instance:tag"]
    check.process_stats_summary(pod_list_utils, stats, tags, False)

    # As we did not activate `use_stats_summary_as_source`,
    # we only have ephemeral storage metrics and kubelet stats
    aggregator.assert_metric('kubernetes.ephemeral_storage.usage', 69406720.0,
                             ['instance:tag', 'pod_name:dd-agent-ntepl'])
    aggregator.assert_metric(
        'kubernetes.ephemeral_storage.usage', 49152.0,
        ['instance:tag', 'pod_name:demo-app-success-c485bc67b-klj45'])
    aggregator.assert_metric('kubernetes.runtime.cpu.usage', 19442853.0,
                             ['instance:tag'])
    aggregator.assert_metric('kubernetes.kubelet.cpu.usage', 36755862.0,
                             ['instance:tag'])
    aggregator.assert_metric('kubernetes.runtime.memory.rss', 101273600.0,
                             ['instance:tag'])
    aggregator.assert_metric('kubernetes.kubelet.memory.rss', 88477696.0,
                             ['instance:tag'])
Esempio n. 3
0
def test_filter_staticpods(monkeypatch):
    is_excluded = mock.Mock(return_value=True)
    monkeypatch.setattr('datadog_checks.kubelet.common.is_excluded', is_excluded)

    pods = json.loads(mock_from_file('pods.json'))
    pod_list_utils = PodListUtils(pods)

    # kube-proxy-gke-haissam-default-pool-be5066f1-wnvn is static
    assert pod_list_utils.is_excluded("cid", "260c2b1d43b094af6d6b4ccba082c2db") is False
    is_excluded.assert_not_called()

    # fluentd-gcp-v2.0.10-9q9t4 is not static
    assert pod_list_utils.is_excluded("docker://5741ed2471c0e458b6b95db40ba05d1a5ee168256638a0264f08703e48d76561",
                                      "2edfd4d9-10ce-11e8-bd5a-42010af00137") is True
Esempio n. 4
0
def test_kubelet_check_disable_summary_rates(monkeypatch, aggregator):
    check = KubeletCheck('kubelet', {}, [{
        'enabled_rates': ['*unsupported_regex*']
    }])
    pod_list_utils = PodListUtils(
        json.loads(mock_from_file('pods_windows.json')))
    stats = json.loads(mock_from_file('stats_summary_windows.json'))

    check.process_stats_summary(pod_list_utils, stats, [],
                                True)  # windows/non-cadvisor case

    assert len(aggregator.metrics(
        'kubernetes.network.tx_bytes')) == 0  # rate disabled
    assert len(aggregator.metrics(
        'kubernetes.filesystem.usage_pct')) > 0  # gauge enabled
Esempio n. 5
0
def test_container_filter(monkeypatch):
    c_is_excluded = mock.Mock(return_value=False)
    monkeypatch.setattr('datadog_checks.kubelet.common.c_is_excluded',
                        c_is_excluded)

    long_cid = "docker://a335589109ce5506aa69ba7481fc3e6c943abd23c5277016c92dac15d0f40479"
    ctr_name = "datadog-agent"
    ctr_image = "datadog/agent-dev:haissam-tagger-pod-entity"
    namespace = "default"

    pods = json.loads(mock_from_file('pods.json'))
    pod_list_utils = PodListUtils(pods)

    assert pod_list_utils is not None
    assert len(pod_list_utils.containers) == 10
    assert long_cid in pod_list_utils.containers
    c_is_excluded.assert_not_called()

    # Test cid == None
    c_is_excluded.reset_mock()
    assert pod_list_utils.is_excluded(None) is True
    c_is_excluded.assert_not_called()

    # Test cid == ""
    c_is_excluded.reset_mock()
    assert pod_list_utils.is_excluded("") is True
    c_is_excluded.assert_not_called()

    # Test non-existing container
    c_is_excluded.reset_mock()
    assert pod_list_utils.is_excluded("invalid") is True
    c_is_excluded.assert_not_called()

    # Test existing unfiltered container
    c_is_excluded.reset_mock()
    assert pod_list_utils.is_excluded(long_cid) is False
    c_is_excluded.assert_called_once()
    c_is_excluded.assert_called_with(ctr_name, ctr_image, namespace)

    # Clear exclusion cache
    pod_list_utils.cache = {}

    # Test existing filtered container
    c_is_excluded.reset_mock()
    c_is_excluded.return_value = True
    assert pod_list_utils.is_excluded(long_cid) is True
    c_is_excluded.assert_called_once()
    c_is_excluded.assert_called_with(ctr_name, ctr_image, namespace)
Esempio n. 6
0
def test_process_stats_summary_not_source_windows(monkeypatch, aggregator, tagger):
    check = KubeletCheck('kubelet', {}, [{}])
    pod_list_utils = PodListUtils(json.loads(mock_from_file('pods_windows.json')))
    stats = json.loads(mock_from_file('stats_summary_windows.json'))

    tagger.reset()
    tagger.set_tags(WINDOWS_TAGS)

    tags = ["instance:tag"]
    check.process_stats_summary(pod_list_utils, stats, tags, False)

    # As we did not activate `use_stats_summary_as_source`, we only have ephemeral storage metrics
    # Kubelet stats not present as they are not returned on Windows
    aggregator.assert_metric(
        'kubernetes.ephemeral_storage.usage', 919980.0, tags + ['kube_namespace:default', 'pod_name:dd-datadog-lbvkl']
    )
Esempio n. 7
0
def test_get_cid_by_labels():
    pods = json.loads(mock_from_file('podlist_containerd.json'))
    pod_list_utils = PodListUtils(pods)

    # k8s >= 1.16
    labels = {
        "container": "datadog-agent",
        "namespace": "default",
        "pod": "datadog-agent-pbqt2"
    }
    container_id = pod_list_utils.get_cid_by_labels(labels)
    assert container_id == "containerd://51cba2ca229069039575750d44ed3a67e9b5ead651312ba7ff218dd9202fde64"

    # k8s < 1.16
    labels = {
        "container_name": "datadog-agent",
        "namespace": "default",
        "pod_name": "datadog-agent-pbqt2"
    }
    container_id = pod_list_utils.get_cid_by_labels(labels)
    assert container_id == "containerd://51cba2ca229069039575750d44ed3a67e9b5ead651312ba7ff218dd9202fde64"

    assert pod_list_utils.get_cid_by_labels([]) is None
Esempio n. 8
0
def test_process_stats_summary_as_source(monkeypatch, aggregator, tagger):
    check = KubeletCheck('kubelet', {}, [{}])
    pod_list_utils = PodListUtils(
        json.loads(mock_from_file('pods_windows.json')))
    stats = json.loads(mock_from_file('stats_summary_windows.json'))

    tagger.reset()
    tagger.set_tags(WINDOWS_TAGS)

    tags = ["instance:tag"]
    check.process_stats_summary(pod_list_utils, stats, tags, True)

    aggregator.assert_metric(
        'kubernetes.ephemeral_storage.usage', 919980.0,
        tags + ['kube_namespace:default', 'pod_name:dd-datadog-lbvkl'])
    aggregator.assert_metric(
        'kubernetes.network.tx_bytes', 163670.0,
        tags + ['kube_namespace:default', 'pod_name:dd-datadog-lbvkl'])
    aggregator.assert_metric(
        'kubernetes.network.rx_bytes', 694636.0,
        tags + ['kube_namespace:default', 'pod_name:dd-datadog-lbvkl'])
    aggregator.assert_metric(
        'kubernetes.network.tx_bytes',
        258157.0,
        tags + [
            'kube_namespace:default',
            'pod_name:windows-server-iis-6c68545d57-gwtn9'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.network.rx_bytes',
        509185.0,
        tags + [
            'kube_namespace:default',
            'pod_name:windows-server-iis-6c68545d57-gwtn9'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.cpu.usage.total',
        13796875000.0,
        tags + [
            'kube_container_name:agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.cpu.usage.total',
        9359375000.0,
        tags + [
            'kube_container_name:process-agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.cpu.usage.total',
        70140625000.0,
        tags + [
            'kube_container_name:windows-server-iis',
            'kube_namespace:default',
            'pod_name:windows-server-iis-6c68545d57-gwtn9',
        ],
    )
    aggregator.assert_metric(
        'kubernetes.memory.working_set',
        136089600.0,
        tags + [
            'kube_container_name:agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.memory.working_set',
        65474560.0,
        tags + [
            'kube_container_name:process-agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.memory.working_set',
        136814592.0,
        tags + [
            'kube_container_name:windows-server-iis',
            'kube_namespace:default',
            'pod_name:windows-server-iis-6c68545d57-gwtn9',
        ],
    )
    aggregator.assert_metric(
        'kubernetes.filesystem.usage',
        0.0,
        tags + [
            'kube_container_name:agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.filesystem.usage',
        0.0,
        tags + [
            'kube_container_name:process-agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.filesystem.usage',
        0.0,
        tags + [
            'kube_container_name:windows-server-iis',
            'kube_namespace:default',
            'pod_name:windows-server-iis-6c68545d57-gwtn9',
        ],
    )
    aggregator.assert_metric(
        'kubernetes.filesystem.usage_pct',
        0.0,
        tags + [
            'kube_container_name:agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.filesystem.usage_pct',
        0.0,
        tags + [
            'kube_container_name:process-agent', 'kube_namespace:default',
            'pod_name:dd-datadog-lbvkl'
        ],
    )
    aggregator.assert_metric(
        'kubernetes.filesystem.usage_pct',
        0.0,
        tags + [
            'kube_container_name:windows-server-iis',
            'kube_namespace:default',
            'pod_name:windows-server-iis-6c68545d57-gwtn9',
        ],
    )