コード例 #1
0
    def test_get_k8s_metadata_container_empty(self):
        labels_list = (k8s_utils.get_k8s_metadata())

        self.assertDictEqual(labels_list, {
            'k8s.io/namespace/name': 'namespace',
            'k8s.io/pod/name': 'localhost'
        })
コード例 #2
0
def get_instance():
    """Get a resource based on the application environment.

    Returns a `Resource` configured for the current environment, or None if the
    environment is unknown or unsupported.

    :rtype: :class:`opencensus.common.resource.Resource` or None
    :return: A `Resource` configured for the current environment.
    """
    resources = []
    env_resource = resource.get_from_env()
    if env_resource is not None:
        resources.append(env_resource)

    if k8s_utils.is_k8s_environment():
        resources.append(resource.Resource(
            _K8S_CONTAINER, k8s_utils.get_k8s_metadata()))

    if is_gce_environment():
        resources.append(resource.Resource(
            _GCE_INSTANCE,
            gcp_metadata_config.GcpMetadataConfig().get_gce_metadata()))
    elif is_aws_environment():
        resources.append(resource.Resource(
            _AWS_EC2_INSTANCE,
            (aws_identity_doc_utils.AwsIdentityDocumentUtils()
             .get_aws_metadata())))

    if not resources:
        return None
    return resource.merge_resources(resources)
コード例 #3
0
    def test_get_k8s_metadata(self, http_request_mock):
        def assign_attribute_value(*args, **kwargs):
            if args[0].split('/')[-1] == 'cluster-name':
                return 'cluster'
            raise AssertionError

        http_request_mock.side_effect = assign_attribute_value
        labels_list = (k8s_utils.get_k8s_metadata())
        self.assertDictEqual(
            labels_list, {
                'k8s.io/cluster/name': 'cluster',
                'k8s.io/container/name': 'container',
                'k8s.io/namespace/name': 'namespace',
                'k8s.io/pod/name': 'localhost'
            })