-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new relation to pass gateway info (#83)
* new relation to pass gateway info * set model name in unit test * fix lint * remove debug code * remove list comprehension for getting relations * charm library for relation * add lib to pythonpath - clean up libraries - update test * use resource handler to create crd or else lightkube would complain about same crd with diff signature when using create_global_resource * update receiver file * fix tests * PR review updates: - merge charm lib files - rename interface * update comment * use separate light kube client * put mocker in fixture * add doc * pin dep * fix istio-gateway not building in ci * mock validate function instead of lightkube client * remove relation len check * mock lightkube client * WIP take provider out of lib * rename method - only check for leadership at init - rename gatewayprovider method - update tests * fix lint * add info log if gateway relation data not sent
- Loading branch information
1 parent
d346f6c
commit 78eb1af
Showing
9 changed files
with
225 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
charms/istio-pilot/lib/charms/istio_pilot/v0/istio_gateway_name.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"""Library for sharing istio gateway information | ||
This library wraps the relation endpoints using the `istio-gateway-name` | ||
interface. It provides a Python API for both requesting and providing | ||
gateway information. | ||
## Getting Started | ||
### Fetch library with charmcraft | ||
You can fetch the library using the following commands with charmcraft. | ||
```shell | ||
cd some-charm | ||
charmcraft fetch-lib charms.istio_pilot.v0.istio_gateway_name | ||
``` | ||
### Add relation to metadata.yaml | ||
```yaml | ||
requires: | ||
gateway: | ||
interface: istio_gateway_name | ||
limit: 1 | ||
``` | ||
### Initialise the library in charm.py | ||
```python | ||
from charms.istio_pilot.v0.istio_gateway_name import GatewayProvider, GatewayRelationError | ||
Class SomeCharm(CharmBase): | ||
def __init__(self, *args): | ||
self.gateway = GatewayProvider(self) | ||
self.framework.observe(self.on.some_event_emitted, self.some_event_function) | ||
def some_event_function(): | ||
# use the getter function wherever the info is needed | ||
try: | ||
gateway_data = self.gateway_relation.get_relation_data() | ||
except GatewayRelationError as error: | ||
... | ||
``` | ||
""" | ||
|
||
import logging | ||
from ops.framework import Object | ||
from ops.model import Application | ||
|
||
# Increment this major API version when introducing breaking changes | ||
LIBAPI = 0 | ||
|
||
# Increment this PATCH version before using `charmcraft publish-lib` or reset | ||
# to 0 if you are raising the major API version | ||
LIBPATCH = 1 | ||
|
||
|
||
DEFAULT_RELATION_NAME = "gateway" | ||
DEFAULT_INTERFACE_NAME = "istio-gateway-name" | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GatewayRelationError(Exception): | ||
pass | ||
|
||
|
||
class GatewayRelationMissingError(GatewayRelationError): | ||
def __init__(self): | ||
self.message = "Missing gateway relation with istio-pilot" | ||
super().__init__(self.message) | ||
|
||
|
||
class GatewayRelationTooManyError(GatewayRelationError): | ||
def __init__(self): | ||
self.message = "Too many istio-gateway-name relations" | ||
super().__init__(self.message) | ||
|
||
|
||
class GatewayRelationDataMissingError(GatewayRelationError): | ||
def __init__(self, message): | ||
self.message = message | ||
super().__init__(self.message) | ||
|
||
|
||
class GatewayRequirer(Object): | ||
def __init__(self, charm, relation_name: str = DEFAULT_RELATION_NAME): | ||
super().__init__(charm, relation_name) | ||
self.charm = charm | ||
self.relation_name = relation_name | ||
|
||
def get_relation_data(self): | ||
if not self.model.unit.is_leader(): | ||
return | ||
gateway = self.model.relations[self.relation_name] | ||
if len(gateway) == 0: | ||
raise GatewayRelationMissingError() | ||
if len(gateway) > 1: | ||
raise GatewayRelationTooManyError() | ||
|
||
remote_app = [ | ||
app | ||
for app in gateway[0].data.keys() | ||
if isinstance(app, Application) and not app._is_our_app | ||
][0] | ||
|
||
data = gateway[0].data[remote_app] | ||
|
||
if not "gateway_name" in data: | ||
logger.error( | ||
"Missing gateway name in gateway relation data. Waiting for gateway creation in istio-pilot" | ||
) | ||
raise GatewayRelationDataMissingError( | ||
"Missing gateway name in gateway relation data. Waiting for gateway creation in istio-pilot" | ||
) | ||
|
||
if not "gateway_namespace" in data: | ||
logger.error("Missing gateway namespace in gateway relation data") | ||
raise GatewayRelationDataMissingError( | ||
"Missing gateway namespace in gateway relation data" | ||
) | ||
|
||
return { | ||
"gateway_name": data["gateway_name"], | ||
"gateway_namespace": data["gateway_namespace"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import logging | ||
from ops.framework import Object | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
DEFAULT_RELATION_NAME = "gateway" | ||
|
||
|
||
class GatewayProvider(Object): | ||
def __init__(self, charm, relation_name=DEFAULT_RELATION_NAME): | ||
super().__init__(charm, relation_name) | ||
|
||
def send_gateway_relation_data(self, charm, gateway_name, gateway_namespace): | ||
relations = self.model.relations["gateway"] | ||
for relation in relations: | ||
relation.data[charm].update( | ||
{ | ||
"gateway_name": gateway_name, | ||
"gateway_namespace": gateway_namespace, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
[flake8] | ||
max-line-length = 100 | ||
|
||
[vars] | ||
src_path = {toxinidir}/src | ||
tst_path = {toxinidir}/tests | ||
all_path = {[vars]src_path} {[vars]tst_path} | ||
|
||
|
||
[tox] | ||
skipsdist = True | ||
|
||
[testenv] | ||
setenv = | ||
PYTHONPATH={toxinidir}/src | ||
PYTHONPATH = {toxinidir}:{toxinidir}/lib:{[vars]src_path} | ||
passenv = | ||
PYTHONPATH | ||
CHARM_BUILD_DIR | ||
MODEL_SETTINGS | ||
deps = | ||
-rtest-requirements.txt | ||
-rrequirements.txt | ||
|
||
[testenv:unit] | ||
commands = | ||
pytest tests/unit {posargs} | ||
|
||
[vars] | ||
paths = {toxinidir}/src {toxinidir}/tests | ||
pytest {[vars]tst_path}/unit -v --tb native -s {posargs} | ||
|
||
[testenv:lint] | ||
commands = | ||
flake8 {[vars]paths} | ||
black --check {[vars]paths} | ||
flake8 {[vars]all_path} | ||
black --check {[vars]all_path} |