From bcd2ed7bac0fd23f71ee0f0496fdc17feff53803 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Rodriguez Date: Thu, 14 Nov 2024 12:22:12 +0100 Subject: [PATCH] Create CephBlockPoolRadosNamespace for OCPV clusters (#97) * Create CephBlockPoolRadosNamespace for OCPV clusters * Create CephBlockPoolRadosNamespace for OCPV clusters --- internal/models/ocp_sandbox.go | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/internal/models/ocp_sandbox.go b/internal/models/ocp_sandbox.go index 3d6071e..bc0d29a 100644 --- a/internal/models/ocp_sandbox.go +++ b/internal/models/ocp_sandbox.go @@ -17,6 +17,9 @@ import ( rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -1098,6 +1101,14 @@ func (a *OcpSandboxProvider) Request(serviceUuid string, cloud_selector map[stri return } + // Create an dynamic OpenShift client for non regular objects + dynclientset, err := dynamic.NewForConfig(config) + if err != nil { + log.Logger.Error("Error creating OCP client", "error", err) + rnew.SetStatus("error") + return + } + serviceAccountName := "sandbox" suffix := annotations["namespace_suffix"] if suffix == "" { @@ -1395,6 +1406,33 @@ func (a *OcpSandboxProvider) Request(serviceUuid string, cloud_selector map[stri } } } + // TODO: decide if we want another flag to configure the RadosNamespace + // Define the CephBlockPoolRadosNamespace GroupVersionResource + cephBlockPoolRadosNamespaceGVR := schema.GroupVersionResource{ + Group: "ceph.rook.io", + Version: "v1", + Resource: "cephblockpoolradosnamespaces", + } + // Create the CephBlockPoolRadosNamespace object as an unstructured object + cephBlockPoolRadosNamespace := &unstructured.Unstructured{ + Object: map[string]any{ + "apiVersion": "ceph.rook.io/v1", + "kind": "CephBlockPoolRadosNamespace", + "metadata": map[string]any{ + "name": namespaceName, + "namespace": "openshift-storage", + }, + "spec": map[string]any{ + "blockPoolName": "ocpv-tenants", + }, + }, + } + _, err = dynclientset.Resource(cephBlockPoolRadosNamespaceGVR).Namespace("openshift-storage").Create(context.TODO(), cephBlockPoolRadosNamespace, metav1.CreateOptions{}) + if err != nil { + log.Logger.Error("Error creating CephBlockPoolRadosNamespace", "error", err) + } + + log.Logger.Debug("CephBlockPoolRadosNamespace created successfully") } // Create secret to generate a token, for the clusters without image registry and for future versions of OCP @@ -1730,6 +1768,14 @@ func (account *OcpSandboxWithCreds) Delete() error { return err } + // Create an dynamic OpenShift client for non regular objects + dynclientset, err := dynamic.NewForConfig(config) + if err != nil { + log.Logger.Error("Error creating OCP client", "error", err, "name", account.Name) + account.SetStatus("error") + return err + } + // Check if the namespace exists _, err = clientset.CoreV1().Namespaces().Get(context.TODO(), account.Namespace, metav1.GetOptions{}) if err != nil { @@ -1772,6 +1818,21 @@ func (account *OcpSandboxWithCreds) Delete() error { } } + // Delete the cephBlockPoolRadosNamespace from the openshift-storage namespace + // Define the CephBlockPoolRadosNamespace GroupVersionResource + cephBlockPoolRadosNamespaceGVR := schema.GroupVersionResource{ + Group: "ceph.rook.io", + Version: "v1", + Resource: "cephblockpoolradosnamespaces", + } + if _, err := dynclientset.Resource(cephBlockPoolRadosNamespaceGVR).Namespace("openshift-storage").Get(context.TODO(), account.Namespace, metav1.GetOptions{}); err == nil { + if err := dynclientset.Resource(cephBlockPoolRadosNamespaceGVR).Namespace("openshift-storage").Delete(context.TODO(), account.Namespace, metav1.DeleteOptions{}); err != nil { + log.Logger.Error("Error deleting rolebinding on CephBlockPoolRadosNamespace", "error", err) + account.SetStatus("error") + return err + } + } + _, err = account.Provider.DbPool.Exec( context.Background(), "DELETE FROM resources WHERE id = $1",