Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carefully use a status context on actions #362

Merged
merged 3 commits into from
Aug 19, 2024

Conversation

addyess
Copy link
Member

@addyess addyess commented Aug 16, 2024

Overview

Addresses LP#2077189

Charm actions shouldn't use status.context because all that happens is that it ignores all exceptions and sets the unit status to Active/Idle

But the upgrade action has to use it since kubernetes_snaps depends on using the status.add method, so we have to carefully manage it

Details

  • A bunch of files are touched b/c ruff wants to test things harder.
  • The LP bug was discovered by @pedrofragola -- thanks so much
  • But it turns out that using the status.context wipes out the charm's unit.status
    • if status.context is used outside of a reconciled action, then we must run the reconciler again
    • MOST IMPORTANTLY -- we don't need WIPE out a WaitingStatus or BlockedStatus just because we ran an action. That's kinda bonkers

Copy link
Member

@mateoflorido mateoflorido left a comment

Choose a reason for hiding this comment

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

LGTM!

@addyess addyess changed the title Don't use a status context on actions Carefully use a status context on actions Aug 16, 2024
Copy link
Contributor

@kwmonroe kwmonroe left a comment

Choose a reason for hiding this comment

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

Thanks @addyess. ruff bits were a bit rough to absorb in context, but it's an overall better charm for it. Invoking the reconciler after a successful active action is 💯

I appreciate the deep dive into solving this upgrade ux; props to Pedro for helping get to the root cause!

kubernetes_snaps.upgrade_snaps(channel=channel, event=event, control_plane=True)
if isinstance(charm.unit.status, ops.ActiveStatus):
# After successful upgrade, reconcile the charm to ensure it is in the desired state
charm.reconciler.reconcile(event)
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

Comment on lines -8 to +18
with status.context(event.framework.model.unit):
kubernetes_snaps.service_restart("snap.kube-apiserver.daemon")
event.set_results({"api-server": {"status": "restarted"}})
kubernetes_snaps.service_restart("snap.kube-controller-manager.daemon")
event.set_results({"controller-manager": {"status": "restarted"}})
kubernetes_snaps.service_restart("snap.kube-scheduler.daemon")
event.set_results({"kube-scheduler": {"status": "restarted"}})
try:
for service, snap in SERVICES.items():
kubernetes_snaps.service_restart(snap)
event.set_results({service: {"status": "restarted"}})
except Exception as e:
event.fail(str(e))
Copy link
Member Author

Choose a reason for hiding this comment

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

@kwmonroe in case you missed it -- this runs this action in a try/catch rather than using status.context which would then require us to reconcile again. I think this is a better approach to what the action intends

@@ -132,6 +134,7 @@ def __init__(self, *args):

# register charm actions
actions = [
self.on.restart_action,
Copy link
Member Author

Choose a reason for hiding this comment

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

This actually REGISTERS the restart action -- it wasn't ever handled by the charm code !?!

@addyess addyess merged commit 659d4d0 into main Aug 19, 2024
6 of 7 checks passed
@addyess addyess deleted the bugs/lp2077189/actions-shouldnt-clear-unit-status branch August 19, 2024 21:22
addyess added a commit that referenced this pull request Aug 20, 2024
* Don't use a status context on actions
* Giant batch of ruff reformatting
* Upgrade-action must use a context then reconcile if successful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants