Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create CephBlockPoolRadosNamespace for OCPV clusters #97

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions internal/models/ocp_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
Expand Down
Loading