Skip to content

Commit

Permalink
fix bug of port parsing in destination annotation (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlanni authored Mar 30, 2023
1 parent c96ede2 commit 7fd3f43
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pkg/ingress/kube/annotations/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions pkg/ingress/kube/annotations/destination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7fd3f43

Please sign in to comment.