sunbeam-charms/ops-sunbeam/doc/writing-OS-API-charm.rst
Samuel Walladge 7684a0db73 Switch to mysql-k8s for mysql db provider
Breaking changes for dependent charms:

- `self.db: str` has been removed in favour of
  `self.dbs: Mapping[str, str]` mapping relation names to dabatase names

- interface name for database has changed
  from mysql_datastore to mysql_client.

- The default database relation name has changed from `shared-db`
  to `database`.  We'll follow this convention in all dependent charms.

Change-Id: I750a8311c1e3db0b414207f712fa9061004b6920
2022-09-08 10:58:17 +09:30

4.2 KiB

New API Charm

The example below will walk through the creation of a basic API charm for the OpenStack Ironic service designed to run on kubernetes.

Create the skeleton charm

Prerequisite

Build a base geneeric charm with the charmcraft tool.

mkdir charm-ironic-operator
cd charm-ironic-operator
charmcraft init --name sunbeam-ironic-operator

Add ASO common files to new charm. The script will ask a few basic questions:

git clone https://opendev.org/openstack/charm-ops-sunbeam
cd charm-ops-sunbeam
./sunbeam-charm-init.sh ~/branches/charm-ironic-operator

This tool is designed to be used after 'charmcraft init' was initially run
service_name [ironic]: ironic
charm_name [sunbeam-ironic-operator]: sunbeam-ironic-operator
ingress_port []: 6385
db_sync_command [] ironic-dbsync --config-file /etc/ironic/ironic.conf create_schema: 

Fetch interface libs corresponding to the requires interfaces:

cd charm-ironic-operator
charmcraft login
charmcraft fetch-lib charms.nginx_ingress_integrator.v0.ingress
charmcraft fetch-lib charms.data_platform_libs.v0.database_requires
charmcraft fetch-lib charms.sunbeam_keystone_operator.v0.identity_service
charmcraft fetch-lib charms.sunbeam_rabbitmq_operator.v0.amqp
charmcraft fetch-lib charms.observability_libs.v0.kubernetes_service_patch

Templates

Much of the service configuration is covered by common templates which were copied into the charm in the previous step. The only additional template for this charm is for ironic.conf. Add the following into ./src/templates/ironic.conf.j2

[DEFAULT]
debug = {{ options.debug }}
auth_strategy=keystone
transport_url = {{ amqp.transport_url }}

[keystone_authtoken]
{% include "parts/identity-data" %}

[database]
{% include "parts/database-connection" %}

[neutron]
{% include "parts/identity-data" %}

[glance]
{% include "parts/identity-data" %}

[cinder]
{% include "parts/identity-data" %}

[service_catalog]
{% include "parts/identity-data" %}

Make charm deployable

The next step is to pack the charm into a deployable format

cd charm-ironic-operator
charmcraft pack

Deploy Charm

The charm can now be deployed. The Kolla project has images that can be used to run the service. Juju can pull the image directly from dockerhub.

juju deploy ./sunbeam-ironic-operator_ubuntu-20.04-amd64.charm --resource ironic-api-image=kolla/ubuntu-binary-ironic-api:wallaby ironic
juju add-relation ironic mysql
juju add-relation ironic keystone
juju add-relation ironic rabbitmq

Test Service

Check that the juju status shows the charms is active and no error messages are preset. Then check the ironic api service is reponding.

$ juju status ironic
Model  Controller  Cloud/Region        Version  SLA          Timestamp
ks     micro       microk8s/localhost  2.9.22   unsupported  13:31:41Z

App     Version  Status  Scale  Charm                    Store  Channel  Rev  OS          Address        Message
ironic           active      1  sunbeam-ironic-operator  local             0  kubernetes  10.152.183.73  

Unit       Workload  Agent  Address       Ports  Message
ironic/0*  active    idle   10.1.155.106

$ curl http://10.1.155.106:6385 | jq '.'
{
  "name": "OpenStack Ironic API",
  "description": "Ironic is an OpenStack project which aims to provision baremetal machines.",
  "default_version": {
    "id": "v1",
    "links": [
      {
        "href": "http://10.1.155.106:6385/v1/",
        "rel": "self"
      }
     ],
    "status": "CURRENT",
    "min_version": "1.1",
    "version": "1.72"
  },
  "versions": [
    {
      "id": "v1",
      "links": [
        {
          "href": "http://10.1.155.106:6385/v1/",
          "rel": "self"
        }
       ],
      "status": "CURRENT",
      "min_version": "1.1",
      "version": "1.72"
    }
  ]
}