Skip to content

Commit 31d41d0

Browse files
committed
make clusterquota/status endpoint
1 parent 970b73a commit 31d41d0

File tree

10 files changed

+130
-13
lines changed

10 files changed

+130
-13
lines changed

pkg/client/clusteresourcequota.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type ClusterResourceQuotaInterface interface {
1818
Update(resourceQuota *quotaapi.ClusterResourceQuota) (*quotaapi.ClusterResourceQuota, error)
1919
Delete(name string) error
2020
Watch(opts kapi.ListOptions) (watch.Interface, error)
21+
22+
UpdateStatus(resourceQuota *quotaapi.ClusterResourceQuota) (*quotaapi.ClusterResourceQuota, error)
2123
}
2224

2325
type clusterResourceQuotas struct {
@@ -63,3 +65,9 @@ func (c *clusterResourceQuotas) Delete(name string) (err error) {
6365
func (c *clusterResourceQuotas) Watch(opts kapi.ListOptions) (watch.Interface, error) {
6466
return c.r.Get().Prefix("watch").Resource("clusterresourcequotas").VersionedParams(&opts, kapi.ParameterCodec).Watch()
6567
}
68+
69+
func (c *clusterResourceQuotas) UpdateStatus(resourceQuota *quotaapi.ClusterResourceQuota) (result *quotaapi.ClusterResourceQuota, err error) {
70+
result = &quotaapi.ClusterResourceQuota{}
71+
err = c.r.Put().Resource("clusterresourcequotas").Name(resourceQuota.Name).SubResource("status").Body(resourceQuota).Do().Into(result)
72+
return
73+
}

pkg/client/testclient/fake_clusterresourcequota.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@ func (c *FakeClusterResourceQuotas) Delete(name string) error {
5757
func (c *FakeClusterResourceQuotas) Watch(opts kapi.ListOptions) (watch.Interface, error) {
5858
return c.Fake.InvokesWatch(ktestclient.NewRootWatchAction("clusterresourcequotas", opts))
5959
}
60+
61+
func (c *FakeClusterResourceQuotas) UpdateStatus(inObj *quotaapi.ClusterResourceQuota) (*quotaapi.ClusterResourceQuota, error) {
62+
action := ktestclient.UpdateActionImpl{}
63+
action.Verb = "update"
64+
action.Resource = "clusterresourcequotas"
65+
action.Subresource = "status"
66+
action.Object = inObj
67+
68+
obj, err := c.Fake.Invokes(action, inObj)
69+
if obj == nil {
70+
return nil, err
71+
}
72+
73+
return obj.(*quotaapi.ClusterResourceQuota), err
74+
75+
}

pkg/cmd/server/origin/helpers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ func restInPeace(s rest.StandardStorage, err error) rest.StandardStorage {
1313
}
1414
return s
1515
}
16+
17+
func updateInPeace(s rest.Updater, err error) rest.Updater {
18+
if err != nil {
19+
glog.Fatal(err)
20+
}
21+
return s
22+
}

pkg/cmd/server/origin/master.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ func (c *MasterConfig) GetRestStorage() map[string]rest.Storage {
618618
"clusterRoleBindings": clusterRoleBindingStorage,
619619
"clusterRoles": clusterRoleStorage,
620620

621-
"clusterResourceQuotas": restInPeace(clusterresourcequotaregistry.NewStorage(c.RESTOptionsGetter)),
621+
"clusterResourceQuotas": restInPeace(clusterresourcequotaregistry.NewStorage(c.RESTOptionsGetter)),
622+
"clusterResourceQuotas/status": updateInPeace(clusterresourcequotaregistry.NewStatusStorage(c.RESTOptionsGetter)),
622623
"appliedClusterResourceQuotas": appliedclusterresourcequotaregistry.NewREST(
623624
c.ClusterQuotaMappingController.GetClusterQuotaMapper(), c.Informers.ClusterResourceQuotas().Lister(), c.Informers.Namespaces().Lister()),
624625
}

pkg/quota/admission/clusterresourcequota/accessor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (e *clusterQuotaAccessor) UpdateQuotaStatus(newQuota *kapi.ResourceQuota) e
8282
newNamespaceTotals.Used = utilquota.Add(oldNamespaceTotals.Used, usageDiff)
8383
clusterQuota.Status.Namespaces.Insert(newQuota.Namespace, newNamespaceTotals)
8484

85-
updatedQuota, err := e.clusterQuotaClient.ClusterResourceQuotas().Update(clusterQuota)
85+
updatedQuota, err := e.clusterQuotaClient.ClusterResourceQuotas().UpdateStatus(clusterQuota)
8686
if err != nil {
8787
return err
8888
}

pkg/quota/admission/clusterresourcequota/accessor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ func TestUpdateQuota(t *testing.T) {
123123

124124
var actualQuota *quotaapi.ClusterResourceQuota
125125
for _, action := range client.Actions() {
126-
updateAction, ok := action.(ktestclient.UpdateAction)
126+
updateAction, ok := action.(ktestclient.UpdateActionImpl)
127127
if !ok {
128128
continue
129129
}
130-
if updateAction.Matches("update", "clusterresourcequotas") {
130+
if updateAction.Matches("update", "clusterresourcequotas") && updateAction.Subresource == "status" {
131131
actualQuota = updateAction.GetObject().(*quotaapi.ClusterResourceQuota)
132132
break
133133
}

pkg/quota/controller/clusterquotareconciliation/reconcilation_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ func (c *ClusterQuotaReconcilationController) syncQuotaForNamespaces(originalQuo
291291
return kutilerrors.NewAggregate(reconcilationErrors), retryItems
292292
}
293293

294-
// TODO separate out status updating
295-
if _, err := c.clusterQuotaClient.ClusterResourceQuotas().Update(quota); err != nil {
294+
if _, err := c.clusterQuotaClient.ClusterResourceQuotas().UpdateStatus(quota); err != nil {
296295
return kutilerrors.NewAggregate(append(reconcilationErrors, err)), workItems
297296
}
298297

pkg/quota/controller/clusterquotareconciliation/reconciliation_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ func TestSyncFunc(t *testing.T) {
249249

250250
var actualQuota *quotaapi.ClusterResourceQuota
251251
for _, action := range client.Actions() {
252-
updateAction, ok := action.(ktestclient.UpdateAction)
252+
updateAction, ok := action.(ktestclient.UpdateActionImpl)
253253
if !ok {
254254
continue
255255
}
256-
if updateAction.Matches("update", "clusterresourcequotas") {
256+
if updateAction.Matches("update", "clusterresourcequotas") && updateAction.Subresource == "status" {
257257
actualQuota = updateAction.GetObject().(*quotaapi.ClusterResourceQuota)
258258
break
259259
}

pkg/quota/registry/clusterresourcequota/etcd.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package clusterresourcequota
22

33
import (
44
kapi "k8s.io/kubernetes/pkg/api"
5+
"k8s.io/kubernetes/pkg/api/rest"
56
"k8s.io/kubernetes/pkg/fields"
67
"k8s.io/kubernetes/pkg/labels"
78
"k8s.io/kubernetes/pkg/registry/generic"
@@ -21,6 +22,46 @@ type REST struct {
2122

2223
// NewStorage returns a RESTStorage object that will work against nodes.
2324
func NewStorage(optsGetter restoptions.Getter) (*REST, error) {
25+
store, err := makeStore(optsGetter)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
return &REST{Store: store}, nil
31+
}
32+
33+
func NewStatusStorage(optsGetter restoptions.Getter) (*StatusREST, error) {
34+
store, err := makeStore(optsGetter)
35+
if err != nil {
36+
return nil, err
37+
}
38+
store.CreateStrategy = nil
39+
store.DeleteStrategy = nil
40+
store.UpdateStrategy = StatusStrategy
41+
42+
return &StatusREST{store: store}, nil
43+
}
44+
45+
// StatusREST implements the REST endpoint for changing the status of a resourcequota.
46+
type StatusREST struct {
47+
store *registry.Store
48+
}
49+
50+
func (r *StatusREST) New() runtime.Object {
51+
return &quotaapi.ClusterResourceQuota{}
52+
}
53+
54+
// Get retrieves the object from the storage. It is required to support Patch.
55+
func (r *StatusREST) Get(ctx kapi.Context, name string) (runtime.Object, error) {
56+
return r.store.Get(ctx, name)
57+
}
58+
59+
// Update alters the status subset of an object.
60+
func (r *StatusREST) Update(ctx kapi.Context, name string, objInfo rest.UpdatedObjectInfo) (runtime.Object, bool, error) {
61+
return r.store.Update(ctx, name, objInfo)
62+
}
63+
64+
func makeStore(optsGetter restoptions.Getter) (*registry.Store, error) {
2465
store := &registry.Store{
2566
NewFunc: func() runtime.Object { return &quotaapi.ClusterResourceQuota{} },
2667
NewListFunc: func() runtime.Object { return &quotaapi.ClusterResourceQuotaList{} },
@@ -48,5 +89,5 @@ func NewStorage(optsGetter restoptions.Getter) (*REST, error) {
4889
return nil, err
4990
}
5091

51-
return &REST{store}, nil
92+
return store, nil
5293
}

pkg/quota/registry/clusterresourcequota/strategy.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func (strategy) NamespaceScoped() bool {
2424
return false
2525
}
2626

27-
// AllowCreateOnUpdate is false for policies.
2827
func (strategy) AllowCreateOnUpdate() bool {
2928
return false
3029
}
@@ -37,14 +36,17 @@ func (strategy) GenerateName(base string) string {
3736
return base
3837
}
3938

40-
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
4139
func (strategy) PrepareForCreate(obj runtime.Object) {
42-
_ = obj.(*quotaapi.ClusterResourceQuota)
40+
quota := obj.(*quotaapi.ClusterResourceQuota)
41+
quota.Status = quotaapi.ClusterResourceQuotaStatus{}
4342
}
4443

4544
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
4645
func (strategy) PrepareForUpdate(obj, old runtime.Object) {
47-
_ = obj.(*quotaapi.ClusterResourceQuota)
46+
curr := obj.(*quotaapi.ClusterResourceQuota)
47+
prev := old.(*quotaapi.ClusterResourceQuota)
48+
49+
curr.Status = prev.Status
4850
}
4951

5052
// Canonicalize normalizes the object after validation.
@@ -73,3 +75,46 @@ func Matcher(label labels.Selector, field fields.Selector) generic.Matcher {
7375
},
7476
}
7577
}
78+
79+
type statusStrategy struct {
80+
runtime.ObjectTyper
81+
}
82+
83+
var StatusStrategy = statusStrategy{kapi.Scheme}
84+
85+
func (statusStrategy) NamespaceScoped() bool {
86+
return false
87+
}
88+
89+
func (statusStrategy) AllowCreateOnUpdate() bool {
90+
return false
91+
}
92+
93+
func (statusStrategy) AllowUnconditionalUpdate() bool {
94+
return false
95+
}
96+
97+
func (statusStrategy) GenerateName(base string) string {
98+
return base
99+
}
100+
101+
func (statusStrategy) PrepareForCreate(obj runtime.Object) {
102+
}
103+
104+
func (statusStrategy) PrepareForUpdate(obj, old runtime.Object) {
105+
curr := obj.(*quotaapi.ClusterResourceQuota)
106+
prev := old.(*quotaapi.ClusterResourceQuota)
107+
108+
curr.Spec = prev.Spec
109+
}
110+
111+
func (statusStrategy) Canonicalize(obj runtime.Object) {
112+
}
113+
114+
func (statusStrategy) Validate(ctx kapi.Context, obj runtime.Object) field.ErrorList {
115+
return validation.ValidateClusterResourceQuota(obj.(*quotaapi.ClusterResourceQuota))
116+
}
117+
118+
func (statusStrategy) ValidateUpdate(ctx kapi.Context, obj, old runtime.Object) field.ErrorList {
119+
return validation.ValidateClusterResourceQuotaUpdate(obj.(*quotaapi.ClusterResourceQuota), old.(*quotaapi.ClusterResourceQuota))
120+
}

0 commit comments

Comments
 (0)