Skip to content

docs: grab workload version in k8s tutorial#2559

Draft
tromai wants to merge 8 commits into
canonical:mainfrom
tromai:grab-workload-version-in-k8s-tutorial
Draft

docs: grab workload version in k8s tutorial#2559
tromai wants to merge 8 commits into
canonical:mainfrom
tromai:grab-workload-version-in-k8s-tutorial

Conversation

@tromai

@tromai tromai commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Closes #2282.

I open this as a draft for an initial review of the structure and direction of this update. My changes now are for the first tutorial chapter: creating a minimal k8s charm. After the content is finalized, I will port them to the remaining chapters.

@tromai tromai self-assigned this Jun 10, 2026
tromai added 3 commits June 11, 2026 17:02
port the grab workfload version code to tutorial 2->5 and use auto fixture for patching get_version in unit tests

and also change localhost -> 0.0.0.0 in the fastapi_demo.py modules in examples
return "1.0.4"


@pytest.fixture(autouse=True)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's avoid autouse fixtures unless absolutely necessary, as they can be quite confusing action-at-a-distance. Something like:

@pytest.fixture
def fake_version(monkeypath):
    monkeypath.setattr(...)

def test_pebble_layer(fake_version):
    ...

Comment on lines +243 to +256
def _on_demo_server_pebble_ready(self, event: ops.PebbleReadyEvent) -> None:
"""Define and start a workload using the Pebble API."""
# Get a reference the container attribute on the PebbleReadyEvent
container = event.workload
# Add initial Pebble config layer using the Pebble API
container.add_layer("fastapi_demo", self._get_pebble_layer(), combine=True)
# Make Pebble reevaluate its plan, ensuring any services are started if enabled.
container.replan()
# Set the workload version of this charm.
version = fastapi_demo.get_version(port=8000)
self.unit.set_workload_version(version)
# Learn more about statuses at
# https://documentation.ubuntu.com/juju/3.6/reference/status/
self.unit.status = ops.ActiveStatus()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It's hard to tell what's being added here. Could we consider something like this?

Suggested change
def _on_demo_server_pebble_ready(self, event: ops.PebbleReadyEvent) -> None:
"""Define and start a workload using the Pebble API."""
# Get a reference the container attribute on the PebbleReadyEvent
container = event.workload
# Add initial Pebble config layer using the Pebble API
container.add_layer("fastapi_demo", self._get_pebble_layer(), combine=True)
# Make Pebble reevaluate its plan, ensuring any services are started if enabled.
container.replan()
# Set the workload version of this charm.
version = fastapi_demo.get_version(port=8000)
self.unit.set_workload_version(version)
# Learn more about statuses at
# https://documentation.ubuntu.com/juju/3.6/reference/status/
self.unit.status = ops.ActiveStatus()
def _on_demo_server_pebble_ready(self, event: ops.PebbleReadyEvent) -> None:
# ... previous lines ...
# Set the workload version of this charm.
version = fastapi_demo.get_version(port=8000)
self.unit.set_workload_version(version)

# version. If the workload has an API or some other way of getting the
# version, the test should get it from there and use that to compare to the
# unit setting.
assert version == "1.0.4"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider using something a bit more general, to avoid updates if the demo-server is updated with a minor or patch release:

Suggested change
assert version == "1.0.4"
assert version.startswith("1.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grab workload version in K8s tutorial

3 participants