Skip to content

Commit d83baf8

Browse files
authored
Merge pull request #842 from YaoZengzeng/panic
fix crash when waypoint is not network address type
2 parents 0d8daf2 + 0956425 commit d83baf8

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

pkg/controller/workload/workload_processor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func (p *Processor) storeServiceData(serviceName string, waypoint *workloadapi.G
473473

474474
newValue := bpf.ServiceValue{}
475475
newValue.LbPolicy = LbPolicyRandom
476-
if waypoint != nil {
476+
if waypoint != nil && waypoint.GetAddress() != nil {
477477
nets.CopyIpByteFromSlice(&newValue.WaypointAddr, waypoint.GetAddress().Address)
478478
newValue.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
479479
}
@@ -518,7 +518,7 @@ func (p *Processor) handleService(service *workloadapi.Service) error {
518518
}
519519

520520
// Preprocess service, remove the waypoint from waypoint service, otherwise it will fall into a loop in bpf
521-
if service.Waypoint != nil {
521+
if service.Waypoint != nil && service.GetWaypoint().GetAddress() != nil && len(service.Addresses) != 0 {
522522
// Currently istiod only set the waypoint address to the first address of the service
523523
// When waypoints of different granularities are deployed together, the only waypoint service to be determined
524524
// is whether it contains port 15021, ref: https://github.com/kmesh-net/kmesh/issues/691

pkg/controller/workload/workload_processor_test.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package workload
1818

1919
import (
2020
"net/netip"
21+
"strings"
2122
"testing"
2223

2324
service_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
@@ -170,6 +171,21 @@ func Test_handleWorkload(t *testing.T) {
170171
hashNameClean(p)
171172
}
172173

174+
func Test_handleServiceWithWaypoint(t *testing.T) {
175+
// Mainly used to test whether processor can correctly handle
176+
// different types of waypoint address without panic.
177+
workloadMap := bpfcache.NewFakeWorkloadMap(t)
178+
p := newProcessor(workloadMap)
179+
180+
// Waypoint with network address.
181+
svc1 := createFakeService("svc1", "10.240.10.1", "10.240.10.200")
182+
// Waypoint with hostname.
183+
svc2 := createFakeService("svc2", "10.240.10.1", "gateway.example.com/default")
184+
185+
assert.NoError(t, p.handleService(svc1))
186+
assert.NoError(t, p.handleService(svc2))
187+
}
188+
173189
func Test_hostnameNetworkMode(t *testing.T) {
174190
workloadMap := bpfcache.NewFakeWorkloadMap(t)
175191
p := newProcessor(workloadMap)
@@ -378,6 +394,29 @@ func createFakeWorkload(ip string, networkMode workloadapi.NetworkMode) *workloa
378394
}
379395

380396
func createFakeService(name, ip, waypoint string) *workloadapi.Service {
397+
var w *workloadapi.GatewayAddress
398+
res := strings.Split(waypoint, "/")
399+
if len(res) == 2 {
400+
w = &workloadapi.GatewayAddress{
401+
Destination: &workloadapi.GatewayAddress_Hostname{
402+
Hostname: &workloadapi.NamespacedHostname{
403+
Namespace: res[1],
404+
Hostname: res[0],
405+
},
406+
},
407+
HboneMtlsPort: 15008,
408+
}
409+
} else {
410+
w = &workloadapi.GatewayAddress{
411+
Destination: &workloadapi.GatewayAddress_Address{
412+
Address: &workloadapi.NetworkAddress{
413+
Address: netip.MustParseAddr(waypoint).AsSlice(),
414+
},
415+
},
416+
HboneMtlsPort: 15008,
417+
}
418+
}
419+
381420
return &workloadapi.Service{
382421
Name: name,
383422
Namespace: "default",
@@ -401,14 +440,7 @@ func createFakeService(name, ip, waypoint string) *workloadapi.Service {
401440
TargetPort: 82,
402441
},
403442
},
404-
Waypoint: &workloadapi.GatewayAddress{
405-
Destination: &workloadapi.GatewayAddress_Address{
406-
Address: &workloadapi.NetworkAddress{
407-
Address: netip.MustParseAddr(waypoint).AsSlice(),
408-
},
409-
},
410-
HboneMtlsPort: 15008,
411-
},
443+
Waypoint: w,
412444
}
413445
}
414446

0 commit comments

Comments
 (0)