-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from dlmiddlecote/45-filter-by-labels-annotat…
…ions Filter by labels and annotations
- Loading branch information
Showing
12 changed files
with
473 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Kopf example for testing the filtering of handlers | ||
|
||
Kopf has the ability to execute handlers only if the watched objects | ||
match the filters passed to the handler. This includes matching on: | ||
* labels of a resource | ||
* annotations of a resource | ||
|
||
Start the operator: | ||
|
||
```bash | ||
kopf run example.py | ||
``` | ||
|
||
Trigger the object creation and monitor the stderr of the operator: | ||
|
||
```bash | ||
$ kubectl apply -f ../obj.yaml | ||
``` | ||
|
||
``` | ||
[2019-07-04 14:19:33,393] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Label satisfied. | ||
[2019-07-04 14:19:33,395] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Handler 'create_with_labels_satisfied' succeeded. | ||
[2019-07-04 14:19:33,648] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Label exists. | ||
[2019-07-04 14:19:33,649] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Handler 'create_with_labels_exist' succeeded. | ||
[2019-07-04 14:19:33,807] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Annotation satisfied. | ||
[2019-07-04 14:19:33,809] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Handler 'create_with_annotations_satisfied' succeeded. | ||
[2019-07-04 14:19:33,966] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Annotation exists. | ||
[2019-07-04 14:19:33,967] kopf.reactor.handlin [INFO ] [default/kopf-example-1] Handler 'create_with_annotations_exist' succeeded. | ||
[2019-07-04 14:19:33,967] kopf.reactor.handlin [INFO ] [default/kopf-example-1] All handlers succeeded for creation. | ||
``` | ||
|
||
Here, notice that only the handlers that have labels or annotations that match the applied | ||
object are executed, and the ones that don't, aren't. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import kopf | ||
|
||
|
||
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', labels={'somelabel': 'somevalue'}) | ||
def create_with_labels_satisfied(logger, **kwargs): | ||
logger.info("Label satisfied.") | ||
|
||
|
||
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', labels={'somelabel': None}) | ||
def create_with_labels_exist(logger, **kwargs): | ||
logger.info("Label exists.") | ||
|
||
|
||
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', labels={'somelabel': 'othervalue'}) | ||
def create_with_labels_not_satisfied(logger, **kwargs): | ||
logger.info("Label not satisfied.") | ||
|
||
|
||
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', annotations={'someannotation': 'somevalue'}) | ||
def create_with_annotations_satisfied(logger, **kwargs): | ||
logger.info("Annotation satisfied.") | ||
|
||
|
||
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', annotations={'someannotation': None}) | ||
def create_with_annotations_exist(logger, **kwargs): | ||
logger.info("Annotation exists.") | ||
|
||
|
||
@kopf.on.create('zalando.org', 'v1', 'kopfexamples', annotations={'someannotation': 'othervalue'}) | ||
def create_with_annotations_not_satisfied(logger, **kwargs): | ||
logger.info("Annotation not satisfied.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os.path | ||
import subprocess | ||
import time | ||
|
||
import pytest | ||
|
||
import kopf.testing | ||
|
||
crd_yaml = os.path.relpath(os.path.join(os.path.dirname(__file__), '..', 'crd.yaml')) | ||
obj_yaml = os.path.relpath(os.path.join(os.path.dirname(__file__), '..', 'obj.yaml')) | ||
example_py = os.path.relpath(os.path.join(os.path.dirname(__file__), 'example.py')) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def crd_exists(): | ||
subprocess.run(f"kubectl apply -f {crd_yaml}", | ||
check=True, timeout=10, capture_output=True, shell=True) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def obj_absent(): | ||
# Operator is not running in fixtures, so we need a force-delete (or this patch). | ||
subprocess.run(['kubectl', 'patch', '-f', obj_yaml, | ||
'-p', '{"metadata":{"finalizers":[]}}', | ||
'--type', 'merge'], | ||
check=False, timeout=10, capture_output=True) | ||
subprocess.run(f"kubectl delete -f {obj_yaml}", | ||
check=False, timeout=10, capture_output=True, shell=True) | ||
|
||
|
||
def test_handler_filtering(mocker): | ||
|
||
# To prevent lengthy threads in the loop executor when the process exits. | ||
mocker.patch('kopf.config.WatchersConfig.default_stream_timeout', 10) | ||
|
||
# Run an operator and simulate some activity with the operated resource. | ||
with kopf.testing.KopfRunner(['run', '--verbose', '--standalone', example_py]) as runner: | ||
subprocess.run(f"kubectl create -f {obj_yaml}", shell=True, check=True) | ||
time.sleep(5) # give it some time to react | ||
subprocess.run(f"kubectl delete -f {obj_yaml}", shell=True, check=True) | ||
time.sleep(1) # give it some time to react | ||
|
||
# Ensure that the operator did not die on start, or during the operation. | ||
assert runner.exception is None | ||
assert runner.exit_code == 0 | ||
|
||
# Check for correct log lines (to indicate correct handlers were executed). | ||
assert '[default/kopf-example-1] Label satisfied.' in runner.stdout | ||
assert '[default/kopf-example-1] Label exists.' in runner.stdout | ||
assert '[default/kopf-example-1] Label not satisfied.' not in runner.stdout | ||
assert '[default/kopf-example-1] Annotation satisfied.' in runner.stdout | ||
assert '[default/kopf-example-1] Annotation exists.' in runner.stdout | ||
assert '[default/kopf-example-1] Annotation not satisfied.' not in runner.stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.