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
24 changes: 24 additions & 0 deletions pkg/builder/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"sigs.k8s.io/cluster-autoscaler/pkg/processors/scaledowncandidates"
"sigs.k8s.io/cluster-autoscaler/pkg/processors/scaledowncandidates/emptycandidates"
"sigs.k8s.io/cluster-autoscaler/pkg/processors/scaledowncandidates/previouscandidates"
"sigs.k8s.io/cluster-autoscaler/pkg/processors/skippednodegroups"
"sigs.k8s.io/cluster-autoscaler/pkg/processors/status"
"sigs.k8s.io/cluster-autoscaler/pkg/resourcequotas/capacityquota"
"sigs.k8s.io/cluster-autoscaler/pkg/simulator/clustersnapshot"
Expand Down Expand Up @@ -180,6 +181,15 @@ func (b *AutoscalerBuilder) Build(ctx context.Context) (core.Autoscaler, *loop.L

opts.Processors = ca_processors.DefaultProcessors(autoscalingOptions)
opts.Processors.TemplateNodeInfoProvider = nodeinfosprovider.NewDefaultTemplateNodeInfoProvider(&autoscalingOptions.NodeInfoCacheExpireTime, autoscalingOptions.ForceDaemonSets)

var skippedNGEvaluator *skippednodegroups.Evaluator
if autoscalingOptions.ScaleUpSimulationForSkippedNodeGroupsEnabled {
evaluatorHandle, err := skippednodegroups.NewEvaluatorFrameworkHandle(ctx, b.informerFactory, autoscalingOptions.SchedulerConfig)
if err != nil {
return nil, nil, fmt.Errorf("failed to create framework handle for the skipped node groups evaluator: %w", err)
}
skippedNGEvaluator = skippednodegroups.NewEvaluator(evaluatorHandle, skippednodegroups.DefaultEvaluatorOptions())
}
podListProcessor := podlistprocessor.NewDefaultPodListProcessor(scheduling.ScheduleAnywhere)

opts.ScaleUpFailuresRegistry = scaleupfailures.NewRegistry()
Expand Down Expand Up @@ -243,6 +253,14 @@ func (b *AutoscalerBuilder) Build(ctx context.Context) (core.Autoscaler, *loop.L
}
}

// Compose skippednodegroups before CapacityBuffer's FakePodsScaleUpStatusProcessor (which
// emits NotTriggerScaleUp events for buffers before filtering out its fake pods) and after
// ProactiveScaleup's podinjection filter below (which strips injected fake pods without events).
if skippedNGEvaluator != nil {
opts.Processors.ScaleUpStatusProcessor = status.NewCombinedScaleUpStatusProcessor([]status.ScaleUpStatusProcessor{
skippednodegroups.NewScaleUpStatusProcessor(skippedNGEvaluator), opts.Processors.ScaleUpStatusProcessor})
}

opts.Processors.AutoscalingStatusProcessor = status.NewCombinedAutoscalingStatusProcessor(autoscalingStatusProcessors)

if autoscalingOptions.CapacityQuotasEnabled {
Expand Down Expand Up @@ -324,6 +342,12 @@ func (b *AutoscalerBuilder) Build(ctx context.Context) (core.Autoscaler, *loop.L
}
}

// Start the background evaluator only after Build succeeds and informer caches have synced so
// an earlier error cannot leak the worker goroutine.
if skippedNGEvaluator != nil {
go skippedNGEvaluator.Run(ctx)
}

if b.podObserver == nil {
b.podObserver = loop.StartPodObserver(ctx, b.kubeClient)
}
Expand Down
41 changes: 41 additions & 0 deletions pkg/builder/autoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,44 @@ func TestAutoscalerBuilderNoError(t *testing.T) {
time.Sleep(1 * time.Second)
})
}

