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

import (
R
runzexia 已提交
4 5 6 7 8 9 10 11
	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/labels"
	"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 已提交
12 13 14
)

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

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

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

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

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

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

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

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

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