Skip to content

Commit

Permalink
Remove chapters covered by how-to guides.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Dec 18, 2024
1 parent d2508f8 commit 16bb8bc
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 1,121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you are familiar with Juju, as we assume here, you'll know that, to start us

As you already know from your knowledge of Juju, when you deploy a Kubernetes charm, the following things happen:

1. The Juju controller provisions a pod with two containers, one for the Juju unit agent and the charm itself and one container for each application workload container that is specified in the `containers` field of a file in the charm that is called `charmcraft.yaml`.
1. The Juju controller provisions a pod with at least two containers, one for the Juju unit agent and the charm itself and one container for each application workload container that is specified in the `containers` field of a file in the charm that is called `charmcraft.yaml`.
1. The same Juju controller injects Pebble -- a lightweight, API-driven process supervisor -- into each workload container and overrides the container entrypoint so that Pebble starts when the container is ready.
1. When the Kubernetes API reports that a workload container is ready, the Juju controller informs the charm that the instance of Pebble in that container is ready. At that point, the charm knows that it can start communicating with Pebble.
1. Typically, at this point the charm will make calls to Pebble so that Pebble can configure and start the workload and begin operations.
Expand Down Expand Up @@ -59,12 +59,12 @@ title: |
demo-fastapi-k8s
description: |
This is a demo charm built on top of a small Python FastAPI server.
This charm could be related to PostgreSQL charm and COS Lite bundle (Canonical Observability Stack).
This charm can be related to PostgreSQL charm and COS Lite bundle (Canonical Observability Stack).
summary: |
FastAPI Demo charm for Kubernetes
```

Second, add an environment constraint assuming the latest major Juju version and a Kubernetes-type cloud:
Second, add an environment constraint assuming the Juju version with the desired features and a Kubernetes-type cloud:

```text
assumes:
Expand Down Expand Up @@ -425,7 +425,4 @@ For the full code see: [01_create_minimal_charm](https://github.com/canonical/ju
For a comparative view of the code before and after our edits see:
[Comparison](https://github.com/canonical/juju-sdk-tutorial-k8s/compare/main...01_create_minimal_charm)



>**See next: {ref}`Make your charm configurable <make-your-charm-configurable>`**
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

> <small> {ref}`From Zero to Hero: Write your first Kubernetes charm <from-zero-to-hero-write-your-first-kubernetes-charm>` > Expose operational tasks via actions </small>
>
> **See previous: {ref}`Preserve your charm's data <preserve-your-charms-data>`**
> **See previous: {ref}`Integrate your charm with PostgreSQL <integrate-your-charm-with-postgresql>`**
````{important}
This document is part of a series, and we recommend you follow it in sequence. However, you can also jump straight in by checking out the code from the previous branches:
```text
git clone https://github.com/canonical/juju-sdk-tutorial-k8s.git
cd juju-sdk-tutorial-k8s
git checkout 05_preserve_charm_data
git checkout -b 06_create_actions
git checkout 03_integrate_with_psql
git checkout -b 04_create_actions
```
````
Expand All @@ -28,7 +26,6 @@ This can be done by adding an `actions` section in your `charmcraft.yaml` file a

In this part of the tutorial we will follow this process to add an action that will allow a charm user to view the current database access points and, if set, also the username and the password.


## Define the actions

Open the `charmcraft.yaml` file and add to it a block defining an action, as below. As you can see, the action is called `get-db-info` and it is intended to help the user access database authentication information. The action has a single parameter, `show-password`; if set to `True`, it will show the username and the password.
Expand All @@ -44,45 +41,28 @@ actions:
default: False
```
## Define the action event handlers
Open the `src/charm.py` file.

In the charm `__init__` method, add an action event observer, as below. As you can see, the name of the event consists of the name defined in the `charmcraft.yaml` file (`get-db-info`) and the word `action`.

```python
# events on custom actions that are run via 'juju run-action'
# Events on charm actions that are run via 'juju run'
framework.observe(self.on.get_db_info_action, self._on_get_db_info_action)
```

<!-- I THINK THIS IS IMPLICIT IN THE FACT THAT ACTION EVENT NAMES ARE ACTION-SPECIFIC.
```{important}
In case of multiple actions defined you need to add a separate observer for each.
```
-->



Now, define the action event handler, as below: First, read the value of the parameter defined in the `charmcraft.yaml` file (`show-password`). Then, use the `fetch_postgres_relation_data` method (that we defined in a previous chapter) to read the contents of the database relation data and, if the parameter value read earlier is `True`, add the username and password to the output. Finally, use `event.set_results` to attach the results to the event that has called the action; this will print the output to the terminal.

If we are not able to get the data (for example, if the charm has not yet been integrated with the postgresql-k8s application) then we use the `fail` method of the event to let the user know.

<!--
The action will read the contents of the database relation data with the help of the `fetch_postgres_relation_data` method (that we have defined in previous chapter) and output its content.
As you can see we read the value of the defined parameter `show-password` and only if it is `True` we add username and password to the output.
To print output to the terminal we need to attach results to the event that has called the action via `event.set_results`.
-->

```python
def _on_get_db_info_action(self, event: ops.ActionEvent) -> None:
"""This method is called when "get_db_info" action is called. It shows information about
database access points by calling the `fetch_postgres_relation_data` method and creates
an output dictionary containing the host, port, if show_password is True, then include
username, and password of the database.
If PSQL charm is not integrated, the output is set to "No database connected".
If the postgresql charm is not integrated, the output is set to "No database connected".

Learn more about actions at https://juju.is/docs/sdk/actions
"""
Expand Down Expand Up @@ -117,11 +97,10 @@ juju refresh \
demo-server-image=ghcr.io/canonical/api_demo_server:1.0.1
```


Next, test that the basic action invocation works:

```text
juju run demo-api-charm/0 get-db-info --wait 1m
juju run demo-api-charm/0 get-db-info
```

It might take a few seconds, but soon you should see an output similar to the one below, showing the database host and port:
Expand All @@ -138,7 +117,7 @@ db-port: "5432"
Now, test that the action parameter (`show-password`) works as well by setting it to `True`:

```text
juju run demo-api-charm/0 get-db-info show-password=True --wait 1m
juju run demo-api-charm/0 get-db-info show-password=True
```

The output should now include the username and the password:
Expand All @@ -153,14 +132,12 @@ db-port: "5432"
db-username: relation_id_4
```


Congratulations, you now know how to expose operational tasks via actions!

## Review the final code

For the full code see: [06_create_actions](https://github.com/canonical/juju-sdk-tutorial-k8s/tree/06_create_actions)

For a comparative view of the code before and after this doc see: [Comparison](https://github.com/canonical/juju-sdk-tutorial-k8s/compare/05_preserve_charm_data...06_create_actions)
For the full code see: [04_create_actions](https://github.com/canonical/juju-sdk-tutorial-k8s/tree/04_create_actions)

For a comparative view of the code before and after this doc see: [Comparison](https://github.com/canonical/juju-sdk-tutorial-k8s/compare/03_integrate_with_psql...04_create_actions)

> **See next: {ref}`Observe your charm with COS Lite <observe-your-charm-with-cos-lite>`**

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ The application that we will charm in this tutorial is based on the Python Fast
- Familiarity with the Python programming language, Object-Oriented Programming, event handlers.
- Understanding of Kubernetes fundamentals.


**What you'll do:**

<!--
Expand All @@ -36,15 +35,11 @@ The application that we will charm in this tutorial is based on the Python Fast
- Develop your charm:
1. {ref}`Create a minimal Kubernetes charm <create-a-minimal-kubernetes-charm>`
1. {ref}`Make your charm configurable <make-your-charm-configurable>`
1. {ref}`Expose the version of the application behind your charm <expose-the-version-of-the-application-behind-your-charm>`
1. {ref}`Integrate your charm with PostgreSQL <integrate-your-charm-with-postgresql>`
1. {ref}`Preserve your charm's data <preserve-your-charms-data>`
1. {ref}`Expose your charm's operational tasks via actions <expose-operational-tasks-via-actions>`
1. {ref}`Observe your charm with COS Lite and set up cross-model integrations <observe-your-charm-with-cos-lite>`
1. {ref}`Write unit tests for your charm <write-unit-tests-for-your-charm>`
1. {ref}`Write scenario tests for your charm <write-scenario-tests-for-your-charm>`
1. {ref}`Write integration tests for your charm <write-integration-tests-for-your-charm>`
1. {ref}`Open a Kubernetes port in your charm <open-a-kubernetes-port-in-your-charm>`
1. {ref}`Publish your charm on Charmhub <publish-your-charm-on-charmhub>`
-->

Expand All @@ -56,20 +51,14 @@ set-up-your-development-environment
study-your-application
create-a-minimal-kubernetes-charm
make-your-charm-configurable
expose-the-version-of-the-application-behind-your-charm
integrate-your-charm-with-postgresql
preserve-your-charms-data
expose-operational-tasks-via-actions
observe-your-charm-with-cos-lite
write-unit-tests-for-your-charm
write-scenario-tests-for-your-charm
write-integration-tests-for-your-charm
open-a-kubernetes-port-in-your-charm
publish-your-charm-on-charmhub
```



(tutorial-kubernetes-next-steps)=
## Next steps

Expand All @@ -80,5 +69,3 @@ By the end of this tutorial you will have built a machine charm and evolved it i
| "How do I...?" | {ref}`how-to-guides` |
| "What is...?" | {ref}`reference` |
| "Why...?", "So what?" | {ref}`explanation` |


Loading

0 comments on commit 16bb8bc

Please sign in to comment.