tenant.go 4.4 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*

 Copyright 2019 The KubeSphere Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

*/
package tenant

import (
	"k8s.io/api/core/v1"
H
hongming 已提交
22 23
	k8sinformers "k8s.io/client-go/informers"
	"k8s.io/client-go/kubernetes"
H
hongming 已提交
24
	"kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
H
hongming 已提交
25
	ksinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
H
hongming 已提交
26
	"kubesphere.io/kubesphere/pkg/models"
H
update  
hongming 已提交
27
	"kubesphere.io/kubesphere/pkg/models/iam/am"
J
Jeff 已提交
28
	"kubesphere.io/kubesphere/pkg/server/params"
H
hongming 已提交
29
	"kubesphere.io/kubesphere/pkg/simple/client/mysql"
H
hongming 已提交
30 31
)

H
hongming 已提交
32 33 34 35 36 37
type Interface interface {
	CreateNamespace(workspace string, namespace *v1.Namespace, username string) (*v1.Namespace, error)
	DeleteNamespace(workspace, namespace string) error
	DescribeWorkspace(username, workspace string) (*v1alpha1.Workspace, error)
	ListWorkspaces(username string, conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
	ListNamespaces(username string, conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error)
H
hongming 已提交
38 39 40
	ListDevopsProjects(username string, conditions *params.Conditions, orderBy string, reverse bool, limit int, offset int) (*models.PageableResponse, error)
	CountDevOpsProjects(username string) (uint32, error)
	DeleteDevOpsProject(username, projectId string) error
H
hongming 已提交
41
}
H
hongming 已提交
42

H
hongming 已提交
43 44 45
type tenantOperator struct {
	workspaces WorkspaceInterface
	namespaces NamespaceInterface
H
update  
hongming 已提交
46
	am         am.AccessManagementInterface
H
hongming 已提交
47 48 49 50 51 52 53 54 55 56 57 58
	devops     DevOpsProjectOperator
}

func (t *tenantOperator) CountDevOpsProjects(username string) (uint32, error) {
	return t.devops.GetDevOpsProjectsCount(username)
}

func (t *tenantOperator) DeleteDevOpsProject(username, projectId string) error {
	return t.devops.DeleteDevOpsProject(projectId, username)
}

func (t *tenantOperator) GetUserDevopsSimpleRules(username string, projectId string) (interface{}, error) {
H
update  
hongming 已提交
59
	panic("implement me")
H
hongming 已提交
60 61 62 63
}

func (t *tenantOperator) ListDevopsProjects(username string, conditions *params.Conditions, orderBy string, reverse bool, limit int, offset int) (*models.PageableResponse, error) {
	return t.devops.ListDevOpsProjects(conditions.Match["workspace"], username, conditions, orderBy, reverse, limit, offset)
H
hongming 已提交
64 65 66 67 68
}

func (t *tenantOperator) DeleteNamespace(workspace, namespace string) error {
	return t.workspaces.DeleteNamespace(workspace, namespace)
}
H
hongming 已提交
69

H
hongming 已提交
70
func New(client kubernetes.Interface, informers k8sinformers.SharedInformerFactory, ksinformers ksinformers.SharedInformerFactory, db *mysql.Database) Interface {
H
update  
hongming 已提交
71
	amOperator := am.NewAMOperator(client, informers)
H
hongming 已提交
72
	return &tenantOperator{
H
hongming 已提交
73 74 75
		workspaces: newWorkspaceOperator(client, informers, ksinformers, amOperator, db),
		namespaces: newNamespaceOperator(client, informers, amOperator),
		am:         amOperator,
H
hongming 已提交
76 77
	}
}
H
hongming 已提交
78

H
hongming 已提交
79 80
func (t *tenantOperator) CreateNamespace(workspaceName string, namespace *v1.Namespace, username string) (*v1.Namespace, error) {
	return t.namespaces.CreateNamespace(workspaceName, namespace, username)
H
hongming 已提交
81 82
}

H
hongming 已提交
83 84
func (t *tenantOperator) DescribeWorkspace(username, workspaceName string) (*v1alpha1.Workspace, error) {
	workspace, err := t.workspaces.GetWorkspace(workspaceName)
H
hongming 已提交
85 86 87 88 89 90 91 92

	if err != nil {
		return nil, err
	}

	return workspace, nil
}

H
hongming 已提交
93
func (t *tenantOperator) ListWorkspaces(username string, conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
H
update  
hongming 已提交
94
	panic("implement me")
H
hongming 已提交
95 96
}

H
hongming 已提交
97
func (t *tenantOperator) ListNamespaces(username string, conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
H
hongming 已提交
98

H
hongming 已提交
99
	namespaces, err := t.namespaces.Search(username, conditions, orderBy, reverse)
H
hongming 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

	if err != nil {
		return nil, err
	}

	// limit offset
	result := make([]interface{}, 0)
	for i, v := range namespaces {
		if len(result) < limit && i >= offset {
			result = append(result, v)
		}
	}

	return &models.PageableResponse{Items: result, TotalCount: len(namespaces)}, nil
}