forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.go
104 lines (88 loc) · 3.73 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
100
101
102
103
104
package appliedclusterresourcequota
import (
kapierrors "k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage"
kcorelisters "k8s.io/kubernetes/pkg/client/listers/core/internalversion"
"k8s.io/kubernetes/pkg/printers"
printerstorage "k8s.io/kubernetes/pkg/printers/storage"
oapi "github.com/openshift/origin/pkg/api"
printersinternal "github.com/openshift/origin/pkg/printers/internalversion"
quotaapi "github.com/openshift/origin/pkg/quota/apis/quota"
"github.com/openshift/origin/pkg/quota/controller/clusterquotamapping"
quotalister "github.com/openshift/origin/pkg/quota/generated/listers/quota/internalversion"
)
type AppliedClusterResourceQuotaREST struct {
quotaMapper clusterquotamapping.ClusterQuotaMapper
quotaLister quotalister.ClusterResourceQuotaLister
namespaceLister kcorelisters.NamespaceLister
rest.TableConvertor
}
func NewREST(quotaMapper clusterquotamapping.ClusterQuotaMapper, quotaLister quotalister.ClusterResourceQuotaLister, namespaceLister kcorelisters.NamespaceLister) *AppliedClusterResourceQuotaREST {
return &AppliedClusterResourceQuotaREST{
quotaMapper: quotaMapper,
quotaLister: quotaLister,
namespaceLister: namespaceLister,
TableConvertor: printerstorage.TableConvertor{TablePrinter: printers.NewTablePrinter().With(printersinternal.AddHandlers)},
}
}
var _ rest.Getter = &AppliedClusterResourceQuotaREST{}
var _ rest.Lister = &AppliedClusterResourceQuotaREST{}
func (r *AppliedClusterResourceQuotaREST) New() runtime.Object {
return "aapi.AppliedClusterResourceQuota{}
}
func (r *AppliedClusterResourceQuotaREST) Get(ctx apirequest.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
namespace, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, kapierrors.NewBadRequest("namespace is required")
}
quotaNames, _ := r.quotaMapper.GetClusterQuotasFor(namespace)
quotaNamesSet := sets.NewString(quotaNames...)
if !quotaNamesSet.Has(name) {
return nil, kapierrors.NewNotFound(quotaapi.Resource("appliedclusterresourcequota"), name)
}
clusterQuota, err := r.quotaLister.Get(name)
if err != nil {
return nil, err
}
return quotaapi.ConvertClusterResourceQuotaToAppliedClusterResourceQuota(clusterQuota), nil
}
func (r *AppliedClusterResourceQuotaREST) NewList() runtime.Object {
return "aapi.AppliedClusterResourceQuotaList{}
}
func (r *AppliedClusterResourceQuotaREST) List(ctx apirequest.Context, options *metainternal.ListOptions) (runtime.Object, error) {
namespace, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, kapierrors.NewBadRequest("namespace is required")
}
// TODO max resource version? watch?
list := "aapi.AppliedClusterResourceQuotaList{}
matcher := matcher(oapi.InternalListOptionsToSelectors(options))
quotaNames, _ := r.quotaMapper.GetClusterQuotasFor(namespace)
for _, name := range quotaNames {
quota, err := r.quotaLister.Get(name)
if err != nil {
continue
}
if matches, err := matcher.Matches(quota); err != nil || !matches {
continue
}
list.Items = append(list.Items, *quotaapi.ConvertClusterResourceQuotaToAppliedClusterResourceQuota(quota))
}
return list, nil
}
// Matcher returns a generic matcher for a given label and field selector.
func matcher(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
return storage.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: storage.DefaultClusterScopedAttr,
}
}