Skip to content

Commit

Permalink
Merge pull request #857 from samj1912/webhook-cause
Browse files Browse the repository at this point in the history
Add old, new, diff and operation to WebhookCause
  • Loading branch information
nolar authored Nov 17, 2021
2 parents 14825f7 + 027880c commit 408686a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion kopf/_core/engines/admission.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from kopf._cogs.aiokits import aiovalues
from kopf._cogs.clients import creating, errors, patching
from kopf._cogs.configs import configuration
from kopf._cogs.structs import bodies, ephemera, ids, patches, references, reviews
from kopf._cogs.structs import bodies, diffs, ephemera, ids, patches, references, reviews
from kopf._core.actions import execution, lifecycles, loggers, progression
from kopf._core.intents import causes, filters, handlers, registries

Expand Down Expand Up @@ -126,6 +126,9 @@ async def serve_admission_request(

memo = await memories.recall_memo(raw_body, memobase=memobase, ephemeral=operation=='CREATE')
body = bodies.Body(raw_body)
old = bodies.Body(old_body) if old_body is not None else None
new = bodies.Body(new_body) if new_body is not None else None
diff = diffs.diff(old, new)
patch = patches.Patch(body=raw_body)
warnings: List[str] = []
cause = causes.WebhookCause(
Expand All @@ -144,6 +147,9 @@ async def serve_admission_request(
headers=headers if headers is not None else {}, # ensure a mapping even if not provided.
webhook=webhook,
reason=reason,
old=old,
new=new,
diff=diff,
)

# Retrieve the handlers to be executed; maybe only one if the webhook server provides a hint.
Expand Down
4 changes: 3 additions & 1 deletion kopf/_core/intents/causes.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ class WebhookCause(ResourceCause):
warnings: List[str] # mutable!
operation: Optional[reviews.Operation] # None if not provided for some reason
subresource: Optional[str] # e.g. "status", "scale"; None for the main resource body
old: Optional[bodies.Body] = None
new: Optional[bodies.Body] = None
diff: Optional[diffs.Diff] = None

@property
def _kwargs(self) -> Mapping[str, Any]:
kwargs = dict(super()._kwargs)
del kwargs['reason']
del kwargs['webhook']
del kwargs['operation']
return kwargs


Expand Down
10 changes: 9 additions & 1 deletion tests/causation/test_kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from kopf._cogs.configs.configuration import OperatorSettings
from kopf._cogs.structs import diffs
from kopf._cogs.structs.bodies import Body, BodyEssence
from kopf._cogs.structs.diffs import Diff
from kopf._cogs.structs.ephemera import Memo
Expand Down Expand Up @@ -85,13 +86,16 @@ def test_admission_kwargs(resource, attr):
reason=None,
operation=None,
subresource=None,
new=BodyEssence(body),
old=None,
diff=diffs.diff(BodyEssence(body), None),
)
kwargs = getattr(cause, attr) # cause.kwargs / cause.sync_kwargs / cause.async_kwargs
assert set(kwargs) == {'logger', 'resource',
'dryrun', 'headers', 'sslpeer', 'userinfo', 'warnings', 'subresource',
'patch', 'memo',
'body', 'spec', 'status', 'meta', 'uid', 'name', 'namespace',
'labels', 'annotations'}
'labels', 'annotations', 'old', 'new', 'diff', 'operation'}
assert kwargs['resource'] is cause.resource
assert kwargs['logger'] is cause.logger
assert kwargs['dryrun'] is cause.dryrun
Expand All @@ -110,6 +114,10 @@ def test_admission_kwargs(resource, attr):
assert kwargs['uid'] == cause.body.metadata.uid
assert kwargs['name'] == cause.body.metadata.name
assert kwargs['namespace'] == cause.body.metadata.namespace
assert kwargs['operation'] == cause.operation
assert kwargs['new'] == cause.new
assert kwargs['old'] == cause.old
assert kwargs['diff'] == cause.diff


@pytest.mark.parametrize('attr', ['kwargs', 'sync_kwargs', 'async_kwargs'])
Expand Down

0 comments on commit 408686a

Please sign in to comment.