lingsamuel commented on code in PR #1306:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/1306#discussion_r1000222427


##########
pkg/providers/apisix/translation/apisix_upstream.go:
##########
@@ -47,3 +56,116 @@ func (t *translator) translateService(namespace, svcName, 
subset, svcResolveGran
        ups.ID = id.GenID(ups.Name)
        return ups, nil
 }
+
+// TODO: Scheme (http/https)
+func (t *translator) TranslateApisixUpstreamExternalNodes(au 
*v2.ApisixUpstream) ([]apisixv1.UpstreamNode, error) {
+       var nodes []apisixv1.UpstreamNode
+       for i, node := range au.Spec.ExternalNodes {
+               if node.Type == v2.ExternalTypeDomain {
+                       arr := strings.Split(node.Name, ":")
+
+                       weight := translation.DefaultWeight
+                       if node.Weight != nil {
+                               weight = *node.Weight
+                       }
+                       n := apisixv1.UpstreamNode{
+                               Host:   arr[0],
+                               Weight: weight,
+                       }
+
+                       if len(arr) == 1 {
+                               if strings.HasPrefix(arr[0], "https://";) {
+                                       n.Port = 443
+                               } else {
+                                       n.Port = 80
+                               }
+                       } else if len(arr) == 2 {
+                               port, err := strconv.Atoi(arr[1])
+                               if err != nil {
+                                       return nil, errors.Wrap(err, 
fmt.Sprintf("failed to parse ApisixUpstream %s/%s port: at ExternalNodes[%v]: 
%s", au.Namespace, au.Name, i, node.Name))
+                               }
+
+                               n.Port = port
+                       }
+
+                       nodes = append(nodes, n)
+               } else if node.Type == v2.ExternalTypeService {
+                       svc, err := 
t.ServiceLister.Services(au.Namespace).Get(node.Name)
+                       if err != nil {
+                               // In theory, ApisixRoute now watches all 
service add event, a not found error is already handled
+                               if k8serrors.IsNotFound(err) {
+                                       // TODO: Should retry
+                                       return nil, err
+                               }
+                               return nil, err
+                       }
+
+                       if svc.Spec.Type != corev1.ServiceTypeExternalName {
+                               return nil, fmt.Errorf("ApisixUpstream %s/%s 
ExternalNodes[%v] must refers to a ExternalName service: %s", au.Namespace, 
au.Name, i, node.Name)
+                       }
+
+                       weight := translation.DefaultWeight
+                       if node.Weight != nil {
+                               weight = *node.Weight
+                       }
+                       n := apisixv1.UpstreamNode{
+                               Host:   svc.Spec.ExternalName,
+                               Weight: weight,
+                       }
+
+                       arr := strings.Split(svc.Spec.ExternalName, ":")
+                       if len(arr) == 1 {
+                               if strings.HasPrefix(arr[0], "https://";) {
+                                       n.Port = 443
+                               } else {
+                                       n.Port = 80
+                               }
+                       } else if len(arr) == 2 {
+                               port, err := strconv.Atoi(arr[1])
+                               if err != nil {
+                                       return nil, errors.Wrap(err, 
fmt.Sprintf("failed to parse ApisixUpstream %s/%s port: at ExternalNodes[%v]: 
%s", au.Namespace, au.Name, i, node.Name))
+                               }
+
+                               n.Port = port
+                       }

Review Comment:
   APISIX requires a port and the port field has not been implemented yet. This 
is a temporary solution.



-- 
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