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 7e9dc2f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
31 changes: 21 additions & 10 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 @@ -250,7 +251,7 @@ func (d *DependenciesDistributor) Reconcile(ctx context.Context, request reconci

// in case users set PropagateDeps field from "true" to "false"
if !bindingObject.Spec.PropagateDeps || !bindingObject.DeletionTimestamp.IsZero() {
err = d.handleIndependentBindingDeletion(bindingObject.Labels[workv1alpha2.ResourceBindingPermanentIDLabel], request.Namespace, request.Name)
err = d.handleIndependentBindingDeletion(ctx, bindingObject.Labels[workv1alpha2.ResourceBindingPermanentIDLabel], request.Namespace, request.Name)
if err != nil {
klog.Errorf("Failed to cleanup attached bindings for independent binding(%s): %v", request.NamespacedName, err)
return reconcile.Result{}, err
Expand Down Expand Up @@ -297,8 +298,8 @@ func (d *DependenciesDistributor) removeFinalizer(ctx context.Context, independe
return nil
}

func (d *DependenciesDistributor) handleIndependentBindingDeletion(id, namespace, name string) error {
attachedBindings, err := d.listAttachedBindings(id, namespace, name)
func (d *DependenciesDistributor) handleIndependentBindingDeletion(ctx context.Context, id, namespace, name string) error {
attachedBindings, err := d.listAttachedBindings(ctx, id, namespace, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -445,7 +446,7 @@ func (d *DependenciesDistributor) recordDependencies(ctx context.Context, indepe
}

func (d *DependenciesDistributor) findOrphanAttachedBindings(ctx context.Context, independentBinding *workv1alpha2.ResourceBinding, dependencies []configv1alpha1.DependentObjectReference) ([]*workv1alpha2.ResourceBinding, error) {
attachedBindings, err := d.listAttachedBindings(independentBinding.Labels[workv1alpha2.ResourceBindingPermanentIDLabel],
attachedBindings, err := d.listAttachedBindings(ctx, independentBinding.Labels[workv1alpha2.ResourceBindingPermanentIDLabel],
independentBinding.Namespace, independentBinding.Name)
if err != nil {
return nil, err
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(ctx context.Context, bindingID, bindingNamespace, bindingName string) ([]*workv1alpha2.ResourceBinding, error) {
bindingList := &workv1alpha2.ResourceBindingList{}
err = d.Client.List(context.TODO(), bindingList, &client.ListOptions{
err := d.Client.List(ctx, 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
4 changes: 2 additions & 2 deletions pkg/dependenciesdistributor/dependencies_distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func Test_handleIndependentBindingDeletion(t *testing.T) {
d := &DependenciesDistributor{
Client: tt.fields.Client,
}
err := d.handleIndependentBindingDeletion(tt.args.id, tt.args.namespace, tt.args.name)
err := d.handleIndependentBindingDeletion(context.Background(), tt.args.id, tt.args.namespace, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("handleIndependentBindingDeletion() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down Expand Up @@ -1890,7 +1890,7 @@ func Test_listAttachedBindings(t *testing.T) {
d := &DependenciesDistributor{
Client: tt.setupClient(),
}
gotBindings, err := d.listAttachedBindings(tt.bindingID, tt.bindingNamespace, tt.bindingName)
gotBindings, err := d.listAttachedBindings(context.Background(), tt.bindingID, tt.bindingNamespace, tt.bindingName)
if (err != nil) != tt.wantErr {
t.Errorf("listAttachedBindings() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down

0 comments on commit 7e9dc2f

Please sign in to comment.