Exemplo n.º 1
0
def get_prometheus_rules(cluster_name):
    """Returns a dict of dicts indexed by path with rule data"""
    gqlapi = gql.get_api()
    rules = {}
    for r in gqlapi.query(PROMETHEUS_RULES_PATHS_QUERY)["resources"]:
        rules[r["path"]] = {}

    for n in gqlapi.query(orb.NAMESPACES_QUERY)["namespaces"]:
        cluster = n["name"]
        namespace = n["cluster"]["name"]

        if cluster_name and cluster != cluster_name:
            continue

        if (not n.get("managedResourceTypes")
                or "PrometheusRule" not in n["managedResourceTypes"]):
            continue

        openshift_resources = n.get("openshiftResources")
        if not openshift_resources:
            logging.warning("No openshiftResources defined for namespace "
                            f"{namespace} in cluster {cluster}")
            continue

        for r in openshift_resources:
            path = r["path"]
            if path not in rules:
                continue

            # Or we will get an unexepected and confusing html_url annotation
            if "add_path_to_prom_rules" not in r:
                r["add_path_to_prom_rules"] = False

            openshift_resource = orb.fetch_openshift_resource(resource=r,
                                                              parent=n)

            if cluster not in rules[path]:
                rules[path][cluster] = {}

            rules[path][cluster][namespace] = {
                "spec": openshift_resource.body["spec"]
            }

            # we keep variables to use them in the rule tests
            variables = r.get("variables")
            if variables:
                rules[path][cluster][namespace]["variables"] = json.loads(
                    variables)

    # We return rules that are actually consumed from a namespace becaused
    # those are the only ones that can be resolved as they can be templates
    return {path: data for path, data in rules.items() if data}
Exemplo n.º 2
0
def get_prometheus_rules(cluster_name):
    '''Returns a dict of dicts indexed by path with rule data'''
    gqlapi = gql.get_api()
    rules = {}
    for r in gqlapi.query(PROMETHEUS_RULES_PATHS_QUERY)['resources']:
        rules[r['path']] = {}

    for n in gqlapi.query(orb.NAMESPACES_QUERY)['namespaces']:
        cluster = n['name']
        namespace = n['cluster']['name']

        if cluster_name and cluster != cluster_name:
            continue

        if not n.get('managedResourceTypes') or \
           'PrometheusRule' not in n['managedResourceTypes']:
            continue

        openshift_resources = n.get('openshiftResources')
        if not openshift_resources:
            logging.warning('No openshiftResources defined for namespace '
                            f'{namespace} in cluster {cluster}')
            continue

        for r in openshift_resources:
            path = r['path']
            if path not in rules:
                continue

            # Or we will get an unexepected and confusing html_url annotation
            if 'add_path_to_prom_rules' not in r:
                r['add_path_to_prom_rules'] = False

            openshift_resource = \
                orb.fetch_openshift_resource(resource=r, parent=n)

            if cluster not in rules[path]:
                rules[path][cluster] = {}

            rules[path][cluster][namespace] = \
                {'spec': openshift_resource.body['spec']}

            # we keep variables to use them in the rule tests
            if 'variables' in r:
                rules[path][cluster][namespace]['variables'] = \
                    json.loads(r['variables'])

    # We return rules that are actually consumed from a namespace becaused
    # those are the only ones that can be resolved as they can be templates
    return {path: data for path, data in rules.items() if data}
def run(dry_run, thread_pool_size=10, cluster_name=None):
    orb.QONTRACT_INTEGRATION = QONTRACT_INTEGRATION
    orb.QONTRACT_INTEGRATION_VERSION = QONTRACT_INTEGRATION_VERSION

    gqlapi = gql.get_api()
    rules_paths = queries.get_prometheus_rules_paths()

    rules = []
    for n in gqlapi.query(orb.NAMESPACES_QUERY)['namespaces']:
        if cluster_name and n['cluster']['name'] != cluster_name:
            continue

        if not n['managedResourceTypes'] or \
           'PrometheusRule' not in n['managedResourceTypes']:
            continue

        openshift_resources = n.get('openshiftResources')
        if not openshift_resources:
            logging.warning("No openshiftResources defined for namespace"
                            f"{n['name']} in cluster {n['cluster']['name']}")
            continue

        for r in openshift_resources:
            if r['path'] not in rules_paths:
                continue

            openshift_resource = orb.fetch_openshift_resource(r, n)
            rules.append({
                'path': r['path'],
                'spec': openshift_resource.body['spec'],
                'namespace': n['name'],
                'cluster': n['cluster']['name']
            })

    failed = [
        r for r in threaded.run(check_rule, rules, thread_pool_size)
        if not r['check_result']
    ]

    if failed:
        for f in failed:
            logging.warning(f"Error in rule {f['path']} from namespace "
                            f"{f['namespace']} in cluster {f['cluster']}: "
                            f"{f['check_result']}")

        sys.exit(1)
Exemplo n.º 4
0
def template(ctx, cluster, namespace, kind, name):
    gqlapi = gql.get_api()
    namespaces = gqlapi.query(orb.NAMESPACES_QUERY)['namespaces']
    namespace_info = [n for n in namespaces
                      if n['cluster']['name'] == cluster
                      and n['name'] == namespace]
    if len(namespace_info) != 1:
        print(f"{cluster}/{namespace} error")
        sys.exit(1)

    [namespace_info] = namespace_info
    openshift_resources = namespace_info.get('openshiftResources')
    for r in openshift_resources:
        openshift_resource = orb.fetch_openshift_resource(r, namespace_info)
        if openshift_resource.kind.lower() != kind.lower():
            continue
        if openshift_resource.name != name:
            continue
        print_output('yaml', openshift_resource.body)
        break