An example charm that reproduces the issue:
```
import os
import subprocess
import requests
from ops.charm import CharmBase
from ops.main import main
from ops.model import ActiveStatus
class Operator(CharmBase):
def __init__(self, *args):
super().__init__(*args)
self.framework.observe(self.on.foo_pebble_ready, self.install)
self.framework.observe(self.on.remove, self.remove)
def install(self, event):
response = requests.get('https://dl.k8s.io/release/v1.22.1/bin/linux/amd64/kubectl')
response.raise_for_status()
with open('/bin/kubectl', 'wb') as f:
f.write(response.content)
os.chmod("/bin/kubectl", 0o755)
subprocess.check_call(["kubectl", "get", "pods"])
self.unit.status = ActiveStatus()
def remove(self, event):
subprocess.check_call(["kubectl", "get", "pods"])
if __name__ == "__main__":
main(Operator)
```
Charm deployment works without an issue. However, when removing the charm, I get this error:
```
unit-foo-0: 16:00:00 WARNING unit.foo/0.remove error: You must be logged in to the server (Unauthorized)
unit-foo-0: 16:00:00 ERROR unit.foo/0.juju-log Uncaught exception while in charm code:
Traceback (most recent call last):
File "/var/lib/juju/agents/unit-foo-0/charm/./src/charm.py", line 85, in <module>
main(Operator)
File "/var/lib/juju/agents/unit-foo-0/charm/venv/ops/main.py", line 406, in main
_emit_charm_event(charm, dispatcher.event_name)
File "/var/lib/juju/agents/unit-foo-0/charm/venv/ops/main.py", line 140, in _emit_charm_event
event_to_emit.emit(*args, **kwargs)
File "/var/lib/juju/agents/unit-foo-0/charm/venv/ops/framework.py", line 278, in emit
framework._emit(event)
File "/var/lib/juju/agents/unit-foo-0/charm/venv/ops/framework.py", line 722, in _emit
self._reemit(event_path)
File "/var/lib/juju/agents/unit-foo-0/charm/venv/ops/framework.py", line 767, in _reemit
custom_handler(event)
File "/var/lib/juju/agents/unit-foo-0/charm/./src/charm.py", line 57, in remove
subprocess.check_call(["kubectl", "get", "pods"])
File "/usr/lib/python3.9/subprocess.py", line 373, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['kubectl', 'get', 'pods']' returned non-zero exit status 1.
```
This bug also affects charmed-magma (https:/ /github. com/canonical/ charmed- magma). We need to do manual cleanups of k8s resources created by charms because of it.