Skip to content

Commit 1bfd8db

Browse files
committed
rename update_termination_grace_period + close all k8s clients
1 parent a31f73c commit 1bfd8db

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/charm.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -714,13 +714,14 @@ def _on_webhook_mutator_pebble_ready(self, event) -> None:
714714
generate_mutating_webhook(
715715
client, self.unit, self.model.name, cert, self.mutator_service_name
716716
)
717+
client.close()
717718

718719
# We must ensure that juju does not overwrite our termination period, so we should update
719720
# it as needed. However, updating the termination period can result in an onslaught of
720721
# events, including the upgrade event. To prevent this from messing with upgrades do not
721722
# update the termination period when an upgrade is occurring.
722723
if self.get_current_termination_period() != ONE_YEAR and not self.upgrade_in_progress:
723-
self.update_termination_grace_period(ONE_YEAR)
724+
self.update_termination_grace_period_to_one_year()
724725

725726
def _configure_layers(self, container: Container) -> None:
726727
"""Configure the layers of the container."""
@@ -988,6 +989,7 @@ def get_current_termination_period(self) -> int:
988989
"""Returns the current termination period for the stateful set of this juju application."""
989990
client = Client()
990991
statefulset = client.get(StatefulSet, name=self.app.name, namespace=self.model.name)
992+
client.close()
991993
return statefulset.spec.template.spec.terminationGracePeriodSeconds
992994

993995
def get_termination_period_for_pod(self) -> int:
@@ -996,9 +998,10 @@ def get_termination_period_for_pod(self) -> int:
996998
client = Client()
997999
pod = client.get(Pod, name=pod_name, namespace=self.model.name)
9981000
termination_grace_period = pod.spec.terminationGracePeriodSeconds
1001+
client.close()
9991002
return termination_grace_period
10001003

1001-
def update_termination_grace_period(self, seconds: int) -> None:
1004+
def update_termination_grace_period_to_one_year(self) -> None:
10021005
"""Patch the termination grace period for the stateful set of this juju application."""
10031006
client = Client()
10041007
patch_data = {
@@ -1018,6 +1021,7 @@ def update_termination_grace_period(self, seconds: int) -> None:
10181021
)
10191022
# Works because we're leader.
10201023
self.needs_new_termination_period = False
1024+
client.close()
10211025

