|
| 1 | +package finalizer_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + "k8s.io/client-go/dynamic/fake" |
| 13 | + |
| 14 | + "github.com/authzed/controller-idioms/finalizer" |
| 15 | +) |
| 16 | + |
| 17 | +func ExampleGetAddFunc() { |
| 18 | + ctx, cancel := context.WithCancel(context.Background()) |
| 19 | + defer cancel() |
| 20 | + |
| 21 | + // Create a test object without a finalizer |
| 22 | + testObj := &unstructured.Unstructured{ |
| 23 | + Object: map[string]interface{}{ |
| 24 | + "apiVersion": "example.com/v1", |
| 25 | + "kind": "MyObject", |
| 26 | + "metadata": map[string]interface{}{ |
| 27 | + "name": "test-object", |
| 28 | + "namespace": "default", |
| 29 | + }, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + // Create a fake dynamic client with the test object |
| 34 | + scheme := runtime.NewScheme() |
| 35 | + dynamicClient := fake.NewSimpleDynamicClient(scheme, testObj) |
| 36 | + |
| 37 | + // Define the GVR for our resource |
| 38 | + gvr := schema.GroupVersionResource{ |
| 39 | + Group: "example.com", |
| 40 | + Version: "v1", |
| 41 | + Resource: "myobjects", |
| 42 | + } |
| 43 | + |
| 44 | + // Create an add function for finalizers |
| 45 | + addFunc := finalizer.GetAddFunc[*metav1.PartialObjectMetadata]( |
| 46 | + dynamicClient, |
| 47 | + gvr, |
| 48 | + "my-controller.example.com/finalizer", |
| 49 | + "my-controller", |
| 50 | + ) |
| 51 | + |
| 52 | + // Use the function to add a finalizer |
| 53 | + result, err := addFunc(ctx, types.NamespacedName{ |
| 54 | + Name: "test-object", |
| 55 | + Namespace: "default", |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + fmt.Printf("Error adding finalizer: %v\n", err) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + fmt.Printf("Finalizer added: %v\n", len(result.GetFinalizers()) > 0) |
| 63 | + fmt.Printf("Finalizer name: %s\n", result.GetFinalizers()[0]) |
| 64 | + |
| 65 | + // Output: Finalizer added: true |
| 66 | + // Finalizer name: my-controller.example.com/finalizer |
| 67 | +} |
| 68 | + |
| 69 | +func ExampleGetRemoveFunc() { |
| 70 | + ctx, cancel := context.WithCancel(context.Background()) |
| 71 | + defer cancel() |
| 72 | + |
| 73 | + now := metav1.Now() |
| 74 | + |
| 75 | + // Create a test object with a finalizer and deletion timestamp |
| 76 | + testObj := &unstructured.Unstructured{ |
| 77 | + Object: map[string]interface{}{ |
| 78 | + "apiVersion": "example.com/v1", |
| 79 | + "kind": "MyObject", |
| 80 | + "metadata": map[string]interface{}{ |
| 81 | + "name": "test-object", |
| 82 | + "namespace": "default", |
| 83 | + "finalizers": []interface{}{ |
| 84 | + "my-controller.example.com/finalizer", |
| 85 | + }, |
| 86 | + "deletionTimestamp": now.Format("2006-01-02T15:04:05Z"), |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + // Create a fake dynamic client with the test object |
| 92 | + scheme := runtime.NewScheme() |
| 93 | + dynamicClient := fake.NewSimpleDynamicClient(scheme, testObj) |
| 94 | + |
| 95 | + // Define the GVR for our resource |
| 96 | + gvr := schema.GroupVersionResource{ |
| 97 | + Group: "example.com", |
| 98 | + Version: "v1", |
| 99 | + Resource: "myobjects", |
| 100 | + } |
| 101 | + |
| 102 | + // Create a remove function for finalizers |
| 103 | + removeFunc := finalizer.GetRemoveFunc[*metav1.PartialObjectMetadata]( |
| 104 | + dynamicClient, |
| 105 | + gvr, |
| 106 | + "my-controller.example.com/finalizer", |
| 107 | + "my-controller", |
| 108 | + ) |
| 109 | + |
| 110 | + // Use the function to remove a finalizer |
| 111 | + result, err := removeFunc(ctx, types.NamespacedName{ |
| 112 | + Name: "test-object", |
| 113 | + Namespace: "default", |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + fmt.Printf("Error removing finalizer: %v\n", err) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + fmt.Printf("Finalizer removed: %v\n", len(result.GetFinalizers()) == 0) |
| 121 | + fmt.Printf("Has deletion timestamp: %v\n", result.GetDeletionTimestamp() != nil) |
| 122 | + |
| 123 | + // Output: Finalizer removed: true |
| 124 | + // Has deletion timestamp: true |
| 125 | +} |
| 126 | + |
| 127 | +func ExampleGetAddFunc_alreadyHasFinalizer() { |
| 128 | + ctx, cancel := context.WithCancel(context.Background()) |
| 129 | + defer cancel() |
| 130 | + |
| 131 | + // Create a test object that already has the finalizer |
| 132 | + testObj := &unstructured.Unstructured{ |
| 133 | + Object: map[string]interface{}{ |
| 134 | + "apiVersion": "example.com/v1", |
| 135 | + "kind": "MyObject", |
| 136 | + "metadata": map[string]interface{}{ |
| 137 | + "name": "test-object", |
| 138 | + "namespace": "default", |
| 139 | + "finalizers": []interface{}{ |
| 140 | + "my-controller.example.com/finalizer", |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + // Create a fake dynamic client with the test object |
| 147 | + scheme := runtime.NewScheme() |
| 148 | + dynamicClient := fake.NewSimpleDynamicClient(scheme, testObj) |
| 149 | + |
| 150 | + // Define the GVR for our resource |
| 151 | + gvr := schema.GroupVersionResource{ |
| 152 | + Group: "example.com", |
| 153 | + Version: "v1", |
| 154 | + Resource: "myobjects", |
| 155 | + } |
| 156 | + |
| 157 | + // Create an add function for finalizers |
| 158 | + addFunc := finalizer.GetAddFunc[*metav1.PartialObjectMetadata]( |
| 159 | + dynamicClient, |
| 160 | + gvr, |
| 161 | + "my-controller.example.com/finalizer", |
| 162 | + "my-controller", |
| 163 | + ) |
| 164 | + |
| 165 | + // Use the function - it should be a no-op since finalizer already exists |
| 166 | + result, err := addFunc(ctx, types.NamespacedName{ |
| 167 | + Name: "test-object", |
| 168 | + Namespace: "default", |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + fmt.Printf("Error: %v\n", err) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + fmt.Printf("Finalizer count: %d\n", len(result.GetFinalizers())) |
| 176 | + fmt.Printf("Still has finalizer: %v\n", result.GetFinalizers()[0] == "my-controller.example.com/finalizer") |
| 177 | + |
| 178 | + // Output: Finalizer count: 1 |
| 179 | + // Still has finalizer: true |
| 180 | +} |
0 commit comments