workspaceroles.go 3.7 KB
Newer Older
H
hongming 已提交
1
/*
H
hongming 已提交
2
Copyright 2019 The KubeSphere Authors.
H
hongming 已提交
3

H
hongming 已提交
4 5 6
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
H
hongming 已提交
7

H
hongming 已提交
8
    http://www.apache.org/licenses/LICENSE-2.0
H
hongming 已提交
9

H
hongming 已提交
10 11 12 13 14
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.
H
hongming 已提交
15
*/
H
hongming 已提交
16

H
hongming 已提交
17 18 19
package workspacerole

import (
H
hongming 已提交
20 21
	"encoding/json"
	"k8s.io/apimachinery/pkg/api/errors"
H
hongming 已提交
22
	"k8s.io/apimachinery/pkg/runtime"
H
hongming 已提交
23
	"k8s.io/klog"
H
hongming 已提交
24 25
	"kubesphere.io/kubesphere/pkg/api"
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
H
hongming 已提交
26
	tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
H
hongming 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
	"kubesphere.io/kubesphere/pkg/apiserver/query"
	informers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
)

type workspacerolesGetter struct {
	sharedInformers informers.SharedInformerFactory
}

func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
	return &workspacerolesGetter{sharedInformers: sharedInformers}
}

func (d *workspacerolesGetter) Get(_, name string) (runtime.Object, error) {
	return d.sharedInformers.Iam().V1alpha2().WorkspaceRoles().Lister().Get(name)
}

H
hongming 已提交
44 45 46 47 48 49 50 51 52 53 54
func (d *workspacerolesGetter) List(_ string, queryParam *query.Query) (*api.ListResult, error) {

	var roles []*iamv1alpha2.WorkspaceRole
	var err error

	if aggregateTo := queryParam.Filters[iamv1alpha2.AggregateTo]; aggregateTo != "" {
		roles, err = d.fetchAggregationRoles(string(aggregateTo))
		delete(queryParam.Filters, iamv1alpha2.AggregateTo)
	} else {
		roles, err = d.sharedInformers.Iam().V1alpha2().WorkspaceRoles().Lister().List(queryParam.Selector())
	}
H
hongming 已提交
55 56 57 58 59 60

	if err != nil {
		return nil, err
	}

	var result []runtime.Object
H
hongming 已提交
61 62
	for _, role := range roles {
		result = append(result, role)
H
hongming 已提交
63 64
	}

H
hongming 已提交
65
	return v1alpha3.DefaultList(result, queryParam, d.compare, d.filter), nil
H
hongming 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

func (d *workspacerolesGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {

	leftRole, ok := left.(*iamv1alpha2.WorkspaceRole)
	if !ok {
		return false
	}

	rightRole, ok := right.(*iamv1alpha2.WorkspaceRole)
	if !ok {
		return false
	}

	return v1alpha3.DefaultObjectMetaCompare(leftRole.ObjectMeta, rightRole.ObjectMeta, field)
}

func (d *workspacerolesGetter) filter(object runtime.Object, filter query.Filter) bool {
	role, ok := object.(*iamv1alpha2.WorkspaceRole)

	if !ok {
		return false
	}

H
hongming 已提交
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
	switch filter.Field {
	case iamv1alpha2.ScopeWorkspace:
		return role.Labels[tenantv1alpha1.WorkspaceLabel] == string(filter.Value)
	default:
		return v1alpha3.DefaultObjectMetaFilter(role.ObjectMeta, filter)
	}

}

func (d *workspacerolesGetter) fetchAggregationRoles(name string) ([]*iamv1alpha2.WorkspaceRole, error) {
	roles := make([]*iamv1alpha2.WorkspaceRole, 0)

	obj, err := d.Get("", name)

	if err != nil {
		if errors.IsNotFound(err) {
			return roles, nil
		}
		return nil, err
	}

	if annotation := obj.(*iamv1alpha2.WorkspaceRole).Annotations[iamv1alpha2.AggregationRolesAnnotation]; annotation != "" {
		var roleNames []string
		if err = json.Unmarshal([]byte(annotation), &roleNames); err == nil {

			for _, roleName := range roleNames {
				role, err := d.Get("", roleName)

				if err != nil {
					if errors.IsNotFound(err) {
						klog.Warningf("invalid aggregation role found: %s, %s", name, roleName)
						continue
					}
					return nil, err
				}

				roles = append(roles, role.(*iamv1alpha2.WorkspaceRole))
			}
		}
	}

	return roles, nil
H
hongming 已提交
132
}