tokers commented on a change in pull request #740:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/740#discussion_r748951999



##########
File path: pkg/ingress/status.go
##########
@@ -163,8 +173,142 @@ func (c *Controller) recordStatus(at interface{}, reason 
string, err error, stat
                                )
                        }
                }
+       case *networkingv1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.NetworkingV1().Ingresses(v.Namespace).UpdateStatus(context.TODO(), 
v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
+
+       case *networkingv1beta1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.NetworkingV1beta1().Ingresses(v.Namespace).UpdateStatus(context.TODO(),
 v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
+       case *extensionsv1beta1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.ExtensionsV1beta1().Ingresses(v.Namespace).UpdateStatus(context.TODO(),
 v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
        default:
                // This should not be executed
                log.Errorf("unsupported resource record: %s", v)
        }
 }
+
+// ingressPublishAddresses get addressed used to expose Ingress
+func (c *Controller) ingressPublishAddresses() ([]string, error) {
+       ingressPublishService := c.cfg.IngressPublishService
+       ingressStatusAddress := c.cfg.IngressStatusAddress
+       addrs := []string{}
+
+       // if ingressStatusAddress is specified, it will be used first
+       if len(ingressStatusAddress) > 0 {
+               addrs = append(addrs, ingressStatusAddress...)
+               return addrs, nil

Review comment:
       As per the description of this option, the static addresses should only 
be used if the LB is not ready.

##########
File path: pkg/ingress/status.go
##########
@@ -163,8 +173,142 @@ func (c *Controller) recordStatus(at interface{}, reason 
string, err error, stat
                                )
                        }
                }
+       case *networkingv1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.NetworkingV1().Ingresses(v.Namespace).UpdateStatus(context.TODO(), 
v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
+
+       case *networkingv1beta1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.NetworkingV1beta1().Ingresses(v.Namespace).UpdateStatus(context.TODO(),
 v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
+       case *extensionsv1beta1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.ExtensionsV1beta1().Ingresses(v.Namespace).UpdateStatus(context.TODO(),
 v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
        default:
                // This should not be executed
                log.Errorf("unsupported resource record: %s", v)
        }
 }
+
+// ingressPublishAddresses get addressed used to expose Ingress
+func (c *Controller) ingressPublishAddresses() ([]string, error) {
+       ingressPublishService := c.cfg.IngressPublishService
+       ingressStatusAddress := c.cfg.IngressStatusAddress
+       addrs := []string{}
+
+       // if ingressStatusAddress is specified, it will be used first
+       if len(ingressStatusAddress) > 0 {
+               addrs = append(addrs, ingressStatusAddress...)
+               return addrs, nil
+       }
+
+       namespace, name, err := 
cache.SplitMetaNamespaceKey(ingressPublishService)

Review comment:
       The separation  can be handled in advance.

##########
File path: pkg/ingress/status.go
##########
@@ -163,8 +173,142 @@ func (c *Controller) recordStatus(at interface{}, reason 
string, err error, stat
                                )
                        }
                }
+       case *networkingv1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.NetworkingV1().Ingresses(v.Namespace).UpdateStatus(context.TODO(), 
v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
+
+       case *networkingv1beta1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.NetworkingV1beta1().Ingresses(v.Namespace).UpdateStatus(context.TODO(),
 v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
+       case *extensionsv1beta1.Ingress:
+               // set to status
+               lbips, err := c.ingressLBStatusIPs()
+               if err != nil {
+                       log.Errorw("failed to get APISIX gateway external IPs",
+                               zap.Error(err),
+                       )
+
+               }
+
+               v.Status.LoadBalancer.Ingress = lbips
+               if _, errRecord := 
kubeClient.ExtensionsV1beta1().Ingresses(v.Namespace).UpdateStatus(context.TODO(),
 v, metav1.UpdateOptions{}); errRecord != nil {
+                       log.Errorw("failed to record status change for 
IngressV1",
+                               zap.Error(errRecord),
+                               zap.String("name", v.Name),
+                               zap.String("namespace", v.Namespace),
+                       )
+               }
        default:
                // This should not be executed
                log.Errorf("unsupported resource record: %s", v)
        }
 }
+
+// ingressPublishAddresses get addressed used to expose Ingress
+func (c *Controller) ingressPublishAddresses() ([]string, error) {
+       ingressPublishService := c.cfg.IngressPublishService
+       ingressStatusAddress := c.cfg.IngressStatusAddress
+       addrs := []string{}
+
+       // if ingressStatusAddress is specified, it will be used first
+       if len(ingressStatusAddress) > 0 {
+               addrs = append(addrs, ingressStatusAddress...)
+               return addrs, nil
+       }
+
+       namespace, name, err := 
cache.SplitMetaNamespaceKey(ingressPublishService)
+       if err != nil {
+               log.Errorf("invalid ingressPublishService %s: %s", 
ingressPublishService, err)
+               return nil, err
+       }
+
+       kubeClient := c.kubeClient.Client
+       svc, err := kubeClient.CoreV1().Services(namespace).Get(context.TODO(), 
name, metav1.GetOptions{})
+       if err != nil {
+               return nil, err
+       }
+
+       switch svc.Spec.Type {
+       case apiv1.ServiceTypeLoadBalancer:
+               if len(svc.Status.LoadBalancer.Ingress) < 1 {
+                       return addrs, fmt.Errorf(_gatewayLBNotReadyMessage)
+               }
+
+               for _, ip := range svc.Status.LoadBalancer.Ingress {
+                       if ip.IP == "" {
+                               // typically AWS load-balancers
+                               addrs = append(addrs, ip.Hostname)
+                       } else {
+                               addrs = append(addrs, ip.IP)
+                       }
+               }
+
+               addrs = append(addrs, svc.Spec.ExternalIPs...)
+               return addrs, nil
+       default:
+               return addrs, nil
+       }
+
+}
+
+// ingressLBStatusIPs organizes the available addresses
+func (c *Controller) ingressLBStatusIPs() ([]apiv1.LoadBalancerIngress, error) 
{
+       lbips := []apiv1.LoadBalancerIngress{}
+       var ips []string
+
+       for {

Review comment:
       Use https://pkg.go.dev/k8s.io/apimachinery/pkg/util/wait




-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to