namespaces.go 1.6 KB
Newer Older
Z
zryfish 已提交
1 2 3
package namespace

import (
R
runzexia 已提交
4 5 6 7 8 9 10
	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/informers"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/apiserver/query"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
	"strings"
Z
zryfish 已提交
11 12 13
)

type namespaceGetter struct {
R
runzexia 已提交
14
	informers informers.SharedInformerFactory
Z
zryfish 已提交
15 16
}

H
hongming 已提交
17
func New(informers informers.SharedInformerFactory) v1alpha3.Interface {
R
runzexia 已提交
18
	return &namespaceGetter{informers: informers}
Z
zryfish 已提交
19 20 21
}

func (n namespaceGetter) Get(_, name string) (runtime.Object, error) {
R
runzexia 已提交
22
	return n.informers.Core().V1().Namespaces().Lister().Get(name)
Z
zryfish 已提交
23 24 25
}

func (n namespaceGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
H
hongming 已提交
26
	ns, err := n.informers.Core().V1().Namespaces().Lister().List(query.Selector())
R
runzexia 已提交
27 28 29
	if err != nil {
		return nil, err
	}
Z
zryfish 已提交
30

R
runzexia 已提交
31 32 33 34
	var result []runtime.Object
	for _, item := range ns {
		result = append(result, item)
	}
Z
zryfish 已提交
35

R
runzexia 已提交
36
	return v1alpha3.DefaultList(result, query, n.compare, n.filter), nil
Z
zryfish 已提交
37 38 39
}

func (n namespaceGetter) filter(item runtime.Object, filter query.Filter) bool {
R
runzexia 已提交
40 41 42 43 44 45
	namespace, ok := item.(*v1.Namespace)
	if !ok {
		return false
	}
	switch filter.Field {
	case query.FieldStatus:
H
hongming 已提交
46
		return strings.Compare(string(namespace.Status.Phase), string(filter.Value)) == 0
R
runzexia 已提交
47
	default:
H
hongming 已提交
48
		return v1alpha3.DefaultObjectMetaFilter(namespace.ObjectMeta, filter)
R
runzexia 已提交
49
	}
Z
zryfish 已提交
50 51 52
}

func (n namespaceGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
R
runzexia 已提交
53 54 55 56
	leftNs, ok := left.(*v1.Namespace)
	if !ok {
		return false
	}
Z
zryfish 已提交
57

R
runzexia 已提交
58 59 60 61
	rightNs, ok := right.(*v1.Namespace)
	if !ok {
		return true
	}
H
hongming 已提交
62
	return v1alpha3.DefaultObjectMetaCompare(leftNs.ObjectMeta, rightNs.ObjectMeta, field)
R
runzexia 已提交
63
}