@@ -34,6 +34,7 @@ import (
34
34
"k8s.io/client-go/dynamic"
35
35
dynamicfake "k8s.io/client-go/dynamic/fake"
36
36
"k8s.io/client-go/kubernetes/scheme"
37
+ "k8s.io/utils/ptr"
37
38
"sigs.k8s.io/controller-runtime/pkg/client"
38
39
"sigs.k8s.io/controller-runtime/pkg/client/fake"
39
40
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -2224,6 +2225,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2224
2225
Namespace : "test" ,
2225
2226
ResourceVersion : "1000" ,
2226
2227
Labels : map [string ]string {"app" : "nginx" },
2228
+ OwnerReferences : []metav1.OwnerReference {
2229
+ {
2230
+ UID : "foo-bar" ,
2231
+ Controller : ptr.To [bool ](true ),
2232
+ },
2233
+ },
2227
2234
},
2228
2235
Spec : workv1alpha2.ResourceBindingSpec {
2229
2236
Resource : workv1alpha2.ObjectReference {
@@ -2274,6 +2281,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2274
2281
Namespace : "test" ,
2275
2282
ResourceVersion : "1001" ,
2276
2283
Labels : map [string ]string {"app" : "nginx" , "foo" : "bar" },
2284
+ OwnerReferences : []metav1.OwnerReference {
2285
+ {
2286
+ UID : "foo-bar" ,
2287
+ Controller : ptr.To [bool ](true ),
2288
+ },
2289
+ },
2277
2290
},
2278
2291
Spec : workv1alpha2.ResourceBindingSpec {
2279
2292
Resource : workv1alpha2.ObjectReference {
@@ -2344,6 +2357,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2344
2357
Namespace : "test" ,
2345
2358
ResourceVersion : "1000" ,
2346
2359
Labels : map [string ]string {"foo" : "bar" },
2360
+ OwnerReferences : []metav1.OwnerReference {
2361
+ {
2362
+ UID : "foo-bar" ,
2363
+ Controller : ptr.To [bool ](true ),
2364
+ },
2365
+ },
2347
2366
},
2348
2367
Spec : workv1alpha2.ResourceBindingSpec {
2349
2368
RequiredBy : []workv1alpha2.BindingSnapshot {
@@ -2390,6 +2409,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2390
2409
Name : "test-binding" ,
2391
2410
Namespace : "test" ,
2392
2411
Labels : map [string ]string {"app" : "nginx" },
2412
+ OwnerReferences : []metav1.OwnerReference {
2413
+ {
2414
+ UID : "foo-bar" ,
2415
+ Controller : ptr.To [bool ](true ),
2416
+ },
2417
+ },
2393
2418
},
2394
2419
Spec : workv1alpha2.ResourceBindingSpec {
2395
2420
Resource : workv1alpha2.ObjectReference {
@@ -2440,6 +2465,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2440
2465
Namespace : "test" ,
2441
2466
ResourceVersion : "1" ,
2442
2467
Labels : map [string ]string {"app" : "nginx" },
2468
+ OwnerReferences : []metav1.OwnerReference {
2469
+ {
2470
+ UID : "foo-bar" ,
2471
+ Controller : ptr.To [bool ](true ),
2472
+ },
2473
+ },
2443
2474
},
2444
2475
Spec : workv1alpha2.ResourceBindingSpec {
2445
2476
Resource : workv1alpha2.ObjectReference {
@@ -2495,6 +2526,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2495
2526
Namespace : "test" ,
2496
2527
ResourceVersion : "1000" ,
2497
2528
Labels : map [string ]string {"app" : "nginx" },
2529
+ OwnerReferences : []metav1.OwnerReference {
2530
+ {
2531
+ UID : "foo-bar" ,
2532
+ Controller : ptr.To [bool ](true ),
2533
+ },
2534
+ },
2498
2535
},
2499
2536
Spec : workv1alpha2.ResourceBindingSpec {
2500
2537
Resource : workv1alpha2.ObjectReference {
@@ -2546,6 +2583,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2546
2583
Namespace : "test" ,
2547
2584
ResourceVersion : "1001" ,
2548
2585
Labels : map [string ]string {"app" : "nginx" , "foo" : "bar" },
2586
+ OwnerReferences : []metav1.OwnerReference {
2587
+ {
2588
+ UID : "foo-bar" ,
2589
+ Controller : ptr.To [bool ](true ),
2590
+ },
2591
+ },
2549
2592
},
2550
2593
Spec : workv1alpha2.ResourceBindingSpec {
2551
2594
Resource : workv1alpha2.ObjectReference {
@@ -2617,6 +2660,12 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2617
2660
Namespace : "test" ,
2618
2661
ResourceVersion : "1000" ,
2619
2662
Labels : map [string ]string {"foo" : "bar" },
2663
+ OwnerReferences : []metav1.OwnerReference {
2664
+ {
2665
+ UID : "foo-bar" ,
2666
+ Controller : ptr.To [bool ](true ),
2667
+ },
2668
+ },
2620
2669
},
2621
2670
Spec : workv1alpha2.ResourceBindingSpec {
2622
2671
RequiredBy : []workv1alpha2.BindingSnapshot {
@@ -2656,6 +2705,113 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2656
2705
return fake .NewClientBuilder ().WithScheme (Scheme ).WithObjects (rb ).Build ()
2657
2706
},
2658
2707
},
2708
+ {
2709
+ name : "update attached binding which is being deleted" ,
2710
+ attachedBinding : & workv1alpha2.ResourceBinding {
2711
+ ObjectMeta : metav1.ObjectMeta {
2712
+ Name : "test-binding" ,
2713
+ Namespace : "test" ,
2714
+ ResourceVersion : "1000" ,
2715
+ Labels : map [string ]string {"app" : "nginx" },
2716
+ OwnerReferences : []metav1.OwnerReference {
2717
+ {
2718
+ UID : "foo-bar" ,
2719
+ Controller : ptr.To [bool ](true ),
2720
+ },
2721
+ },
2722
+ },
2723
+ Spec : workv1alpha2.ResourceBindingSpec {
2724
+ Resource : workv1alpha2.ObjectReference {
2725
+ APIVersion : "apps/v1" ,
2726
+ Kind : "Deployment" ,
2727
+ Namespace : "fake-ns" ,
2728
+ Name : "demo-app" ,
2729
+ ResourceVersion : "22222" ,
2730
+ },
2731
+ RequiredBy : []workv1alpha2.BindingSnapshot {
2732
+ {
2733
+ Namespace : "test-1" ,
2734
+ Name : "test-binding-1" ,
2735
+ Clusters : []workv1alpha2.TargetCluster {
2736
+ {
2737
+ Name : "foo" ,
2738
+ Replicas : 1 ,
2739
+ },
2740
+ },
2741
+ },
2742
+ },
2743
+ ConflictResolution : policyv1alpha1 .ConflictOverwrite ,
2744
+ },
2745
+ },
2746
+ wantErr : true ,
2747
+ wantBinding : & workv1alpha2.ResourceBinding {
2748
+ ObjectMeta : metav1.ObjectMeta {
2749
+ Name : "test-binding" ,
2750
+ Namespace : "test" ,
2751
+ ResourceVersion : "1001" ,
2752
+ Labels : map [string ]string {"app" : "nginx" , "foo" : "bar" },
2753
+ OwnerReferences : []metav1.OwnerReference {
2754
+ {
2755
+ UID : "foo-bar" ,
2756
+ Controller : ptr.To [bool ](true ),
2757
+ },
2758
+ },
2759
+ },
2760
+ Spec : workv1alpha2.ResourceBindingSpec {
2761
+ Resource : workv1alpha2.ObjectReference {
2762
+ APIVersion : "apps/v1" ,
2763
+ Kind : "Deployment" ,
2764
+ Namespace : "fake-ns" ,
2765
+ Name : "demo-app" ,
2766
+ ResourceVersion : "22222" ,
2767
+ },
2768
+ RequiredBy : []workv1alpha2.BindingSnapshot {
2769
+ {
2770
+ Namespace : "default-1" ,
2771
+ Name : "default-binding-1" ,
2772
+ Clusters : []workv1alpha2.TargetCluster {
2773
+ {
2774
+ Name : "member1" ,
2775
+ Replicas : 2 ,
2776
+ },
2777
+ },
2778
+ },
2779
+ },
2780
+ ConflictResolution : policyv1alpha1 .ConflictOverwrite ,
2781
+ },
2782
+ },
2783
+ setupClient : func () client.Client {
2784
+ rb := & workv1alpha2.ResourceBinding {
2785
+ ObjectMeta : metav1.ObjectMeta {
2786
+ Name : "test-binding" ,
2787
+ Namespace : "test" ,
2788
+ ResourceVersion : "1000" ,
2789
+ Labels : map [string ]string {"foo" : "bar" },
2790
+ OwnerReferences : []metav1.OwnerReference {
2791
+ {
2792
+ UID : "bar-foo" ,
2793
+ Controller : ptr.To [bool ](true ),
2794
+ },
2795
+ },
2796
+ },
2797
+ Spec : workv1alpha2.ResourceBindingSpec {
2798
+ RequiredBy : []workv1alpha2.BindingSnapshot {
2799
+ {
2800
+ Namespace : "default-1" ,
2801
+ Name : "default-binding-1" ,
2802
+ Clusters : []workv1alpha2.TargetCluster {
2803
+ {
2804
+ Name : "member1" ,
2805
+ Replicas : 2 ,
2806
+ },
2807
+ },
2808
+ },
2809
+ },
2810
+ },
2811
+ }
2812
+ return fake .NewClientBuilder ().WithScheme (Scheme ).WithObjects (rb ).Build ()
2813
+ },
2814
+ },
2659
2815
}
2660
2816
for _ , tt := range tests {
2661
2817
t .Run (tt .name , func (t * testing.T ) {
@@ -2666,10 +2822,13 @@ func Test_createOrUpdateAttachedBinding(t *testing.T) {
2666
2822
if (err != nil ) != tt .wantErr {
2667
2823
t .Errorf ("createOrUpdateAttachedBinding() error = %v, wantErr %v" , err , tt .wantErr )
2668
2824
}
2825
+ if tt .wantErr {
2826
+ return
2827
+ }
2669
2828
existBinding := & workv1alpha2.ResourceBinding {}
2670
2829
bindingKey := client .ObjectKeyFromObject (tt .attachedBinding )
2671
2830
err = d .Client .Get (context .TODO (), bindingKey , existBinding )
2672
- if ( err != nil ) != tt . wantErr {
2831
+ if err != nil {
2673
2832
t .Errorf ("createOrUpdateAttachedBinding(), Client.Get() error = %v, wantErr %v" , err , tt .wantErr )
2674
2833
}
2675
2834
if ! reflect .DeepEqual (existBinding , tt .wantBinding ) {
0 commit comments