You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2022/08/16 04:37:21 UTC

[GitHub] [apisix-ingress-controller] bzp2010 commented on a diff in pull request #1222: fix: nodes convert failed

bzp2010 commented on code in PR #1222:
URL: https://github.com/apache/apisix-ingress-controller/pull/1222#discussion_r946328504


##########
pkg/types/apisix/v1/types.go:
##########
@@ -216,21 +217,55 @@ type UpstreamNodes []UpstreamNode
 // and by default empty array will be encoded as '{}'.
 // We have to maintain the compatibility.
 func (n *UpstreamNodes) UnmarshalJSON(p []byte) error {
+	var data []UpstreamNode
 	if p[0] == '{' {
-		if len(p) != 2 {
-			return errors.New("unexpected non-empty object")
+		value := map[string]float64{}
+		if err := json.Unmarshal(p, &value); err != nil {
+			return err
+		}
+		for k, v := range value {
+			node, err := mapKV2Node(k, v)
+			if err != nil {
+				return err
+			}
+			data = append(data, *node)
 		}
-		*n = UpstreamNodes{}
+		*n = data
 		return nil
 	}
-	var data []UpstreamNode
 	if err := json.Unmarshal(p, &data); err != nil {
 		return err
 	}
 	*n = data
 	return nil
 }
 
+func mapKV2Node(key string, val float64) (*UpstreamNode, error) {
+	hp := strings.Split(key, ":")
+	host := hp[0]
+	//  according to APISIX upstream nodes policy, port is optional
+	port := "0"

Review Comment:
   I think it is appropriate to configure the default port to `80`, unless the user manually specifies a schema for Upstream.
   
   ---------------------
   
   Tips: 
   The dashboard now uses the `object` to store nodes data (when created graphically using a form) it can be created without filling in the port, at this point there will be APISIX to determine the port automatically based on the scheme; when the user manually enters JSON to create it, a schema check will be performed, and if the port does not exist it will not be created successfully.
   



-- 
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-unsubscribe@apisix.apache.org

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