-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ProvisioningRequestPodsFilter processor #6386
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
cluster-autoscaler/processors/provreq/provisioning_request_processors.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2023 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 provreq | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/autoscaler/cluster-autoscaler/context" | ||
"k8s.io/autoscaler/cluster-autoscaler/processors/pods" | ||
"k8s.io/autoscaler/cluster-autoscaler/utils/klogx" | ||
) | ||
|
||
const ( | ||
provisioningRequestPodAnnotationKey = "cluster-autoscaler.kubernetes.io/consume-provisioning-request" | ||
maxProvReqEvent = 50 | ||
) | ||
|
||
// EventManager is an interface for handling events for provisioning request. | ||
type EventManager interface { | ||
LogIgnoredInScaleUpEvent(context *context.AutoscalingContext, now time.Time, pod *apiv1.Pod, prName string) | ||
Reset() | ||
} | ||
|
||
type defaultEventManager struct { | ||
loggedEvents int | ||
limit int | ||
} | ||
|
||
// NewDefautlEventManager return basic event manager. | ||
func NewDefautlEventManager() *defaultEventManager { | ||
return &defaultEventManager{limit: maxProvReqEvent} | ||
} | ||
|
||
// LogIgnoredInScaleUpEvent adds event about ignored scale up for unscheduled pod, that consumes Provisioning Request. | ||
func (e *defaultEventManager) LogIgnoredInScaleUpEvent(context *context.AutoscalingContext, now time.Time, pod *apiv1.Pod, prName string) { | ||
message := fmt.Sprintf("Unschedulable pod didn't trigger scale-up, because it's consuming ProvisioningRequest %s/%s", pod.Namespace, prName) | ||
if e.loggedEvents < e.limit { | ||
context.Recorder.Event(pod, apiv1.EventTypeNormal, "", message) | ||
e.loggedEvents++ | ||
} | ||
} | ||
|
||
// Reset resets event manager internal structure. It will be called once before handling all pods. | ||
func (e *defaultEventManager) Reset() { | ||
e.loggedEvents = 0 | ||
} | ||
|
||
// ProvisioningRequestPodsFilter filter out pods that consumes Provisioning Request | ||
type ProvisioningRequestPodsFilter struct { | ||
eventManager EventManager | ||
} | ||
|
||
// Process filters out all pods that are consuming a Provisioning Request from unschedulable pods list. | ||
func (p *ProvisioningRequestPodsFilter) Process( | ||
context *context.AutoscalingContext, | ||
unschedulablePods []*apiv1.Pod, | ||
) ([]*apiv1.Pod, error) { | ||
now := time.Now() | ||
p.eventManager.Reset() | ||
loggingQuota := klogx.PodsLoggingQuota() | ||
result := make([]*apiv1.Pod, 0, len(unschedulablePods)) | ||
for _, pod := range unschedulablePods { | ||
prName, found := provisioningRequestName(pod) | ||
if !found { | ||
result = append(result, pod) | ||
continue | ||
} | ||
klogx.V(1).UpTo(loggingQuota).Infof("Ignoring unschedulable pod %s/%s as it consumes ProvisioningRequest: %s/%s", pod.Namespace, pod.Name, pod.Namespace, prName) | ||
p.eventManager.LogIgnoredInScaleUpEvent(context, now, pod, prName) | ||
} | ||
klogx.V(1).Over(loggingQuota).Infof("There are also %v other pods which were ignored", -loggingQuota.Left()) | ||
return result, nil | ||
} | ||
|
||
// CleanUp cleans up the processor's internal structures. | ||
func (p *ProvisioningRequestPodsFilter) CleanUp() {} | ||
|
||
// NewProvisioningRequestPodsFilter creates a ProvisioningRequest filter processor. | ||
func NewProvisioningRequestPodsFilter(e EventManager) pods.PodListProcessor { | ||
return &ProvisioningRequestPodsFilter{e} | ||
} | ||
|
||
func provisioningRequestName(pod *v1.Pod) (string, bool) { | ||
if pod == nil || pod.Annotations == nil { | ||
return "", false | ||
} | ||
provReqName, found := pod.Annotations[provisioningRequestPodAnnotationKey] | ||
return provReqName, found | ||
} |
117 changes: 117 additions & 0 deletions
117
cluster-autoscaler/processors/provreq/provisioning_request_processors_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright 2023 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 provreq | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
apiv1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/autoscaler/cluster-autoscaler/context" | ||
. "k8s.io/autoscaler/cluster-autoscaler/utils/test" | ||
"k8s.io/client-go/tools/record" | ||
) | ||
|
||
func TestProvisioningRequestPodsFilter(t *testing.T) { | ||
yaroslava-serdiuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
prPod1 := BuildTestPod("pr-pod-1", 500, 10) | ||
prPod1.Annotations[provisioningRequestPodAnnotationKey] = "pr-class" | ||
|
||
prPod2 := BuildTestPod("pr-pod-2", 500, 10) | ||
prPod2.Annotations[provisioningRequestPodAnnotationKey] = "pr-class-2" | ||
|
||
pod1 := BuildTestPod("pod-1", 500, 10) | ||
pod2 := BuildTestPod("pod-2", 500, 10) | ||
|
||
testCases := map[string]struct { | ||
unschedulableCandidates []*apiv1.Pod | ||
expectedUnscheduledPods []*apiv1.Pod | ||
}{ | ||
"ProvisioningRequest consumer is filtered out": { | ||
unschedulableCandidates: []*v1.Pod{prPod1, pod1}, | ||
expectedUnscheduledPods: []*v1.Pod{pod1}, | ||
}, | ||
"Different ProvisioningRequest consumers are filtered out": { | ||
unschedulableCandidates: []*v1.Pod{prPod1, prPod2, pod1}, | ||
expectedUnscheduledPods: []*v1.Pod{pod1}, | ||
}, | ||
"No pod is filtered": { | ||
unschedulableCandidates: []*v1.Pod{pod1, pod2}, | ||
expectedUnscheduledPods: []*v1.Pod{pod1, pod2}, | ||
}, | ||
"Empty unschedulable pods list": { | ||
unschedulableCandidates: []*v1.Pod{}, | ||
expectedUnscheduledPods: []*v1.Pod{}, | ||
}, | ||
"All ProvisioningRequest consumers are filtered out": { | ||
unschedulableCandidates: []*v1.Pod{prPod1, prPod2}, | ||
expectedUnscheduledPods: []*v1.Pod{}, | ||
}, | ||
} | ||
for _, test := range testCases { | ||
eventRecorder := record.NewFakeRecorder(10) | ||
ctx := &context.AutoscalingContext{AutoscalingKubeClients: context.AutoscalingKubeClients{Recorder: eventRecorder}} | ||
filter := NewProvisioningRequestPodsFilter(NewDefautlEventManager()) | ||
got, _ := filter.Process(ctx, test.unschedulableCandidates) | ||
assert.ElementsMatch(t, got, test.expectedUnscheduledPods) | ||
if len(test.expectedUnscheduledPods) < len(test.expectedUnscheduledPods) { | ||
select { | ||
case event := <-eventRecorder.Events: | ||
assert.Contains(t, event, "Unschedulable pod didn't trigger scale-up, because it's consuming ProvisioningRequest default/pr-class") | ||
case <-time.After(1 * time.Second): | ||
t.Errorf("Timeout waiting for event") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestEventManager(t *testing.T) { | ||
eventLimit := 5 | ||
eventManager := &defaultEventManager{limit: eventLimit} | ||
prFilter := NewProvisioningRequestPodsFilter(eventManager) | ||
eventRecorder := record.NewFakeRecorder(10) | ||
ctx := &context.AutoscalingContext{AutoscalingKubeClients: context.AutoscalingKubeClients{Recorder: eventRecorder}} | ||
unscheduledPods := []*v1.Pod{BuildTestPod("pod", 500, 10)} | ||
|
||
for i := 0; i < 10; i++ { | ||
prPod := BuildTestPod(fmt.Sprintf("pr-pod-%d", i), 10, 10) | ||
prPod.Annotations[provisioningRequestPodAnnotationKey] = "pr-class" | ||
unscheduledPods = append(unscheduledPods, prPod) | ||
} | ||
got, err := prFilter.Process(ctx, unscheduledPods) | ||
assert.NoError(t, err) | ||
if len(got) != 1 { | ||
t.Errorf("Want 1 unschedulable pod, got: %v", got) | ||
} | ||
assert.Equal(t, eventManager.loggedEvents, eventLimit) | ||
for i := 0; i < eventLimit; i++ { | ||
select { | ||
case event := <-eventRecorder.Events: | ||
assert.Contains(t, event, "Unschedulable pod didn't trigger scale-up, because it's consuming ProvisioningRequest default/pr-class") | ||
case <-time.After(1 * time.Second): | ||
t.Errorf("Timeout waiting for event") | ||
} | ||
} | ||
select { | ||
case <-eventRecorder.Events: | ||
t.Errorf("Receive event after reaching event limit") | ||
case <-time.After(1 * time.Millisecond): | ||
return | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd avoid mentioning clusterautoscaler in the description. It is a flag to clusterautoscaler binary, so saying the flag will affect clusterautoscaler is a bit redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the alternative is to put it in passive voice, but I don't think it's necessary. We already mention clusterautoscaler in many flags.