Skip to content

Commit

Permalink
Enhanced K8s API erver triggers filtering (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertSzefler committed Apr 1, 2024
1 parent f1f567b commit c496509
Show file tree
Hide file tree
Showing 15 changed files with 456 additions and 218 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/configuring-sinks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Integrate as many sinks as you like.

.. _sink-matchers:


Routing Alerts to Only One Sink
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand All @@ -50,6 +49,7 @@ The sinks evaluation order, is the order defined in ``generated_values.yaml``.
- namespace: production
stop: true
.. _sink-scope-matching:

Routing Alerts To Specific Sinks
***************************************
Expand Down
39 changes: 39 additions & 0 deletions docs/playbook-reference/triggers/kubernetes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,42 @@ Most Kubernetes triggers support the following filters:
* ``name_prefix``
* ``namespace_prefix``
* ``labels_selector`` - e.g. ``label1=value1,label2=value2``. If multiple labels is provided, all must match.

Additionally, Kubernetes triggers support a ``scope`` filtering mechanism that works almost
exactly like the ``scope`` mechanism for sinks (see :ref:`sink-scope-matching` for more
information). The only difference is that for triggers, there is an additional option
available for the ``include``/``exclude`` sections, ``attributes``, that makes it possible
to filter on *any* attribute inside the YAML representation of the resource. An example
of use of this functionality:

.. code-block:: yaml
customPlaybooks:
- name: "FilteredPodCreation"
triggers:
- on_pod_create:
scope:
include:
- name:
- my-pod.*
- other
namespace: ns1
labels:
- "foo=bar, boo=xx.*"
- "foo=xx, boo=xx.*"
attributes:
- "status.phase=Pending, status.qosClass=BestEffort, metadata.resourceVersion != 123"
- "spec.restartPolicy=OnFailure"
annotations:
- "foo=bar, boo=xx.*"
- "foo=xx, boo=xx.*"
exclude:
- name:
- woof.*
Note that ``attributes`` matching only allows exact equality and inequality. The left-hand side
of each of ``attributes`` filters is a path to select appropriate node in the document. It
supports typical constructs like following nested attributes using the ``.`` operator, or
selecting n-th element of a list using the ``[n]`` operator. In fact the language used to
describe paths is much more versatile, as the implementation uses ``jsonpath-ng`` under
the hood. You can read more about it `here <https://pypi.org/project/jsonpath-ng/>`_.
42 changes: 19 additions & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ prometrix = "0.1.16"
hikaru-model-26 = "^1.1.1"
apprise = "^1.5.0"
rocketchat-api = "^1.30.0"
pydash = "8.0.0"

[tool.poetry.dev-dependencies]
pre-commit = "^2.13.0"
Expand Down
19 changes: 14 additions & 5 deletions scripts/generate_kubernetes_code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import os
import textwrap
from itertools import chain
from typing import TextIO

KUBERNETES_VERSIONS = ["v1"]
Expand Down Expand Up @@ -39,7 +40,7 @@
"Deployment": "RobustaDeployment",
"Job": "RobustaJob",
}
CUSTOM_SUBCLASSES_NAMES_STR = ",".join(CUSTOM_SUBCLASSES.values())
CUSTOM_SUBCLASSES_NAMES_STR = ", ".join(CUSTOM_SUBCLASSES.values())

COMMON_PREFIX = """# This file was autogenerated. Do not edit.\n\n"""

Expand All @@ -48,7 +49,9 @@
"Rollout",
]

CUSTOM_RESOURCES_NAMES_STR = ",".join(CUSTOM_RESOURCES)
CUSTOM_RESOURCES_NAMES_STR = ", ".join(CUSTOM_RESOURCES)

CUSTOM_MODELS_IMPORTS = ", ".join(sorted(chain(CUSTOM_SUBCLASSES.values(), CUSTOM_RESOURCES)))


def get_model_class(k8s_resource_name: str) -> str:
Expand Down Expand Up @@ -76,7 +79,7 @@ def autogenerate_events(f: TextIO):
from ....core.reporting.base import FindingSubject
from ....core.reporting.consts import FindingSubjectType, FindingSource
from ....core.reporting.finding_subjects import KubeObjFindingSubject
from robusta.integrations.kubernetes.custom_models import {CUSTOM_SUBCLASSES_NAMES_STR},{CUSTOM_RESOURCES_NAMES_STR}
from robusta.integrations.kubernetes.custom_models import {CUSTOM_MODELS_IMPORTS}
"""
)
)
Expand Down Expand Up @@ -305,7 +308,7 @@ def autogenerate_models(f: TextIO, version: str):
textwrap.dedent(
f"""\
from hikaru.model.rel_1_26.{version} import *
from robusta.integrations.kubernetes.custom_models import {CUSTOM_SUBCLASSES_NAMES_STR},{CUSTOM_RESOURCES_NAMES_STR}
from robusta.integrations.kubernetes.custom_models import {CUSTOM_MODELS_IMPORTS}
"""
Expand Down Expand Up @@ -362,6 +365,7 @@ def autogenerate_triggers(f: TextIO):
from robusta.integrations.kubernetes.base_triggers import K8sBaseTrigger
from robusta.core.model.k8s_operation_type import K8sOperationType
from robusta.integrations.kubernetes.autogenerated.events import *
from robusta.utils.scope import ScopeParams
"""
Expand All @@ -382,6 +386,7 @@ def __init__(
namespace_prefix: str = None,
labels_selector: str = None,
change_filters: Dict[str, List[str]] = None,
scope: ScopeParams = None
):
super().__init__(
kind=\"{resource}\",
Expand All @@ -390,6 +395,7 @@ def __init__(
namespace_prefix=namespace_prefix,
labels_selector=labels_selector,
change_filters=change_filters,
scope=scope,
)
@staticmethod
Expand All @@ -413,13 +419,16 @@ def get_execution_event_type() -> type:
textwrap.dedent(
f"""\
class KubernetesAny{get_trigger_class_name(trigger_name)}Trigger(K8sBaseTrigger):
def __init__(self, name_prefix: str = None, namespace_prefix: str = None, labels_selector: str = None):
def __init__(
self, name_prefix: str = None, namespace_prefix: str = None, labels_selector: str = None, scope: ScopeParams = None
):
super().__init__(
kind=\"Any\",
operation={operation_type},
name_prefix=name_prefix,
namespace_prefix=namespace_prefix,
labels_selector=labels_selector,
scope=scope,
)
@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion src/robusta/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
ScanReportBlock,
ScanReportRow,
TableBlock,
VideoLink
VideoLink,
)

from robusta.core.reporting.base import EnrichmentType
Expand Down
4 changes: 2 additions & 2 deletions src/robusta/core/reporting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from robusta.core.reporting.base import (
BaseBlock,
Emojis,
Enrichment,
Filterable,
Enrichment,
Finding,
FindingSeverity,
FindingSource,
Expand Down Expand Up @@ -40,8 +40,8 @@
"VideoLink",
"FindingSource",
"Enrichment",
"FindingSubjectType",
"Filterable",
"FindingSubjectType",
"FindingSubject",
"Finding",
"MarkdownBlock",
Expand Down

0 comments on commit c496509

Please sign in to comment.