Skip to content

Commit e7422b6

Browse files
committed
fix: refacto + post-rebase issues
1 parent 8e61e97 commit e7422b6

File tree

4 files changed

+26
-63
lines changed

4 files changed

+26
-63
lines changed

src/charm.py

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@
9191
MissingSecretError,
9292
NotConfigServerError,
9393
)
94+
from gen_cert import gen_certificate
95+
from service_manager import SERVICE_NAME, generate_mutating_webhook, generate_service
9496
from upgrades import kubernetes_upgrades
9597
from upgrades.mongodb_upgrades import MongoDBUpgrade
96-
from gen_cert import gen_certificate
97-
from service_manager import generate_mutating_webhook, generate_service
9898

9999
logger = logging.getLogger(__name__)
100100

@@ -639,21 +639,17 @@ def _filesystem_handler(self, container: Container) -> None:
639639
# BEGIN: charm events
640640
def _on_mongod_pebble_ready(self, event) -> None:
641641
"""Configure MongoDB pebble layer specification."""
642-
# Get a reference the container attribute
643642
container = self.unit.get_container(Config.CONTAINER_NAME)
644-
if not container.can_connect():
645-
logger.debug("mongod container is not ready yet.")
646-
event.defer()
647-
return
648643

649-
# We need to check that the storages are attached before starting the services.
650-
# pebble-ready is not guaranteed to run after storage-attached so this check allows
651-
# to ensure that the storages are attached before the pebble-ready hook is run.
652-
if any(not storage for storage in self.model.storages.values()):
653-
logger.debug("Storages are not attached yet")
644+
# Just run the configure layers steps on the container and defer if it fails.
645+
try:
646+
self._configure_container(container)
647+
except ContainerNotReadyError:
654648
event.defer()
655649
return
656650

651+
self.upgrade._reconcile_upgrade(event)
652+
657653
# BEGIN: charm events
658654
def _on_webhook_mutator_pebble_ready(self, event) -> None:
659655
# still need todo use lightkube register the mutating webhook with
@@ -665,8 +661,8 @@ def _on_webhook_mutator_pebble_ready(self, event) -> None:
665661
event.defer()
666662
return
667663

668-
cert = self.get_secret(APP_SCOPE, "webhook-certificate")
669-
private_key = self.get_secret(APP_SCOPE, "webhook-key")
664+
cert = self.get_secret(APP_SCOPE, Config.WebhookManager.CRT_SECRET)
665+
private_key = self.get_secret(APP_SCOPE, Config.WebhookManager.KEY_SECRET)
670666

671667
if not cert or not private_key:
672668
logger.debug("Waiting for certificates")
@@ -688,24 +684,6 @@ def _on_webhook_mutator_pebble_ready(self, event) -> None:
688684
generate_service(client, self.unit, self.model.name)
689685
generate_mutating_webhook(client, self.unit, self.model.name, cert)
690686

691-
def _on_mongod_pebble_ready(self, event) -> None:
692-
"""Configure MongoDB pebble layer specification."""
693-
# Get a reference the container attribute
694-
container = self.unit.get_container(Config.CONTAINER_NAME)
695-
if not container.can_connect():
696-
logger.debug("mongod container is not ready yet.")
697-
event.defer()
698-
return
699-
700-
# We need to check that the storages are attached before starting the services.
701-
# pebble-ready is not guaranteed to run after storage-attached so this check allows
702-
# to ensure that the storages are attached before the pebble-ready hook is run.
703-
if any(not storage for storage in self.model.storages.values()):
704-
logger.debug("Storages are not attached yet")
705-
event.defer()
706-
return
707-
708-
709687
def _configure_layers(self, container: Container) -> None:
710688
"""Configure the layers of the container."""
711689
modified = False
@@ -788,19 +766,6 @@ def _on_upgrade(self, event: UpgradeCharmEvent) -> None:
788766
# Post upgrade event verifies the success of the upgrade.
789767
self.upgrade.post_app_upgrade_event.emit()
790768

791-
def _on_mongod_pebble_ready(self, event) -> None:
792-
"""Configure MongoDB pebble layer specification."""
793-
container = self.unit.get_container(Config.CONTAINER_NAME)
794-
795-
# Just run the configure layers steps on the container and defer if it fails.
796-
try:
797-
self._configure_container(container)
798-
except ContainerNotReadyError:
799-
event.defer()
800-
return
801-
802-
self.upgrade._reconcile_upgrade(event)
803-
804769
def is_db_service_ready(self) -> bool:
805770
"""Checks if the MongoDB service is ready to accept connections."""
806771
with MongoDBConnection(self.mongodb_config, "localhost", direct=True) as direct_mongo:
@@ -892,12 +857,12 @@ def _on_start(self, event: StartEvent) -> None:
892857
if not self.unit.is_leader():
893858
return
894859

