Skip to content

Commit

Permalink
fix backward compatibility issue
Browse files Browse the repository at this point in the history
Signed-off-by: Mehrdad Khojastefar <mehrdadkh.uni+github@gmail.com>
  • Loading branch information
mehrdad-khojastefar committed Sep 29, 2023
1 parent 31a93f0 commit a4deab1
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
2 changes: 2 additions & 0 deletions kopf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
from kopf._cogs.structs.reviews import (
WebhookClientConfigService,
WebhookClientConfig,
Operation,
Operations,
UserInfo,
Headers,
Expand Down Expand Up @@ -203,6 +204,7 @@
'WebhookClientConfigService',
'WebhookClientConfig',
'Operations',
'Operation',
'UserInfo',
'Headers',
'SSLPeer',
Expand Down
2 changes: 1 addition & 1 deletion kopf/_cogs/structs/reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RequestPayload(TypedDict):
userInfo: UserInfo
name: str
namespace: Optional[str]
operation: Operations
operation: Operation
options: Union[None, CreateOptions, UpdateOptions, DeleteOptions]
dryRun: bool
object: bodies.RawBody
Expand Down
6 changes: 5 additions & 1 deletion kopf/_core/engines/admission.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,11 @@ def build_webhooks(
[resource.plural] if handler.subresource is None else
[f'{resource.plural}/{handler.subresource}']
),
'operations': ['*'] if handler.operations is None else handler.operations,
'operations': ['*'] if handler.operation is None
else (
handler.operation if isinstance(handler.operation,list)
else [handler.operation]
),
'scope': '*', # doesn't matter since a specific resource is used.
}
for resource in resources
Expand Down
18 changes: 5 additions & 13 deletions kopf/_core/intents/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def adjust_cause(self, cause: execution.CauseT) -> execution.CauseT:
class WebhookHandler(ResourceHandler):
fn: callbacks.WebhookFn # typing clarification
reason: causes.WebhookType
operations: Optional[list[str]]
operation: Optional[list[str] | str]
subresource: Optional[str]
persistent: Optional[bool]
side_effects: Optional[bool]
Expand Down Expand Up @@ -68,13 +68,9 @@ class ChangingHandler(ResourceHandler):
fn: callbacks.ChangingFn # typing clarification
reason: Optional[causes.Reason]
initial: Optional[bool]
deleted: Optional[
bool
] # used for mixed-in (initial==True) @on.resume handlers only.
deleted: Optional[bool] # used for mixed-in (initial==True) @on.resume handlers only.
requires_finalizer: Optional[bool]
field_needs_change: Optional[
bool
] # to identify on-field/on-update with support for old=/new=.
field_needs_change: Optional[bool] # to identify on-field/on-update with support for old=/new=.
old: Optional[filters.ValueFilter]
new: Optional[filters.ValueFilter]

Expand All @@ -88,12 +84,8 @@ class SpawningHandler(ResourceHandler):
@dataclasses.dataclass(frozen=True)
class DaemonHandler(SpawningHandler):
fn: callbacks.DaemonFn # typing clarification
cancellation_backoff: Optional[
float
] # how long to wait before actual cancellation.
cancellation_timeout: Optional[
float
] # how long to wait before giving up on cancellation.
cancellation_backoff: Optional[float] # how long to wait before actual cancellation.
cancellation_timeout: Optional[float] # how long to wait before giving up on cancellation.
cancellation_polling: Optional[float] # how often to check for cancellation status.

