Skip to content

Commit

Permalink
move exporter to ROCK (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtrkiller authored Jan 18, 2024
1 parent e6efce9 commit 7fe59b5
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 94 deletions.
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ Deploy the charm:
```bash
charmcraft pack
juju deploy ./wordpress-k8s_ubuntu-22.04-amd64.charm \
--resource jenkins-image=localhost:32000/wordpress:test \
--resource apache-prometheus-exporter-image=bitnami/apache-exporter:0.11.0
--resource jenkins-image=localhost:32000/wordpress:test
```

## Generating src docs for every commit
Expand Down
11 changes: 1 addition & 10 deletions docs/explanation/containers.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Containers

The core component of wordpress-k8s charm consists of a wordpress-k8s main workload container and a
`apache-prometheus-exporter` sidecar container. Both services inside the containers are driven by
The core component of wordpress-k8s charm consists of a wordpress-k8s main workload container with an Apache Prometheus exporter. The services inside the container are driven by
Pebble, a lightweight API-driven process supervisor that controls the lifecycle of a service.
Learn more about pebble and its layer configurations [here](https://github.com/canonical/pebble).

Expand Down Expand Up @@ -35,14 +34,6 @@ to both `access.log`, `error.log` files and container logs in
[`000-default.conf`](https://github.com/canonical/wordpress-k8s-operator/blob/main/files/000-default.conf).
These files are essential for promtail to read and push latest logs to loki periodically.

### apache-prometheus-exporter

This is a sidecar container that contains a golang binary to periodically scrape `/server-status`
route from the main wordpress container. It exposes WordPress Apache server’s metrics in open
metrics format on port 9117. It should be noted that some of its own metrics from the Prometheus
Golang client are exposed that are not part of the WordPress Apache’s metrics and are denoted by
`go_` prefix.

### charm

This container is the main point of contact with the juju controller. It communicates with juju to
Expand Down
8 changes: 1 addition & 7 deletions docs/explanation/oci-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,4 @@ image during build step. The latest CLI php archive file from source is used.
Currently, WordPress version 5.9.3 is used alongside Ubuntu 20.04 base image. The Ubuntu base image
hasn't yet been upgraded to 22.04 due to an unsupported php version 8 for
`wordpress-launchpad-integration` plugin (supported php version 7). All other plugins and themes use
the latest stable version by default, downloaded from the source.

### apache-prometheus-exporter-image

This is the image required for sidecar container apache-prometheus-exporter. Openly available image
`bitnami/apache-exporter` is used. Read more about the image from the official Docker Hub
[source](https://hub.docker.com/r/bitnami/apache-exporter/).
the latest stable version by default, downloaded from the source.
3 changes: 1 addition & 2 deletions docs/how-to/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ Deploy the locally built WordPress charm with the following command.

```
juju deploy ./wordpress-k8s_ubuntu-22.04-amd64_ubuntu-20.04-amd64.charm \
--resource wordpress-image=wordpress \
--resource apache-prometheus-exporter-image=bitnami/apache-exporter:0.11.0
--resource wordpress-image=wordpress
```

You should now be able to see your local wordpress-k8s charm progress through the stages of the
Expand Down
6 changes: 0 additions & 6 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ containers:
mounts:
- storage: uploads
location: /var/www/html/wp-content/uploads
apache-prometheus-exporter:
resource: apache-prometheus-exporter-image

storage:
uploads:
Expand All @@ -46,10 +44,6 @@ resources:
wordpress-image:
type: oci-image
description: OCI image for wordpress
apache-prometheus-exporter-image:
type: oci-image
description: Prometheus exporter for apache
upstream-source: bitnami/apache-exporter:0.13.3

provides:
metrics-endpoint:
Expand Down
41 changes: 12 additions & 29 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@
from ops.charm import ActionEvent, CharmBase, HookEvent, PebbleReadyEvent, UpgradeCharmEvent
from ops.framework import EventBase
from ops.main import main
from ops.model import (
ActiveStatus,
BlockedStatus,
MaintenanceStatus,
RelationDataContent,
WaitingStatus,
)
from ops.model import ActiveStatus, RelationDataContent, WaitingStatus
from ops.pebble import ExecProcess
from yaml import safe_load

import exceptions
import types_
from cos import APACHE_LOG_PATHS, PROM_EXPORTER_PEBBLE_CONFIG, WORDPRESS_SCRAPE_JOBS
from cos import (
_APACHE_EXPORTER_PEBBLE_SERVICE,
APACHE_LOG_PATHS,
PROM_EXPORTER_PEBBLE_CONFIG,
WORDPRESS_SCRAPE_JOBS,
)
from state import CharmConfigInvalidError, State

# MySQL logger prints database credentials on debug level, silence it
Expand Down Expand Up @@ -176,10 +175,6 @@ def __init__(self, *args, **kwargs):
self.framework.observe(self.on.wordpress_pebble_ready, self._set_version)
self.framework.observe(self.on.wordpress_pebble_ready, self._reconciliation)
self.framework.observe(self.on["wordpress-replica"].relation_changed, self._reconciliation)
self.framework.observe(
self.on.apache_prometheus_exporter_pebble_ready,
self._on_apache_prometheus_exporter_pebble_ready,
)

def _set_version(self, _: PebbleReadyEvent) -> None:
"""Set WordPress application version to Juju charm's app version status."""
Expand Down Expand Up @@ -716,6 +711,9 @@ def _init_pebble_layer(self):
},
}
self._container().add_layer("wordpress", layer, combine=True)
self._container().add_layer(
_APACHE_EXPORTER_PEBBLE_SERVICE.name, PROM_EXPORTER_PEBBLE_CONFIG, combine=True
)

def _start_server(self):
"""Start WordPress (apache) server. On leader unit, also make sure WordPress is installed.
Expand Down Expand Up @@ -761,6 +759,8 @@ def _start_server(self):
self._init_pebble_layer()
if not self._container().get_service(self._SERVICE_NAME).is_running():
self._container().start(self._SERVICE_NAME)
if not self._container().get_service(_APACHE_EXPORTER_PEBBLE_SERVICE.name).is_running():
self._container().start(_APACHE_EXPORTER_PEBBLE_SERVICE.name)

def _current_wp_config(self):
"""Retrieve the current version of wp-config.php from server, return None if not exists.
Expand Down Expand Up @@ -1473,23 +1473,6 @@ def _reconciliation(self, _event: EventBase) -> None:
else:
self.unit.status = WaitingStatus("Waiting for pebble")

def _on_apache_prometheus_exporter_pebble_ready(self, event: PebbleReadyEvent):
"""Configure and start apache prometheus exporter.
Args:
event: Event triggering the handler.
"""
if not event.workload:
self.unit.status = BlockedStatus("Internal Error, pebble container not found.")
return
container = event.workload
pebble: ops.pebble.Client = container.pebble
self.unit.status = MaintenanceStatus(f"Adding {container.name} layer to pebble")
container.add_layer(container.name, PROM_EXPORTER_PEBBLE_CONFIG, combine=True)
self.unit.status = MaintenanceStatus(f"Starting {container.name} container")
pebble.replan_services()
self._reconciliation(event)


if __name__ == "__main__": # pragma: no cover
main(WordpressCharm)
1 change: 0 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ async def wordpress_fixture(
charm,
resources={
"wordpress-image": wordpress_image,
"apache-prometheus-exporter-image": "bitnami/apache-exporter:0.11.0",
},
num_units=1,
series="focal",
Expand Down
37 changes: 0 additions & 37 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import ops.pebble
import ops.testing
import pytest
from ops.charm import PebbleReadyEvent
from ops.model import Container
from ops.pebble import Client

import types_
from charm import WordpressCharm
Expand Down Expand Up @@ -309,40 +306,6 @@ def test_addon_reconciliation_fail(harness: ops.testing.Harness, monkeypatch: py
charm._addon_reconciliation("theme")


@pytest.mark.usefixtures("attach_storage")
def test_prom_exporter_pebble_ready(
patch: WordpressPatch,
harness: ops.testing.Harness,
setup_replica_consensus: typing.Callable[[], dict],
setup_database_relation_no_port: typing.Callable[[], typing.Tuple[int, dict]],
):
"""
arrange: after required relations ready but before prometheus exporter pebble ready.
act: run prometheus exporter pebble ready.
assert: unit should be active.
"""
harness.set_can_connect(harness.model.unit.containers["wordpress"], True)
setup_replica_consensus()
charm: WordpressCharm = typing.cast(WordpressCharm, harness.charm)
_, db_info = setup_database_relation_no_port()
patch.database.prepare_database(
host=db_info["endpoints"],
database=db_info["database"],
user=db_info["username"],
password=db_info["password"],
)
mock_event = unittest.mock.MagicMock(spec=PebbleReadyEvent)
mock_event.workload = unittest.mock.MagicMock(spec=Container)
mock_event.workload.name = "apache-prometheus-exporter"
mock_event.workload.pebble = unittest.mock.MagicMock(spec=Client)

charm._on_apache_prometheus_exporter_pebble_ready(mock_event)

assert isinstance(
harness.model.unit.status, ops.charm.model.ActiveStatus
), "unit should be in ActiveStatus"


@pytest.mark.usefixtures("attach_storage")
def test_core_reconciliation(
patch: WordpressPatch,
Expand Down
2 changes: 2 additions & 0 deletions wordpress_rock/rockcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ parts:
- WP_VERSION: 6.4.2
build-packages:
- curl
stage-snaps:
- rocks-apache-prometheus-exporter/latest/edge
override-build: |
curl -sSL --create-dirs https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o wp
chmod +x wp
Expand Down

0 comments on commit 7fe59b5

Please sign in to comment.