From d21f319551590850ac2364f4f48437b970f9830d Mon Sep 17 00:00:00 2001 From: Priyashetty17 Date: Tue, 19 Mar 2024 10:38:37 -0400 Subject: [PATCH] Modify CreateNamespace to accept clusterID and projectName instead of norman project --- extensions/kubeapi/namespaces/create.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/extensions/kubeapi/namespaces/create.go b/extensions/kubeapi/namespaces/create.go index 3003cf89..a4f484cf 100644 --- a/extensions/kubeapi/namespaces/create.go +++ b/extensions/kubeapi/namespaces/create.go @@ -3,10 +3,8 @@ package namespaces import ( "context" "fmt" - "strings" "github.com/rancher/shepherd/clients/rancher" - management "github.com/rancher/shepherd/clients/rancher/generated/management/v3" "github.com/rancher/shepherd/extensions/defaults" "github.com/rancher/shepherd/extensions/unstructured" "github.com/rancher/shepherd/pkg/api/scheme" @@ -21,17 +19,20 @@ import ( // CreateNamespace is a helper function that uses the dynamic client to create a namespace on a project. // It registers a delete function with a wait.WatchWait to ensure the namspace is deleted cleanly. -func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultResourceLimit string, labels, annotations map[string]string, project *management.Project) (*coreV1.Namespace, error) { - // Namespace object for a project name space +func CreateNamespace(client *rancher.Client, clusterID, projectName, namespaceName, containerDefaultResourceLimit string, labels, annotations map[string]string) (*coreV1.Namespace, error) { if annotations == nil { annotations = make(map[string]string) } + if containerDefaultResourceLimit != "" { annotations["field.cattle.io/containerDefaultResourceLimit"] = containerDefaultResourceLimit } - if project != nil { - annotations["field.cattle.io/projectId"] = project.ID + + if projectName != "" { + annotationValue := clusterID + ":" + projectName + annotations["field.cattle.io/projectId"] = annotationValue } + namespace := &coreV1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: namespaceName, @@ -40,7 +41,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso }, } - dynamicClient, err := client.GetDownStreamClusterClient(project.ClusterID) + dynamicClient, err := client.GetDownStreamClusterClient(clusterID) if err != nil { return nil, err } @@ -50,7 +51,7 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso return nil, err } - adminDynamicClient, err := adminClient.GetDownStreamClusterClient(project.ClusterID) + adminDynamicClient, err := adminClient.GetDownStreamClusterClient(clusterID) if err != nil { return nil, err } @@ -63,10 +64,9 @@ func CreateNamespace(client *rancher.Client, namespaceName, containerDefaultReso } clusterRoleResource := adminDynamicClient.Resource(rbacv1.SchemeGroupVersion.WithResource("clusterroles")) - projectID := strings.Split(project.ID, ":")[1] clusterRoleWatch, err := clusterRoleResource.Watch(context.TODO(), metav1.ListOptions{ - FieldSelector: "metadata.name=" + fmt.Sprintf("%s-namespaces-edit", projectID), + FieldSelector: "metadata.name=" + fmt.Sprintf("%s-namespaces-edit", projectName), TimeoutSeconds: &defaults.WatchTimeoutSeconds, })