Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nginx workload #17

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/charms/mimir_coordinator_k8s/v0/mimir_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import json
import logging
from collections import defaultdict
from enum import Enum
from typing import Any, Dict, MutableMapping, Set, List, Iterable
from typing import Optional
Expand All @@ -23,7 +24,7 @@
DEFAULT_ENDPOINT_NAME = "mimir-cluster"

LIBAPI = 0
LIBPATCH = 1
LIBPATCH = 2

BUILTIN_JUJU_KEYS = {"ingress-address", "private-address", "egress-subnets"}

Expand Down Expand Up @@ -242,22 +243,35 @@ def gather_roles(self) -> Dict[MimirRole, int]:
data[role] += role_n
return data

def gather_addresses(self) -> Set[str]:
"""Go through the worker's unit databags to collect all the addresses published by the units."""
data = set()
def gather_addresses_by_role(self) -> Dict[str, Set[str]]:
"""Go through the worker's unit databags to collect all the addresses published by the units, by role."""
data = defaultdict(set)
for relation in self._relations:
worker_app_data = MimirClusterRequirerAppData.load(relation.data[relation.app])
worker_roles = set(worker_app_data.roles)
for worker_unit in relation.units:
try:
worker_data = MimirClusterRequirerUnitData.load(relation.data[worker_unit])
unit_address = worker_data.address
data.add(unit_address)
for role in worker_roles:
data[role].add(unit_address)
except DataValidationError as e:
log.error(f"invalid databag contents: {e}")
continue

return data


def gather_addresses(self) -> Set[str]:
"""Go through the worker's unit databags to collect all the addresses published by the units."""
data = set()
addresses_by_role = self.gather_addresses_by_role()
for role, address_set in addresses_by_role.items():
data.update(address_set)

return data


class MimirClusterRemovedEvent(ops.EventBase):
"""Event emitted when the relation with the "mimir-cluster" provider has been severed.

Expand Down
16 changes: 8 additions & 8 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ summary: Mimir coordinator
description: |
Mimir coordinator.

#containers:
# nginx:
# resource: nginx-image
containers:
nginx:
resource: nginx-image

storage:
data:
type: filesystem

#resources:
# nginx-image:
# type: oci-image
# description: OCI image for nginx
# upstream-source: ubuntu/nginx:1.18-22.04_beta
resources:
nginx-image:
type: oci-image
description: OCI image for nginx
upstream-source: ubuntu/nginx:1.18-22.04_beta
# agent-image:
# type: oci-image
# upstream-source: ghcr.io/canonical/grafana-agent:latest
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ops
pydantic
# crossplane is a package from nginxinc to interact with the Nginx config
crossplane
17 changes: 17 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
PrometheusRemoteWriteConsumer,
)
from mimir_coordinator import MimirCoordinator
from nginx import Nginx
from ops.charm import CharmBase, CollectStatusEvent
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, Relation
Expand All @@ -32,6 +33,10 @@ class MimirCoordinatorK8SOperatorCharm(CharmBase):

def __init__(self, *args):
super().__init__(*args)

self._nginx_container = self.unit.get_container("nginx")
self._nginx_config_path = "/etc/nginx/nginx.conf"
Abuelodelanada marked this conversation as resolved.
Show resolved Hide resolved

self.framework.observe(self.on.config_changed, self._on_config_changed)
self.framework.observe(self.on.collect_unit_status, self._on_collect_status)

Expand All @@ -42,6 +47,12 @@ def __init__(self, *args):
self.cluster_provider = MimirClusterProvider(self)
self.coordinator = MimirCoordinator(cluster_provider=self.cluster_provider)

self.nginx = Nginx(cluster_provider=self.cluster_provider)
self.framework.observe(
self.on.nginx_pebble_ready, # pyright: ignore
self._on_nginx_pebble_ready,
)

self.framework.observe(
self.on.mimir_cluster_relation_changed, # pyright: ignore
self._on_mimir_cluster_changed,
Expand Down Expand Up @@ -123,6 +134,12 @@ def _on_loki_relation_changed(self, _):
# TODO Update rules relation with the new list of Loki push-api endpoints
pass

def _on_nginx_pebble_ready(self, _event) -> None:
self._nginx_container.push(self.nginx.config_path, self.nginx.config, make_dirs=True)

self._nginx_container.add_layer("nginx", self.nginx.layer, combine=True)
self._nginx_container.autostart()


if __name__ == "__main__": # pragma: nocover
main(MimirCoordinatorK8SOperatorCharm)
Loading