Skip to content

Commit

Permalink
handle preserveResourceOnDeletion with dependencise distributor
Browse files Browse the repository at this point in the history
Signed-off-by: changzhen <changzhen5@huawei.com>
  • Loading branch information
XiShanYongYe-Chang committed Oct 23, 2024
1 parent 13df63f commit badf01f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions pkg/dependenciesdistributor/dependencies_distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -518,16 +519,19 @@ func (d *DependenciesDistributor) isOrphanAttachedBindings(
return true, nil
}

func (d *DependenciesDistributor) listAttachedBindings(bindingID, bindingNamespace, bindingName string) (res []*workv1alpha2.ResourceBinding, err error) {
labelSet := generateBindingDependedLabels(bindingID, bindingNamespace, bindingName)
selector := labels.SelectorFromSet(labelSet)
func (d *DependenciesDistributor) listAttachedBindings(bindingID, bindingNamespace, bindingName string) ([]*workv1alpha2.ResourceBinding, error) {
bindingList := &workv1alpha2.ResourceBindingList{}
err = d.Client.List(context.TODO(), bindingList, &client.ListOptions{
err := d.Client.List(context.TODO(), bindingList, &client.ListOptions{
Namespace: bindingNamespace,
LabelSelector: selector})
LabelSelector: labels.SelectorFromSet(generateBindingDependedLabels(bindingID, bindingNamespace, bindingName)),
})
if err != nil {
klog.Errorf("Failed to list attached bindings with independent binding(%s/%s): %v",
bindingNamespace, bindingName, err)
return nil, err
}

var res []*workv1alpha2.ResourceBinding
for i := range bindingList.Items {
res = append(res, &bindingList.Items[i])
}
Expand All @@ -546,6 +550,7 @@ func (d *DependenciesDistributor) removeScheduleResultFromAttachedBindings(bindi
delete(attachedBindings[index].Labels, bindingLabelKey)
updatedSnapshot := deleteBindingFromSnapshot(bindingNamespace, bindingName, attachedBindings[index].Spec.RequiredBy)
attachedBindings[index].Spec.RequiredBy = updatedSnapshot
attachedBindings[index].Spec.PreserveResourcesOnDeletion = nil
if err := d.Client.Update(context.TODO(), attachedBindings[index]); err != nil {
klog.Errorf("Failed to update binding(%s/%s): %v", binding.Namespace, binding.Name, err)
errs = append(errs, err)
Expand All @@ -563,6 +568,7 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
existBinding.Spec.RequiredBy = mergeBindingSnapshot(existBinding.Spec.RequiredBy, attachedBinding.Spec.RequiredBy)
existBinding.Labels = util.DedupeAndMergeLabels(existBinding.Labels, attachedBinding.Labels)
existBinding.Spec.Resource = attachedBinding.Spec.Resource
existBinding.Spec.PreserveResourcesOnDeletion = attachedBinding.Spec.PreserveResourcesOnDeletion

if err := d.Client.Update(context.TODO(), existBinding); err != nil {
klog.Errorf("Failed to update resourceBinding(%s): %v", bindingKey, err)
Expand Down Expand Up @@ -685,7 +691,7 @@ func buildAttachedBinding(independentBinding *workv1alpha2.ResourceBinding, obje
Clusters: independentBinding.Spec.Clusters,
})

return &workv1alpha2.ResourceBinding{
attachedBinding := &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: names.GenerateBindingName(object.GetKind(), object.GetName()),
Namespace: independentBinding.GetNamespace(),
Expand All @@ -706,6 +712,11 @@ func buildAttachedBinding(independentBinding *workv1alpha2.ResourceBinding, obje
RequiredBy: result,
},
}

if ptr.Deref(independentBinding.Spec.PreserveResourcesOnDeletion, false) {
attachedBinding.Spec.PreserveResourcesOnDeletion = independentBinding.Spec.PreserveResourcesOnDeletion
}
return attachedBinding
}

func mergeBindingSnapshot(existSnapshot, newSnapshot []workv1alpha2.BindingSnapshot) []workv1alpha2.BindingSnapshot {
Expand Down

0 comments on commit badf01f

Please sign in to comment.