/
filter.go
114 lines (96 loc) · 3.87 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
105
106
107
108
109
110
111
112
113
114
package appliedclusterresourcequota
import (
"context"
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"
"k8s.io/kubernetes/pkg/printers"
printerstorage "k8s.io/kubernetes/pkg/printers/storage"
"github.com/openshift/api/quota"
quotalister "github.com/openshift/client-go/quota/listers/quota/v1"
"github.com/openshift/origin/pkg/api/apihelpers"
printersinternal "github.com/openshift/origin/pkg/printers/internalversion"
quotaapi "github.com/openshift/origin/pkg/quota/apis/quota"
quotav1conversions "github.com/openshift/origin/pkg/quota/apis/quota/v1"
"github.com/openshift/origin/pkg/quota/controller/clusterquotamapping"
)
type AppliedClusterResourceQuotaREST struct {
quotaMapper clusterquotamapping.ClusterQuotaMapper
quotaLister quotalister.ClusterResourceQuotaLister
rest.TableConvertor
}
func NewREST(quotaMapper clusterquotamapping.ClusterQuotaMapper, quotaLister quotalister.ClusterResourceQuotaLister) *AppliedClusterResourceQuotaREST {
return &AppliedClusterResourceQuotaREST{
quotaMapper: quotaMapper,
quotaLister: quotaLister,
TableConvertor: printerstorage.TableConvertor{TablePrinter: printers.NewTablePrinter().With(printersinternal.AddHandlers)},
}
}
var _ rest.Getter = &AppliedClusterResourceQuotaREST{}
var _ rest.Lister = &AppliedClusterResourceQuotaREST{}
var _ rest.Scoper = &AppliedClusterResourceQuotaREST{}
func (r *AppliedClusterResourceQuotaREST) New() runtime.Object {
return "aapi.AppliedClusterResourceQuota{}
}
func (s *AppliedClusterResourceQuotaREST) NamespaceScoped() bool {
return true
}
func (r *AppliedClusterResourceQuotaREST) Get(ctx context.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(quota.Resource("appliedclusterresourcequota"), name)
}
clusterQuota, err := r.quotaLister.Get(name)
if err != nil {
return nil, err
}
return quotaapi.ConvertV1ClusterResourceQuotaToV1AppliedClusterResourceQuota(clusterQuota), nil
}
func (r *AppliedClusterResourceQuotaREST) NewList() runtime.Object {
return "aapi.AppliedClusterResourceQuotaList{}
}
func (r *AppliedClusterResourceQuotaREST) List(ctx context.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(apihelpers.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
}
internalAppliedQuota, err := quotav1conversions.ConvertV1ClusterResourceQuotaToInternalAppliedClusterResourceQuota(quota)
if err != nil {
return nil, err
}
list.Items = append(list.Items, *internalAppliedQuota)
}
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,
}
}