Add deleteNamespace process when tenant deleted

Change-Id: I3089577cf7a6518317ee23704346d489371901ca
Signed-off-by: mozhuli <21621232@zju.edu.cn>
This commit is contained in:
mozhulee 2017-06-30 16:24:14 +08:00
parent 03b3963e38
commit 13db93fe2d
2 changed files with 24 additions and 8 deletions

View File

@ -13,7 +13,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
apierrors "k8s.io/apimachinery/pkg/api/errors" 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" utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api" "k8s.io/client-go/pkg/api"
@ -217,8 +217,8 @@ func (c *TenantController) sync(key string) error {
if !exists { if !exists {
// Delete tenant related resources in k8s // Delete tenant related resources in k8s
tenant := strings.Split(key, "/") tenant := strings.Split(key, "/")
deleteOptions := &apimetav1.DeleteOptions{ deleteOptions := &apismetav1.DeleteOptions{
TypeMeta: apimetav1.TypeMeta{ TypeMeta: apismetav1.TypeMeta{
Kind: "ClusterRoleBinding", Kind: "ClusterRoleBinding",
APIVersion: "rbac.authorization.k8s.io/v1beta1", APIVersion: "rbac.authorization.k8s.io/v1beta1",
}, },
@ -229,6 +229,12 @@ func (c *TenantController) sync(key string) error {
return err return err
} }
glog.V(4).Infof("Deleted ClusterRoleBinding %s", tenant[1]) 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 // Delete all users on a tenant
err = c.osclient.DeleteAllUsersOnTenant(tenant[1]) err = c.osclient.DeleteAllUsersOnTenant(tenant[1])
if err != nil { if err != nil {
@ -256,7 +262,7 @@ func (c *TenantController) sync(key string) error {
func (c *TenantController) createTPRs() error { func (c *TenantController) createTPRs() error {
tprs := []*extensionsobj.ThirdPartyResource{ tprs := []*extensionsobj.ThirdPartyResource{
{ {
ObjectMeta: apimetav1.ObjectMeta{ ObjectMeta: apismetav1.ObjectMeta{
Name: tprTenant, Name: tprTenant,
}, },
Versions: []extensionsobj.APIVersion{ 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 // 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 { if err != nil {
return err return err
} }
@ -330,9 +336,9 @@ func (c *TenantController) createClusterRoles() error {
return nil return nil
} }
func (c *TenantController) createNamespce(namespace string) error { func (c *TenantController) createNamespace(namespace string) error {
_, err := c.kclient.CoreV1().Namespaces().Create(&apiv1.Namespace{ _, err := c.kclient.CoreV1().Namespaces().Create(&apiv1.Namespace{
ObjectMeta: apimetav1.ObjectMeta{ ObjectMeta: apismetav1.ObjectMeta{
Name: namespace, Name: namespace,
}, },
}) })
@ -342,3 +348,12 @@ func (c *TenantController) createNamespce(namespace string) error {
} }
return nil 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
}

View File

@ -16,6 +16,7 @@ import (
"github.com/gophercloud/gophercloud/pagination" "github.com/gophercloud/gophercloud/pagination"
"fmt" "fmt"
drivertypes "git.openstack.org/openstack/stackube/pkg/openstack/types" drivertypes "git.openstack.org/openstack/stackube/pkg/openstack/types"
gcfg "gopkg.in/gcfg.v1" 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) glog.Errorf("Delete openstack user %s error: %v", u.Name, err)
return false, 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 return true, nil
}) })