Пример #1
0
def __get_resource_tables(namespace=None,
                          all_namespaces=None,
                          resource_types=None,
                          resource_names=None,
                          labels=None,
                          field_selectors=None,
                          wide=True,
                          con_ssh=None,
                          fail_ok=False,
                          grep=None):
    if not resource_types:
        resource_types = ''
    elif isinstance(resource_types, (list, tuple)):
        resource_types = ','.join(resource_types)
    resources = resource_types

    if resource_names:
        if ',' in resource_types:
            raise ValueError(
                "At most 1 resource_types can be specified if resource_names "
                "are provided.")
        if all_namespaces and not namespace:
            raise ValueError(
                "all_namespaces is disallowed when resource_names are provided"
            )
        if isinstance(resource_names, (list, tuple)):
            resource_names = ' '.join(resource_names)
        resources = '{} {}'.format(resources, resource_names)

    args_dict = {
        '-n': namespace,
        '--all-namespaces': True if all_namespaces and not namespace else None,
        '-l': labels,
        '--field-selector': field_selectors,
        '-o': 'wide' if wide else None
    }
    args = '{} {}'.format(
        resources, common.parse_args(args_dict, repeat_arg=False,
                                     vals_sep=','))
    code, out = exec_kube_cmd(sub_cmd='get',
                              args=args,
                              con_ssh=con_ssh,
                              fail_ok=fail_ok,
                              grep=grep)
    if code > 0:
        return code, out

    tables = table_parser.tables_kube(out)
    return code, tables
Пример #2
0
def get_resources(field='NAME',
                  namespace=None,
                  all_namespaces=None,
                  resource_names=None,
                  resource_type='pod',
                  labels=None,
                  field_selectors=None,
                  con_ssh=None,
                  fail_ok=False,
                  grep=None,
                  exclude=False,
                  strict=True,
                  **kwargs):
    """
    Get resources values for single resource type via kubectl get
    Args:
        field (str|tuple|list)
        namespace (None|str): e.g., kube-system, openstack, default.
        all_namespaces (bool|None): used only when namespace is unspecified
        resource_names (str|None|list|tuple): e.g., calico-typha
        resource_type (str): e.g., "deployments.apps", "pod", "service"
        labels (dict|str\list|tuple): Used only when resource_names are
            unspecified
        field_selectors (dict|str|list|tuple): Used only when resource_names
        are unspecified
        con_ssh:
        fail_ok:
        grep (str|None): grep on cmd output
        exclude
        strict
        **kwargs: table filters for post processing return values

    Returns (list):
        key is the name prefix, e.g., service, default, deployment.apps,
        replicaset.apps
        value is a list. Each item is a dict rep for a row with lowercase keys.
            e.g., [{'name': 'cinder-api', 'age': '4d19h', ... },  ...]

    """
    name_filter = None
    if resource_names and ((all_namespaces and not namespace)
                           or field_selectors or labels):
        name_filter = {'name': resource_names}
        resource_names = None

    code, tables = __get_resource_tables(namespace=namespace,
                                         all_namespaces=all_namespaces,
                                         resource_types=resource_type,
                                         resource_names=resource_names,
                                         labels=labels,
                                         field_selectors=field_selectors,
                                         con_ssh=con_ssh,
                                         fail_ok=fail_ok,
                                         grep=grep)
    if code > 0:
        output = tables
        if 'NAME ' not in output:  # no resource returned
            return []

        output = output.split('\nError from server')[0]
        tables = table_parser.tables_kube(output)

    final_table = tables[0]
    if len(tables) > 1:
        combined_values = final_table['values']
        column_count = len(combined_values)
        for table_ in tables[1:]:
            table_values = table_['values']
            combined_values = [
                combined_values[i] + table_values[i]
                for i in range(column_count)
            ]
        final_table['values'] = combined_values

    if name_filter:
        final_table = table_parser.filter_table(final_table, **name_filter)

    return table_parser.get_multi_values(final_table,
                                         fields=field,
                                         zip_values=True,
                                         strict=strict,
                                         exclude=exclude,
                                         **kwargs)