Skip to content

Commit 60e098b

Browse files
Add Capacity buffers updater interface to allow wrapping updating logic
1 parent 30b4fcc commit 60e098b

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

cluster-autoscaler/capacitybuffer/controller/controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type bufferController struct {
5151
strategyFilter filters.Filter
5252
translator translators.Translator
5353
quotaAllocator *resourceQuotaAllocator
54-
updater updater.StatusUpdater
54+
updater updater.Updater
5555
queue workqueue.TypedRateLimitingInterface[string]
5656
}
5757

@@ -60,7 +60,7 @@ func NewBufferController(
6060
client *cbclient.CapacityBufferClient,
6161
strategyFilter filters.Filter,
6262
translator translators.Translator,
63-
updater updater.StatusUpdater,
63+
updater updater.Updater,
6464
) BufferController {
6565
bc := &bufferController{
6666
client: client,
@@ -93,7 +93,7 @@ func NewDefaultBufferController(
9393
},
9494
),
9595
quotaAllocator: newResourceQuotaAllocator(client),
96-
updater: *updater.NewStatusUpdater(client),
96+
updater: updater.NewStatusUpdater(client),
9797
queue: workqueue.NewTypedRateLimitingQueueWithConfig(
9898
workqueue.DefaultTypedControllerRateLimiter[string](), workqueue.TypedRateLimitingQueueConfig[string]{Name: "CapacityBuffers"},
9999
),
@@ -307,7 +307,7 @@ func (c *bufferController) reconcileNamespace(namespace string) error {
307307
}
308308

309309
// Update buffer status by calling API server
310-
updateErrors := c.updater.Update(filteredBuffers)
310+
_, updateErrors := c.updater.Update(filteredBuffers)
311311
for _, err := range updateErrors {
312312
runtime.HandleError(fmt.Errorf("capacity buffer controller error: %w", err))
313313
}

cluster-autoscaler/capacitybuffer/updater/status_updater.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,19 @@ func NewStatusUpdater(client *cbclient.CapacityBufferClient) *StatusUpdater {
3333
}
3434
}
3535

36-
// Update updates the buffer status with pod capacity
37-
func (u *StatusUpdater) Update(buffers []*v1.CapacityBuffer) []error {
36+
// Update updates the buffer status and returns the updated buffers objects and list of errors
37+
func (u *StatusUpdater) Update(buffers []*v1.CapacityBuffer) ([]*v1.CapacityBuffer, []error) {
3838
var errors []error
39+
var updatedBuffers []*v1.CapacityBuffer
3940
for _, buffer := range buffers {
40-
_, err := u.client.UpdateCapacityBuffer(buffer)
41+
updatedBuffer, err := u.client.UpdateCapacityBuffer(buffer)
4142
if err != nil {
4243
errors = append(errors, err)
44+
} else {
45+
updatedBuffers = append(updatedBuffers, updatedBuffer)
4346
}
4447
}
45-
return errors
48+
return updatedBuffers, errors
4649
}
4750

4851
// CleanUp cleans up the updater's internal structures.

cluster-autoscaler/capacitybuffer/updater/status_updater_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ func TestStatusUpdater(t *testing.T) {
8989
},
9090
)
9191
buffersUpdater := NewStatusUpdater(fakeCapacityBuffersClient)
92-
errors := buffersUpdater.Update(test.buffers)
92+
updatedBuffers, errors := buffersUpdater.Update(test.buffers)
9393
assert.Equal(t, test.expectedNumberOfErrors, len(errors))
9494
assert.Equal(t, test.expectedNumberOfCalls, updateCallsCount)
95+
assert.Equal(t, len(test.buffers)-test.expectedNumberOfErrors, len(updatedBuffers))
9596
})
9697
}
9798
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package updater
18+
19+
import (
20+
v1 "k8s.io/autoscaler/cluster-autoscaler/apis/capacitybuffer/autoscaling.x-k8s.io/v1beta1"
21+
)
22+
23+
// Updater updates the passed buffers via API server call and returns the
24+
// successfully updated buffers and list of errors
25+
type Updater interface {
26+
Update(buffers []*v1.CapacityBuffer) ([]*v1.CapacityBuffer, []error)
27+
}

0 commit comments

Comments
 (0)