Skip to content

Commit

Permalink
Demonstrate field filters with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nolar committed Nov 11, 2020
1 parent 6a10842 commit 697bce0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/11-filtering-handlers/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,36 @@ def create_with_filter_satisfied(logger, **kwargs):
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', when=lambda body, **_: False)
def create_with_filter_not_satisfied(logger, **kwargs):
logger.info("Filter not satisfied.")


@kopf.on.create('zalando.org', 'v1', 'kopfexamples', field='spec.field', value='value')
def create_with_field_value_satisfied(logger, **kwargs):
logger.info("Field value is satisfied.")


@kopf.on.create('zalando.org', 'v1', 'kopfexamples', field='spec.field', value='something-else')
def create_with_field_value_not_satisfied(logger, **kwargs):
logger.info("Field value is not satisfied.")


@kopf.on.create('zalando.org', 'v1', 'kopfexamples', field='spec.field', value=kopf.PRESENT)
def create_with_field_presence_satisfied(logger, **kwargs):
logger.info("Field presence is satisfied.")


@kopf.on.create('zalando.org', 'v1', 'kopfexamples', field='spec.inexistent', value=kopf.PRESENT)
def create_with_field_presence_not_satisfied(logger, **kwargs):
logger.info("Field presence is not satisfied.")


@kopf.on.update('zalando.org', 'v1', 'kopfexamples',
field='spec.field', old='value', new='changed')
def update_with_field_change_satisfied(logger, **kwargs):
logger.info("Field change is satisfied.")


@kopf.daemon('zalando.org', 'v1', 'kopfexamples', field='spec.field', value='value')
def daemon_with_field(stopped, logger, **kwargs):
while not stopped:
logger.info("Field daemon is satisfied.")
stopped.wait(1)
9 changes: 9 additions & 0 deletions examples/11-filtering-handlers/test_example_11.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def test_handler_filtering():
subprocess.run(f"kubectl create -f {obj_yaml}",
shell=True, check=True, timeout=10, capture_output=True)
time.sleep(5) # give it some time to react
subprocess.run(f"kubectl patch -f {obj_yaml} --type merge -p '" '{"spec":{"field":"changed"}}' "'",
shell=True, check=True, timeout=10, capture_output=True)
time.sleep(2) # give it some time to react
subprocess.run(f"kubectl delete -f {obj_yaml}",
shell=True, check=True, timeout=10, capture_output=True)
time.sleep(1) # give it some time to react
Expand All @@ -65,3 +68,9 @@ def test_handler_filtering():
assert '[default/kopf-example-1] Annotation callback mismatch.' not in runner.stdout
assert '[default/kopf-example-1] Filter satisfied.' in runner.stdout
assert '[default/kopf-example-1] Filter not satisfied.' not in runner.stdout
assert '[default/kopf-example-1] Field value is satisfied.' in runner.stdout
assert '[default/kopf-example-1] Field value is not satisfied.' not in runner.stdout
assert '[default/kopf-example-1] Field presence is satisfied.' in runner.stdout
assert '[default/kopf-example-1] Field presence is not satisfied.' not in runner.stdout
assert '[default/kopf-example-1] Field change is satisfied.' in runner.stdout
assert '[default/kopf-example-1] Field daemon is satisfied.' in runner.stdout

0 comments on commit 697bce0

Please sign in to comment.