workspaces.go 7.5 KB
Newer Older
J
jeff 已提交
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 workspaces

import (
	"fmt"
H
hongming 已提交
22
	"k8s.io/apimachinery/pkg/runtime/schema"
H
hongming 已提交
23 24 25 26
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models"
	"kubesphere.io/kubesphere/pkg/models/iam"
H
hongming 已提交
27 28
	"kubesphere.io/kubesphere/pkg/models/resources"
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
29
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
H
hongming 已提交
30
	"kubesphere.io/kubesphere/pkg/simple/client/kubesphere"
H
hongming 已提交
31
	"kubesphere.io/kubesphere/pkg/simple/client/mysql"
H
hongming 已提交
32 33
	"kubesphere.io/kubesphere/pkg/utils/k8sutil"
	"kubesphere.io/kubesphere/pkg/utils/sliceutil"
J
jeff 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46

	"strings"

	core "k8s.io/api/core/v1"

	"errors"
	"k8s.io/api/rbac/v1"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
)

func UnBindDevopsProject(workspace string, devops string) error {
H
hongming 已提交
47
	db := mysql.Client()
H
hongming 已提交
48
	return db.Delete(&models.WorkspaceDPBinding{Workspace: workspace, DevOpsProject: devops}).Error
J
jeff 已提交
49 50
}

H
hongming 已提交
51
func CreateDevopsProject(username string, workspace string, devops *models.DevopsProject) (*models.DevopsProject, error) {
J
jeff 已提交
52

H
hongming 已提交
53
	created, err := kubesphere.Client().CreateDevopsProject(username, devops)
J
jeff 已提交
54 55 56 57 58

	if err != nil {
		return nil, err
	}

H
hongming 已提交
59
	err = BindingDevopsProject(workspace, created.ProjectId)
J
jeff 已提交
60 61 62 63 64

	if err != nil {
		return nil, err
	}

H
hongming 已提交
65
	return created, nil
J
jeff 已提交
66 67 68
}

func Namespaces(workspaceName string) ([]*core.Namespace, error) {
H
hongming 已提交
69
	namespaceLister := informers.SharedInformerFactory().Core().V1().Namespaces().Lister()
H
hongming 已提交
70
	namespaces, err := namespaceLister.List(labels.SelectorFromSet(labels.Set{constants.WorkspaceLabelKey: workspaceName}))
J
jeff 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

	if err != nil {
		return nil, err
	}

	if namespaces == nil {
		return make([]*core.Namespace, 0), nil
	}

	out := make([]*core.Namespace, len(namespaces))

	for i, v := range namespaces {
		out[i] = v.DeepCopy()
	}

	return out, nil
}

func BindingDevopsProject(workspace string, devops string) error {
H
hongming 已提交
90
	db := mysql.Client()
H
hongming 已提交
91
	return db.Create(&models.WorkspaceDPBinding{Workspace: workspace, DevOpsProject: devops}).Error
J
jeff 已提交
92 93 94
}

func DeleteNamespace(workspace string, namespaceName string) error {
H
hongming 已提交
95
	namespace, err := k8s.Client().CoreV1().Namespaces().Get(namespaceName, meta_v1.GetOptions{})
J
jeff 已提交
96 97 98
	if err != nil {
		return err
	}
H
hongming 已提交
99
	if namespace.Labels[constants.WorkspaceLabelKey] == workspace {
J
jeff 已提交
100
		deletePolicy := meta_v1.DeletePropagationForeground
H
hongming 已提交
101
		return k8s.Client().CoreV1().Namespaces().Delete(namespaceName, &meta_v1.DeleteOptions{PropagationPolicy: &deletePolicy})
J
jeff 已提交
102 103 104 105 106
	} else {
		return errors.New("resource not found")
	}
}

H
hongming 已提交
107 108
func RemoveUser(workspaceName string, username string) error {
	workspaceRole, err := iam.GetUserWorkspaceRole(workspaceName, username)
J
jeff 已提交
109 110 111
	if err != nil {
		return err
	}
H
hongming 已提交
112
	err = DeleteWorkspaceRoleBinding(workspaceName, username, workspaceRole.Annotations[constants.DisplayNameAnnotationKey])
J
jeff 已提交
113 114 115 116 117 118
	if err != nil {
		return err
	}
	return nil
}

H
hongming 已提交
119
func InviteUser(workspaceName string, user *models.User) error {
J
jeff 已提交
120

H
hongming 已提交
121
	workspaceRole, err := iam.GetUserWorkspaceRole(workspaceName, user.Username)
J
jeff 已提交
122

H
hongming 已提交
123
	if err != nil && !apierrors.IsNotFound(err) {
J
jeff 已提交
124 125 126
		return err
	}

H
hongming 已提交
127
	workspaceRoleName := fmt.Sprintf("workspace:%s:%s", workspaceName, strings.TrimPrefix(user.WorkspaceRole, "workspace-"))
J
jeff 已提交
128

H
hongming 已提交
129 130
	if workspaceRole != nil && workspaceRole.Name != workspaceRoleName {
		err := DeleteWorkspaceRoleBinding(workspaceName, user.Username, user.WorkspaceRole)
J
jeff 已提交
131 132 133 134 135
		if err != nil {
			return err
		}
	}

H
hongming 已提交
136
	return CreateWorkspaceRoleBinding(workspaceName, user.Username, user.WorkspaceRole)
J
jeff 已提交
137 138
}