def __str__(self) -> str:
Expand Down
4 changes: 3 additions & 1 deletion kopf/_core/intents/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ def iter_handlers(
# For deletion, exclude all mutation handlers unless explicitly enabled.
non_mutating = handler.reason != causes.WebhookType.MUTATING
non_deletion = cause.operation != 'DELETE'
explicitly_for_deletion = handler.operations == ['DELETE']
explicitly_for_deletion = (
handler.operation == ['DELETE'] or handler.operation == 'DELETE'
)
if non_mutating or non_deletion or explicitly_for_deletion:
# Filter by usual criteria: labels, annotations, fields, callbacks.
if match(handler=handler, cause=cause):
Expand Down
8 changes: 4 additions & 4 deletions kopf/on.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def validate( # lgtm[py/similar-function]
# Handler's behaviour specification:
id: Optional[str] = None,
param: Optional[Any] = None,
operations: Optional[reviews.Operations] = None, # -> .webhooks.*.rules.*.operations[0]
operation: Optional[reviews.Operations | reviews.Operation] = None,
subresource: Optional[str] = None, # -> .webhooks.*.rules.*.resources[]
persistent: Optional[bool] = None,
side_effects: Optional[bool] = None, # -> .webhooks.*.sideEffects
Expand Down Expand Up @@ -186,7 +186,7 @@ def decorator( # lgtm[py/similar-function]
errors=None, timeout=None, retries=None, backoff=None, # TODO: add some meaning later
selector=selector, labels=labels, annotations=annotations, when=when,
field=real_field, value=value,
reason=causes.WebhookType.VALIDATING, operations=operations, subresource=subresource,
reason=causes.WebhookType.VALIDATING, operation=operation, subresource=subresource,
persistent=persistent, side_effects=side_effects, ignore_failures=ignore_failures,
)
real_registry._webhooks.append(handler)
Expand All @@ -210,7 +210,7 @@ def mutate( # lgtm[py/similar-function]
# Handler's behaviour specification:
id: Optional[str] = None,
param: Optional[Any] = None,
operations: Optional[reviews.Operations] = None, # -> .webhooks.*.rules.*.operations[0]
operation: Optional[reviews.Operations | reviews.Operation] = None,
subresource: Optional[str] = None, # -> .webhooks.*.rules.*.resources[]
persistent: Optional[bool] = None,
side_effects: Optional[bool] = None, # -> .webhooks.*.sideEffects
Expand Down Expand Up @@ -243,7 +243,7 @@ def decorator( # lgtm[py/similar-function]
errors=None, timeout=None, retries=None, backoff=None, # TODO: add some meaning later
selector=selector, labels=labels, annotations=annotations, when=when,
field=real_field, value=value,
reason=causes.WebhookType.MUTATING, operations=operations, subresource=subresource,
reason=causes.WebhookType.MUTATING, operation=operation, subresource=subresource,
persistent=persistent, side_effects=side_effects, ignore_failures=ignore_failures,
)
real_registry._webhooks.append(handler)
Expand Down
10 changes: 6 additions & 4 deletions tests/admission/test_managed_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ def fn(**_):

@pytest.mark.parametrize('opts, key, val', [
(dict(), 'operations', ['*']),
(dict(operations=['CREATE']), 'operations', ['CREATE']),
(dict(operations=['UPDATE']), 'operations', ['UPDATE']),
(dict(operations=['DELETE']), 'operations', ['DELETE']),
(dict(operations=['CREATE','UPDATE']), 'operations', ['CREATE','UPDATE']),
(dict(operation='CREATE'), 'operations', ['CREATE']),
(dict(operation='UPDATE'), 'operations', ['UPDATE']),
(dict(operation='DELETE'), 'operations', ['DELETE']),
(dict(operation=['CREATE','UPDATE']), 'operations', ['CREATE','UPDATE']),
(dict(operation=['CREATE','DELETE']), 'operations', ['CREATE','DELETE']),
(dict(operation=['UPDATE','DELETE']), 'operations', ['UPDATE','DELETE']),
])
@pytest.mark.parametrize('decorator', [kopf.on.validate, kopf.on.mutate])
def test_rule_options_are_mapped(registry, resource, decorator, opts, key, val):
Expand Down
2 changes: 1 addition & 1 deletion tests/admission/test_serving_handler_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async def test_mutating_handlers_are_selected_for_deletion_if_explicitly_marked(
def v_fn(**kwargs):
v_mock(**kwargs)

@kopf.on.mutate(*resource, operations=['DELETE'])
@kopf.on.mutate(*resource, operation='DELETE')
def m_fn(**kwargs):
m_mock(**kwargs)

Expand Down

0 comments on commit a4deab1

Please sign in to comment.