func TestAutoscalerBuilderWithSkippedNodeGroupsEnabled(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx, cancel := testutils.GetTestContextWithCancel(t)

options := config.AutoscalingOptions{
CloudProviderName: "gce",
EstimatorName: estimator.BinpackingEstimatorName,
ExpanderNames: expander.LeastWasteExpanderName,
ScaleUpSimulationForSkippedNodeGroupsEnabled: true,
ProactiveScaleupEnabled: true,
}

debuggingSnapshotter := debuggingsnapshot.NewDebuggingSnapshotter(false)
kubeClient := fake.NewClientset()

mgr, err := manager.New(&rest.Config{}, manager.Options{
Metrics: metricsserver.Options{
BindAddress: "0",
},
HealthProbeBindAddress: "0",
})
assert.NoError(t, err)

autoscaler, trigger, err := New(options).
WithDebuggingSnapshotter(debuggingSnapshotter).
WithManager(mgr).
WithKubeClient(kubeClient).
WithInformerFactory(informers.NewSharedInformerFactory(kubeClient, 0)).
WithCloudProvider(test.NewCloudProvider(nil)).
WithPodObserver(&loop.UnschedulablePodObserver{}).
Build(ctx)

assert.NoError(t, err)
assert.NotNil(t, autoscaler)
assert.NotNil(t, trigger)

cancel()
time.Sleep(1 * time.Second)
})
}
90 changes: 16 additions & 74 deletions pkg/core/scaleup/orchestrator/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (o *ScaleUpOrchestrator) ScaleUp(
markedEquivalenceGroups := markAllGroupsAsUnschedulable(podEquivalenceGroups, ScaleUpExecutionErrorReason)
return status.UpdateScaleUpError(
&status.ScaleUpStatus{
PodsRemainUnschedulable: o.GetRemainingPods(ctx, markedEquivalenceGroups, nodeGroups, map[string]status.Reasons{}, nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(markedEquivalenceGroups, map[string]status.Reasons{}),
ConsideredNodeGroups: nodeGroups,
},
errors.ToAutoscalerError(errors.InternalError, err),
Expand All @@ -133,7 +133,7 @@ func (o *ScaleUpOrchestrator) ScaleUp(
markedEquivalenceGroups := markAllGroupsAsUnschedulable(podEquivalenceGroups, ScaleUpExecutionErrorReason)
return status.UpdateScaleUpError(
&status.ScaleUpStatus{
PodsRemainUnschedulable: o.GetRemainingPods(ctx, markedEquivalenceGroups, nodeGroups, map[string]status.Reasons{}, nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(markedEquivalenceGroups, map[string]status.Reasons{}),
ConsideredNodeGroups: nodeGroups,
},
errors.ToAutoscalerError(errors.InternalError, err).AddPrefix("could not create quotas tracker: "),
Expand Down Expand Up @@ -177,7 +177,7 @@ func (o *ScaleUpOrchestrator) ScaleUp(
CreateNodeGroupResults: plan.createNodeGroupResults,
FailedResizeNodeGroups: failedNodeGroups,
PodsTriggeredScaleUp: plan.bestOption.Pods,
PodsRemainUnschedulable: o.GetRemainingPods(ctx, markedEquivalenceGroups, plan.nodeGroups, skippedNodeGroups, nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(markedEquivalenceGroups, skippedNodeGroups),
ConsideredNodeGroups: plan.nodeGroups,
},
aErr,
Expand All @@ -188,7 +188,7 @@ func (o *ScaleUpOrchestrator) ScaleUp(
return &status.ScaleUpStatus{
Result: status.ScaleUpSuccessful,
ScaleUpInfos: plan.scaleUpInfos,
PodsRemainUnschedulable: o.GetRemainingPods(ctx, podEquivalenceGroups, plan.nodeGroups, skippedNodeGroups, nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(podEquivalenceGroups, skippedNodeGroups),
ConsideredNodeGroups: plan.nodeGroups,
CreateNodeGroupResults: plan.createNodeGroupResults,
PodsTriggeredScaleUp: plan.bestOption.Pods,
Expand Down Expand Up @@ -796,66 +796,20 @@ func (o *ScaleUpOrchestrator) ComputeSimilarNodeGroups(
}

// GetRemainingPods returns information about pods which CA is unable to help
// at this moment.
func (o *ScaleUpOrchestrator) GetRemainingPods(ctx context.Context, egs []*equivalence.PodGroup, nodeGroups []cloudprovider.NodeGroup, skipped map[string]status.Reasons, nodeInfos map[string]*framework.NodeInfo) []status.NoScaleUpInfo {
if !o.autoscalingCtx.ScaleUpSimulationForSkippedNodeGroupsEnabled {
remaining := []status.NoScaleUpInfo{}
for _, eg := range egs {
if eg.Schedulable {
continue
}
for _, pod := range eg.Pods {
noScaleUpInfo := status.NoScaleUpInfo{
Pod: pod,
RejectedNodeGroups: eg.SchedulingErrors,
SkippedNodeGroups: skipped,
}
remaining = append(remaining, noScaleUpInfo)
}
}
return remaining
}
// If ScaleUpSimulationForSkippedNodeGroupsEnabled is true we perform the SchedulablePodGroups simulation for the skipped node groups.
// We will also report how much time does this simulation take.
skippedNodeGroupsSimulationStart := time.Now()
nonSchedulableEgs := []*equivalence.PodGroup{}
for _, eg := range egs {
// there is no need to run the simulation for the schedulable pod groups, because for them we still do not return anything.
if !eg.Schedulable {
nonSchedulableEgs = append(nonSchedulableEgs, eg.Clone())
}
}
noScaleUpInfosAfterSimulation := o.getRemainingPodsConsideringSkippedNodeGroups(ctx, nonSchedulableEgs, nodeGroups, skipped, nodeInfos)
metrics.UpdateDurationFromStart(ctx, metrics.SkipNodeGroupSimulation, skippedNodeGroupsSimulationStart)
return noScaleUpInfosAfterSimulation
}

func (o *ScaleUpOrchestrator) getRemainingPodsConsideringSkippedNodeGroups(ctx context.Context, egs []*equivalence.PodGroup, nodeGroups []cloudprovider.NodeGroup, skipped map[string]status.Reasons, nodeInfos map[string]*framework.NodeInfo) []status.NoScaleUpInfo {
logger := klog.FromContext(ctx)
// at this moment. Every remaining pod is reported with the full set of skipped
// node groups; attributing skipped node groups to the pods that could actually
// use them is done off the main loop by the skippednodegroups status processor.
func (o *ScaleUpOrchestrator) GetRemainingPods(egs []*equivalence.PodGroup, skipped map[string]status.Reasons) []status.NoScaleUpInfo {
remaining := []status.NoScaleUpInfo{}
// Perform the SchedulablePodGroups simulation for the skipped node groups.
for _, nodeGroup := range nodeGroups {
nodeGroupId := nodeGroup.Id()
if _, found := skipped[nodeGroupId]; !found {
continue
}
nodeInfo, found := nodeInfos[nodeGroupId]
if !found {
logger.Error(nil, "No node info for node group", "nodeGroupId", nodeGroupId)
for _, eg := range egs {
if eg.Schedulable {
continue
}
// We ignore the return value here, because we are not interested in which egs are now (theoretically) schedulable or not. We are only interested in the rejected node groups per eg.
_ = o.SchedulablePodGroups(ctx, egs, nodeGroup, nodeInfo)
}
// For all egs here we need to generate the NoScaleUpInfo object because we only considered egs that are unschedulable in the first place.
// eg.SchedulingErrors will contain scheduling errors of this eg for not skipped nodegroups from the previous simulation + errors for skipped nodegroups from current simulation.
for _, eg := range egs {
skippedNGsPerEg := findSkippedNodeGroupsSatisfyingPodPredicates(eg, skipped)
for _, pod := range eg.Pods {
noScaleUpInfo := status.NoScaleUpInfo{
Pod: pod,
RejectedNodeGroups: eg.SchedulingErrors,
SkippedNodeGroups: skippedNGsPerEg,
SkippedNodeGroups: skipped,
}
remaining = append(remaining, noScaleUpInfo)
}
Expand Down Expand Up @@ -890,7 +844,7 @@ func deletePodEquivalenceGroupsId(podEquivalenceGroups []*equivalence.PodGroup,
func (o *ScaleUpOrchestrator) noOptionsAvailableStatus(ctx context.Context, args scaleUpCtx) *status.ScaleUpStatus {
return &status.ScaleUpStatus{
Result: status.ScaleUpNoOptionsAvailable,
PodsRemainUnschedulable: o.GetRemainingPods(ctx, args.podEquivalenceGroups, args.nodeGroups, args.skippedNodeGroups, args.nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(args.podEquivalenceGroups, args.skippedNodeGroups),
ConsideredNodeGroups: args.nodeGroups,
}
}
Expand All @@ -903,18 +857,6 @@ func (o *ScaleUpOrchestrator) abortAllOrNothing(ctx context.Context, args scaleU
return o.noOptionsAvailableStatus(ctx, args)
}

func findSkippedNodeGroupsSatisfyingPodPredicates(eg *equivalence.PodGroup, skipped map[string]status.Reasons) map[string]status.Reasons {
matchingNodeGroups := make(map[string]status.Reasons)
for skippedNodeGroupId, skipReason := range skipped {
// if a node group is in skipped and for this eg it is not in scheduling errors, it means that it will stay in skipped, because it satisfies the pod predicates.
if _, hasPredicateError := eg.SchedulingErrors[skippedNodeGroupId]; !hasPredicateError {
matchingNodeGroups[skippedNodeGroupId] = skipReason
klog.V(4).InfoS("Skipped node group satisfies pod predicates of pod's equivalence group", "nodeGroupId", skippedNodeGroupId, "pod", klog.KObj(eg.Pods[0]))
}
}
return matchingNodeGroups
}

func matchingSchedulablePodGroups(podGroups []estimator.PodEquivalenceGroup, similarPodGroups []estimator.PodEquivalenceGroup) bool {
schedulableSamplePods := make(map[*apiv1.Pod]bool)
for _, podGroup := range similarPodGroups {
Expand Down Expand Up @@ -1102,7 +1044,7 @@ func (o *ScaleUpOrchestrator) prepareScaleUp(ctx context.Context, args scaleUpCt
st, err := status.UpdateScaleUpError(
&status.ScaleUpStatus{
PodsTriggeredScaleUp: bestOption.Pods,
PodsRemainUnschedulable: o.GetRemainingPods(ctx, markedEquivalenceGroups, args.nodeGroups, args.skippedNodeGroups, args.nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(markedEquivalenceGroups, args.skippedNodeGroups),
ConsideredNodeGroups: args.nodeGroups,
},
aErr,
Expand All @@ -1116,7 +1058,7 @@ func (o *ScaleUpOrchestrator) prepareScaleUp(ctx context.Context, args scaleUpCt
st, err := status.UpdateScaleUpError(
&status.ScaleUpStatus{
PodsTriggeredScaleUp: bestOption.Pods,
PodsRemainUnschedulable: o.GetRemainingPods(ctx, markedEquivalenceGroups, args.nodeGroups, args.skippedNodeGroups, args.nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(markedEquivalenceGroups, args.skippedNodeGroups),
ConsideredNodeGroups: args.nodeGroups,
},
aErr,
Expand Down Expand Up @@ -1148,7 +1090,7 @@ func (o *ScaleUpOrchestrator) prepareScaleUp(ctx context.Context, args scaleUpCt
}
if aErr != nil {
markedEquivalenceGroups := markAllGroupsAsUnschedulable(args.podEquivalenceGroups, ScaleUpExecutionErrorReason)
scaleUpStatus.PodsRemainUnschedulable = o.GetRemainingPods(ctx, markedEquivalenceGroups, args.nodeGroups, args.skippedNodeGroups, args.nodeInfos)
scaleUpStatus.PodsRemainUnschedulable = o.GetRemainingPods(markedEquivalenceGroups, args.skippedNodeGroups)
scaleUpStatus.ConsideredNodeGroups = args.nodeGroups
return scaleUpPlan{}, scaleUpStatus, aErr
}
Expand All @@ -1162,7 +1104,7 @@ func (o *ScaleUpOrchestrator) prepareScaleUp(ctx context.Context, args scaleUpCt
&status.ScaleUpStatus{
CreateNodeGroupResults: createNodeGroupResults,
PodsTriggeredScaleUp: bestOption.Pods,
PodsRemainUnschedulable: o.GetRemainingPods(ctx, markedEquivalenceGroups, args.nodeGroups, args.skippedNodeGroups, args.nodeInfos),
PodsRemainUnschedulable: o.GetRemainingPods(markedEquivalenceGroups, args.skippedNodeGroups),
ConsideredNodeGroups: args.nodeGroups,
},
aErr,
Expand Down
Loading
Loading