Skip to content

Commit

Permalink
Expose ports and apps when running in public clouds
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Mar 25, 2024
1 parent 36b1f0d commit e6bb441
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions charms/worker/k8s/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pathlib import Path
from time import sleep
from typing import Dict, Optional
from urllib.parse import urlparse

import charms.contextual_status as status
import ops
Expand Down Expand Up @@ -444,6 +445,7 @@ def _reconcile(self, event):
self._apply_node_labels()
if self.is_control_plane:
self._copy_internal_kubeconfig()
self._expose_ports()

@on_error(
ops.WaitingStatus("Cluster not yet ready"),
Expand Down Expand Up @@ -528,6 +530,13 @@ def _copy_internal_kubeconfig(self):
KUBECONFIG.parent.mkdir(parents=True, exist_ok=True)
KUBECONFIG.write_bytes(self._internal_kubeconfig.read_bytes())

def _expose_ports(self):
"""Expose ports for public clouds to access api endpoints."""
log.info("Exposing api ports")
content = yaml.safe_load(KUBECONFIG.read_text())
endpoint = urlparse(content["clusters"][0]["cluster"]["server"])
self.unit.open_port("tcp", endpoint.port)

def _get_external_kubeconfig(self, event: ops.ActionEvent):
"""Retrieve a public kubeconfig via a charm action.
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ applications:
charm: k8s
channel: latest/edge
num_units: 3
expose: true
k8s-worker:
charm: k8s-worker
channel: latest/edge
Expand Down

0 comments on commit e6bb441

Please sign in to comment.