10221026
def __handle_partition_on_stop(self) -> None:
10231027
"""Raise partition to prevent other units from restarting if an upgrade is in progress.
@@ -1187,7 +1191,7 @@ def _handle_termination(self):
11871191
return
11881192
try:
11891193
if self.get_current_termination_period() != ONE_YEAR and not self.upgrade_in_progress:
1190-
self.update_termination_grace_period(ONE_YEAR)
1194+
self.update_termination_grace_period_to_one_year()
11911195
except ApiError:
11921196
logger.info("Failed to update termination period.")
11931197
return

tests/unit/test_charm.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_mongod_pebble_ready(self, connect_exporter, fix_data_dir, defer, pull_l
135135

136136
@patch("charm.gen_certificate", return_value=(b"", b""))
137137
@patch("charm.MongoDBCharm.get_current_termination_period")
138-
@patch("charm.MongoDBCharm.update_termination_grace_period")
138+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
139139
@patch("ops.framework.EventBase.defer")
140140
@patch("charm.MongoDBCharm._push_keyfile_to_workload")
141141
def test_pebble_ready_cannot_retrieve_container(
@@ -163,7 +163,7 @@ def test_pebble_ready_cannot_retrieve_container(
163163
@patch("charm.gen_certificate", return_value=(b"", b""))
164164
@patch("charm.gen_certificate", return_value=(b"", b""))
165165
@patch("charm.MongoDBCharm.get_current_termination_period")
166-
@patch("charm.MongoDBCharm.update_termination_grace_period")
166+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
167167
@patch("ops.framework.EventBase.defer")
168168
@patch("charm.MongoDBCharm._push_keyfile_to_workload")
169169
def test_pebble_ready_container_cannot_connect(self, push_keyfile_to_workload, defer, *unused):
@@ -230,7 +230,7 @@ def test_pebble_ready_no_storage_yet(self, defer):
230230

231231
@patch("charm.gen_certificate", return_value=(b"", b""))
232232
@patch("charm.MongoDBCharm.get_current_termination_period")
233-
@patch("charm.MongoDBCharm.update_termination_grace_period")
233+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
234234
@patch("ops.framework.EventBase.defer")
235235
@patch("charm.MongoDBProvider")
236236
@patch("charm.MongoDBCharm._init_operator_user")
@@ -262,7 +262,7 @@ def test_start_cannot_retrieve_container(
262262

263263
@patch("charm.gen_certificate", return_value=(b"", b""))
264264
@patch("charm.MongoDBCharm.get_current_termination_period")
265-
@patch("charm.MongoDBCharm.update_termination_grace_period")
265+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
266266
@patch("ops.framework.EventBase.defer")
267267
@patch("charm.MongoDBProvider")
268268
@patch("charm.MongoDBCharm._init_operator_user")
@@ -292,7 +292,7 @@ def test_start_container_cannot_connect(self, connection, init_user, provider, d
292292

293293
@patch("charm.gen_certificate", return_value=(b"", b""))
294294
@patch("charm.MongoDBCharm.get_current_termination_period")
295-
@patch("charm.MongoDBCharm.update_termination_grace_period")
295+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
296296
@patch("ops.framework.EventBase.defer")
297297
@patch("charm.MongoDBProvider")
298298
@patch("charm.MongoDBCharm._init_operator_user")
@@ -323,7 +323,7 @@ def test_start_container_does_not_exist(self, connection, init_user, provider, d
323323

324324
@patch("charm.gen_certificate", return_value=(b"", b""))
325325
@patch("charm.MongoDBCharm.get_current_termination_period")
326-
@patch("charm.MongoDBCharm.update_termination_grace_period")
326+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
327327
@patch("charm.MongoDBCharm._configure_container", return_value=None)
328328
@patch("ops.framework.EventBase.defer")
329329
@patch("charm.MongoDBProvider")
@@ -355,7 +355,7 @@ def test_start_container_exists_fails(self, connection, init_user, provider, def
355355
defer.assert_not_called()
356356

357357
@patch("charm.MongoDBCharm.get_current_termination_period")
358-
@patch("charm.MongoDBCharm.update_termination_grace_period")
358+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
359359
@patch("charm.MongoDBCharm._configure_container", return_value=None)
360360
@patch("charm.gen_certificate", return_value=(b"", b""))
361361
@patch("ops.framework.EventBase.defer")
@@ -389,7 +389,7 @@ def test_start_already_initialised(self, connection, init_user, provider, defer,
389389

390390
@patch("charm.gen_certificate", return_value=(b"", b""))
391391
@patch("charm.MongoDBCharm.get_current_termination_period")
392-
@patch("charm.MongoDBCharm.update_termination_grace_period")
392+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
393393
@patch("ops.framework.EventBase.defer")
394394
@patch("charm.MongoDBProvider")
395395
@patch("charm.MongoDBCharm._init_operator_user")
@@ -422,7 +422,7 @@ def test_start_mongod_not_ready(self, connection, init_user, provider, defer, *u
422422
defer.assert_called()
423423

424424
@patch("charm.MongoDBCharm.get_current_termination_period")
425-
@patch("charm.MongoDBCharm.update_termination_grace_period")
425+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
426426
@patch("charm.gen_certificate", return_value=(b"", b""))
427427
@patch("ops.framework.EventBase.defer")
428428
@patch("charm.MongoDBProvider")
@@ -455,7 +455,7 @@ def test_start_mongod_error_initalising_replica_set(
455455

456456
@patch("charm.gen_certificate", return_value=(b"", b""))
457457
@patch("charm.MongoDBCharm.get_current_termination_period")
458-
@patch("charm.MongoDBCharm.update_termination_grace_period")
458+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
459459
@patch("ops.framework.EventBase.defer")
460460
@patch("charm.MongoDBProvider")
461461
@patch("charm.MongoDBCharm._init_operator_user")
@@ -487,7 +487,7 @@ def test_error_initialising_users(self, connection, init_user, provider, defer,
487487
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
488488

489489
@patch("charm.MongoDBCharm.get_current_termination_period")
490-
@patch("charm.MongoDBCharm.update_termination_grace_period")
490+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
491491
@patch("charm.MongoDBCharm._init_operator_user")
492492
@patch("charm.gen_certificate", return_value=(b"", b""))
493493
@patch("ops.framework.EventBase.defer")
@@ -702,7 +702,7 @@ def test_reconfigure_add_member_failure(self, connection, defer, *unused):
702702
defer.assert_called()
703703

704704
@patch("charm.MongoDBCharm.get_current_termination_period")
705-
@patch("charm.MongoDBCharm.update_termination_grace_period")
705+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
706706
@patch("charm.MongoDBCharm._configure_container", return_value=None)
707707
@patch("charm.gen_certificate", return_value=(b"", b""))
708708
@patch("ops.framework.EventBase.defer")
@@ -1077,7 +1077,7 @@ def test__connect_mongodb_exporter_success(
10771077
self.assertEqual(expected_uri, new_uri)
10781078

10791079
@patch("charm.MongoDBCharm.get_current_termination_period")
1080-
@patch("charm.MongoDBCharm.update_termination_grace_period")
1080+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
10811081
@patch("tenacity.nap.time.sleep", MagicMock())
10821082
@patch("charm.USER_CREATING_MAX_ATTEMPTS", 1)
10831083
@patch("charm.USER_CREATION_COOLDOWN", 1)
@@ -1105,7 +1105,7 @@ def test_backup_user_created(self, *unused):
11051105

11061106
@patch("charm.gen_certificate", return_value=(b"", b""))
11071107
@patch("charm.MongoDBCharm.get_current_termination_period")
1108-
@patch("charm.MongoDBCharm.update_termination_grace_period")
1108+
@patch("charm.MongoDBCharm.update_termination_grace_period_to_one_year")
11091109
@patch("charm.MongoDBConnection")
11101110
def test_set_password_provided(self, *unused):
11111111
"""Tests that a given password is set as the new mongodb password for backup user."""

0 commit comments

Comments
 (0)