Skip to content

Commit

Permalink
enable/disable cache calculation feature with env var
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Sep 6, 2024
1 parent 768eb06 commit c1ebc5e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
13 changes: 13 additions & 0 deletions charms/kubernetes_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ def configure_scheduler(extra_args_config, kubeconfig):
)


def _enable_config_hashing() -> bool:
"""This is a debug option to enable config hashing."""
env = os.environ.get("JUJU_FEATURE_KUBERNETES_SNAP_CONFIG_HASHING") or ""
return env.lower() == "on"


def _sha256_file(config_file: str) -> hashlib.sha256:
h = hashlib.sha256()
h.update(config_file.encode())
Expand Down Expand Up @@ -510,6 +516,13 @@ def _dict_compare(d1, d2):
def _calculate_config_difference(service: str, args, config_files):
args_hash = _snap_common_path(service) / "args-hash.txt"

if not _enable_config_hashing():
log.debug("Config hashing disabled, skipping config change detection")
yield True
args_hash.unlink(missing_ok=True)
return

log.debug("Checking for config changes for %s", service)
# gather existing config hash
current, updated = {}, {}
if Path.is_file(args_hash):
Expand Down
34 changes: 32 additions & 2 deletions tests/unit/test_kubernetes_snaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,36 @@ def test_configure_apiserver(mock_path, configure_kubernetes_service, external_c
@mock.patch("pathlib.Path.is_file", mock.MagicMock(return_value=True))
@mock.patch("charms.kubernetes_snaps.check_call")
@mock.patch("charms.kubernetes_snaps.service_restart")
def test_configure_kubernetes_service_same_config(service_restart, check_call, caplog):
def test_configure_kubernetes_service_no_hashing(
service_restart, check_call, caplog, monkeypatch
):
monkeypatch.setenv("JUJU_FEATURE_KUBERNETES_SNAP_CONFIG_HASHING", "")
caplog.set_level(logging.DEBUG)
log_message = "Config hashing disabled, skipping config change detection"
base_args = {"arg1": "val1", "arg2": "val2"}
extra_args = "arg2=val2-updated arg3=val3"
with mock.patch("pathlib.Path.unlink") as mock_unlink:
kubernetes_snaps.configure_kubernetes_service("test", base_args, extra_args)
service_restart.assert_called_once_with("snap.test.daemon")
check_call.assert_called_once_with(
[
"snap",
"set",
"test",
'args=--arg1="val1" --arg2="val2-updated" --arg3="val3"',
]
)
mock_unlink.assert_called_once_with(missing_ok=True)
assert log_message in caplog.text


@mock.patch("pathlib.Path.is_file", mock.MagicMock(return_value=True))
@mock.patch("charms.kubernetes_snaps.check_call")
@mock.patch("charms.kubernetes_snaps.service_restart")
def test_configure_kubernetes_service_same_config(
service_restart, check_call, caplog, monkeypatch
):
monkeypatch.setenv("JUJU_FEATURE_KUBERNETES_SNAP_CONFIG_HASHING", "on")
caplog.set_level(logging.DEBUG)
log_message = "Test: No config changes detected"
base_args = {"arg1": "val1", "arg2": "val2"}
Expand Down Expand Up @@ -285,8 +314,9 @@ def test_configure_kubernetes_service_same_config(service_restart, check_call, c
ids=["drop_key", "add_key", "update_key"],
)
def test_configure_kubernetes_service_difference(
service_restart, check_call, extra_args, log_message, caplog
service_restart, check_call, extra_args, log_message, caplog, monkeypatch
):
monkeypatch.setenv("JUJU_FEATURE_KUBERNETES_SNAP_CONFIG_HASHING", "on")
caplog.set_level(logging.DEBUG)
base_args = {"arg1": "val1", "arg2": "val2"}
hashed = {
Expand Down

0 comments on commit c1ebc5e

Please sign in to comment.