def test_parse_metadata_missing_annotations_and_labels(
            self, apps_client, dummy_host):
        mocked_deployments = {
            "items": [
                {
                    "metadata": {
                        "name": "cluster-collector",
                        "namespace": "checkmk-monitoring",
                        "uid": "debc9fe4-9e45-4688-ad04-a95604fa1f30",
                        "resourceVersion": "207264",
                        "generation": 2,
                        "creationTimestamp": "2022-03-25T13:24:42Z",
                    },
                },
            ]
        }
        Entry.single_register(
            Entry.GET,
            f"{dummy_host}/apis/apps/v1/deployments",
            body=json.dumps(mocked_deployments),
            headers={"content-type": "application/json"},
        )
        with Mocketizer():
            deployment = list(
                apps_client.list_deployment_for_all_namespaces().items)[0]

        metadata = parse_metadata(deployment.metadata)
        assert metadata.name == "cluster-collector"
        assert metadata.namespace == "checkmk-monitoring"
        assert isinstance(metadata.creation_timestamp, float)
        assert metadata.labels == {}
        assert metadata.annotations == {}
    def test_parse_metadata(self):
        labels = {
            "beta.kubernetes.io/arch": "amd64",
            "beta.kubernetes.io/os": "linux",
            "kubernetes.io/arch": "amd64",
            "kubernetes.io/hostname": "k8",
            "kubernetes.io/os": "linux",
            "node-role.kubernetes.io/master": "",
        }

        node_raw_metadata = {
            "name":
            "k8",
            "creation_timestamp":
            datetime.datetime.strptime("2021-05-04T09:01:13Z",
                                       "%Y-%m-%dT%H:%M:%SZ"),
            "uid":
            "42c82288-5524-49cb-af75-065e73fedc88",
            "labels":
            labels,
        }
        metadata_obj = client.V1ObjectMeta(**node_raw_metadata)
        metadata = parse_metadata(metadata_obj)
        assert metadata.name == "k8"
        assert metadata.namespace is None
