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

By hashing the config values and files, only restart services on differences #31

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"


Comment on lines +481 to +486
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 allows the charm at runtime (per hook even!!!) decide whether or not it wants to enable hashing.

Now an upgrade action could restart the service even if there aren't any config changes. We could even expose via charm config (though i think that'd be going too far).

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
Comment on lines +519 to +523
Copy link
Member Author

Choose a reason for hiding this comment

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

if hashing isn't enabled, this method declares there are changes, and we should reconfigure the service (as before this feature existed) and it also clears the cache file if one exists.


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