Skip to content

Commit

Permalink
Support kube-control v2 schema
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Dec 6, 2024
1 parent 6a28597 commit 0fb3e72
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 168 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
name: Lint Unit
uses: charmed-kubernetes/workflows/.github/workflows/lint-unit.yaml@main
with:
python: "['3.8', '3.9', '3.10', '3.11']"
python: "['3.8', '3.10', '3.12']"
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
lightkube>=0.10.1,<1.0.0
ops>=1.3.0,<2.0.0
ops
ops.manifest>=1.1.0,<2.0.0
pydantic==1.*
git+https://github.com/charmed-kubernetes/interface-kube-control.git@6dd289d1c795fdeda1bed17873b8d6562227c829#subdirectory=ops
git+https://github.com/charmed-kubernetes/interface-openstack-integration.git@066d177e8ef1acb1e7b1d92832a32c9a1fb41c9e#subdirectory=ops
ops.interface-kube-control @ git+https://github.com/charmed-kubernetes/interface-kube-control.git@edc07bce7ea4c25d472fa4d95834602a7ebce5cd#subdirectory=ops
ops.interface-tls-certificates @ git+https://github.com/charmed-kubernetes/interface-tls-certificates.git@4a1081da098154b96337a09c8e9c40acff2d330e#subdirectory=ops
ops.interface-openstack-integration @ git+https://github.com/charmed-kubernetes/interface-openstack-integration.git@066d177e8ef1acb1e7b1d92832a32c9a1fb41c9e#subdirectory=ops
67 changes: 34 additions & 33 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,22 @@
import os
from pathlib import Path

from ops.charm import CharmBase
from ops.framework import StoredState
import ops
from ops.interface_kube_control import KubeControlRequirer
from ops.interface_openstack_integration import OpenstackIntegrationRequirer
from ops.main import main
from ops.interface_tls_certificates import CertificatesRequires
from ops.manifests import Collector, ManifestClientError
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus

from config import CharmConfig
from requires_certificates import CertificatesRequires
from storage_manifests import StorageManifests

log = logging.getLogger(__name__)


class CinderCSICharm(CharmBase):
class CinderCSICharm(ops.CharmBase):
"""Deploy and manage the Cinder CSI plugin for K8s on OpenStack."""

stored = StoredState()
stored = ops.StoredState()

def __init__(self, *args):
super().__init__(*args)
Expand All @@ -34,7 +31,7 @@ def __init__(self, *args):
self._kubeconfig_path.parent.mkdir(parents=True, exist_ok=True)

# Relation Validator and datastore
self.kube_control = KubeControlRequirer(self)
self.kube_control = KubeControlRequirer(self, schemas="0,1")
self.certificates = CertificatesRequires(self)
self.integrator = OpenstackIntegrationRequirer(self)
# Config Validator and datastore
Expand Down Expand Up @@ -111,61 +108,65 @@ def _update_status(self, _):

unready = self.collector.unready
if unready:
self.unit.status = WaitingStatus(", ".join(unready))
self.unit.status = ops.WaitingStatus(", ".join(unready))
else:
self.unit.status = ActiveStatus("Ready")
self.unit.status = ops.ActiveStatus("Ready")
self.unit.set_workload_version(self.collector.short_version)
self.app.status = ActiveStatus(self.collector.long_version)
self.app.status = ops.ActiveStatus(self.collector.long_version)

def _kube_control(self, event):
self.kube_control.set_auth_request(self.unit.name)
self.kube_control.set_auth_request(self.unit.name, "system:masters")
return self._merge_config(event)

def _check_integrator(self, event):
self.unit.status = MaintenanceStatus("Evaluating Openstack relation.")
self.unit.status = ops.MaintenanceStatus("Evaluating Openstack relation.")
evaluation = self.integrator.evaluate_relation(event)
if evaluation:
if "Waiting" in evaluation:
self.unit.status = WaitingStatus(evaluation)
self.unit.status = ops.WaitingStatus(evaluation)
else:
self.unit.status = BlockedStatus(evaluation)
self.unit.status = ops.BlockedStatus(evaluation)
return False
return True

