-
Notifications
You must be signed in to change notification settings - Fork 1k
Enable multiple components estimating #6857
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
Merged
karmada-bot
merged 1 commit into
karmada-io:master
from
RainbowMango:pr_enable_multiple_template_scheduling
Oct 30, 2025
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,103 @@ | ||
| /* | ||
| Copyright 2025 The Karmada 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 core | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "k8s.io/klog/v2" | ||
|
|
||
| clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1" | ||
| policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" | ||
| workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
| estimatorclient "github.com/karmada-io/karmada/pkg/estimator/client" | ||
| "github.com/karmada-io/karmada/pkg/util/names" | ||
| ) | ||
|
|
||
| // isMultiTemplateSchedulingApplicable checks if the given ResourceBindingSpec | ||
| // meets the criteria for multi-template scheduling: | ||
| // 1. The referenced resource holds multiple pod templates (multiple components). | ||
| // 2. The placement configuration schedules the resource to exactly one cluster. | ||
| // This is currently determined by checking if spread constraints is set and requires exactly one cluster. | ||
| // | ||
| // Returns true if both conditions are satisfied, false otherwise. | ||
| // Note: We do not infer required cluster number from placement.clusterAffinity and | ||
| // placement.clusterAffinities because it's impossible to determine without cluster metadata | ||
| // whether the affinity rule matches exactly one cluster in the current environment, and the | ||
| // only reliable way is spread constraints. | ||
| func isMultiTemplateSchedulingApplicable(spec *workv1alpha2.ResourceBindingSpec) bool { | ||
| if spec == nil { | ||
| return false | ||
| } | ||
|
|
||
| if len(spec.Components) < 2 { | ||
| return false | ||
| } | ||
RainbowMango marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Check if placement targets exactly one cluster | ||
| if spec.Placement == nil { | ||
| return false | ||
| } | ||
| for i := range spec.Placement.SpreadConstraints { | ||
| if spec.Placement.SpreadConstraints[i].SpreadByField == policyv1alpha1.SpreadByFieldCluster && | ||
| spec.Placement.SpreadConstraints[i].MinGroups == 1 && | ||
| spec.Placement.SpreadConstraints[i].MaxGroups == 1 { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // calculateMultiTemplateAvailableSets calculates available sets for multi-template scheduling. | ||
| // It uses MaxAvailableComponentSets to estimate capacity for workloads with multiple pod templates. | ||
| func calculateMultiTemplateAvailableSets(ctx context.Context, estimator estimatorclient.ReplicaEstimator, name string, clusters []*clusterv1alpha1.Cluster, spec *workv1alpha2.ResourceBindingSpec, availableTargetClusters []workv1alpha2.TargetCluster) ([]workv1alpha2.TargetCluster, error) { | ||
| req := estimatorclient.ComponentSetEstimationRequest{ | ||
| Clusters: clusters, | ||
| Components: spec.Components, | ||
| Namespace: spec.Resource.Namespace, | ||
| } | ||
|
|
||
| namespacedKey := names.NamespacedKey(spec.Resource.Namespace, spec.Resource.Name) | ||
| resp, err := estimator.MaxAvailableComponentSets(ctx, req) | ||
| if err != nil { | ||
| klog.Errorf("Failed to calculate available component set with estimator(%s) for workload(%s, kind=%s, %s): %v", | ||
| name, spec.Resource.APIVersion, spec.Resource.Kind, namespacedKey, err) | ||
| return availableTargetClusters, err | ||
| } | ||
|
|
||
| // Use a map to safely update replicas regardless of order. | ||
| resMap := make(map[string]int32, len(resp)) | ||
| for i := range resp { | ||
| if resp[i].Sets == estimatorclient.UnauthenticReplica { | ||
| continue | ||
| } | ||
| resMap[resp[i].Name] = resp[i].Sets | ||
| } | ||
| for i := range availableTargetClusters { | ||
| if newReplicas, ok := resMap[availableTargetClusters[i].Name]; ok { | ||
| if availableTargetClusters[i].Replicas > newReplicas { | ||
| availableTargetClusters[i].Replicas = newReplicas | ||
| } | ||
| } else { | ||
| klog.Warningf("The estimator(%s) missed estimation from cluster(%s) when estimating for workload(%s, kind=%s, %s).", | ||
| name, availableTargetClusters[i].Name, spec.Resource.APIVersion, spec.Resource.Kind, namespacedKey) | ||
| } | ||
| } | ||
|
|
||
| return availableTargetClusters, nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.