Exemplo n.º 1
0
 def filter_pod(pod):
     pod_labels = safeget(pod.obj, 'metadata', 'labels')
     if pod_labels is None:
         return False
     return (dictionary_is_subset(selector, pod_labels)
             and (safeget(pod.obj, 'metadata', 'namespace') == safeget(
                 service.obj, 'metadata', 'namespace')))
Exemplo n.º 2
0
def has_label(labelname, service):
    """Returns true if the supplied service resource has label called
    *labelname*, else false"""

    labels = safeget(service.obj, 'metadata', 'labels')
    if labels is None:
        return False
    return labelname in labels.keys()
Exemplo n.º 3
0
def service_selector(service):
    """Returns the kubernetes selector for a service.

    The selector defines which pods are expected to implement the
    service.

    """
    return safeget(service.obj, 'spec', 'selector')
Exemplo n.º 4
0
def get_name_record_updates(services, pods, domain_label, name_label,
                            failover_label, quota_label):
    """Generate name records from a list of services and a list of pods

    services: a list of all available services as returned by pykube
    pods: a list of all available pods as returned by pykube
    domain_label: a string, all services with this label name are queried.
                  The value of this label defines, on which domain the
                  service will be published, e.g. schneevonmorgen.com.
    name_label: a string, all services with this label name are queried.
                The value of this label defines under which name the
                service will be published.
    failover_label: a string, to get a failover address in case the service
                    is unavailable
    quota_label: a string, to get the required quota for the service to be
                 considered "live"

    Returns a dictionary where the keys are domains and the values are
    lists of kuberoute.dns.Record.
    """
    records = {}
    for service in services:
        if not (is_node_port_service(service) and has_label(
                domain_label, service) and has_label(name_label, service)):
            continue
        domain = safeget(service.obj, 'metadata', 'labels', domain_label)
        name = safeget(service.obj, 'metadata', 'labels', name_label)
        if domain is None or name is None:
            continue
        filtered_pods = filter(
            lambda obj: is_pod_running(obj) and is_pod_ready(obj),
            get_pods_for_service(service, pods))
        ip_addresses = list(map(get_host_ip, filtered_pods))
        failover = safeget(service.obj, 'metadata', 'labels', failover_label)
        try:
            quota = int(safeget(service.obj, 'metadata', 'labels',
                                quota_label))
        except TypeError:
            quota = 0
        if records.get(domain, None) is None:
            records[domain] = []
        records[domain].append(
            Record(name, domain, ip_addresses, failover=failover, quota=quota))
    return records
Exemplo n.º 5
0
 def __init__(self, specs):
     super().__init__(specs)
     if 'status' not in self.obj:
         self.obj['status'] = {}
     self.obj['status']['phase'] = safeget(specs, 'status', 'phase', default_value='Running')
     try:
         conds = self.obj['status']['conditions']
     except KeyError:
         self.obj['status']['conditions'] = []
         conds = self.obj['status']['conditions']
     if len([ d for d in conds if 'type' in d and d['type'] == 'Ready']) == 0:
         conds += [{
             'type': 'Ready',
             'status': 'True',
         }]
Exemplo n.º 6
0
def node_unschedulable(node):
    return safeget(node, 'spec', 'unschedulable')
Exemplo n.º 7
0
def report_from_nodes(nodes):
    return dict([(safeget(node, "metadata", "name"), {
        "schedulable": not node_unschedulable(node),
        "ready": node_ready(node),
    }) for node in nodes])
Exemplo n.º 8
0
def is_pod_running(pod):
    return (safeget(pod.obj, 'status', 'phase') == 'Running')
Exemplo n.º 9
0
def is_node_port_service(service):
    return (safeget(service.obj, 'spec', 'type')) == 'NodePort'
Exemplo n.º 10
0
def get_host_ip(pod):
    return safeget(pod.obj, 'status', 'hostIP')