Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cluster-autoscaler/capacitybuffer/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type bufferController struct {
strategyFilter filters.Filter
translator translators.Translator
quotaAllocator *resourceQuotaAllocator
updater updater.StatusUpdater
updater updater.BufferUpdater
queue workqueue.TypedRateLimitingInterface[string]
}

Expand All @@ -60,7 +60,7 @@ func NewBufferController(
client *cbclient.CapacityBufferClient,
strategyFilter filters.Filter,
translator translators.Translator,
updater updater.StatusUpdater,
updater updater.BufferUpdater,
) BufferController {
bc := &bufferController{
client: client,
Expand Down Expand Up @@ -93,7 +93,7 @@ func NewDefaultBufferController(
},
),
quotaAllocator: newResourceQuotaAllocator(client),
updater: *updater.NewStatusUpdater(client),
updater: updater.NewStatusUpdater(client),
queue: workqueue.NewTypedRateLimitingQueueWithConfig(
workqueue.DefaultTypedControllerRateLimiter[string](), workqueue.TypedRateLimitingQueueConfig[string]{Name: "CapacityBuffers"},
),
Expand Down Expand Up @@ -307,7 +307,7 @@ func (c *bufferController) reconcileNamespace(namespace string) error {
}

// Update buffer status by calling API server
updateErrors := c.updater.Update(filteredBuffers)
_, updateErrors := c.updater.Update(filteredBuffers)
for _, err := range updateErrors {
runtime.HandleError(fmt.Errorf("capacity buffer controller error: %w", err))
}
Expand Down
11 changes: 7 additions & 4 deletions cluster-autoscaler/capacitybuffer/updater/status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ func NewStatusUpdater(client *cbclient.CapacityBufferClient) *StatusUpdater {
}
}

// Update updates the buffer status with pod capacity
func (u *StatusUpdater) Update(buffers []*v1.CapacityBuffer) []error {
// Update updates the buffer status and returns the updated buffers objects and list of errors
func (u *StatusUpdater) Update(buffers []*v1.CapacityBuffer) ([]*v1.CapacityBuffer, []error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are introducing an interface here that others may use maybe it would make sense to also return a list of failed buffers for completeness and not only errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller already can get the failed objects (the passed buffers list - the returned ones) and the reason we return the successfully updated buffers is that the successful call returns the newly updated object written to API server while the other object will be redundant.

But I am okay with both approaches if you insist

var errors []error
var updatedBuffers []*v1.CapacityBuffer
for _, buffer := range buffers {
_, err := u.client.UpdateCapacityBuffer(buffer)
updatedBuffer, err := u.client.UpdateCapacityBuffer(buffer)
if err != nil {
errors = append(errors, err)
} else {
updatedBuffers = append(updatedBuffers, updatedBuffer)
}
}
return errors
return updatedBuffers, errors
}

// CleanUp cleans up the updater's internal structures.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ func TestStatusUpdater(t *testing.T) {
},
)
buffersUpdater := NewStatusUpdater(fakeCapacityBuffersClient)
errors := buffersUpdater.Update(test.buffers)
updatedBuffers, errors := buffersUpdater.Update(test.buffers)
assert.Equal(t, test.expectedNumberOfErrors, len(errors))
assert.Equal(t, test.expectedNumberOfCalls, updateCallsCount)
assert.Equal(t, len(test.buffers)-test.expectedNumberOfErrors, len(updatedBuffers))
})
}
}
27 changes: 27 additions & 0 deletions cluster-autoscaler/capacitybuffer/updater/updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package updater

import (
v1 "k8s.io/autoscaler/cluster-autoscaler/apis/capacitybuffer/autoscaling.x-k8s.io/v1beta1"
)

// BufferUpdater updates the passed buffers via API server call and returns the
// successfully updated buffers and list of errors
type BufferUpdater interface {
Update(buffers []*v1.CapacityBuffer) ([]*v1.CapacityBuffer, []error)
}
Loading