chinmoysahu commented on code in PR #815:
URL: https://github.com/apache/solr-operator/pull/815#discussion_r2728890251


##########
controllers/solrcloud_controller.go:
##########
@@ -436,6 +441,223 @@ func (r *SolrCloudReconciler) Reconcile(ctx 
context.Context, req ctrl.Request) (
                }
        }
 
+       // Reconcile Gateway API HTTPRoutes
+       if extAddressabilityOpts != nil && extAddressabilityOpts.Method == 
solrv1beta1.Gateway {
+               // Validate that gateway config is provided
+               if extAddressabilityOpts.Gateway == nil || 
len(extAddressabilityOpts.Gateway.ParentRefs) == 0 {
+                       return requeueOrNot, fmt.Errorf("gateway.parentRefs is 
required when using method=Gateway")
+               }
+
+               // Reconcile Common HTTPRoute (if not hidden)
+               if !extAddressabilityOpts.HideCommon {
+                       commonHTTPRoute := 
util.GenerateCommonHTTPRoute(instance, solrNodeNames)
+                       commonHTTPRouteLogger := logger.WithValues("httproute", 
commonHTTPRoute.Name)
+                       foundCommonHTTPRoute := &gatewayv1.HTTPRoute{}
+                       err = r.Get(ctx, types.NamespacedName{Name: 
commonHTTPRoute.Name, Namespace: commonHTTPRoute.Namespace}, 
foundCommonHTTPRoute)
+                       if err != nil && errors.IsNotFound(err) {
+                               commonHTTPRouteLogger.Info("Creating common 
HTTPRoute")
+                               if err = 
controllerutil.SetControllerReference(instance, commonHTTPRoute, r.Scheme); err 
== nil {
+                                       err = r.Create(ctx, commonHTTPRoute)
+                               }
+                       } else if err == nil {
+                               var needsUpdate bool
+                               needsUpdate, err = 
util.OvertakeControllerRef(instance, foundCommonHTTPRoute, r.Scheme)
+                               needsUpdate = 
util.CopyHTTPRouteFields(commonHTTPRoute, foundCommonHTTPRoute, 
commonHTTPRouteLogger) || needsUpdate
+
+                               if needsUpdate && err == nil {
+                                       commonHTTPRouteLogger.Info("Updating 
common HTTPRoute")
+                                       err = r.Update(ctx, 
foundCommonHTTPRoute)
+                               }
+                       }
+                       if err != nil {
+                               return requeueOrNot, err
+                       }
+               } else {
+                       // Delete common HTTPRoute if it exists but should be 
hidden
+                       foundCommonHTTPRoute := &gatewayv1.HTTPRoute{}
+                       err := r.Get(ctx, types.NamespacedName{Name: 
instance.CommonHTTPRouteName(), Namespace: instance.GetNamespace()}, 
foundCommonHTTPRoute)
+                       if err == nil {
+                               logger.Info("Deleting common HTTPRoute 
(hideCommon=true)")
+                               err = r.Delete(ctx, foundCommonHTTPRoute)
+                               if err != nil && !errors.IsNotFound(err) {
+                                       return requeueOrNot, err
+                               }
+                       }
+               }
+
+               // Reconcile Node HTTPRoutes (if not hidden)
+               if !extAddressabilityOpts.HideNodes {
+                       for _, nodeName := range solrNodeNames {
+                               nodeHTTPRoute := 
util.GenerateNodeHTTPRoute(instance, nodeName)
+                               nodeHTTPRouteLogger := 
logger.WithValues("httproute", nodeHTTPRoute.Name)
+                               foundNodeHTTPRoute := &gatewayv1.HTTPRoute{}
+                               err = r.Get(ctx, types.NamespacedName{Name: 
nodeHTTPRoute.Name, Namespace: nodeHTTPRoute.Namespace}, foundNodeHTTPRoute)
+                               if err != nil && errors.IsNotFound(err) {
+                                       nodeHTTPRouteLogger.Info("Creating node 
HTTPRoute")
+                                       if err = 
controllerutil.SetControllerReference(instance, nodeHTTPRoute, r.Scheme); err 
== nil {
+                                               err = r.Create(ctx, 
nodeHTTPRoute)
+                                       }
+                               } else if err == nil {
+                                       var needsUpdate bool
+                                       needsUpdate, err = 
util.OvertakeControllerRef(instance, foundNodeHTTPRoute, r.Scheme)
+                                       needsUpdate = 
util.CopyHTTPRouteFields(nodeHTTPRoute, foundNodeHTTPRoute, 
nodeHTTPRouteLogger) || needsUpdate
+
+                                       if needsUpdate && err == nil {
+                                               
nodeHTTPRouteLogger.Info("Updating node HTTPRoute")
+                                               err = r.Update(ctx, 
foundNodeHTTPRoute)
+                                       }
+                               }
+                               if err != nil {
+                                       return requeueOrNot, err
+                               }
+                       }
+               }
+
+               // Cleanup orphaned node HTTPRoutes (when scaling down)
+               httpRouteList := &gatewayv1.HTTPRouteList{}
+               labelSelector := labels.SelectorFromSet(instance.SharedLabels())
+               listOps := &client.ListOptions{
+                       Namespace:     instance.Namespace,
+                       LabelSelector: labelSelector,
+               }
+               err = r.List(ctx, httpRouteList, listOps)
+               if err == nil {
+                       for _, httpRoute := range httpRouteList.Items {
+                               // Skip the common HTTPRoute
+                               if httpRoute.Name == 
instance.CommonHTTPRouteName() {
+                                       continue
+                               }
+                               // Check if this node still exists
+                               nodeExists := false
+                               for _, nodeName := range solrNodeNames {
+                                       if httpRoute.Name == 
instance.NodeHTTPRouteName(nodeName) {
+                                               nodeExists = true
+                                               break
+                                       }
+                               }
+                               // Delete orphaned HTTPRoute
+                               if !nodeExists {
+                                       logger.Info("Deleting orphaned node 
HTTPRoute", "httproute", httpRoute.Name)
+                                       err = r.Delete(ctx, &httpRoute)
+                                       if err != nil && 
!errors.IsNotFound(err) {
+                                               return requeueOrNot, err
+                                       }
+                               }
+                       }
+               }
+
+               // Reconcile BackendTLSPolicy resources if configured
+               if extAddressabilityOpts.HasBackendTLSPolicy() {
+                       // Reconcile Common BackendTLSPolicy (if not hidden)
+                       if !extAddressabilityOpts.HideCommon {
+                               commonPolicy := 
util.GenerateCommonBackendTLSPolicy(instance)
+                               if commonPolicy != nil {
+                                       commonPolicyLogger := 
logger.WithValues("backendtlspolicy", commonPolicy.Name)
+                                       foundCommonPolicy := 
&gatewayv1.BackendTLSPolicy{}
+                                       err = r.Get(ctx, 
types.NamespacedName{Name: commonPolicy.Name, Namespace: 
commonPolicy.Namespace}, foundCommonPolicy)
+
+                                       if err != nil && errors.IsNotFound(err) 
{
+                                               
commonPolicyLogger.Info("Creating BackendTLSPolicy")
+                                               if err = 
controllerutil.SetControllerReference(instance, commonPolicy, r.Scheme); err == 
nil {
+                                                       err = r.Create(ctx, 
commonPolicy)
+                                               }
+                                       } else if err == nil {
+                                               var needsUpdate bool
+                                               needsUpdate, err = 
util.OvertakeControllerRef(instance, foundCommonPolicy, r.Scheme)
+                                               if err == nil && 
util.CopyBackendTLSPolicyFields(commonPolicy, foundCommonPolicy, 
commonPolicyLogger) {
+                                                       needsUpdate = true
+                                               }
+                                               if needsUpdate {
+                                                       
commonPolicyLogger.Info("Updating BackendTLSPolicy")
+                                                       err = r.Update(ctx, 
foundCommonPolicy)
+                                               }
+                                       }
+                                       if err != nil {
+                                               return requeueOrNot, err
+                                       }
+                               }
+                       }
+
+                       // Reconcile Node BackendTLSPolicies (if not hidden)
+                       if !extAddressabilityOpts.HideNodes {
+                               for _, nodeName := range solrNodeNames {
+                                       nodePolicy := 
util.GenerateNodeBackendTLSPolicy(instance, nodeName)
+                                       if nodePolicy != nil {
+                                               nodePolicyLogger := 
logger.WithValues("backendtlspolicy", nodePolicy.Name)
+                                               foundNodePolicy := 
&gatewayv1.BackendTLSPolicy{}
+                                               err = r.Get(ctx, 
types.NamespacedName{Name: nodePolicy.Name, Namespace: nodePolicy.Namespace}, 
foundNodePolicy)
+
+                                               if err != nil && 
errors.IsNotFound(err) {
+                                                       
nodePolicyLogger.Info("Creating BackendTLSPolicy")
+                                                       if err = 
controllerutil.SetControllerReference(instance, nodePolicy, r.Scheme); err == 
nil {
+                                                               err = 
r.Create(ctx, nodePolicy)
+                                                       }
+                                               } else if err == nil {
+                                                       var needsUpdate bool
+                                                       needsUpdate, err = 
util.OvertakeControllerRef(instance, foundNodePolicy, r.Scheme)
+                                                       if err == nil && 
util.CopyBackendTLSPolicyFields(nodePolicy, foundNodePolicy, nodePolicyLogger) {
+                                                               needsUpdate = 
true
+                                                       }
+                                                       if needsUpdate {
+                                                               
nodePolicyLogger.Info("Updating BackendTLSPolicy")
+                                                               err = 
r.Update(ctx, foundNodePolicy)
+                                                       }
+                                               }
+
+                                               if err != nil {
+                                                       return requeueOrNot, err
+                                               }
+                                       }
+                               }
+                       }
+
+                       // Cleanup orphaned node BackendTLSPolicies (when 
scaling down)
+                       backendTLSPolicyList := 
&gatewayv1.BackendTLSPolicyList{}
+                       err = r.List(ctx, backendTLSPolicyList, listOps)
+                       if err == nil {
+                               for _, policy := range 
backendTLSPolicyList.Items {
+                                       // Skip the common BackendTLSPolicy
+                                       if policy.Name == 
instance.CommonBackendTLSPolicyName() {
+                                               continue
+                                       }
+                                       // Check if this node still exists
+                                       nodeExists := false
+                                       for _, nodeName := range solrNodeNames {
+                                               if policy.Name == 
instance.NodeBackendTLSPolicyName(nodeName) {
+                                                       nodeExists = true
+                                                       break
+                                               }
+                                       }
+                                       if !nodeExists {
+                                               logger.Info("Deleting orphaned 
BackendTLSPolicy", "backendtlspolicy", policy.Name)
+                                               err = r.Delete(ctx, &policy)
+                                               if err != nil && 
!errors.IsNotFound(err) {
+                                                       return requeueOrNot, err
+                                               }
+                                       }
+                               }
+                       }
+               }
+       } else {
+               // If not using Gateway method, clean up any existing HTTPRoutes
+               httpRouteList := &gatewayv1.HTTPRouteList{}
+               labelSelector := labels.SelectorFromSet(instance.SharedLabels())
+               listOps := &client.ListOptions{
+                       Namespace:     instance.Namespace,
+                       LabelSelector: labelSelector,
+               }
+               err := r.List(ctx, httpRouteList, listOps)
+               if err == nil && len(httpRouteList.Items) > 0 {
+                       logger.Info("Cleaning up HTTPRoutes (method changed 
from Gateway)")
+                       for _, httpRoute := range httpRouteList.Items {
+                               err = r.Delete(ctx, &httpRoute)
+                               if err != nil && !errors.IsNotFound(err) {
+                                       return requeueOrNot, err
+                               }
+                       }
+               }

Review Comment:
   Good catch. Added cleanup logic for BackendTLSPolicy similar to HTTPRoute. 
And tested end-to-end again.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to