Skip to content

Commit 5e3e25a

Browse files
committed
Resolve binding components using GetComponents interpreter hook
Signed-off-by: wei-chenglai <[email protected]>
1 parent 601081e commit 5e3e25a

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

pkg/detector/detector.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -759,14 +759,8 @@ func (d *ResourceDetector) BuildResourceBinding(object *unstructured.Unstructure
759759

760760
claimFunc(propagationBinding, policyID, policyMeta)
761761

762-
if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) {
763-
replicas, replicaRequirements, err := d.ResourceInterpreter.GetReplicas(object)
764-
if err != nil {
765-
klog.Errorf("Failed to customize replicas for %s(%s), %v", object.GroupVersionKind(), object.GetName(), err)
766-
return nil, err
767-
}
768-
propagationBinding.Spec.Replicas = replicas
769-
propagationBinding.Spec.ReplicaRequirements = replicaRequirements
762+
if err := d.applyReplicaInterpretation(object, &propagationBinding.Spec); err != nil {
763+
return nil, err
770764
}
771765

772766
if features.FeatureGate.Enabled(features.PriorityBasedScheduling) && policySpec.SchedulePriority != nil {
@@ -833,14 +827,8 @@ func (d *ResourceDetector) BuildClusterResourceBinding(object *unstructured.Unst
833827

834828
AddCPPClaimMetadata(binding, policyID, policyMeta)
835829

836-
if d.ResourceInterpreter.HookEnabled(object.GroupVersionKind(), configv1alpha1.InterpreterOperationInterpretReplica) {
837-
replicas, replicaRequirements, err := d.ResourceInterpreter.GetReplicas(object)
838-
if err != nil {
839-
klog.Errorf("Failed to customize replicas for %s(%s), %v", object.GroupVersionKind(), object.GetName(), err)
840-
return nil, err
841-
}
842-
binding.Spec.Replicas = replicas
843-
binding.Spec.ReplicaRequirements = replicaRequirements
830+
if err := d.applyReplicaInterpretation(object, &binding.Spec); err != nil {
831+
return nil, err
844832
}
845833

846834
return binding, nil
@@ -1350,8 +1338,38 @@ func (d *ResourceDetector) CleanupClusterResourceBindingClaimMetadata(crb *workv
13501338
if err = d.Client.Get(context.TODO(), client.ObjectKey{Name: crb.GetName()}, updated); err == nil {
13511339
crb = updated.DeepCopy()
13521340
} else {
1353-
klog.Errorf("Failed to get updated ClusterResourceBinding(%s):: %v", crb.GetName(), err)
1341+
klog.Errorf("Failed to get updated ClusterResourceBinding(%s): %v", crb.GetName(), err)
13541342
}
13551343
return updateErr
13561344
})
13571345
}
1346+
1347+
// applyReplicaInterpretation handles the logic for interpreting replicas or components from an object.
1348+
func (d *ResourceDetector) applyReplicaInterpretation(object *unstructured.Unstructured, spec *workv1alpha2.ResourceBindingSpec) error {
1349+
gvk := object.GroupVersionKind()
1350+
name := object.GetName()
1351+
1352+
// Prioritize component interpretation if the feature and GetComponents are enabled.
1353+
if features.FeatureGate.Enabled(features.MultiplePodTemplatesScheduling) && d.ResourceInterpreter.HookEnabled(gvk, configv1alpha1.InterpreterOperationInterpretComponent) {
1354+
components, err := d.ResourceInterpreter.GetComponents(object)
1355+
if err != nil {
1356+
klog.Errorf("Failed to get components for %s(%s): %v", gvk, name, err)
1357+
return err
1358+
}
1359+
spec.Components = components
1360+
return nil
1361+
}
1362+
1363+
// GetReplicas is executed if the MultiplePodTemplatesScheduling feature gate is disabled, or if GetComponents is not implemented.
1364+
if d.ResourceInterpreter.HookEnabled(gvk, configv1alpha1.InterpreterOperationInterpretReplica) {
1365+
replicas, replicaRequirements, err := d.ResourceInterpreter.GetReplicas(object)
1366+
if err != nil {
1367+
klog.Errorf("Failed to customize replicas for %s(%s): %v", gvk, name, err)
1368+
return err
1369+
}
1370+
spec.Replicas = replicas
1371+
spec.ReplicaRequirements = replicaRequirements
1372+
}
1373+
1374+
return nil
1375+
}

0 commit comments

Comments
 (0)