|
| 1 | +# Refactor FetchBindingFromKey to use types.NamespacedName |
| 2 | + |
| 3 | +**Date**: July 12, 2025 15:00 UTC |
| 4 | +**Task**: Refactor FetchBindingFromKey function to accept types.NamespacedName instead of string as the bindingKey parameter, and remove all converter function usage in the workgenerator controller. |
| 5 | + |
| 6 | +## Requirements |
| 7 | + |
| 8 | +1. Change FetchBindingFromKey function signature to accept `types.NamespacedName` instead of `queue.PlacementKey` (string) |
| 9 | +2. Update the function implementation to use the Namespace and Name fields directly |
| 10 | +3. Fix all callers of FetchBindingFromKey to pass types.NamespacedName |
| 11 | +4. Remove usage of converter functions like `GetObjectKeyFromRequest`, `ExtractNamespaceNameFromKey`, `GetObjectKeyFromObj`, and `GetObjectKeyFromNamespaceName` in the workgenerator controller |
| 12 | +5. Use direct namespace/name access from objects instead of converter functions |
| 13 | +6. Update tests accordingly |
| 14 | +7. Clean up unused imports |
| 15 | + |
| 16 | +## Additional Comments from User |
| 17 | + |
| 18 | +The user specifically requested to avoid using any converter functions and to use `types.NamespacedName` directly throughout the codebase. This improves type safety and removes unnecessary string parsing/formatting operations. |
| 19 | + |
| 20 | +## Plan |
| 21 | + |
| 22 | +### Phase 1: Function Signature Update |
| 23 | +1. ✅ **Task 1.1**: Update FetchBindingFromKey function to accept types.NamespacedName |
| 24 | +2. ✅ **Task 1.2**: Update ListBindingsFromKey function to accept types.NamespacedName for consistency |
| 25 | +3. ✅ **Task 1.3**: Update function implementations to use Namespace and Name fields directly |
| 26 | + |
| 27 | +### Phase 2: Update Callers in Controllers |
| 28 | +1. ✅ **Task 2.1**: Update workgenerator controller reconcile function to use req.Namespace and req.Name directly |
| 29 | +2. ✅ **Task 2.2**: Update retry logic to use resourceBinding.GetNamespace() and resourceBinding.GetName() directly |
| 30 | +3. ✅ **Task 2.3**: Update rollout controller (if any calls exist) |
| 31 | + |
| 32 | +### Phase 3: Remove Converter Functions in Workgenerator |
| 33 | +1. ✅ **Task 3.1**: Replace GetObjectKeyFromRequest usage with direct req.Namespace/req.Name |
| 34 | +2. ✅ **Task 3.2**: Replace ExtractNamespaceNameFromKey with direct namespace/name access |
| 35 | +3. ✅ **Task 3.3**: Replace GetObjectKeyFromObj with direct object.GetNamespace()/object.GetName() |
| 36 | +4. ✅ **Task 3.4**: Replace GetObjectKeyFromNamespaceName with direct string formatting |
| 37 | + |
| 38 | +### Phase 4: Update Tests and Clean Up |
| 39 | +1. ✅ **Task 4.1**: Update binding_resolver_test.go to use types.NamespacedName |
| 40 | +2. ✅ **Task 4.2**: Add necessary imports (k8s.io/apimachinery/pkg/types) |
| 41 | +3. ✅ **Task 4.3**: Remove unused imports (fmt, queue package) |
| 42 | +4. ✅ **Task 4.4**: Update all test cases to use proper NamespacedName structs |
| 43 | + |
| 44 | +## Decisions |
| 45 | + |
| 46 | +1. **Direct Type Usage**: Use `types.NamespacedName` directly instead of string-based placement keys to improve type safety and avoid parsing errors. |
| 47 | + |
| 48 | +2. **Eliminate String Conversion**: Remove all converter functions in favor of direct field access from Kubernetes objects (GetNamespace(), GetName()). |
| 49 | + |
| 50 | +3. **Simplified Key Generation**: For placement keys needed by other functions, use simple string formatting instead of converter functions. |
| 51 | + |
| 52 | +4. **Error Message Formatting**: In error messages, manually format namespace/name combinations instead of using converter functions. |
| 53 | + |
| 54 | +5. **Test Structure**: Update all test cases to use proper NamespacedName struct initialization instead of string-based keys. |
| 55 | + |
| 56 | +## Implementation Details |
| 57 | + |
| 58 | +### Key Changes Made |
| 59 | + |
| 60 | +1. **Updated FetchBindingFromKey function signature and implementation**: |
| 61 | + ```go |
| 62 | + // Before |
| 63 | + func FetchBindingFromKey(ctx context.Context, c client.Reader, bindingKey queue.PlacementKey) (placementv1beta1.BindingObj, error) |
| 64 | + |
| 65 | + // After |
| 66 | + func FetchBindingFromKey(ctx context.Context, c client.Reader, bindingKey types.NamespacedName) (placementv1beta1.BindingObj, error) |
| 67 | + ``` |
| 68 | + |
| 69 | +2. **Simplified implementation without converter functions**: |
| 70 | + ```go |
| 71 | + // Use bindingKey.Namespace and bindingKey.Name directly |
| 72 | + if bindingKey.Namespace == "" { |
| 73 | + // ClusterResourceBinding |
| 74 | + var crb placementv1beta1.ClusterResourceBinding |
| 75 | + err := c.Get(ctx, bindingKey, &crb) |
| 76 | + return &crb, err |
| 77 | + } |
| 78 | + // ResourceBinding |
| 79 | + var rb placementv1beta1.ResourceBinding |
| 80 | + err := c.Get(ctx, bindingKey, &rb) |
| 81 | + return &rb, err |
| 82 | + ``` |
| 83 | + |
| 84 | +3. **Updated workgenerator controller calls**: |
| 85 | + ```go |
| 86 | + // Before |
| 87 | + placementKey := controller.GetObjectKeyFromRequest(req) |
| 88 | + resourceBinding, err := controller.FetchBindingFromKey(ctx, r.Client, placementKey) |
| 89 | + |
| 90 | + // After |
| 91 | + bindingKey := types.NamespacedName{Namespace: req.Namespace, Name: req.Name} |
| 92 | + resourceBinding, err := controller.FetchBindingFromKey(ctx, r.Client, bindingKey) |
| 93 | + ``` |
| 94 | + |
| 95 | +4. **Direct object field access in retry logic**: |
| 96 | + ```go |
| 97 | + // Before |
| 98 | + placementKeyStr := controller.GetObjectKeyFromObj(resourceBinding) |
| 99 | + namespace, name, err := controller.ExtractNamespaceNameFromKey(placementKeyStr) |
| 100 | + latestBinding, err := controller.FetchBindingFromKey(ctx, r.Client, types.NamespacedName{Namespace: namespace, Name: name}) |
| 101 | + |
| 102 | + // After |
| 103 | + bindingKey := types.NamespacedName{Namespace: resourceBinding.GetNamespace(), Name: resourceBinding.GetName()} |
| 104 | + latestBinding, err := controller.FetchBindingFromKey(ctx, r.Client, bindingKey) |
| 105 | + ``` |
| 106 | + |
| 107 | +5. **Simplified placement key generation**: |
| 108 | + ```go |
| 109 | + // Before |
| 110 | + placemenKey := controller.GetObjectKeyFromNamespaceName(resourceBinding.GetNamespace(), resourceBinding.GetLabels()[fleetv1beta1.CRPTrackingLabel]) |
| 111 | + |
| 112 | + // After |
| 113 | + placementKey := resourceBinding.GetNamespace() + "/" + resourceBinding.GetLabels()[fleetv1beta1.CRPTrackingLabel] |
| 114 | + if resourceBinding.GetNamespace() == "" { |
| 115 | + placementKey = resourceBinding.GetLabels()[fleetv1beta1.CRPTrackingLabel] |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | +6. **Manual error message formatting**: |
| 120 | + ```go |
| 121 | + // Before |
| 122 | + controller.GetObjectKeyFromObj(resourceSnapshot) |
| 123 | + |
| 124 | + // After |
| 125 | + snapshotKey := resourceSnapshot.GetName() |
| 126 | + if resourceSnapshot.GetNamespace() != "" { |
| 127 | + snapshotKey = resourceSnapshot.GetNamespace() + "/" + resourceSnapshot.GetName() |
| 128 | + } |
| 129 | + ``` |
| 130 | + |
| 131 | +## Changes Made |
| 132 | + |
| 133 | +### Files Modified: |
| 134 | + |
| 135 | +1. **`/home/zhangryan/github/kubefleet/kubefleet/pkg/utils/controller/binding_resolver.go`**: |
| 136 | + - Changed FetchBindingFromKey function signature to accept `types.NamespacedName` |
| 137 | + - Updated ListBindingsFromKey function signature to accept `types.NamespacedName` |
| 138 | + - Simplified implementation to use Namespace and Name fields directly |
| 139 | + - Removed unused imports (fmt, queue package) |
| 140 | + - Added k8s.io/apimachinery/pkg/types import |
| 141 | + |
| 142 | +2. **`/home/zhangryan/github/kubefleet/kubefleet/pkg/controllers/workgenerator/controller.go`**: |
| 143 | + - Updated Reconcile function to use `req.Namespace` and `req.Name` directly |
| 144 | + - Updated retry logic in updateBindingStatusWithRetry to use direct field access |
| 145 | + - Replaced GetObjectKeyFromNamespaceName with simple string formatting |
| 146 | + - Replaced GetObjectKeyFromObj with manual namespace/name formatting in error messages |
| 147 | + - Removed all converter function usage |
| 148 | + |
| 149 | +3. **`/home/zhangryan/github/kubefleet/kubefleet/pkg/utils/controller/binding_resolver_test.go`**: |
| 150 | + - Updated test struct to use `types.NamespacedName` instead of `queue.PlacementKey` |
| 151 | + - Updated all test cases to use proper NamespacedName struct initialization |
| 152 | + - Added k8s.io/apimachinery/pkg/types import |
| 153 | + - Updated function calls to use NamespacedName parameters |
| 154 | + |
| 155 | +## Before/After Comparison |
| 156 | + |
| 157 | +### Before: |
| 158 | +- **String-based Keys**: Used `queue.PlacementKey` (string) requiring parsing and conversion |
| 159 | +- **Converter Functions**: Heavy reliance on utility functions for string formatting/parsing |
| 160 | +- **Error Prone**: String parsing could fail or produce incorrect results |
| 161 | +- **Complex Flow**: Multiple conversion steps between different representations |
| 162 | +- **Import Dependencies**: Required queue package and multiple utility functions |
| 163 | + |
| 164 | +### After: |
| 165 | +- **Type Safety**: Direct use of `types.NamespacedName` struct with compile-time type checking |
| 166 | +- **Direct Access**: Use object methods (GetNamespace(), GetName()) directly |
| 167 | +- **Simplified Logic**: No conversion steps or string parsing required |
| 168 | +- **Clean Code**: Reduced complexity and eliminated conversion function dependencies |
| 169 | +- **Better Performance**: No string parsing/formatting overhead for normal operations |
| 170 | + |
| 171 | +## Success Criteria |
| 172 | + |
| 173 | +✅ All success criteria met: |
| 174 | + |
| 175 | +1. **Function Signature Updated**: FetchBindingFromKey now accepts types.NamespacedName |
| 176 | +2. **Implementation Simplified**: Direct use of Namespace and Name fields |
| 177 | +3. **All Callers Updated**: Workgenerator controller uses direct field access |
| 178 | +4. **Converter Functions Removed**: No usage of converter functions in workgenerator controller |
| 179 | +5. **Tests Updated**: All test cases use proper NamespacedName structs |
| 180 | +6. **Clean Imports**: Removed unused imports and added necessary ones |
| 181 | +7. **Compilation Success**: Code compiles without errors |
| 182 | +8. **Type Safety Improved**: Better compile-time checking with structured types |
| 183 | + |
| 184 | +The refactoring successfully eliminates string-based key conversion and improves type safety throughout the binding resolution system while maintaining full functionality. |
| 185 | + |
| 186 | +## References |
| 187 | + |
| 188 | +- **Types Package**: k8s.io/apimachinery/pkg/types for NamespacedName |
| 189 | +- **Binding Interface**: apis/placement/v1beta1/binding_types.go for BindingObj interface |
| 190 | +- **Controller Utils**: pkg/utils/controller/binding_resolver.go for binding resolution functions |
| 191 | +- **Test Files**: pkg/utils/controller/binding_resolver_test.go for comprehensive test coverage |
0 commit comments