H
hongming 已提交
139
func CreateWorkspaceRoleBinding(workspace, username string, role string) error {
J
jeff 已提交
140

H
hongming 已提交
141 142
	if !sliceutil.HasString(constants.WorkSpaceRoles, role) {
		return apierrors.NewNotFound(schema.GroupResource{Resource: "workspace role"}, role)
J
jeff 已提交
143 144
	}

H
hongming 已提交
145 146 147
	roleBindingName := fmt.Sprintf("workspace:%s:%s", workspace, strings.TrimPrefix(role, "workspace-"))
	workspaceRoleBinding, err := informers.SharedInformerFactory().Rbac().V1().ClusterRoleBindings().Lister().Get(roleBindingName)
	workspaceRoleBinding = workspaceRoleBinding.DeepCopy()
J
jeff 已提交
148 149 150 151
	if err != nil {
		return err
	}

H
hongming 已提交
152 153 154
	if !k8sutil.ContainsUser(workspaceRoleBinding.Subjects, username) {
		workspaceRoleBinding.Subjects = append(workspaceRoleBinding.Subjects, v1.Subject{APIGroup: "rbac.authorization.k8s.io", Kind: "User", Name: username})
		_, err = k8s.Client().RbacV1().ClusterRoleBindings().Update(workspaceRoleBinding)
J
jeff 已提交
155 156
	}

H
hongming 已提交
157
	return err
J
jeff 已提交
158 159
}

H
hongming 已提交
160
func DeleteWorkspaceRoleBinding(workspace, username string, role string) error {
J
jeff 已提交
161

H
hongming 已提交
162 163
	if !sliceutil.HasString(constants.WorkSpaceRoles, role) {
		return apierrors.NewNotFound(schema.GroupResource{Resource: "workspace role"}, role)
J
jeff 已提交
164 165
	}

H
hongming 已提交
166
	roleBindingName := fmt.Sprintf("workspace:%s:%s", workspace, strings.TrimPrefix(role, "workspace-"))
J
jeff 已提交
167

H
hongming 已提交
168
	workspaceRoleBinding, err := informers.SharedInformerFactory().Rbac().V1().ClusterRoleBindings().Lister().Get(roleBindingName)
J
jeff 已提交
169 170 171
	if err != nil {
		return err
	}
H
hongming 已提交
172
	workspaceRoleBinding = workspaceRoleBinding.DeepCopy()
J
jeff 已提交
173

H
hongming 已提交
174 175 176 177
	for i, v := range workspaceRoleBinding.Subjects {
		if v.Kind == v1.UserKind && v.Name == username {
			workspaceRoleBinding.Subjects = append(workspaceRoleBinding.Subjects[:i], workspaceRoleBinding.Subjects[i+1:]...)
			i--
J
jeff 已提交
178 179 180
		}
	}

H
hongming 已提交
181 182 183
	workspaceRoleBinding, err = k8s.Client().RbacV1().ClusterRoleBindings().Update(workspaceRoleBinding)

	return err
J
jeff 已提交
184 185 186 187
}

func GetDevOpsProjects(workspaceName string) ([]string, error) {

H
hongming 已提交
188
	db := mysql.Client()
J
jeff 已提交
189

H
hongming 已提交
190
	var workspaceDOPBindings []models.WorkspaceDPBinding
J
jeff 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203

	if err := db.Where("workspace = ?", workspaceName).Find(&workspaceDOPBindings).Error; err != nil {
		return nil, err
	}

	devOpsProjects := make([]string, 0)

	for _, workspaceDOPBinding := range workspaceDOPBindings {
		devOpsProjects = append(devOpsProjects, workspaceDOPBinding.DevOpsProject)
	}
	return devOpsProjects, nil
}

H
hongming 已提交
204 205
func WorkspaceUserCount(workspace string) (int, error) {
	count, err := iam.WorkspaceUsersTotalCount(workspace)
J
jeff 已提交
206
	if err != nil {
H
hongming 已提交
207
		return 0, err
J
jeff 已提交
208
	}
H
hongming 已提交
209
	return count, nil
J
jeff 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
}

func GetOrgRoles(name string) ([]string, error) {
	return constants.WorkSpaceRoles, nil
}

func WorkspaceNamespaces(workspaceName string) ([]string, error) {
	ns, err := Namespaces(workspaceName)

	namespaces := make([]string, 0)

	if err != nil {
		return namespaces, err
	}

	for i := 0; i < len(ns); i++ {
		namespaces = append(namespaces, ns[i].Name)
	}

	return namespaces, nil
}

Z
zryfish 已提交
232
func WorkspaceCount() (int, error) {
J
jeff 已提交
233

H
hongming 已提交
234
	ws, err := resources.ListResources("", resources.Workspaces, &params.Conditions{}, "", false, 1, 0)
J
jeff 已提交
235 236 237 238 239

	if err != nil {
		return 0, err
	}

H
hongming 已提交
240
	return ws.TotalCount, nil
J
jeff 已提交
241 242 243
}

func GetAllProjectNums() (int, error) {
H
hongming 已提交
244
	namespaceLister := informers.SharedInformerFactory().Core().V1().Namespaces().Lister()
J
jeff 已提交
245 246 247 248 249 250 251 252
	list, err := namespaceLister.List(labels.Everything())
	if err != nil {
		return 0, err
	}
	return len(list), nil
}

func GetAllDevOpsProjectsNums() (int, error) {
H
hongming 已提交
253
	db := mysql.Client()
J
jeff 已提交
254
	var count int
H
hongming 已提交
255
	if err := db.Model(&models.WorkspaceDPBinding{}).Count(&count).Error; err != nil {
J
jeff 已提交
256 257 258 259 260 261
		return 0, err
	}
	return count, nil
}

func GetAllAccountNums() (int, error) {
H
hongming 已提交
262
	users, err := iam.ListUsers(&params.Conditions{}, "", false, 1, 0)
J
jeff 已提交
263 264 265
	if err != nil {
		return 0, err
	}
H
hongming 已提交
266
	return users.TotalCount, nil
J
jeff 已提交
267
}