本文整理汇总了Python中utils.platform.Platform.is_rancher方法的典型用法代码示例。如果您正苦于以下问题:Python Platform.is_rancher方法的具体用法?Python Platform.is_rancher怎么用?Python Platform.is_rancher使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.platform.Platform
的用法示例。
在下文中一共展示了Platform.is_rancher方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_host_address
# 需要导入模块: from utils.platform import Platform [as 别名]
# 或者: from utils.platform.Platform import is_rancher [as 别名]
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_inspect.get('Id', '')
c_img = self.dockerutil.image_name_extractor(c_inspect)
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
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')
if pod_ip:
return pod_ip
if Platform.is_rancher():
# try to get the rancher IP address
log.debug("No IP address was found in container %s (%s) "
"trying with the Rancher label" % (c_id[:12], c_img))
ip_addr = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_CONTAINER_IP)
if ip_addr:
return ip_addr.split('/')[0]
log.error("No IP address was found for container %s (%s)" % (c_id[:12], c_img))
return None
示例2: get_tags
# 需要导入模块: from utils.platform import Platform [as 别名]
# 或者: from utils.platform.Platform import is_rancher [as 别名]
def get_tags(self, state, c_id):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
c_inspect = state.inspect_container(c_id)
tags = self.dockerutil.extract_container_tags(c_inspect)
if Platform.is_k8s():
pod_metadata = state.get_kube_config(c_id, 'metadata')
if pod_metadata is None:
log.warning("Failed to fetch pod metadata for container %s."
" Kubernetes tags may be missing." % c_id[:12])
return []
# get pod labels
kube_labels = pod_metadata.get('labels', {})
for label, value in kube_labels.iteritems():
tags.append('%s:%s' % (label, value))
# get kubernetes namespace
namespace = pod_metadata.get('namespace')
tags.append('kube_namespace:%s' % namespace)
# add creator tags
creator_tags = self.kubeutil.get_pod_creator_tags(pod_metadata)
tags.extend(creator_tags)
# add services tags
if self.kubeutil.collect_service_tag:
services = self.kubeutil.match_services_for_pod(pod_metadata)
for s in services:
if s is not None:
tags.append('kube_service:%s' % s)
elif Platform.is_swarm():
c_labels = c_inspect.get('Config', {}).get('Labels', {})
swarm_svc = c_labels.get(SWARM_SVC_LABEL)
if swarm_svc:
tags.append('swarm_service:%s' % swarm_svc)
elif Platform.is_rancher():
service_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_SVC_NAME)
stack_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_STACK_NAME)
container_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_CONTAINER_NAME)
if service_name:
tags.append('rancher_service:%s' % service_name)
if stack_name:
tags.append('rancher_stack:%s' % stack_name)
if container_name:
tags.append('rancher_container:%s' % container_name)
if self.metadata_collector.has_detected():
orch_tags = self.metadata_collector.get_container_tags(co=c_inspect)
tags.extend(orch_tags)
return tags
示例3: get_tags
# 需要导入模块: from utils.platform import Platform [as 别名]
# 或者: from utils.platform.Platform import is_rancher [as 别名]
def get_tags(self, state, c_id):
"""Extract useful tags from docker or platform APIs. These are collected by default."""
tags = []
ctr = state.inspect_container(c_id)
# TODO: extend with labels, container ID, etc.
tags.append('docker_image:%s' % self.dockerutil.image_name_extractor(ctr))
tags.append('image_name:%s' % self.dockerutil.image_tag_extractor(ctr, 0)[0])
tags.append('image_tag:%s' % self.dockerutil.image_tag_extractor(ctr, 1)[0])
if Platform.is_k8s():
pod_metadata = state.get_kube_config(c_id, 'metadata')
if pod_metadata is None:
log.warning("Failed to fetch pod metadata for container %s."
" Kubernetes tags may be missing." % c_id[:12])
return []
# get pod labels
kube_labels = pod_metadata.get('labels', {})
for label, value in kube_labels.iteritems():
tags.append('%s:%s' % (label, value))
# get kubernetes namespace
namespace = pod_metadata.get('namespace')
tags.append('kube_namespace:%s' % namespace)
# add creator tags
creator_tags = self.kubeutil.get_pod_creator_tags(pod_metadata)
tags.extend(creator_tags)
# add services tags
services = self.kubeutil.match_services_for_pod(pod_metadata)
for s in services:
if s is not None:
tags.append('kube_service:%s' % s)
elif Platform.is_swarm():
c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {})
swarm_svc = c_labels.get(SWARM_SVC_LABEL)
if swarm_svc:
tags.append('swarm_service:%s' % swarm_svc)
elif Platform.is_rancher():
c_inspect = state.inspect_container(c_id)
service_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_SVC_NAME)
stack_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_STACK_NAME)
container_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_CONTAINER_NAME)
if service_name:
tags.append('rancher_service:%s' % service_name)
if stack_name:
tags.append('rancher_stack:%s' % stack_name)
if container_name:
tags.append('rancher_container:%s' % container_name)
elif Platform.is_nomad():
nomad_tags = self.nomadutil.extract_container_tags(state.inspect_container(c_id))
if nomad_tags:
tags.extend(nomad_tags)
elif Platform.is_ecs_instance():
ecs_tags = self.ecsutil.extract_container_tags(state.inspect_container(c_id))
tags.extend(ecs_tags)
return tags
示例4: _get_tags
# 需要导入模块: from utils.platform import Platform [as 别名]
# 或者: from utils.platform.Platform import is_rancher [as 别名]
def _get_tags(self, entity=None, tag_type=None):
"""Generate the tags for a given entity (container or image) according to a list of tag names."""
# Start with custom tags
tags = list(self.custom_tags)
# Collect pod names as tags on kubernetes
if Platform.is_k8s() and KubeUtil.POD_NAME_LABEL not in self.collect_labels_as_tags:
self.collect_labels_as_tags.append(KubeUtil.POD_NAME_LABEL)
# Collect container names as tags on rancher
if Platform.is_rancher():
if RANCHER_CONTAINER_NAME not in self.collect_labels_as_tags:
self.collect_labels_as_tags.append(RANCHER_CONTAINER_NAME)
if RANCHER_SVC_NAME not in self.collect_labels_as_tags:
self.collect_labels_as_tags.append(RANCHER_SVC_NAME)
if RANCHER_STACK_NAME not in self.collect_labels_as_tags:
self.collect_labels_as_tags.append(RANCHER_STACK_NAME)
if entity is not None:
pod_name = None
# Get labels as tags
labels = entity.get("Labels")
if labels is not None:
for k in self.collect_labels_as_tags:
if k in labels:
v = labels[k]
if k == KubeUtil.POD_NAME_LABEL and Platform.is_k8s():
pod_name = v
k = "pod_name"
if "-" in pod_name:
replication_controller = "-".join(pod_name.split("-")[:-1])
if "/" in replication_controller: # k8s <= 1.1
namespace, replication_controller = replication_controller.split("/", 1)
elif KubeUtil.NAMESPACE_LABEL in labels: # k8s >= 1.2
namespace = labels[KubeUtil.NAMESPACE_LABEL]
pod_name = "{0}/{1}".format(namespace, pod_name)
tags.append("kube_namespace:%s" % namespace)
tags.append("kube_replication_controller:%s" % replication_controller)
tags.append("pod_name:%s" % pod_name)
elif k == SWARM_SVC_LABEL and Platform.is_swarm():
if v:
tags.append("swarm_service:%s" % v)
elif k == RANCHER_CONTAINER_NAME and Platform.is_rancher():
if v:
tags.append('rancher_container:%s' % v)
elif k == RANCHER_SVC_NAME and Platform.is_rancher():
if v:
tags.append('rancher_service:%s' % v)
elif k == RANCHER_STACK_NAME and Platform.is_rancher():
if v:
tags.append('rancher_stack:%s' % v)
elif not v:
tags.append(k)
else:
tags.append("%s:%s" % (k,v))
if k == KubeUtil.POD_NAME_LABEL and Platform.is_k8s() and k not in labels:
tags.append("pod_name:no_pod")
# Get entity specific tags
if tag_type is not None:
tag_names = self.tag_names[tag_type]
for tag_name in tag_names:
tag_value = self._extract_tag_value(entity, tag_name)
if tag_value is not None:
for t in tag_value:
tags.append('%s:%s' % (tag_name, str(t).strip()))
# Add kube labels and creator/service tags
if Platform.is_k8s():
kube_tags = self.kube_pod_tags.get(pod_name)
if kube_tags:
tags.extend(list(kube_tags))
if self.metadata_collector.has_detected():
orch_tags = self.metadata_collector.get_container_tags(co=entity)
tags.extend(orch_tags)
return tags