895-
if not self.get_secret(APP_SCOPE, "webhook-certificate") or not self.get_secret(
896-
APP_SCOPE, "webhook-key"
860+
if not self.get_secret(APP_SCOPE, Config.WebhookManager.CRT_SECRET) or not self.get_secret(
861+
APP_SCOPE, Config.WebhookManager.KEY_SECRET
897862
):
898-
cert, key = gen_certificate(Config.WebhookManager.SERVICE_NAME, self.model.name)
899-
self.set_secret(APP_SCOPE, "webhook-certificate", cert.decode())
900-
self.set_secret(APP_SCOPE, "webhook-key", key.decode())
863+
cert, key = gen_certificate(SERVICE_NAME, self.model.name)
864+
self.set_secret(APP_SCOPE, Config.WebhookManager.CRT_SECRET, cert.decode())
865+
self.set_secret(APP_SCOPE, Config.WebhookManager.KEY_SECRET, key.decode())
901866
self._initialise_replica_set(event)
902867
try:
903868
self._initialise_users(event)
@@ -1041,7 +1006,7 @@ def _on_stop(self, event) -> None:
10411006
client.delete(
10421007
MutatingWebhookConfiguration,
10431008
namespace=self.model.name,
1044-
name=Config.WebhookManager.SERVICE_NAME,
1009+
name=SERVICE_NAME,
10451010
)
10461011
self.__handle_partition_on_stop()
10471012
if self.unit_departed:

src/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ class WebhookManager:
162162
PORT = 8000
163163
CRT_PATH = "/app/certificate.crt"
164164
KEY_PATH = "/app/certificate.key"
165+
CRT_SECRET = "webhook-certificate"
166+
KEY_SECRET = "webhook-key"
165167

166168
@staticmethod
167169
def get_license_path(license_name: str) -> str:

src/service_manager.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
logger = getLogger()
2525

26+
SERVICE_NAME = f"{Config.WebhookManager.SERVICE_NAME}-{Config.WebhookManager.CONTAINER_NAME}"
27+
2628

2729
def get_pod(client: Client, pod_name: str) -> Pod:
2830
"""Gets a pod definition from k8s."""
@@ -43,7 +45,7 @@ def generate_service(client: Client, unit: Unit, model_name: str):
4345
try:
4446
service = Service(
4547
metadata=ObjectMeta(
46-
name=Config.WebhookManager.SERVICE_NAME,
48+
name=SERVICE_NAME,
4749
namespace=model_name,
4850
ownerReferences=[
4951
OwnerReference(
@@ -63,7 +65,7 @@ def generate_service(client: Client, unit: Unit, model_name: str):
6365
protocol="TCP",
6466
port=Config.WebhookManager.PORT,
6567
targetPort=Config.WebhookManager.PORT,
66-
name=f"{Config.WebhookManager.SERVICE_NAME}-port",
68+
name=f"{SERVICE_NAME}-port",
6769
),
6870
],
6971
),
@@ -82,7 +84,7 @@ def generate_mutating_webhook(client: Client, unit: Unit, model_name: str, cert:
8284
webhooks = client.get(
8385
MutatingWebhookConfiguration,
8486
namespace=model_name,
85-
name=Config.WebhookManager.SERVICE_NAME,
87+
name=SERVICE_NAME,
8688
)
8789
if webhooks:
8890
return
@@ -94,7 +96,7 @@ def generate_mutating_webhook(client: Client, unit: Unit, model_name: str, cert:
9496
logger.debug("Registering our Mutating Wehook.")
9597
webhook_config = MutatingWebhookConfiguration(
9698
metadata=ObjectMeta(
97-
name=Config.WebhookManager.SERVICE_NAME,
99+
name=SERVICE_NAME,
98100
namespace=model_name,
99101
ownerReferences=pod.metadata.ownerReferences,
100102
),
@@ -105,7 +107,7 @@ def generate_mutating_webhook(client: Client, unit: Unit, model_name: str, cert:
105107
clientConfig=WebhookClientConfig(
106108
service=ServiceReference(
107109
namespace=model_name,
108-
name=Config.WebhookManager.SERVICE_NAME,
110+
name=SERVICE_NAME,
109111
port=8000,
110112
path="/mutate",
111113
),

tests/integration/backup_tests/test_backups.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,11 @@ async def test_restore_new_cluster(
403403
# deploy a new cluster with a different name
404404
db_charm = await ops_test.build_charm(".")
405405
await ops_test.model.deploy(
406-
<<<<<<< HEAD
407406
db_charm,
408407
num_units=3,
409-
resources=resources,
408+
resources=RESOURCES,
410409
application_name=new_cluster_app_name,
411410
trust=True,
412-
||||||| parent of 0ded9b66 (wip: test part)
413-
db_charm, num_units=3, resources=resources, application_name=new_cluster_app_name
414-
=======
415-
db_charm, num_units=3, resources=RESOURCES, application_name=new_cluster_app_name
416-
>>>>>>> 0ded9b66 (wip: test part)
417411
)
418412

419413
await asyncio.gather(

0 commit comments

Comments
 (0)