Skip to content

Commit

Permalink
Add warning if ingress is not established
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-scribner committed Mar 22, 2024
1 parent 0d0071f commit d406758
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ops.main import main

from components.config_generation import GenerateEnvoyConfig, GenerateEnvoyConfigInputs
from components.ingress import IngressRelationWarnIfMissing, IngressRelationWarnIfMissingInputs
from components.pebble import EnvoyPebbleService, EnvoyPebbleServiceInputs

ENVOY_CONFIG_FILE_PATH = "/envoy/envoy.json"
Expand Down Expand Up @@ -68,6 +69,17 @@ def __init__(self, *args):
depends_on=[self.leadership_gate],
)

self.ingress_relation_warn_if_missing = self.charm_reconciler.add(
component=IngressRelationWarnIfMissing(
charm=self,
name="ingress-relation-warn-if-missing",
inputs_getter=lambda: IngressRelationWarnIfMissingInputs(
interface=self.ingress_relation.component.get_interface()
),
),
depends_on=[self.ingress_relation],
)

self.envoy_config_generator = self.charm_reconciler.add(
GenerateEnvoyConfig(
charm=self,
Expand Down
39 changes: 39 additions & 0 deletions src/components/ingress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dataclasses
import logging
from typing import Optional

from charmed_kubeflow_chisme.components import Component
from ops import ActiveStatus, BlockedStatus, StatusBase


@dataclasses.dataclass
class IngressRelationWarnIfMissingInputs:
"""Defines the required inputs for IngressRelationWarnIfMissing."""

interface: Optional[dict]


class IngressRelationWarnIfMissing(Component):
"""Component that logs a warning if we have no Ingress relation established."""

def __init__(self, *args, **kwargs):
"""Component that logs a warning if we have no Ingress relation established."""
super().__init__(*args, **kwargs)
# Attach a logger. Use this instead of the global one to make mocking easier
self.logger = logging.getLogger(__name__)

def get_status(self) -> StatusBase:
"""Always Active unless it cannot get inputs."""
try:
inputs = self._inputs_getter()
if not inputs.interface:
self.logger.warning(
"No ingress relation established. To be used by KFP in Charmed Kubeflow, this"
" charm requires an Ingress relation."
)
return ActiveStatus("Active but no ingress relation established")

return ActiveStatus()
except Exception as e:
self.logger.error(f"Error getting inputs for IngressRelationWarnIfMissing: {e}")
return BlockedStatus("Error getting inputs. See logs")
35 changes: 35 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,41 @@ def test_with_ingress_relation(self, harness):

assert harness.charm.ingress_relation.status == ActiveStatus()

def test_warning_if_ingress_missing(self, harness, mocker):
"""Test that we emit a warning if we do not have an ingress established."""
harness.set_leader(True)
setup_grpc_relation(harness, "grpc-one", "8080")

harness.begin()

# Mock the ingress warning logger so we can check if it was called
mock_logger = mocker.MagicMock()
harness.charm.ingress_relation_warn_if_missing.component.logger = mock_logger

# Do something that will reconcile the charm
harness.charm.on.config_changed.emit()

# Assert that we've logged the missing ingress relation
assert "No ingress relation established" in mock_logger.warning.call_args[0][0]

def test_no_warning_if_ingress_is_established(self, harness, mocker):
"""Test that we emit a warning if we do not have an ingress established."""
harness.set_leader(True)
setup_grpc_relation(harness, "grpc-one", "8080")
setup_ingress_relation(harness)

harness.begin()

# Mock the ingress warning logger so we can check if it was called
mock_logger = mocker.MagicMock()
harness.charm.ingress_relation_warn_if_missing.component.logger = mock_logger

# Do something that will reconcile the charm
harness.charm.on.config_changed.emit()

# Assert that we haven't logged anything
assert mock_logger.warning.call_args is None

def test_envoy_config_generator(self, harness):
"""Test the envoy_config_generator Component is active when prerequisites are ready."""
harness.set_leader(True)
Expand Down

0 comments on commit d406758

Please sign in to comment.