Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Use memo for arbitrary per-resource payload during operator lifetime #234

Merged
merged 3 commits into from
Nov 14, 2019

Conversation

nolar
Copy link
Contributor

@nolar nolar commented Nov 14, 2019

Issue : #112

Description

Since the per-resource in-memory runtime-only containers are already present in the core for internal purposes, it is easy to expose memo kwarg to the handlers, so that the can store arbitrary information or objects about resources.

Same as for the internal use, this should be the objects that are scoped to the operator life time, fully lost on restarts, i.e. not persistent. E.g. threads, tasks, locks, events. Maybe some regular scalars, if their meaning is also scoped to the operator process.

Example:

import kopf

@kopf.on.create('zalando.org', 'v1', 'kopfexamples', cooldown=10)
def create_fn_1(memo, **kwargs):
    print(f'{memo.xyz}')

@kopf.on.create('zalando.org', 'v1', 'kopfexamples')
def create_fn_2(memo, **kwargs):
    memo.xyz = 100

In this example, create_fn_1 raises AttributeError on the 1st attempt, then goes to create_fn_2, and then retries create_fn_1 in 10 seconds with the .xyz field already set.

Both dictionary and object interfaces are supported.

The documentation is intentionally limited — let's first see how it will be used, and what can be the examples.

Types of Changes

  • New feature (non-breaking change which adds functionality)

Review

List of tasks the reviewer must do to review the PR

  • Tests
  • Documentation

@nolar nolar added the enhancement New feature or request label Nov 14, 2019
@nolar nolar requested a review from samurang87 as a code owner November 14, 2019 12:48
@zincr
Copy link

zincr bot commented Nov 14, 2019

🤖 zincr found 0 problems , 0 warnings

✅ Large Commits
✅ Approvals
✅ Specification
✅ Dependency Licensing

@zincr
Copy link

zincr bot commented Nov 14, 2019

🤖 zincr found 1 problem , 0 warnings

❌ Approvals
✅ Large Commits
✅ Specification
✅ Dependency Licensing

Details on how to resolve are provided below


Approvals

All proposed changes must be reviewed by project maintainers before they can be merged

Not enough people have approved this pull request - please ensure that 1 additional user, who have not contributed to this pull request approve the changes.

  • ✅ Approved by PR author @nolar
  • ❌ 1 additional approval needed
     

@nolar nolar requested review from dneuhaeuser-zalando and removed request for samurang87 November 14, 2019 12:50
try:
del self[key]
except KeyError as e:
raise AttributeError(str(e))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't del self[key] call self.__delitem__(key)? Could it be that you intended to implement __delattr__? If not, I do think that implementing the latter would be a good idea for consistency reasons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. That was __delattr__, of course. Fixed. And few more tests added for deletion of the attrs & keys.

@nolar nolar merged commit 1c45fc2 into zalando-incubator:master Nov 14, 2019
@nolar nolar deleted the 112-user-memos branch November 14, 2019 13:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants