Skip to content

Commit

Permalink
Merge pull request karmada-io#4418 from chaunceyjiang/pr_4410
Browse files Browse the repository at this point in the history
feat: set conflictResolution for dependent resources.
  • Loading branch information
karmada-bot authored Oct 25, 2024
2 parents 53c00f7 + 38bb553 commit f7d6da3
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/dependenciesdistributor/dependencies_distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ func (d *DependenciesDistributor) createOrUpdateAttachedBinding(attachedBinding
bindingKey := client.ObjectKeyFromObject(attachedBinding)
err := d.Client.Get(context.TODO(), bindingKey, existBinding)
if err == nil {
// If the spec.Placement is nil, this means that existBinding is generated by the dependency mechanism.
// If the spec.Placement is not nil, then it must be generated by PropagationPolicy.
if existBinding.Spec.Placement == nil {
existBinding.Spec.ConflictResolution = attachedBinding.Spec.ConflictResolution
}
existBinding.Spec.RequiredBy = mergeBindingSnapshot(existBinding.Spec.RequiredBy, attachedBinding.Spec.RequiredBy)
existBinding.Labels = util.DedupeAndMergeLabels(existBinding.Labels, attachedBinding.Labels)
existBinding.Spec.Resource = attachedBinding.Spec.Resource
Expand Down Expand Up @@ -707,6 +712,7 @@ func buildAttachedBinding(independentBinding *workv1alpha2.ResourceBinding, obje
},
RequiredBy: result,
PreserveResourcesOnDeletion: independentBinding.Spec.PreserveResourcesOnDeletion,
ConflictResolution: independentBinding.Spec.ConflictResolution,
},
}
}
Expand Down
170 changes: 170 additions & 0 deletions pkg/dependenciesdistributor/dependencies_distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager"
Expand Down Expand Up @@ -2486,6 +2487,175 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
return fake.NewClientBuilder().WithScheme(Scheme).Build()
},
},
{
name: "update attached binding with ConflictResolution",
attachedBinding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "test-binding",
Namespace: "test",
ResourceVersion: "1000",
Labels: map[string]string{"app": "nginx"},
},
Spec: workv1alpha2.ResourceBindingSpec{
Resource: workv1alpha2.ObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Namespace: "fake-ns",
Name: "demo-app",
ResourceVersion: "22222",
},
RequiredBy: []workv1alpha2.BindingSnapshot{
{
Namespace: "test-1",
Name: "test-binding-1",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "foo",
Replicas: 1,
},
},
},
{
Namespace: "default-2",
Name: "default-binding-2",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member2",
Replicas: 4,
},
},
},
{
Namespace: "test-2",
Name: "test-binding-2",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "bar",
Replicas: 1,
},
},
},
},
ConflictResolution: policyv1alpha1.ConflictOverwrite,
},
},
wantErr: false,
wantBinding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "test-binding",
Namespace: "test",
ResourceVersion: "1001",
Labels: map[string]string{"app": "nginx", "foo": "bar"},
},
Spec: workv1alpha2.ResourceBindingSpec{
Resource: workv1alpha2.ObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Namespace: "fake-ns",
Name: "demo-app",
ResourceVersion: "22222",
},
RequiredBy: []workv1alpha2.BindingSnapshot{
{
Namespace: "default-1",
Name: "default-binding-1",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member1",
Replicas: 2,
},
},
},
{
Namespace: "default-2",
Name: "default-binding-2",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member2",
Replicas: 4,
},
},
},
{
Namespace: "default-3",
Name: "default-binding-3",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member3",
Replicas: 4,
},
},
},
{
Namespace: "test-1",
Name: "test-binding-1",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "foo",
Replicas: 1,
},
},
},
{
Namespace: "test-2",
Name: "test-binding-2",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "bar",
Replicas: 1,
},
},
},
},
ConflictResolution: policyv1alpha1.ConflictOverwrite,
},
},
setupClient: func() client.Client {
rb := &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "test-binding",
Namespace: "test",
ResourceVersion: "1000",
Labels: map[string]string{"foo": "bar"},
},
Spec: workv1alpha2.ResourceBindingSpec{
RequiredBy: []workv1alpha2.BindingSnapshot{
{
Namespace: "default-1",
Name: "default-binding-1",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member1",
Replicas: 2,
},
},
},
{
Namespace: "default-2",
Name: "default-binding-2",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member2",
Replicas: 3,
},
},
},
{
Namespace: "default-3",
Name: "default-binding-3",
Clusters: []workv1alpha2.TargetCluster{
{
Name: "member3",
Replicas: 4,
},
},
},
},
},
}
return fake.NewClientBuilder().WithScheme(Scheme).WithObjects(rb).Build()
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit f7d6da3

Please sign in to comment.