def _check_kube_control(self, event):
self.unit.status = MaintenanceStatus("Evaluating kubernetes authentication.")
self.unit.status = ops.MaintenanceStatus("Evaluating kubernetes authentication.")
evaluation = self.kube_control.evaluate_relation(event)
if evaluation:
if "Waiting" in evaluation:
self.unit.status = WaitingStatus(evaluation)
self.unit.status = ops.WaitingStatus(evaluation)
else:
self.unit.status = BlockedStatus(evaluation)
self.unit.status = ops.BlockedStatus(evaluation)
return False
if not self.kube_control.get_auth_credentials(self.unit.name):
self.unit.status = WaitingStatus("Waiting for kube-control: unit credentials")
self.unit.status = ops.WaitingStatus("Waiting for kube-control: unit credentials")
return False
self.kube_control.create_kubeconfig(
self._ca_cert_path, self._kubeconfig_path, "root", self.unit.name
)
return True

def _check_certificates(self, event):
self.unit.status = MaintenanceStatus("Evaluating certificates.")
if self.kube_control.get_ca_certificate():
log.info("CA Certificate is available from kube-control.")
return True

self.unit.status = ops.MaintenanceStatus("Evaluating certificates.")
evaluation = self.certificates.evaluate_relation(event)
if evaluation:
if "Waiting" in evaluation:
self.unit.status = WaitingStatus(evaluation)
self.unit.status = ops.WaitingStatus(evaluation)
else:
self.unit.status = BlockedStatus(evaluation)
self.unit.status = ops.BlockedStatus(evaluation)
return False
self._ca_cert_path.write_text(self.certificates.ca)
return True

def _check_config(self):
self.unit.status = MaintenanceStatus("Evaluating charm config.")
self.unit.status = ops.MaintenanceStatus("Evaluating charm config.")
evaluation = self.charm_config.evaluate()
if evaluation:
self.unit.status = BlockedStatus(evaluation)
self.unit.status = ops.BlockedStatus(evaluation)
return False
return True

Expand All @@ -182,12 +183,12 @@ def _merge_config(self, event):
if not self._check_config():
return

self.unit.status = MaintenanceStatus("Evaluating Manifests")
self.unit.status = ops.MaintenanceStatus("Evaluating Manifests")
new_hash = 0
for controller in self.collector.manifests.values():
evaluation = controller.evaluate()
if evaluation:
self.unit.status = BlockedStatus(evaluation)
self.unit.status = ops.BlockedStatus(evaluation)
return
new_hash += controller.hash()

Expand All @@ -201,31 +202,31 @@ def _install_or_upgrade(self, event, config_hash=None):
log.info("Skipping until the config is evaluated.")
return True

self.unit.status = MaintenanceStatus("Deploying Cinder Storage")
self.unit.status = ops.MaintenanceStatus("Deploying Cinder Storage")
self.unit.set_workload_version("")
for controller in self.collector.manifests.values():
try:
controller.apply_manifests()
except ManifestClientError as e:
self.unit.status = WaitingStatus("Waiting for kube-apiserver")
log.warn(f"Encountered retryable installation error: {e}")
self.unit.status = ops.WaitingStatus("Waiting for kube-apiserver")
log.warning("Encountered retryable installation error: %s", e)
event.defer()
return False
return True

def _cleanup(self, event):
if self.stored.config_hash:
self.unit.status = MaintenanceStatus("Cleaning up Openstack Storage")
self.unit.status = ops.MaintenanceStatus("Cleaning up Openstack Storage")
for controller in self.collector.manifests.values():
try:
controller.delete_manifests(ignore_unauthorized=True)
except ManifestClientError:
self.unit.status = WaitingStatus("Waiting for kube-apiserver")
self.unit.status = ops.WaitingStatus("Waiting for kube-apiserver")
event.defer()
return
self.unit.status = MaintenanceStatus("Shutting down")
self.unit.status = ops.MaintenanceStatus("Shutting down")
self._kubeconfig_path.parent.unlink(missing_ok=True)


if __name__ == "__main__":
main(CinderCSICharm)
ops.main(CinderCSICharm)
131 changes: 0 additions & 131 deletions src/requires_certificates.py

This file was deleted.

0 comments on commit 0fb3e72

Please sign in to comment.