summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2023-10-24 15:42:22 +0200
committerPatrick Spek <p.spek@tyil.nl>2023-10-24 15:42:22 +0200
commit6ba3dec1e3b5fe5e5f81fa5c273caa9d2ac4309c (patch)
tree4589f01e6b61366c8f8b54e70c2403d997a8a3ae
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--nfs_operator.py87
2 files changed, 88 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bee8a64
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+__pycache__
diff --git a/nfs_operator.py b/nfs_operator.py
new file mode 100644
index 0000000..321a37c
--- /dev/null
+++ b/nfs_operator.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+
+import kopf
+import kubernetes
+import os
+import pathlib
+import uuid
+
+# Create the configuration dict
+configuration = { }
+
+@kopf.on.startup()
+def init(logger, memo: kopf.Memo, **kwargs):
+ # Set the configuration
+ memo.config = {
+ "server": os.environ.get("X_NFS_OPERATOR_SERVER_ADDRESS", "127.0.0.1"),
+ "basedir": os.environ.get("X_NFS_OPERATOR_BASEDIR", "/var/nfs"),
+ "pvcPrefix": os.environ.get("X_NFS_OPERATOR_PREFIX", "pvc-"),
+ "name": os.environ.get("X_NFS_OPERATOR_NAME", "nfs-provisioner"),
+ "storageClassName": os.environ.get("X_NFS_OPERATOR_STORAGE_CLASS_NAME", "nfs"),
+ }
+
+@kopf.on.resume("persistentvolumeclaim")
+@kopf.on.create("persistentvolumeclaim")
+def create_pv(spec, name, namespace, annotations, logger, memo: kopf.Memo, **kwargs):
+ # Check whether the PVC has the correct storageClassName
+ if spec.get("storageClassName") != memo["config"]["storageClassName"]:
+ logger.debug("Not handling storageClassName '" + spec.get("storageClassName") + "'")
+ return
+
+ # Generate some names and paths for later use
+ volume = uuid.uuid4()
+ pvc_name = memo["config"]["pvcPrefix"] + str(volume)
+ pvc_path = pathlib.Path(annotations.get("nfs-operator.tyil.nl/basedir", memo["config"]["basedir"]) + "/" + pvc_name)
+
+ # Create NFS share
+ logger.info(f"Creating directory at {pvc_path}")
+ pvc_path.mkdir(parents = True)
+
+ # Connect to the Kubernetes API
+ k8s = kubernetes.client.CoreV1Api()
+
+ # Create PV
+ logger.info(f"Creating PersistentVolume {pvc_name}")
+ #k8s.create_persistent_volume({
+ # "metadata": {
+ # "name": name,
+ # "labels": {
+ # "app.kubernetes.io/managed-by": configuration["name"],
+ # "app.kubernetes.io/name": name,
+ # "pv.kubernetes.io/provisioned-by": configuration["name"],
+ # },
+ # },
+ # "spec": {
+ # "accessModes": spec.get("accessModes"),
+ # "capacity": {
+ # "storage": spec.get("resources.requests.storage"),
+ # },
+ # "storageClassName": spec.get("storageClassName"),
+ # "volumeMode": "Filesystem",
+ # "persistentVolumeReclaimPolicy": "Delete",
+ # "nfs": {
+ # "path": pvc_path,
+ # "server": memo["config"]["server"],
+ # },
+ # "mountOptions": [
+ # "hard",
+ # ],
+ # },
+ #})
+
+ # Update PVC
+ logger.info(f"Updating PersistentVolumeClaim {namespace}/{name}")
+ k8s.patch_namespaced_persistent_volume_claim(name, namespace, {
+ "spec": {
+ "volumeMode": "Filesystem",
+ "volumeName": pvc_name,
+ },
+ })
+
+@kopf.on.update("persistentvolumeclaim")
+def update_pv(logger, **kwargs):
+ logger.debug("PVC updates are NYI")
+
+@kopf.on.delete("persistentvolumeclaim")
+def delete_pv(logger, **kwargs):
+ logger.debug("Deleting the thing...")