canonical / cos-configuration-k8s-operator

https://charmhub.io/cos-configuration-k8s
Apache License 2.0
4 stars 6 forks source link

Gracefully handle charm initialization when storage not attached #108

Closed theoctober19th closed 1 month ago

theoctober19th commented 1 month ago

Issue

When deploying cos-configuration-k8s in a bundle, the tests are very flaky and often fail because cos-configuration-k8s goes to "hook failed: install" state. This is caused by trying to access self.model.storages["content-from-git"][0].location from the charm initialization code when the storage has not yet been attached.

https://github.com/canonical/cos-configuration-k8s-operator/issues/84

Solution

When the charm is initialized, we try to access the storage, and if it is not yet available, return gracefully.

Context

This was faced during deploying cos-configuration-k8s in a bundle, where tests would fail if charm hooks were fired before storage was attached.

For the solution, I noticed a few lines earlier we check:

if len(self.model.storages["content-from-git"]) == 0:
    return

However, even if this check passes, the self.model.storages["content-from-git"][0].location may still fail, and I've added an a similar logic to check to detect this and return.

Testing Instructions