namespaces.go 4.0 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
/*

 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 (
H
hongming 已提交
21
	"github.com/golang/glog"
H
hongming 已提交
22 23 24
	"k8s.io/api/core/v1"
	rbacv1 "k8s.io/api/rbac/v1"
	"k8s.io/apimachinery/pkg/labels"
H
hongming 已提交
25
	"kubesphere.io/kubesphere/pkg/constants"
H
hongming 已提交
26 27
	"kubesphere.io/kubesphere/pkg/informers"
	"kubesphere.io/kubesphere/pkg/models/iam"
H
hongming 已提交
28
	"kubesphere.io/kubesphere/pkg/models/resources"
H
hongming 已提交
29
	"kubesphere.io/kubesphere/pkg/params"
H
hongming 已提交
30
	"kubesphere.io/kubesphere/pkg/utils/sliceutil"
H
hongming 已提交
31 32 33 34 35 36 37 38 39 40 41
	"sort"
	"strings"
)

type namespaceSearcher struct {
}

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

func (*namespaceSearcher) fuzzy(fuzzy map[string]string, item *v1.Namespace) bool {

	for k, v := range fuzzy {
		switch k {
H
hongming 已提交
65 66
		case resources.Name:
			if !strings.Contains(item.Name, v) && !strings.Contains(item.Annotations[constants.DisplayNameAnnotationKey], v) {
H
hongming 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
				return false
			}
		default:
			return false
		}
	}

	return true
}

func (*namespaceSearcher) compare(a, b *v1.Namespace, orderBy string) bool {
	switch orderBy {
	case "createTime":
		return a.CreationTimestamp.Time.Before(b.CreationTimestamp.Time)
	case "name":
		fallthrough
	default:
		return strings.Compare(a.Name, b.Name) <= 0
	}
}

func (*namespaceSearcher) GetNamespaces(username string) ([]*v1.Namespace, error) {

	roles, err := iam.GetUserRoles("", username)

	if err != nil {
		return nil, err
	}
	namespaces := make([]*v1.Namespace, 0)
	namespaceLister := informers.SharedInformerFactory().Core().V1().Namespaces().Lister()
	for _, role := range roles {
		namespace, err := namespaceLister.Get(role.Namespace)
		if err != nil {
H
hongming 已提交
100
			glog.Errorf("get namespace failed: %+v", err)
H
hongming 已提交
101 102
			return nil, err
		}
H
hongming 已提交
103 104 105
		if !containsNamespace(namespaces, namespace) {
			namespaces = append(namespaces, namespace)
		}
H
hongming 已提交
106 107 108 109 110
	}

	return namespaces, nil
}

H
hongming 已提交
111 112 113 114 115 116 117 118 119
func containsNamespace(namespaces []*v1.Namespace, namespace *v1.Namespace) bool {
	for _, item := range namespaces {
		if item.Name == namespace.Name {
			return true
		}
	}
	return false
}

H
hongming 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
func (s *namespaceSearcher) search(username string, conditions *params.Conditions, orderBy string, reverse bool) ([]*v1.Namespace, error) {

	rules, err := iam.GetUserClusterRules(username)

	if err != nil {
		return nil, err
	}

	namespaces := make([]*v1.Namespace, 0)

	if iam.RulesMatchesRequired(rules, rbacv1.PolicyRule{Verbs: []string{"list"}, APIGroups: []string{"tenant.kubesphere.io"}, Resources: []string{"namespaces"}}) {
		namespaces, err = informers.SharedInformerFactory().Core().V1().Namespaces().Lister().List(labels.Everything())
	} else {
		namespaces, err = s.GetNamespaces(username)
	}

	if err != nil {
		return nil, err
	}

	result := make([]*v1.Namespace, 0)

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

	// 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
}