namespaces.go 2.2 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
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.
*/

Z
zryfish 已提交
17 18 19
package namespace

import (
R
runzexia 已提交
20 21 22 23 24 25 26
	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 已提交
27 28
)

H
hongming 已提交
29
type namespacesGetter struct {
R
runzexia 已提交
30
	informers informers.SharedInformerFactory
Z
zryfish 已提交
31 32
}

H
hongming 已提交
33
func New(informers informers.SharedInformerFactory) v1alpha3.Interface {
H
hongming 已提交
34
	return &namespacesGetter{informers: informers}
Z
zryfish 已提交
35 36
}

H
hongming 已提交
37
func (n namespacesGetter) Get(_, name string) (runtime.Object, error) {
R
runzexia 已提交
38
	return n.informers.Core().V1().Namespaces().Lister().Get(name)
Z
zryfish 已提交
39 40
}

H
hongming 已提交
41
func (n namespacesGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
H
hongming 已提交
42
	ns, err := n.informers.Core().V1().Namespaces().Lister().List(query.Selector())
R
runzexia 已提交
43 44 45
	if err != nil {
		return nil, err
	}
Z
zryfish 已提交
46

R
runzexia 已提交
47 48 49 50
	var result []runtime.Object
	for _, item := range ns {
		result = append(result, item)
	}
Z
zryfish 已提交
51

R
runzexia 已提交
52
	return v1alpha3.DefaultList(result, query, n.compare, n.filter), nil
Z
zryfish 已提交
53 54
}

H
hongming 已提交
55
func (n namespacesGetter) filter(item runtime.Object, filter query.Filter) bool {
R
runzexia 已提交
56 57 58 59 60 61
	namespace, ok := item.(*v1.Namespace)
	if !ok {
		return false
	}
	switch filter.Field {
	case query.FieldStatus:
H
hongming 已提交
62
		return strings.Compare(string(namespace.Status.Phase), string(filter.Value)) == 0
R
runzexia 已提交
63
	default:
H
hongming 已提交
64
		return v1alpha3.DefaultObjectMetaFilter(namespace.ObjectMeta, filter)
R
runzexia 已提交
65
	}
Z
zryfish 已提交
66 67
}

H
hongming 已提交
68
func (n namespacesGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
R
runzexia 已提交
69 70 71 72
	leftNs, ok := left.(*v1.Namespace)
	if !ok {
		return false
	}
Z
zryfish 已提交
73

R
runzexia 已提交
74 75 76 77
	rightNs, ok := right.(*v1.Namespace)
	if !ok {
		return true
	}
H
hongming 已提交
78
	return v1alpha3.DefaultObjectMetaCompare(leftNs.ObjectMeta, rightNs.ObjectMeta, field)
R
runzexia 已提交
79
}