Method to run an action in a workload container in sidecar charms

Bug #1923822 reported by Tom Haddon
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Canonical Juju
Fix Released
High
Ben Hoyt

Bug Description

Possibly this isn't a bug but me not understanding how this works. I'm doing a proof of concept conversion of the mattermost k8s charm to the sidecar approach, and am testing the `grant-admin-role` action.

This action takes a string parameter of "user" and runs the following:
```
/mattermost/bin/mattermost roles system_admin $user
```
However, when executing it I get the following output:
```
UnitId: mattermost/0
id: "2"
message: exit status 1
results:
  ReturnCode: 1
status: failed
timing:
  completed: 2021-04-14 10:07:42 +0000 UTC
  enqueued: 2021-04-14 10:07:37 +0000 UTC
  started: 2021-04-14 10:07:41 +0000 UTC
```
Checking the logs for the "charm" container I see:
```
microk8s.kubectl logs -n mattermost -c charm mattermost-0
starting containeragent unit command
containeragent unit "unit-mattermost-0" start (2.9-rc9 [gc])
ERROR Uncaught exception while in charm code:
Traceback (most recent call last):
  File "./src/charm.py", line 623, in <module>
    main(MattermostK8sCharm)
  File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/main.py", line 406, in main
    _emit_charm_event(charm, dispatcher.event_name)
  File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/main.py", line 140, in _emit_charm_event
    event_to_emit.emit(*args, **kwargs)
  File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/framework.py", line 278, in emit
    framework._emit(event)
  File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/framework.py", line 722, in _emit
    self._reemit(event_path)
  File "/var/lib/juju/agents/unit-mattermost-0/charm/venv/ops/framework.py", line 767, in _reemit
    custom_handler(event)
  File "./src/charm.py", line 611, in _on_grant_admin_role_action
    granted = subprocess.run(cmd, capture_output=True)
  File "/usr/lib/python3.8/subprocess.py", line 489, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/mattermost/bin/mattermost'
```
This is because the binary in question is in the workload container, not the charm container. Is there a way I'm missing to run the action on the workload container? The function that executes this is as follows:
```
     def _on_grant_admin_role_action(self, event):
         """Handle the grant-admin-role action."""
         user = event.params["user"]
         cmd = ["/mattermost/bin/mattermost", "roles", "system_admin", user]
         granted = subprocess.run(cmd, capture_output=True)
         if granted.returncode != 0:
             event.fail("Failed to run '{}'. Output was:\n{}".format(" ".join(cmd), granted.stderr.decode("utf-8")))
         else:
             msg = (
                 "Ran grant-admin-role for user '{}'. They will need to log out and log back in "
                 "to Mattermost to fully receive their permissions upgrade.".format(user)
             )
             event.set_results({"info": msg})
```

Tom Haddon (mthaddon)
tags: added: sidecar-charm
Revision history for this message
Jon Seager (jnsgruk) wrote :

I think this raises the question of whether we want to run an action in the workload container, or have the ability to execute a command in the workload container.

At least this way, the action itself will run in the charm container, so there is no need for Python + deps in the workload container.

A workaround could include adding a Pebble layer that you start/stop with the command specified, but I wonder if the Pebble API/Operator Framework might be extended later to enable running arbitrary commands in a one-shot fashion in the workload container

Harry Pidcock (hpidcock)
Changed in juju:
status: New → Triaged
importance: Undecided → High
milestone: none → 2.9.1
Revision history for this message
John A Meinel (jameinel) wrote : Re: [Bug 1923822] Re: Method to run an action in a workload container in sidecar charms

I think it makes more sense to have a way to run the action in the sidecar,
but provide a way (via pebble) to execute a command in the workload.

On Wed, Apr 14, 2021 at 6:55 AM Jon Seager <email address hidden>
wrote:

> I think this raises the question of whether we want to run an action in
> the workload container, or have the ability to execute a command in the
> workload container.
>
> At least this way, the action itself will run in the charm container, so
> there is no need for Python + deps in the workload container.
>
> A workaround could include adding a Pebble layer that you start/stop
> with the command specified, but I wonder if the Pebble API/Operator
> Framework might be extended later to enable running arbitrary commands
> in a one-shot fashion in the workload container
>
> --
> You received this bug notification because you are subscribed to juju.
> Matching subscriptions: juju bugs
> https://bugs.launchpad.net/bugs/1923822
>
> Title:
> Method to run an action in a workload container in sidecar charms
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/juju/+bug/1923822/+subscriptions
>

Ian Booth (wallyworld)
Changed in juju:
milestone: 2.9.1 → 2.9.2
Changed in juju:
milestone: 2.9.2 → 2.9.3
John A Meinel (jameinel)
Changed in juju:
milestone: 2.9.3 → 2.9.5
John A Meinel (jameinel)
Changed in juju:
status: Triaged → In Progress
assignee: nobody → Harry Pidcock (hpidcock)
milestone: 2.9.5 → 2.9.6
Revision history for this message
Harry Pidcock (hpidcock) wrote :

JU012 spec looks into how to run a command in a workload container, I think this is a appropriate method to allow actions to interact with the workload.

My reasoning is:
- Actions need an environment (python, bash etc)
- Actions can involve more than one container
- Actions need juju hook tools and a connection to the juju unit

Changed in juju:
assignee: Harry Pidcock (hpidcock) → Ben Hoyt (benhoyt)
milestone: 2.9.6 → 3.0.0
milestone: 3.0.0 → 2.9.6
Changed in juju:
milestone: 2.9.6 → 2.9.7
Changed in juju:
milestone: 2.9.7 → 2.9.8
Changed in juju:
milestone: 2.9.8 → 2.9.9
Changed in juju:
milestone: 2.9.9 → 2.9.10
Changed in juju:
milestone: 2.9.10 → 2.9.11
Changed in juju:
milestone: 2.9.11 → 2.9.12
Changed in juju:
milestone: 2.9.12 → 2.9.13
Changed in juju:
milestone: 2.9.13 → 2.9.14
Changed in juju:
milestone: 2.9.14 → 2.9.15
Changed in juju:
milestone: 2.9.15 → 2.9.16
Changed in juju:
milestone: 2.9.16 → 2.9.17
Changed in juju:
milestone: 2.9.17 → 2.9.18
Revision history for this message
Jon Seager (jnsgruk) wrote :

Fixed in 2.9.17 when one-shot commands became available with the packaged version of Pebble.

Changed in juju:
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.