From 13db93fe2df92fa91c4d61a68199cc19cc2fb4a8 Mon Sep 17 00:00:00 2001 From: mozhulee <21621232@zju.edu.cn> Date: Fri, 30 Jun 2017 16:24:14 +0800 Subject: [PATCH] Add deleteNamespace process when tenant deleted Change-Id: I3089577cf7a6518317ee23704346d489371901ca Signed-off-by: mozhuli <21621232@zju.edu.cn> --- pkg/auth-controller/tenant/controller.go | 29 ++++++++++++++++++------ pkg/openstack/client.go | 3 ++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/auth-controller/tenant/controller.go b/pkg/auth-controller/tenant/controller.go index e9fada1..97dc5ee 100644 --- a/pkg/auth-controller/tenant/controller.go +++ b/pkg/auth-controller/tenant/controller.go @@ -13,7 +13,7 @@ import ( "github.com/golang/glog" apierrors "k8s.io/apimachinery/pkg/api/errors" - apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + apismetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/pkg/api" @@ -217,8 +217,8 @@ func (c *TenantController) sync(key string) error { if !exists { // Delete tenant related resources in k8s tenant := strings.Split(key, "/") - deleteOptions := &apimetav1.DeleteOptions{ - TypeMeta: apimetav1.TypeMeta{ + deleteOptions := &apismetav1.DeleteOptions{ + TypeMeta: apismetav1.TypeMeta{ Kind: "ClusterRoleBinding", APIVersion: "rbac.authorization.k8s.io/v1beta1", }, @@ -229,6 +229,12 @@ func (c *TenantController) sync(key string) error { return err } glog.V(4).Infof("Deleted ClusterRoleBinding %s", tenant[1]) + //Delete namespace + err = c.deleteNamespace(tenant[1]) + if err != nil { + return err + } + glog.V(4).Infof("Deleted namespace %s", tenant[1]) // Delete all users on a tenant err = c.osclient.DeleteAllUsersOnTenant(tenant[1]) if err != nil { @@ -256,7 +262,7 @@ func (c *TenantController) sync(key string) error { func (c *TenantController) createTPRs() error { tprs := []*extensionsobj.ThirdPartyResource{ { - ObjectMeta: apimetav1.ObjectMeta{ + ObjectMeta: apismetav1.ObjectMeta{ Name: tprTenant, }, Versions: []extensionsobj.APIVersion{ @@ -311,7 +317,7 @@ func (c *TenantController) syncTenant(tenant *v1.Tenant) error { } // Create namespace which name is the same as the tenant's name - err = c.createNamespce(tenant.Name) + err = c.createNamespace(tenant.Name) if err != nil { return err } @@ -330,9 +336,9 @@ func (c *TenantController) createClusterRoles() error { return nil } -func (c *TenantController) createNamespce(namespace string) error { +func (c *TenantController) createNamespace(namespace string) error { _, err := c.kclient.CoreV1().Namespaces().Create(&apiv1.Namespace{ - ObjectMeta: apimetav1.ObjectMeta{ + ObjectMeta: apismetav1.ObjectMeta{ Name: namespace, }, }) @@ -342,3 +348,12 @@ func (c *TenantController) createNamespce(namespace string) error { } return nil } + +func (c *TenantController) deleteNamespace(namespace string) error { + err := c.kclient.CoreV1().Namespaces().Delete(namespace, apismetav1.NewDeleteOptions(0)) + if err != nil { + glog.Errorf("Failed delete namespace %s: %v", namespace, err) + return err + } + return nil +} diff --git a/pkg/openstack/client.go b/pkg/openstack/client.go index 2192096..0c435f7 100644 --- a/pkg/openstack/client.go +++ b/pkg/openstack/client.go @@ -16,6 +16,7 @@ import ( "github.com/gophercloud/gophercloud/pagination" "fmt" + drivertypes "git.openstack.org/openstack/stackube/pkg/openstack/types" gcfg "gopkg.in/gcfg.v1" ) @@ -223,7 +224,7 @@ func (c *Client) DeleteAllUsersOnTenant(tenantName string) error { glog.Errorf("Delete openstack user %s error: %v", u.Name, err) return false, err } - glog.V(4).Infof("User %s deleted: %v", u.Name, res) + glog.V(4).Infof("User %s deleted", u.Name) } return true, nil })