本文整理汇总了Python中utils.dockerutil.DockerUtil.containers方法的典型用法代码示例。如果您正苦于以下问题:Python DockerUtil.containers方法的具体用法?Python DockerUtil.containers怎么用?Python DockerUtil.containers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.dockerutil.DockerUtil
的用法示例。
在下文中一共展示了DockerUtil.containers方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SDDockerBackend
# 需要导入模块: from utils.dockerutil import DockerUtil [as 别名]
# 或者: from utils.dockerutil.DockerUtil import containers [as 别名]
class SDDockerBackend(AbstractSDBackend):
"""Docker-based service discovery"""
def __init__(self, agentConfig):
try:
self.config_store = get_config_store(agentConfig=agentConfig)
except Exception as e:
log.error('Failed to instantiate the config store client. '
'Auto-config only will be used. %s' % str(e))
agentConfig['sd_config_backend'] = None
self.config_store = get_config_store(agentConfig=agentConfig)
self.docker_client = DockerUtil(config_store=self.config_store).client
if Platform.is_k8s():
self.kubeutil = KubeUtil()
self.VAR_MAPPING = {
'host': self._get_host_address,
'port': self._get_port,
'tags': self._get_additional_tags,
}
AbstractSDBackend.__init__(self, agentConfig)
def _make_fetch_state(self):
pod_list = []
if Platform.is_k8s():
try:
pod_list = self.kubeutil.retrieve_pods_list().get('items', [])
except Exception as ex:
log.warning("Failed to retrieve pod list: %s" % str(ex))
return _SDDockerBackendConfigFetchState(self.docker_client.inspect_container, pod_list)
def update_checks(self, changed_containers):
state = self._make_fetch_state()
conf_reload_set = set()
for c_id in changed_containers:
checks = self._get_checks_to_refresh(state, c_id)
if checks:
conf_reload_set.update(set(checks))
if conf_reload_set:
self.reload_check_configs = conf_reload_set
def _get_checks_to_refresh(self, state, c_id):
"""Get the list of checks applied to a container from the identifier_to_checks cache in the config store.
Use the DATADOG_ID label or the image."""
inspect = state.inspect_container(c_id)
# If the container was removed we can't tell which check is concerned
# so we have to reload everything.
# Same thing if it's stopped and we're on Kubernetes in auto_conf mode
# because the pod was deleted and its template could have been in the annotations.
if not inspect or \
(not inspect.get('State', {}).get('Running')
and Platform.is_k8s() and not self.agentConfig.get('sd_config_backend')):
self.reload_check_configs = True
return
identifier = inspect.get('Config', {}).get('Labels', {}).get(DATADOG_ID) or \
inspect.get('Config', {}).get('Image')
platform_kwargs = {}
if Platform.is_k8s():
kube_metadata = state.get_kube_config(c_id, 'metadata') or {}
platform_kwargs = {
'kube_annotations': kube_metadata.get('annotations'),
'kube_container_name': state.get_kube_container_name(c_id),
}
return self.config_store.get_checks_to_refresh(identifier, **platform_kwargs)
def _get_host_address(self, state, c_id, tpl_var):
"""Extract the container IP from a docker inspect object, or the kubelet API."""
c_inspect = state.inspect_container(c_id)
c_id, c_img = c_inspect.get('Id', ''), c_inspect.get('Config', {}).get('Image', '')
networks = c_inspect.get('NetworkSettings', {}).get('Networks') or {}
ip_dict = {}
for net_name, net_desc in networks.iteritems():
ip = net_desc.get('IPAddress')
if ip:
ip_dict[net_name] = ip
ip_addr = self._extract_ip_from_networks(ip_dict, tpl_var)
if ip_addr:
return ip_addr
# try to get the bridge (default) IP address
log.debug("No IP address was found in container %s (%s) "
"networks, trying with the IPAddress field" % (c_id[:12], c_img))
ip_addr = c_inspect.get('NetworkSettings', {}).get('IPAddress')
if ip_addr:
return ip_addr
if Platform.is_k8s():
# kubernetes case
log.debug("Couldn't find the IP address for container %s (%s), "
"using the kubernetes way." % (c_id[:12], c_img))
pod_ip = state.get_kube_config(c_id, 'status').get('podIP')
#.........这里部分代码省略.........
示例2: SDDockerBackend
# 需要导入模块: from utils.dockerutil import DockerUtil [as 别名]
# 或者: from utils.dockerutil.DockerUtil import containers [as 别名]
class SDDockerBackend(AbstractSDBackend):
"""Docker-based service discovery"""
def __init__(self, agentConfig):
self.docker_client = DockerUtil().client
if is_k8s():
self.kubeutil = KubeUtil()
try:
self.config_store = get_config_store(agentConfig=agentConfig)
except Exception as e:
log.error('Failed to instantiate the config store client. '
'Auto-config only will be used. %s' % str(e))
agentConfig['sd_config_backend'] = None
self.config_store = get_config_store(agentConfig=agentConfig)
self.VAR_MAPPING = {
'host': self._get_host,
'port': self._get_ports,
'tags': self._get_additional_tags,
}
AbstractSDBackend.__init__(self, agentConfig)
def _get_host(self, container_inspect):
"""Extract the host IP from a docker inspect object, or the kubelet API."""
ip_addr = container_inspect.get('NetworkSettings', {}).get('IPAddress')
if not ip_addr:
if not is_k8s():
return
# kubernetes case
log.debug("Didn't find the IP address for container %s (%s), using the kubernetes way." %
(container_inspect.get('Id', '')[:12], container_inspect.get('Config', {}).get('Image', '')))
pod_list = self.kubeutil.retrieve_pods_list().get('items', [])
c_id = container_inspect.get('Id')
for pod in pod_list:
pod_ip = pod.get('status', {}).get('podIP')
if pod_ip is None:
continue
else:
c_statuses = pod.get('status', {}).get('containerStatuses', [])
for status in c_statuses:
# compare the container id with those of containers in the current pod
if c_id == status.get('containerID', '').split('//')[-1]:
ip_addr = pod_ip
return ip_addr
def _get_ports(self, container_inspect):
"""Extract a list of available ports from a docker inspect object. Sort them numerically."""
c_id = container_inspect.get('Id', '')
try:
ports = map(lambda x: x.split('/')[0], container_inspect['NetworkSettings']['Ports'].keys())
except (IndexError, KeyError, AttributeError):
log.debug("Didn't find the port for container %s (%s), trying the kubernetes way." %
(c_id[:12], container_inspect.get('Config', {}).get('Image', '')))
# first we try to get it from the docker API
# it works if the image has an EXPOSE instruction
ports = map(lambda x: x.split('/')[0], container_inspect['Config'].get('ExposedPorts', {}).keys())
# if it failed, try with the kubernetes API
if not ports and is_k8s():
co_statuses = self._get_kube_config(c_id, 'status').get('containerStatuses', [])
c_name = None
for co in co_statuses:
if co.get('containerID', '').split('//')[-1] == c_id:
c_name = co.get('name')
break
containers = self._get_kube_config(c_id, 'spec').get('containers', [])
for co in containers:
if co.get('name') == c_name:
ports = map(lambda x: str(x.get('containerPort')), co.get('ports', []))
ports = sorted(ports, key=lambda x: int(x))
return ports
def get_tags(self, c_inspect):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
tags = []
if is_k8s():
pod_metadata = self._get_kube_config(c_inspect.get('Id'), 'metadata')
if pod_metadata is None:
log.warning("Failed to fetch pod metadata for container %s."
" Kubernetes tags may be missing." % c_inspect.get('Id', '')[:12])
return []
# get labels
kube_labels = pod_metadata.get('labels', {})
for label, value in kube_labels.iteritems():
tags.append('%s:%s' % (label, value))
# get replication controller
created_by = json.loads(pod_metadata.get('annotations', {}).get('kubernetes.io/created-by', '{}'))
if created_by.get('reference', {}).get('kind') == 'ReplicationController':
tags.append('kube_replication_controller:%s' % created_by.get('reference', {}).get('name'))
# get kubernetes namespace
tags.append('kube_namespace:%s' % pod_metadata.get('namespace'))
return tags
def _get_additional_tags(self, container_inspect):
tags = []
#.........这里部分代码省略.........