From 7fd3f43c0dfb66f40b1ee3be43b93e1248bc4257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BE=84=E6=BD=AD?= Date: Thu, 30 Mar 2023 19:24:55 +0800 Subject: [PATCH] fix bug of port parsing in destination annotation (#262) --- pkg/ingress/kube/annotations/destination.go | 17 +++++----- .../kube/annotations/destination_test.go | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/pkg/ingress/kube/annotations/destination.go b/pkg/ingress/kube/annotations/destination.go index 1cddbf145..9919ae4ec 100644 --- a/pkg/ingress/kube/annotations/destination.go +++ b/pkg/ingress/kube/annotations/destination.go @@ -73,10 +73,14 @@ func (a destination) Parse(annotations Annotations, config *Ingress, _ *GlobalCo } address := pairs[addrIndex] host := address - var port string + var port uint64 colon := strings.LastIndex(address, ":") if colon != -1 { - host, port = address[:colon], address[colon+1:] + var err error + port, err = strconv.ParseUint(address[colon+1:], 10, 32) + if err == nil && port > 0 && port < 65536 { + host = address[:colon] + } } var subset string if len(pairs) >= addrIndex+2 { @@ -89,14 +93,9 @@ func (a destination) Parse(annotations Annotations, config *Ingress, _ *GlobalCo }, Weight: int32(weight), } - if port != "" { - portNumber, err := strconv.ParseUint(port, 10, 32) - if err != nil { - IngressLog.Errorf("destination addr %s has invalid port %s within ingress %s/%s", address, port, config.Namespace, config.Name) - return nil - } + if port > 0 { dest.Destination.Port = &networking.PortSelector{ - Number: uint32(portNumber), + Number: uint32(port), } } IngressLog.Debugf("destination generated for ingress %s/%s: %v", config.Namespace, config.Name, dest) diff --git a/pkg/ingress/kube/annotations/destination_test.go b/pkg/ingress/kube/annotations/destination_test.go index aead0606e..871af0453 100644 --- a/pkg/ingress/kube/annotations/destination_test.go +++ b/pkg/ingress/kube/annotations/destination_test.go @@ -84,6 +84,39 @@ func TestDestinationParse(t *testing.T) { WeightSum: 100, }, }, + { + input: Annotations{ + buildHigressAnnotationKey(destinationKey): "providers:com.alibaba.nacos.example.dubbo.service.DemoService:1.0.0:.DEFAULT-GROUP.public.nacos", + }, + expect: &DestinationConfig{ + McpDestination: []*networking.HTTPRouteDestination{ + { + Destination: &networking.Destination{ + Host: "providers:com.alibaba.nacos.example.dubbo.service.DemoService:1.0.0:.DEFAULT-GROUP.public.nacos", + }, + Weight: 100, + }, + }, + WeightSum: 100, + }, + }, + { + input: Annotations{ + buildHigressAnnotationKey(destinationKey): "providers:com.alibaba.nacos.example.dubbo.service.DemoService:1.0.0:.DEFAULT-GROUP.public.nacos:8080", + }, + expect: &DestinationConfig{ + McpDestination: []*networking.HTTPRouteDestination{ + { + Destination: &networking.Destination{ + Host: "providers:com.alibaba.nacos.example.dubbo.service.DemoService:1.0.0:.DEFAULT-GROUP.public.nacos", + Port: &networking.PortSelector{Number: 8080}, + }, + Weight: 100, + }, + }, + WeightSum: 100, + }, + }, } for _, testCase := range testCases {