Skip to content

Commit 3d8b674

Browse files
author
Kimonas Sotirchos
committed
feat: use istio-ingress-route interface
1 parent 3a1c663 commit 3d8b674

2 files changed

Lines changed: 84 additions & 7 deletions

File tree

metadata.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ provides:
2323
interface: prometheus_scrape
2424
grafana-dashboard:
2525
interface: grafana_dashboard
26+
provide-cmr-mesh:
27+
interface: cross_model_mesh
28+
description: |
29+
If this app is generating polciies to provide access to related applications that are cross-model, relate that app to this additional relation to retrieve additional data required for these policies. This is required because Juju does not natively provide all information required to build these policies when related cross-model.
2630
requires:
31+
istio-ingress-route:
32+
interface: istio_ingress_route
2733
ingress:
2834
interface: ingress
2935
schema:
@@ -83,3 +89,13 @@ requires:
8389
logging:
8490
interface: loki_push_api
8591
optional: true
92+
# service mesh
93+
service-mesh:
94+
limit: 1
95+
interface: service_mesh
96+
description: |
97+
Integrate this charm into a service mesh
98+
require-cmr-mesh:
99+
interface: cross_model_mesh
100+
description: |
101+
If this app relates to other applications on a charmed service mesh cross-model, use this relation to send that related app additional data needed to automatically generate traffic authorization policies. This is required because Juju does not natively provide all information required to build these policies when related cross-model.

src/charm.py

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
from charmed_kubeflow_chisme.kubernetes import KubernetesResourceHandler
1212
from charmed_kubeflow_chisme.lightkube.batch import delete_many
1313
from charms.grafana_k8s.v0.grafana_dashboard import GrafanaDashboardProvider
14+
from charms.istio_beacon_k8s.v0.service_mesh import ServiceMeshConsumer
15+
from charms.istio_ingress_k8s.v0.istio_ingress_route import (
16+
BackendRef,
17+
HTTPPathMatch,
18+
HTTPPathMatchType,
19+
HTTPRoute,
20+
HTTPRouteMatch,
21+
IstioIngressRouteConfig,
22+
IstioIngressRouteRequirer,
23+
Listener,
24+
ProtocolType,
25+
)
1426
from charms.kubeflow_dashboard.v0.kubeflow_dashboard_links import (
1527
DASHBOARD_LINK_LOCATIONS,
1628
KubeflowDashboardLinksProvider,
@@ -25,7 +37,11 @@
2537
from ops.charm import CharmBase
2638
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
2739
from ops.pebble import ChangeError, Layer
28-
from serialized_data_interface import NoCompatibleVersions, NoVersionsListed, get_interfaces
40+
from serialized_data_interface import (
41+
NoCompatibleVersions,
42+
NoVersionsListed,
43+
get_interfaces,
44+
)
2945

3046
from dashboard_links import aggregate_links_as_json
3147

@@ -97,13 +113,21 @@ def __init__(self, *args):
97113
port = ServicePort(int(self._port), name=f"{self.app.name}")
98114
self.service_patcher = KubernetesServicePatch(self, [port])
99115

116+
# Ambient Mesh integration
117+
self._mesh = ServiceMeshConsumer(self, policies=[])
118+
self.ingress = IstioIngressRouteRequirer(
119+
self, relation_name="istio-ingress-route"
120+
)
121+
self._ambient_mesh_ingress()
122+
100123
for event in [
101124
self.on.install,
102125
self.on.leader_elected,
103126
self.on.upgrade_charm,
104127
self.on.config_changed,
105128
self.on["kubeflow-profiles"].relation_changed,
106129
self.on["ingress"].relation_changed,
130+
self.ingress.on.ready,
107131
self.on.kubeflow_dashboard_pebble_ready,
108132
]:
109133
self.framework.observe(event, self.main)
@@ -230,7 +254,9 @@ def _update_layer(self) -> None:
230254
self.unit.status = MaintenanceStatus("Applying new pebble layer")
231255
self.container.add_layer(self._container_name, new_layer, combine=True)
232256
try:
233-
self.logger.info("Pebble plan updated with new configuration, replaning")
257+
self.logger.info(
258+
"Pebble plan updated with new configuration, replaning"
259+
)
234260
self.container.replan()
235261
except ChangeError as e:
236262
raise GenericCharmRuntimeError("Failed to replan") from e
@@ -256,14 +282,43 @@ def _handle_ingress(self, interfaces):
256282
}
257283
)
258284

285+
def _ambient_mesh_ingress(self):
286+
# ambient mesh
287+
http_listener = Listener(port=80, protocol=ProtocolType.HTTP)
288+
289+
config = IstioIngressRouteConfig(
290+
model=self.model.name,
291+
listeners=[http_listener],
292+
http_routes=[
293+
HTTPRoute(
294+
name="http-ingress",
295+
listener=http_listener,
296+
matches=[
297+
HTTPRouteMatch(
298+
path=HTTPPathMatch(
299+
type=HTTPPathMatchType.PathPrefix, value="/"
300+
)
301+
)
302+
],
303+
backends=[BackendRef(service=self.app.name, port=self._port)],
304+
)
305+
],
306+
)
307+
308+
self.ingress.submit_config(config)
309+
259310
def _check_kf_profiles(self, interfaces):
260311
kf_profiles = interfaces["kubeflow-profiles"]
261312

262313
if not kf_profiles:
263-
raise CheckFailed("Add required relation to kubeflow-profiles", BlockedStatus)
314+
raise CheckFailed(
315+
"Add required relation to kubeflow-profiles", BlockedStatus
316+
)
264317

265318
if not kf_profiles.get_data():
266-
raise CheckFailed("Waiting for kubeflow-profiles relation data", WaitingStatus)
319+
raise CheckFailed(
320+
"Waiting for kubeflow-profiles relation data", WaitingStatus
321+
)
267322

268323
return kf_profiles
269324

@@ -283,8 +338,12 @@ def _get_dashboard_links(self):
283338
links_from_relation=self.dashboard_link_provider.get_dashboard_links(
284339
location=location
285340
),
286-
additional_link_config=self.model.config[ADDITIONAL_LINKS_CONFIG_NAME[location]],
287-
link_order_config=self.model.config[EXTERNAL_LINKS_ORDER_CONFIG_NAME[location]],
341+
additional_link_config=self.model.config[
342+
ADDITIONAL_LINKS_CONFIG_NAME[location]
343+
],
344+
link_order_config=self.model.config[
345+
EXTERNAL_LINKS_ORDER_CONFIG_NAME[location]
346+
],
288347
location=location,
289348
)
290349
return links
@@ -315,7 +374,9 @@ def _on_remove(self, _):
315374
k8s_resources_manifests = self.k8s_resource_handler.render_manifests()
316375
configmap_manifest = self.configmap_handler.render_manifests()
317376
try:
318-
delete_many(self.k8s_resource_handler.lightkube_client, k8s_resources_manifests)
377+
delete_many(
378+
self.k8s_resource_handler.lightkube_client, k8s_resources_manifests
379+
)
319380
delete_many(self.configmap_handler.lightkube_client, configmap_manifest)
320381
except ApiError as e:
321382
self.logger.warning(f"Failed to delete resources, with error: {e}")

0 commit comments

Comments
 (0)