Initial boilerplate updates
This commit is contained in:
parent
a9173c1d54
commit
4467d038e6
@ -1,16 +1,2 @@
|
||||
# Copyright 2021 James Page
|
||||
# See LICENSE file for licensing details.
|
||||
#
|
||||
# TEMPLATE-TODO: change this example to suit your needs.
|
||||
# If you don't need actions, you can remove the file entirely.
|
||||
# It ties in to the example _on_fortune_action handler in src/charm.py
|
||||
#
|
||||
# Learn more about actions at: https://juju.is/docs/sdk/actions
|
||||
|
||||
fortune:
|
||||
description: Returns a pithy phrase.
|
||||
params:
|
||||
fail:
|
||||
description: "Fail with this message"
|
||||
type: string
|
||||
default: ""
|
||||
# NOTE: no actions yet!
|
||||
{ }
|
||||
|
@ -1,14 +1,27 @@
|
||||
# Copyright 2021 James Page
|
||||
# See LICENSE file for licensing details.
|
||||
#
|
||||
# TEMPLATE-TODO: change this example to suit your needs.
|
||||
# If you don't need a config, you can remove the file entirely.
|
||||
# It ties in to the example _on_config_changed handler in src/charm.py
|
||||
#
|
||||
# Learn more about config at: https://juju.is/docs/sdk/config
|
||||
|
||||
options:
|
||||
thing:
|
||||
default: 🎁
|
||||
description: A thing used by the charm.
|
||||
debug:
|
||||
default: False
|
||||
description: Enable debug logging.
|
||||
type: boolean
|
||||
os-admin-hostname:
|
||||
default: cinder.juju
|
||||
description: |
|
||||
The hostname or address of the admin endpoints that should be advertised
|
||||
in the cinder volume provider.
|
||||
type: string
|
||||
os-internal-hostname:
|
||||
default: cinder.juju
|
||||
description: |
|
||||
The hostname or address of the internal endpoints that should be advertised
|
||||
in the cinder volume provider.
|
||||
type: string
|
||||
os-public-hostname:
|
||||
default: cinder.juju
|
||||
description: |
|
||||
The hostname or address of the internal endpoints that should be advertised
|
||||
in the cinder volume provider.
|
||||
type: string
|
||||
region:
|
||||
default: RegionOne
|
||||
description: Space delimited list of OpenStack regions
|
||||
type: string
|
||||
|
105
charms/cinder-k8s/lib/charms/mysql/v1/mysql.py
Normal file
105
charms/cinder-k8s/lib/charms/mysql/v1/mysql.py
Normal file
@ -0,0 +1,105 @@
|
||||
"""
|
||||
## Overview
|
||||
|
||||
This document explains how to integrate with the MySQL charm for the purposes of consuming a mysql database. It also explains how alternative implementations of the MySQL charm may maintain the same interface and be backward compatible with all currently integrated charms. Finally this document is the authoritative reference on the structure of relation data that is shared between MySQL charms and any other charm that intends to use the database.
|
||||
|
||||
|
||||
## Consumer Library Usage
|
||||
|
||||
The MySQL charm library uses the [Provider and Consumer](https://ops.readthedocs.io/en/latest/#module-ops.relation) objects from the Operator Framework. Charms that would like to use a MySQL database must use the `MySQLConsumer` object from the charm library. Using the `MySQLConsumer` object requires instantiating it, typically in the constructor of your charm. The `MySQLConsumer` constructor requires the name of the relation over which a database will be used. This relation must use the `mysql_datastore` interface. In addition the constructor also requires a `consumes` specification, which is a dictionary with key `mysql` (also see Provider Library Usage below) and a value that represents the minimum acceptable version of MySQL. This version string can be in any format that is compatible with the Python [Semantic Version module](https://pypi.org/project/semantic-version/). For example, assuming your charm consumes a database over a rlation named "monitoring", you may instantiate `MySQLConsumer` as follows:
|
||||
|
||||
from charms.mysql_k8s.v0.mysql import MySQLConsumer
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
...
|
||||
self.mysql_consumer = MySQLConsumer(
|
||||
self, "monitoring", {"mysql": ">=8"}
|
||||
)
|
||||
...
|
||||
|
||||
This example hard codes the consumes dictionary argument containing the minimal MySQL version required, however you may want to consider generating this dictionary by some other means, such as a `self.consumes` property in your charm. This is because the minimum required MySQL version may change when you upgrade your charm. Of course it is expected that you will keep this version string updated as you develop newer releases of your charm. If the version string can be determined at run time by inspecting the actual deployed version of your charmed application, this would be ideal.
|
||||
An instantiated `MySQLConsumer` object may be used to request new databases using the `new_database()` method. This method requires no arguments unless you require multiple databases. If multiple databases are requested, you must provide a unique `name_suffix` argument. For example:
|
||||
|
||||
def _on_database_relation_joined(self, event):
|
||||
self.mysql_consumer.new_database(name_suffix="db1")
|
||||
self.mysql_consumer.new_database(name_suffix="db2")
|
||||
|
||||
The `address`, `port`, `databases`, and `credentials` methods can all be called
|
||||
to get the relevant information from the relation data.
|
||||
"""
|
||||
|
||||
# !/usr/bin/env python3
|
||||
# Copyright 2021 Canonical Ltd.
|
||||
# See LICENSE file for licensing details.
|
||||
|
||||
import json
|
||||
import uuid
|
||||
import logging
|
||||
|
||||
from ops.relation import ConsumerBase
|
||||
|
||||
LIBID = "abcdef1234" # Will change when uploding the charm to charmhub
|
||||
LIBAPI = 1
|
||||
LIBPATCH = 0
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MySQLConsumer(ConsumerBase):
|
||||
"""
|
||||
MySQLConsumer lib class
|
||||
"""
|
||||
|
||||
def __init__(self, charm, name, consumes, multi=False):
|
||||
super().__init__(charm, name, consumes, multi)
|
||||
self.charm = charm
|
||||
self.relation_name = name
|
||||
|
||||
def databases(self, rel_id=None) -> list:
|
||||
"""
|
||||
List of currently available databases
|
||||
Returns:
|
||||
list: list of database names
|
||||
"""
|
||||
|
||||
rel = self.framework.model.get_relation(self.relation_name, rel_id)
|
||||
relation_data = rel.data[rel.app]
|
||||
dbs = relation_data.get("databases")
|
||||
databases = json.loads(dbs) if dbs else []
|
||||
|
||||
return databases
|
||||
|
||||
def credentials(self, rel_id=None) -> dict:
|
||||
"""
|
||||
Dictionary of credential information to access databases
|
||||
Returns:
|
||||
dict: dictionary of credential information including username,
|
||||
password and address
|
||||
"""
|
||||
rel = self.framework.model.get_relation(self.relation_name, rel_id)
|
||||
relation_data = rel.data[rel.app]
|
||||
data = relation_data.get("data")
|
||||
data = json.loads(data) if data else {}
|
||||
credentials = data.get("credentials")
|
||||
|
||||
return credentials
|
||||
|
||||
def new_database(self, rel_id=None, name_suffix=""):
|
||||
"""
|
||||
Request creation of an additional database
|
||||
"""
|
||||
if not self.charm.unit.is_leader():
|
||||
return
|
||||
|
||||
rel = self.framework.model.get_relation(self.relation_name, rel_id)
|
||||
|
||||
if name_suffix:
|
||||
name_suffix = "_{}".format(name_suffix)
|
||||
|
||||
rid = str(uuid.uuid4()).split("-")[-1]
|
||||
db_name = "db_{}_{}_{}".format(rel.id, rid, name_suffix)
|
||||
logger.debug("CLIENT REQUEST %s", db_name)
|
||||
rel_data = rel.data[self.charm.app]
|
||||
dbs = rel_data.get("databases")
|
||||
dbs = json.loads(dbs) if dbs else []
|
||||
dbs.append(db_name)
|
||||
rel.data[self.charm.app]["databases"] = json.dumps(dbs)
|
@ -0,0 +1,211 @@
|
||||
"""Library for the ingress relation.
|
||||
|
||||
This library contains the Requires and Provides classes for handling
|
||||
the ingress interface.
|
||||
|
||||
Import `IngressRequires` in your charm, with two required options:
|
||||
- "self" (the charm itself)
|
||||
- config_dict
|
||||
|
||||
`config_dict` accepts the following keys:
|
||||
- service-hostname (required)
|
||||
- service-name (required)
|
||||
- service-port (required)
|
||||
- additional-hostnames
|
||||
- limit-rps
|
||||
- limit-whitelist
|
||||
- max-body-size
|
||||
- path-routes
|
||||
- retry-errors
|
||||
- rewrite-enabled
|
||||
- rewrite-target
|
||||
- service-namespace
|
||||
- session-cookie-max-age
|
||||
- tls-secret-name
|
||||
|
||||
See [the config section](https://charmhub.io/nginx-ingress-integrator/configure) for descriptions
|
||||
of each, along with the required type.
|
||||
|
||||
As an example, add the following to `src/charm.py`:
|
||||
```
|
||||
from charms.nginx_ingress_integrator.v0.ingress import IngressRequires
|
||||
|
||||
# In your charm's `__init__` method.
|
||||
self.ingress = IngressRequires(self, {"service-hostname": self.config["external_hostname"],
|
||||
"service-name": self.app.name,
|
||||
"service-port": 80})
|
||||
|
||||
# In your charm's `config-changed` handler.
|
||||
self.ingress.update_config({"service-hostname": self.config["external_hostname"]})
|
||||
```
|
||||
And then add the following to `metadata.yaml`:
|
||||
```
|
||||
requires:
|
||||
ingress:
|
||||
interface: ingress
|
||||
```
|
||||
You _must_ register the IngressRequires class as part of the `__init__` method
|
||||
rather than, for instance, a config-changed event handler. This is because
|
||||
doing so won't get the current relation changed event, because it wasn't
|
||||
registered to handle the event (because it wasn't created in `__init__` when
|
||||
the event was fired).
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from ops.charm import CharmEvents
|
||||
from ops.framework import EventBase, EventSource, Object
|
||||
from ops.model import BlockedStatus
|
||||
|
||||
# The unique Charmhub library identifier, never change it
|
||||
LIBID = "db0af4367506491c91663468fb5caa4c"
|
||||
|
||||
# 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 = 9
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
REQUIRED_INGRESS_RELATION_FIELDS = {
|
||||
"service-hostname",
|
||||
"service-name",
|
||||
"service-port",
|
||||
}
|
||||
|
||||
OPTIONAL_INGRESS_RELATION_FIELDS = {
|
||||
"additional-hostnames",
|
||||
"limit-rps",
|
||||
"limit-whitelist",
|
||||
"max-body-size",
|
||||
"retry-errors",
|
||||
"rewrite-target",
|
||||
"rewrite-enabled",
|
||||
"service-namespace",
|
||||
"session-cookie-max-age",
|
||||
"tls-secret-name",
|
||||
"path-routes",
|
||||
}
|
||||
|
||||
|
||||
class IngressAvailableEvent(EventBase):
|
||||
pass
|
||||
|
||||
|
||||
class IngressCharmEvents(CharmEvents):
|
||||
"""Custom charm events."""
|
||||
|
||||
ingress_available = EventSource(IngressAvailableEvent)
|
||||
|
||||
|
||||
class IngressRequires(Object):
|
||||
"""This class defines the functionality for the 'requires' side of the 'ingress' relation.
|
||||
|
||||
Hook events observed:
|
||||
- relation-changed
|
||||
"""
|
||||
|
||||
def __init__(self, charm, config_dict):
|
||||
super().__init__(charm, "ingress")
|
||||
|
||||
self.framework.observe(charm.on["ingress"].relation_changed, self._on_relation_changed)
|
||||
|
||||
self.config_dict = config_dict
|
||||
|
||||
def _config_dict_errors(self, update_only=False):
|
||||
"""Check our config dict for errors."""
|
||||
blocked_message = "Error in ingress relation, check `juju debug-log`"
|
||||
unknown = [
|
||||
x
|
||||
for x in self.config_dict
|
||||
if x not in REQUIRED_INGRESS_RELATION_FIELDS | OPTIONAL_INGRESS_RELATION_FIELDS
|
||||
]
|
||||
if unknown:
|
||||
logger.error(
|
||||
"Ingress relation error, unknown key(s) in config dictionary found: %s",
|
||||
", ".join(unknown),
|
||||
)
|
||||
self.model.unit.status = BlockedStatus(blocked_message)
|
||||
return True
|
||||
if not update_only:
|
||||
missing = [x for x in REQUIRED_INGRESS_RELATION_FIELDS if x not in self.config_dict]
|
||||
if missing:
|
||||
logger.error(
|
||||
"Ingress relation error, missing required key(s) in config dictionary: %s",
|
||||
", ".join(missing),
|
||||
)
|
||||
self.model.unit.status = BlockedStatus(blocked_message)
|
||||
return True
|
||||
return False
|
||||
|
||||
def _on_relation_changed(self, event):
|
||||
"""Handle the relation-changed event."""
|
||||
# `self.unit` isn't available here, so use `self.model.unit`.
|
||||
if self.model.unit.is_leader():
|
||||
if self._config_dict_errors():
|
||||
return
|
||||
for key in self.config_dict:
|
||||
event.relation.data[self.model.app][key] = str(self.config_dict[key])
|
||||
|
||||
def update_config(self, config_dict):
|
||||
"""Allow for updates to relation."""
|
||||
if self.model.unit.is_leader():
|
||||
self.config_dict = config_dict
|
||||
if self._config_dict_errors(update_only=True):
|
||||
return
|
||||
relation = self.model.get_relation("ingress")
|
||||
if relation:
|
||||
for key in self.config_dict:
|
||||
relation.data[self.model.app][key] = str(self.config_dict[key])
|
||||
|
||||
|
||||
class IngressProvides(Object):
|
||||
"""This class defines the functionality for the 'provides' side of the 'ingress' relation.
|
||||
|
||||
Hook events observed:
|
||||
- relation-changed
|
||||
"""
|
||||
|
||||
def __init__(self, charm):
|
||||
super().__init__(charm, "ingress")
|
||||
# Observe the relation-changed hook event and bind
|
||||
# self.on_relation_changed() to handle the event.
|
||||
self.framework.observe(charm.on["ingress"].relation_changed, self._on_relation_changed)
|
||||
self.charm = charm
|
||||
|
||||
def _on_relation_changed(self, event):
|
||||
"""Handle a change to the ingress relation.
|
||||
|
||||
Confirm we have the fields we expect to receive."""
|
||||
# `self.unit` isn't available here, so use `self.model.unit`.
|
||||
if not self.model.unit.is_leader():
|
||||
return
|
||||
|
||||
ingress_data = {
|
||||
field: event.relation.data[event.app].get(field)
|
||||
for field in REQUIRED_INGRESS_RELATION_FIELDS | OPTIONAL_INGRESS_RELATION_FIELDS
|
||||
}
|
||||
|
||||
missing_fields = sorted(
|
||||
[
|
||||
field
|
||||
for field in REQUIRED_INGRESS_RELATION_FIELDS
|
||||
if ingress_data.get(field) is None
|
||||
]
|
||||
)
|
||||
|
||||
if missing_fields:
|
||||
logger.error(
|
||||
"Missing required data fields for ingress relation: {}".format(
|
||||
", ".join(missing_fields)
|
||||
)
|
||||
)
|
||||
self.model.unit.status = BlockedStatus(
|
||||
"Missing fields for ingress: {}".format(", ".join(missing_fields))
|
||||
)
|
||||
|
||||
# Create an event that our charm can use to decide it's okay to
|
||||
# configure the ingress.
|
||||
self.charm.on.ingress_available.emit()
|
@ -1,23 +1,38 @@
|
||||
# Copyright 2021 James Page
|
||||
# Copyright 2021 Canonical Ltd
|
||||
# See LICENSE file for licensing details.
|
||||
|
||||
# For a complete list of supported options, see:
|
||||
# https://discourse.charmhub.io/t/charm-metadata-v2/3674/15
|
||||
name: charm-cinder-operator
|
||||
display-name: |
|
||||
TEMPLATE-TODO: fill out a display name for the Charmcraft store
|
||||
name: sunbeam-cinder-operator
|
||||
summary: OpenStack volume service
|
||||
maintainer: Openstack Charmers <openstack-charmers@lists.ubuntu.com>
|
||||
description: |
|
||||
TEMPLATE-TODO: fill out the charm's description
|
||||
summary: |
|
||||
TEMPLATE-TODO: fill out the charm's summary
|
||||
Cinder is the OpenStack project that provides volume management for
|
||||
Clous instances.
|
||||
version: 3
|
||||
bases:
|
||||
- name: ubuntu
|
||||
channel: 20.04/stable
|
||||
tags:
|
||||
- openstack
|
||||
- storage
|
||||
- misc
|
||||
|
||||
# TEMPLATE-TODO: replace with containers for your workload (delete for non-k8s)
|
||||
containers:
|
||||
httpbin:
|
||||
resource: httpbin-image
|
||||
resource: cinder-image
|
||||
|
||||
# TEMPLATE-TODO: each container defined above must specify an oci-image resource
|
||||
resources:
|
||||
httpbin-image:
|
||||
cinder-image:
|
||||
type: oci-image
|
||||
description: OCI image for httpbin (kennethreitz/httpbin)
|
||||
description: OCI image for OpenStack Cinder (kolla/cinder)
|
||||
|
||||
requires:
|
||||
keystone-db:
|
||||
interface: mysql_datastore
|
||||
limit: 1
|
||||
ingress:
|
||||
interface: ingress
|
||||
|
||||
# NOTE: might not neede this
|
||||
peers:
|
||||
peers:
|
||||
interface: cinder-peer
|
||||
|
||||
|
@ -1 +1,4 @@
|
||||
ops >= 1.2.0
|
||||
# ops >= 1.2.0
|
||||
jinja2
|
||||
git+https://github.com/canonical/operator@2875e73e#egg=ops
|
||||
git+https://opendev.org/openstack/charm-ops-openstack#egg=ops_openstack
|
||||
|
Loading…
x
Reference in New Issue
Block a user