Skip to content

Commit 454c1f8

Browse files
feat: raise on patching charm container (#129)
* raise on patching charm container * fix tes * add error message for resource patch conflict * ignored complexity issue on _patch * remove override * statix fix
1 parent 089cf10 commit 454c1f8

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

lib/charms/observability_libs/v0/kubernetes_compute_resources_patch.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def setUp(self, *unused):
149149

150150
# Increment this PATCH version before using `charmcraft publish-lib` or reset
151151
# to 0 if you are raising the major API version
152-
LIBPATCH = 8
152+
LIBPATCH = 9
153153

154154

155155
_Decimal = Union[Decimal, float, str, int] # types that are potentially convertible to Decimal
@@ -584,6 +584,14 @@ def __init__(
584584
will be observed to re-apply the patch.
585585
"""
586586
super().__init__(charm, "{}_{}".format(self.__class__.__name__, container_name))
587+
588+
if container_name == "charm":
589+
raise ValueError(
590+
"Starting with juju 3.6.9, juju manages the charm container "
591+
"constraints and you'll get errors by doing this if the charm is deployed "
592+
"on higher juju versions."
593+
)
594+
587595
self._charm = charm
588596
self._container_name = container_name
589597
self.resource_reqs_func = resource_reqs_func
@@ -606,7 +614,7 @@ def __init__(
606614
def _on_config_changed(self, _):
607615
self._patch()
608616

609-
def _patch(self) -> None:
617+
def _patch(self) -> None: # noqa: C901
610618
"""Patch the Kubernetes resources created by Juju to limit cpu or mem.
611619
612620
This method will keep on retrying to patch the kubernetes resource for a default duration of 20 seconds
@@ -657,7 +665,9 @@ def _patch(self) -> None:
657665
except ApiError as e:
658666
if e.status.code == 403:
659667
msg = f"Kubernetes resources patch failed: `juju trust` this application. {e}"
660-
668+
elif e.status.code == 409:
669+
msg = (f"Kubernetes resources patch failed: someone else (likely juju) "
670+
f"owns the resources you're trying to patch {e}")
661671
else:
662672
msg = f"Kubernetes resources patch failed: {e}"
663673

lib/charms/observability_libs/v1/cert_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
LIBID = "b5cd5cd580f3428fa5f59a8876dcbe6a"
4646
LIBAPI = 1
47-
LIBPATCH = 18
47+
LIBPATCH = 19
4848

4949
VAULT_SECRET_LABEL = "cert-handler-private-vault"
5050

@@ -621,7 +621,7 @@ def chain(self) -> Optional[str]:
621621
cert = self.get_cert()
622622
if not cert:
623623
return None
624-
chain = cert.chain_as_pem_string()
624+
chain = cert.chain_as_pem_string() # type: ignore
625625
if cert.certificate not in chain:
626626
# add server cert to chain
627627
chain = cert.certificate + "\n\n" + chain

tests/unit/test_kubernetes_compute_resources.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest.mock import MagicMock, Mock, patch
66

77
import httpx
8+
import pytest
89
import tenacity
910
import yaml
1011
from charms.observability_libs.v0.kubernetes_compute_resources_patch import (
@@ -155,3 +156,28 @@ def test_is_valid_spec(self):
155156

156157
self.assertFalse(is_valid_spec({"bad": "combo"}))
157158
self.assertFalse(is_valid_spec({"invalid-key": "1"}))
159+
160+
161+
@mock.patch(
162+
"charms.observability_libs.v0.kubernetes_compute_resources_patch.ResourcePatcher.apply",
163+
MagicMock(return_value=None),
164+
)
165+
@mock.patch(
166+
"charms.observability_libs.v0.kubernetes_compute_resources_patch.ResourcePatcher.is_failed",
167+
MagicMock(return_value=(False, "")),
168+
)
169+
@mock.patch("lightkube.core.client.GenericSyncClient")
170+
@mock.patch.object(
171+
KubernetesComputeResourcesPatch,
172+
"_namespace",
173+
"test-namespace",
174+
)
175+
def test_patch_charm_container_fails_with_warning(_):
176+
# When we try to patch the charm container we get an error
177+
mm = MagicMock()
178+
with pytest.raises(ValueError):
179+
KubernetesComputeResourcesPatch(
180+
mm,
181+
"charm",
182+
resource_reqs_func=lambda: adjust_resource_requirements(None, None),
183+
)

0 commit comments

Comments
 (0)