Esempio n. 3
0
    def test_parse_metadata_missing_annotations_and_labels(
            self, apps_client, dummy_host):
        statefulsets_metadata = {
            "items": [{
                "metadata": {
                    "name": "web",
                    "namespace": "default",
                    "uid": "29be93ae-eba8-4b2b-8eb9-2e76378b4e87",
                    "resourceVersion": "54122",
                    "generation": 1,
                    "creationTimestamp": "2022-03-09T07:44:17Z",
                },
            }]
        }

        Entry.single_register(
            Entry.GET,
            f"{dummy_host}/apis/apps/v1/statefulsets",
            body=json.dumps(statefulsets_metadata),
            headers={"content-type": "application/json"},
        )
        with Mocketizer():
            statefulset = list(
                apps_client.list_stateful_set_for_all_namespaces().items)[0]
        metadata = parse_metadata(statefulset.metadata)
        assert metadata.labels == {}
        assert metadata.annotations == {}
    def test_parse_metadata_missing_annotations_and_labels(
            self, apps_client, dummy_host):
        daemon_sets_metadata = {
            "items": [{
                "metadata": {
                    "name": "node-collector-container-metrics",
                    "namespace": "checkmk-monitoring",
                    "uid": "6f07cb60-26c7-41ce-afe0-48c97d15a07b",
                    "resourceVersion": "2967286",
                    "generation": 1,
                    "creationTimestamp": "2022-02-16T10:03:21Z",
                }
            }]
        }

        Entry.single_register(
            Entry.GET,
            f"{dummy_host}/apis/apps/v1/daemonsets",
            body=json.dumps(daemon_sets_metadata),
            headers={"content-type": "application/json"},
        )
        with Mocketizer():
            daemon_set = list(
                apps_client.list_daemon_set_for_all_namespaces().items)[0]
        metadata = parse_metadata(daemon_set.metadata)
        assert metadata.labels == {}
        assert metadata.annotations == {}
 def test_parse_metadata_datetime(self):
     now = datetime.datetime(2021, 10, 11, 13, 53, 10)
     node_raw_metadata = {
         "name": "unittest",
         "creation_timestamp": now,
         "uid": "f57f3e64-2a89-11ec-bb97-3f4358ab72b2",
     }
     metadata_obj = client.V1ObjectMeta(**node_raw_metadata)
     metadata = parse_metadata(metadata_obj)
     assert metadata.creation_timestamp == now.timestamp()
 def test_parse_metadata(self):
     node_raw_metadata = {
         "name": "cronjob",
         "namespace": "default",
         "creation_timestamp": datetime.datetime.strptime(
             "2021-05-04T09:01:13Z", "%Y-%m-%dT%H:%M:%SZ"
         ),
         "uid": "uid",
     }
     metadata_obj = client.V1ObjectMeta(**node_raw_metadata)
     metadata = parse_metadata(metadata_obj)
     assert metadata.name == "cronjob"
     assert metadata.namespace is not None
 def test_parse_metadata_missing_annotations_and_labels(self):
     node_raw_metadata = {
         "name":
         "k8",
         "creation_timestamp":
         datetime.datetime.strptime(
             "2021-05-04T09:01:13Z",
             "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc),
         "uid":
         "42c82288-5524-49cb-af75-065e73fedc88",
     }
     metadata_obj = client.V1ObjectMeta(**node_raw_metadata)
     metadata = parse_metadata(metadata_obj, model=api.NodeMetaData)
     assert metadata.labels == {}
     assert metadata.annotations == {}
Esempio n. 8
0
    def test_parse_metadata(self, core_client, dummy_host):
        mocked_pods = {
            "kind": "PodList",
            "apiVersion": "v1",
            "metadata": {"selfLink": "/api/v1/pods", "resourceVersion": "6605101"},
            "items": [
                {
                    "metadata": {
                        "name": "cluster-collector-595b64557d-x9t5q",
                        "generateName": "cluster-collector-595b64557d-",
                        "namespace": "checkmk-monitoring",
                        "uid": "b1c113f5-ee08-44c2-8438-a83ca240e04a",
                        "resourceVersion": "221646",
                        "creationTimestamp": "2022-03-28T09:19:41Z",
                        "labels": {"app": "cluster-collector"},
                        "annotations": {"foo": "case"},
                        "ownerReferences": [
                            {
                                "apiVersion": "apps/v1",
                                "kind": "ReplicaSet",
                                "name": "cluster-collector-595b64557d",
                                "uid": "547e9da2-cbfa-4116-9cb6-67487b11a786",
                                "controller": "true",
                                "blockOwnerDeletion": "true",
                            }
                        ],
                    },
                },
            ],
        }
        Entry.single_register(
            Entry.GET,
            f"{dummy_host}/api/v1/pods",
            body=json.dumps(mocked_pods),
            headers={"content-type": "application/json"},
        )
        with Mocketizer():
            pod = list(core_client.list_pod_for_all_namespaces().items)[0]

        metadata = parse_metadata(pod.metadata, model=api.PodMetaData)
        assert metadata.name == "cluster-collector-595b64557d-x9t5q"
        assert metadata.namespace == "checkmk-monitoring"
        assert isinstance(metadata.creation_timestamp, float)
        assert metadata.labels == {"app": api.Label(name="app", value="cluster-collector")}
        assert metadata.annotations == {"foo": "case"}
    def test_parse_metadata(self, apps_client, dummy_host):
        daemon_sets_metadata = {
            "items": [{
                "metadata": {
                    "name": "node-collector-container-metrics",
                    "namespace": "checkmk-monitoring",
                    "uid": "6f07cb60-26c7-41ce-afe0-48c97d15a07b",
                    "resourceVersion": "2967286",
                    "generation": 1,
                    "creationTimestamp": "2022-02-16T10:03:21Z",
                    "labels": {
                        "app": "node-collector-container-metrics"
                    },
                    "annotations": {
                        "deprecated.daemonset.template.generation":
                        "1",
                        "seccomp.security.alpha.kubernetes.io/pod":
                        "runtime/default",
                    },
                }
            }]
        }

        Entry.single_register(
            Entry.GET,
            f"{dummy_host}/apis/apps/v1/daemonsets",
            body=json.dumps(daemon_sets_metadata),
            headers={"content-type": "application/json"},
        )
        with Mocketizer():
            daemon_set = list(
                apps_client.list_daemon_set_for_all_namespaces().items)[0]
        metadata = parse_metadata(daemon_set.metadata)
        assert isinstance(metadata, api.MetaData)
        assert metadata.name == "node-collector-container-metrics"
        assert isinstance(metadata.creation_timestamp, float)
        assert metadata.labels
        assert metadata.annotations == {
            "deprecated.daemonset.template.generation": "1"
        }
    def test_parse_metadata(self):
        labels = {
            "beta.kubernetes.io/arch": "amd64",
            "beta.kubernetes.io/os": "linux",
            "kubernetes.io/arch": "amd64",
            "kubernetes.io/hostname": "k8",
            "kubernetes.io/os": "linux",
            "node-role.kubernetes.io/master": "",
        }
        annotations = {
            "kubectl.kubernetes.io/last-applied-configuration":
            '{"apiVersion":"v1","kind":"Node","metadata":{"annotations":{},"name":"minikube"}}\n',
            "node.alpha.kubernetes.io/ttl": "0",
            "volumes.kubernetes.io/controller-managed-attach-detach": "true",
        }

        node_raw_metadata = {
            "name":
            "k8",
            "creation_timestamp":
            datetime.datetime.strptime(
                "2021-05-04T09:01:13Z",
                "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc),
            "uid":
            "42c82288-5524-49cb-af75-065e73fedc88",
            "labels":
            labels,
            "annotations":
            annotations,
        }
        metadata_obj = client.V1ObjectMeta(**node_raw_metadata)
        metadata = parse_metadata(metadata_obj, model=api.NodeMetaData)
        assert isinstance(metadata, api.NodeMetaData)
        assert metadata.name == "k8"
        assert metadata.namespace is None
        assert metadata.labels
        assert metadata.annotations == {
            "node.alpha.kubernetes.io/ttl": "0",
            "volumes.kubernetes.io/controller-managed-attach-detach": "true",
        }