This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
82 lines (68 loc) · 2.76 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
package appliedclusterresourcequota
import (
kapi "k8s.io/kubernetes/pkg/api"
kapierrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/sets"
oapi "github.com/openshift/origin/pkg/api"
ocache "github.com/openshift/origin/pkg/client/cache"
quotaapi "github.com/openshift/origin/pkg/quota/api"
"github.com/openshift/origin/pkg/quota/controller/clusterquotamapping"
clusterresourcequotaregistry "github.com/openshift/origin/pkg/quota/registry/clusterresourcequota"
)
type AppliedClusterResourceQuotaREST struct {
quotaMapper clusterquotamapping.ClusterQuotaMapper
quotaLister *ocache.IndexerToClusterResourceQuotaLister
namespaceLister *cache.IndexerToNamespaceLister
}
func NewREST(quotaMapper clusterquotamapping.ClusterQuotaMapper, quotaLister *ocache.IndexerToClusterResourceQuotaLister, namespaceLister *cache.IndexerToNamespaceLister) *AppliedClusterResourceQuotaREST {
return &AppliedClusterResourceQuotaREST{
quotaMapper: quotaMapper,
quotaLister: quotaLister,
namespaceLister: namespaceLister,
}
}
func (r *AppliedClusterResourceQuotaREST) New() runtime.Object {
return "aapi.AppliedClusterResourceQuota{}
}
func (r *AppliedClusterResourceQuotaREST) Get(ctx kapi.Context, name string) (runtime.Object, error) {
namespace, ok := kapi.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 kapi.Context, options *kapi.ListOptions) (runtime.Object, error) {
namespace, ok := kapi.NamespaceFrom(ctx)
if !ok {
return nil, kapierrors.NewBadRequest("namespace is required")
}
// TODO max resource version? watch?
list := "aapi.AppliedClusterResourceQuotaList{}
matcher := clusterresourcequotaregistry.Matcher(oapi.ListOptionsToSelectors(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
}