mirror of
https://github.com/alibaba/higress.git
synced 2026-03-05 17:10:55 +08:00
fix bug of port parsing in destination annotation (#262)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user