Skip to content

Commit

Permalink
implement create_cluster module
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Marten Brüggemann <[email protected]>
  • Loading branch information
brueggemann committed Mar 14, 2024
1 parent e1500b1 commit b189ed1
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ cython_debug/
data.yaml
config.yaml
.ceph
.k8s
10 changes: 8 additions & 2 deletions src/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ ssh:
user: dragon

kubernetes:
host: 192.168.22.10
api_key: abc
config: ../.k8s/config

rook:
cluster:
name: osism-ceph
namespace: osism-ceph
ceph:
image: quay.io/ceph/ceph:v18.2.1

migration_modules:
- migrate_osds
Expand Down
2 changes: 1 addition & 1 deletion src/rookify/modules/analyze_ceph/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class AnalyzeCephHandler(ModuleHandler):
def run(self) -> Dict[str, Any]:
def run(self) -> Any:
commands = ["mon dump", "osd dump", "device ls", "fs dump", "node ls"]

results: Dict[str, Any] = dict()
Expand Down
1 change: 1 addition & 0 deletions src/rookify/modules/create_cluster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# type: ignore

from .main import CreateClusterHandler

Expand Down
102 changes: 102 additions & 0 deletions src/rookify/modules/create_cluster/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# -*- coding: utf-8 -*-

from ..module import ModuleHandler, ModuleException

from typing import Any
import kubernetes


class CreateClusterHandler(ModuleHandler):
def __create_cluster_definition(self) -> Any:
try:
node_ls_data = self._data["analyze_ceph"]["node"]["ls"]

# Get monitor count
mon_count = 0
for node, mons in node_ls_data["mon"].items():
mon_count += 1
if len(mons) > 1:
raise ModuleException(
f"There are more than 1 mon running on node {node}"
)

# Get manager count
mgr_count = 0
for node, mgrs in node_ls_data["mgr"].items():
mgr_count += 1
if len(mons) > 1:
raise ModuleException(
f"There are more than 1 mgr running on node {node}"
)

# Render cluster config from template
self.__cluster_name = self._config["rook"]["cluster"]["name"]
self.__cluster_namespace = self._config["rook"]["cluster"]["namespace"]
self.__cluster_image = self._config["rook"]["ceph"]["image"]
self.__mon_placement_label = (
self._config["rook"]["cluster"]["mon_placement_label"]
if "mon_placement_label" in self._config["rook"]["cluster"]
else f"placement-{self.__cluster_name}-mon"
)
self.__mgr_placement_label = (
self._config["rook"]["cluster"]["mgr_placement_label"]
if "mgr_placement_label" in self._config["rook"]["cluster"]
else f"placement-{self.__cluster_name}-mgr"
)
self.__cluster_definition = self.load_template(
"cluster.yaml.j2",
cluster_name=self.__cluster_name,
cluster_namespace=self.__cluster_namespace,
ceph_image=self.__cluster_image,
mon_count=mon_count,
mgr_count=mgr_count,
mon_placement_label=self.__mon_placement_label,
mgr_placement_label=self.__mgr_placement_label,
)

except KeyError:
raise ModuleException("Ceph monitor data is incomplete")

def __check_k8s_prerequisites(self) -> None:
# We have to check, if our placement labels are disabled or unset
nodes = self.k8s.CoreV1Api.list_node().items
for node in nodes:
node_labels = node.metadata.labels
if (
self.__mon_placement_label in node_labels
and node_labels[self.__mon_placement_label] == "enabled"
):
raise ModuleException(
f"Label {self.__mon_placement_label} is set on node {node.metadata.name}"
)
if (
self.__mgr_placement_label in node_labels
and node_labels[self.__mgr_placement_label] == "enabled"
):
raise ModuleException(
f"Label {self.__mon_placement_label} is set on node {node.metadata.name}"
)

def preflight(self) -> None:
self.__create_cluster_definition()
self.__check_k8s_prerequisites()

def run(self) -> Any:
# Create Namespace
namespace = kubernetes.client.V1Namespace(
metadata=kubernetes.client.V1ObjectMeta(name=self.__cluster_name)
)
try:
self.k8s.CoreV1Api.create_namespace(namespace)
except kubernetes.client.exceptions.ApiException as err:
if err.reason != "Conflict":
raise ModuleException(f"Could not create namespace: {err.reason}")

# Create CephCluster
result = self.k8s.crd_api_apply(
manifest=self.__cluster_definition.yaml
).to_dict()

# Check Cluster

return result
Loading

0 comments on commit b189ed1

Please sign in to comment.