patch ClusterResourceBinding instead of update and update observed generation.

Signed-off-by: yy158775 <1584616775@qq.com>
This commit is contained in:
yy158775 2022-08-07 16:49:40 +08:00
parent 286805c239
commit 194a448b6f
1 changed files with 27 additions and 26 deletions

View File

@ -18,7 +18,6 @@ import (
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record" "k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2" "k8s.io/klog/v2"
@ -417,10 +416,10 @@ func (s *Scheduler) doScheduleClusterBinding(name string) (err error) {
} else { } else {
condition = util.NewCondition(workv1alpha2.Scheduled, scheduleFailedReason, err.Error(), metav1.ConditionFalse) condition = util.NewCondition(workv1alpha2.Scheduled, scheduleFailedReason, err.Error(), metav1.ConditionFalse)
} }
if updateErr := s.updateClusterBindingScheduledConditionIfNeeded(crb, condition); updateErr != nil { if updateErr := s.patchClusterBindingScheduleStatus(crb, condition); updateErr != nil {
klog.Errorf("Failed update condition(%s) for ClusterResourceBinding(%s)", workv1alpha2.Scheduled, crb.Name) klog.Errorf("Failed to patch schedule status to ClusterResourceBinding(%s): %v", crb.Name, err)
if err == nil { if err == nil {
// schedule succeed but update condition failed, return err in order to retry in next loop. // schedule succeed but update status failed, return err in order to retry in next loop.
err = updateErr err = updateErr
} }
} }
@ -662,36 +661,38 @@ func (s *Scheduler) patchBindingScheduleStatus(rb *workv1alpha2.ResourceBinding,
return nil return nil
} }
// updateClusterBindingScheduledConditionIfNeeded sets the scheduled condition of ClusterResourceBinding if needed // patchClusterBindingScheduleStatus patches schedule status of ClusterResourceBinding when necessary
func (s *Scheduler) updateClusterBindingScheduledConditionIfNeeded(crb *workv1alpha2.ClusterResourceBinding, newScheduledCondition metav1.Condition) error { func (s *Scheduler) patchClusterBindingScheduleStatus(crb *workv1alpha2.ClusterResourceBinding, newScheduledCondition metav1.Condition) error {
if crb == nil { if crb == nil {
return nil return nil
} }
oldScheduledCondition := meta.FindStatusCondition(crb.Status.Conditions, workv1alpha2.Scheduled) modifiedObj := crb.DeepCopy()
if oldScheduledCondition != nil { meta.SetStatusCondition(&modifiedObj.Status.Conditions, newScheduledCondition)
if util.IsConditionsEqual(newScheduledCondition, *oldScheduledCondition) { // Postpone setting observed generation until schedule succeed, assume scheduler will retry and
klog.V(4).Infof("No need to update scheduled condition") // will succeed eventually
return nil if newScheduledCondition.Status == metav1.ConditionTrue {
} modifiedObj.Status.SchedulerObservedGeneration = modifiedObj.Generation
} }
return retry.RetryOnConflict(retry.DefaultRetry, func() error { // Short path, ignore patch if no change.
meta.SetStatusCondition(&crb.Status.Conditions, newScheduledCondition) if reflect.DeepEqual(crb.Status, modifiedObj.Status) {
_, updateErr := s.KarmadaClient.WorkV1alpha2().ClusterResourceBindings().UpdateStatus(context.TODO(), crb, metav1.UpdateOptions{})
if updateErr == nil {
return nil return nil
} }
if updated, err := s.KarmadaClient.WorkV1alpha2().ClusterResourceBindings().Get(context.TODO(), crb.Name, metav1.GetOptions{}); err == nil { patchBytes, err := helper.GenMergePatch(crb, modifiedObj)
// make a copy so we don't mutate the shared cache if err != nil {
crb = updated.DeepCopy() return fmt.Errorf("failed to create a merge patch: %v", err)
} else {
klog.Errorf("failed to get updated cluster resource binding %s: %v", crb.Name, err)
} }
return updateErr _, err = s.KarmadaClient.WorkV1alpha2().ClusterResourceBindings().Patch(context.TODO(), crb.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{}, "status")
}) if err != nil {
klog.Errorf("Failed to patch schedule status to ClusterResourceBinding(%s): %v", crb.Name, err)
return err
}
klog.V(4).Infof("Patch schedule status to ClusterResourceBinding(%s) succeed", crb.Name)
return nil
} }
func (s *Scheduler) recordScheduleResultEventForResourceBinding(rb *workv1alpha2.ResourceBinding, schedulerErr error) { func (s *Scheduler) recordScheduleResultEventForResourceBinding(rb *workv1alpha2.ResourceBinding, schedulerErr error) {