workspaces.go 3.9 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 22 23 24 25 26
/*

 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 (
	rbacv1 "k8s.io/api/rbac/v1"
	"k8s.io/apimachinery/pkg/labels"
	"kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
	"kubesphere.io/kubesphere/pkg/constants"
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models/iam"
H
hongming 已提交
27
	"kubesphere.io/kubesphere/pkg/models/resources"
H
hongming 已提交
28
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
29
	"kubesphere.io/kubesphere/pkg/utils/sliceutil"
H
hongming 已提交
30 31 32 33 34 35 36 37 38 39 40
	"sort"
	"strings"
)

type workspaceSearcher struct {
}

// Exactly Match
func (*workspaceSearcher) match(match map[string]string, item *v1alpha1.Workspace) bool {
	for k, v := range match {
		switch k {
H
hongming 已提交
41 42 43
		case resources.Name:
			names := strings.Split(v, "|")
			if !sliceutil.HasString(names, item.Name) {
H
hongming 已提交
44 45
				return false
			}
H
hongming 已提交
46
		case resources.Keyword:
H
hongming 已提交
47 48 49
			if !strings.Contains(item.Name, v) && !contains(item.Labels, "", v) && !contains(item.Annotations, "", v) {
				return false
			}
H
hongming 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
		default:
			if item.Labels[k] != v {
				return false
			}
		}
	}
	return true
}

func (*workspaceSearcher) fuzzy(fuzzy map[string]string, item *v1alpha1.Workspace) bool {

	for k, v := range fuzzy {
		switch k {
H
hongming 已提交
63 64
		case resources.Name:
			if !strings.Contains(item.Name, v) && !strings.Contains(item.Annotations[constants.DisplayNameAnnotationKey], v) {
H
hongming 已提交
65 66 67 68 69 70 71 72 73 74 75 76
				return false
			}
		default:
			return false
		}
	}

	return true
}

func (*workspaceSearcher) compare(a, b *v1alpha1.Workspace, orderBy string) bool {
	switch orderBy {
H
hongming 已提交
77
	case resources.CreateTime:
H
hongming 已提交
78
		return a.CreationTimestamp.Time.Before(b.CreationTimestamp.Time)
H
hongming 已提交
79
	case resources.Name:
H
hongming 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		fallthrough
	default:
		return strings.Compare(a.Name, b.Name) <= 0
	}
}

func (s *workspaceSearcher) search(username string, conditions *params.Conditions, orderBy string, reverse bool) ([]*v1alpha1.Workspace, error) {
	rules, err := iam.GetUserClusterRules(username)

	if err != nil {
		return nil, err
	}

	workspaces := make([]*v1alpha1.Workspace, 0)

	if iam.RulesMatchesRequired(rules, rbacv1.PolicyRule{Verbs: []string{"list"}, APIGroups: []string{"tenant.kubesphere.io"}, Resources: []string{"workspaces"}}) {
		workspaces, err = informers.KsSharedInformerFactory().Tenant().V1alpha1().Workspaces().Lister().List(labels.Everything())
		if err != nil {
			return nil, err
		}
	} else {
		workspaceRoles, err := iam.GetUserWorkspaceRoleMap(username)
		if err != nil {
			return nil, err
		}
		for k := range workspaceRoles {
			workspace, err := informers.KsSharedInformerFactory().Tenant().V1alpha1().Workspaces().Lister().Get(k)
			if err != nil {
				return nil, err
			}
			workspaces = append(workspaces, workspace)
		}
	}

	result := make([]*v1alpha1.Workspace, 0)

	for _, workspace := range workspaces {
		if s.match(conditions.Match, workspace) && s.fuzzy(conditions.Fuzzy, workspace) {
			result = append(result, workspace)
		}
	}

	// order & reverse
	sort.Slice(result, func(i, j int) bool {
		if reverse {
			tmp := i
			i = j
			j = tmp
		}
		return s.compare(result[i], result[j], orderBy)
	})

	return result, nil
}

func GetWorkspace(workspaceName string) (*v1alpha1.Workspace, error) {
	return informers.KsSharedInformerFactory().Tenant().V1alpha1().Workspaces().Lister().Get(workspaceName)
}
H
hongming 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150

func contains(m map[string]string, key, value string) bool {
	for k, v := range m {
		if key == "" {
			if strings.Contains(k, value) || strings.Contains(v, value) {
				return true
			}
		} else if k == key && strings.Contains(v, value) {
			return true
		}
	}
	return false
}