Skip to content

Commit

Permalink
Convert charm to operator framework (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
knkski authored Feb 18, 2021
1 parent ba29862 commit f7f111c
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 113 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/test-microk8s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
run: black --check .

- name: Check flake8
run: flake8 .
run: flake8 . --max-line-length 100

test:
name: Test
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Install dependencies
run: |
set -eux
sudo snap install charm --classic
sudo pip3 install charmcraft
sudo snap install juju --classic
sudo snap install juju-wait --classic
Expand All @@ -52,10 +52,10 @@ jobs:
- name: Deploy MinIO
run: |
set -eux
charm build .
juju deploy /tmp/charm-builds/minio \
--config secret-key=minio-secret-key \
--resource oci-image=minio/minio:RELEASE.2018-02-09T22-40-05Z
charmcraft build
juju deploy ./minio.charm \
--config secret-key=minio-secret-key \
--resource oci-image=minio/minio:RELEASE.2021-02-07T01-31-02Z
juju wait -wvt 300
- name: Test MinIO
Expand All @@ -71,7 +71,7 @@ jobs:
"mc alias set ci http://minio.minio.svc.cluster.local:9000 minio minio-secret-key && mc mb ci/foo && mc rb ci/foo"
- name: Get pod statuses
- name: Get all
run: kubectl get all -A
if: failure()

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.charm
build/
8 changes: 4 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
options:
port:
type: int
default: 9000
description: Port to listen to
type: string
default: '9000'
description: HTTP port
access-key:
type: string
default: 'minio'
default: minio
description: Access key
secret-key:
type: string
Expand Down
6 changes: 0 additions & 6 deletions layer.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ resources:
type: oci-image
description: Backing OCI image
auto-fetch: true
upstream-source: minio/minio:RELEASE.2018-02-09T22-40-05Z
upstream-source: minio/minio:RELEASE.2021-02-07T01-31-02Z
provides:
minio:
interface: generic-ip-port-user-pass
interface: minio
storage:
minio-data:
type: filesystem
Expand Down
94 changes: 0 additions & 94 deletions reactive/minio.py

This file was deleted.

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ops==1.1.0
git+https://github.com/juju-solutions/resource-oci-image@1964d748022b762b9dce6e8bb7bdf12835102c72
96 changes: 96 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3

import logging
from pathlib import Path
from random import choices
from string import ascii_uppercase, digits
from typing import Optional
from ops.charm import CharmBase
from ops.main import main
from ops.model import ActiveStatus, MaintenanceStatus

from oci_image import OCIImageResource, OCIImageResourceError

log = logging.getLogger()


def get_or_set(name: str, *, configured: Optional[str], default: str) -> str:
if configured:
Path(f"/run/{name}").write_text(configured)
return configured

try:
path = Path(f"/run/{name}")
return path.read_text()
except FileNotFoundError:
path.write_text(default)
return default


def gen_pass() -> str:
return "".join(choices(ascii_uppercase + digits, k=30))


class MinioCharm(CharmBase):
def __init__(self, *args):
super().__init__(*args)
self.image = OCIImageResource(self, "oci-image")
self.framework.observe(self.on.install, self.set_pod_spec)
self.framework.observe(self.on.upgrade_charm, self.set_pod_spec)
self.framework.observe(self.on.config_changed, self.set_pod_spec)
self.framework.observe(self.on.minio_relation_joined, self.send_info)

def send_info(self, event):
secret_key = get_or_set("password", default=gen_pass)
event.relation.data[self.unit]["service"] = self.model.app.name
event.relation.data[self.unit]["port"] = self.model.config["port"]
event.relation.data[self.unit]["access-key"] = self.model.config["access-key"]
event.relation.data[self.unit]["secret-key"] = secret_key

def set_pod_spec(self, event):
if not self.model.unit.is_leader():
log.info("Not a leader, skipping set_pod_spec")
self.model.unit.status = ActiveStatus()
return

try:
image_details = self.image.fetch()
except OCIImageResourceError as e:
self.model.unit.status = e.status
log.info(e)
return

secret_key = get_or_set(
"password",
configured=self.model.config["secret-key"],
default=gen_pass(),
)

self.model.unit.status = MaintenanceStatus("Setting pod spec")
self.model.pod.set_spec(
{
"version": 3,
"containers": [
{
"name": "minio",
"args": ["server", "/data"],
"imageDetails": image_details,
"ports": [
{
"name": "minio",
"containerPort": int(self.model.config["port"]),
}
],
"envConfig": {
"MINIO_ACCESS_KEY": self.model.config["access-key"],
"MINIO_SECRET_KEY": secret_key,
},
}
],
}
)
self.model.unit.status = ActiveStatus()


if __name__ == "__main__":
main(MinioCharm)

0 comments on commit f7f111c

Please sign in to comment.