Skip to content

Commit

Permalink
fix crash when waypoint is not network address type
Browse files Browse the repository at this point in the history
Signed-off-by: YaoZengzeng <[email protected]>
  • Loading branch information
YaoZengzeng committed Sep 11, 2024
1 parent 2a93193 commit 0956425
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/workload/workload_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (p *Processor) storeServiceData(serviceName string, waypoint *workloadapi.G

newValue := bpf.ServiceValue{}
newValue.LbPolicy = LbPolicyRandom
if waypoint != nil {
if waypoint != nil && waypoint.GetAddress() != nil {
nets.CopyIpByteFromSlice(&newValue.WaypointAddr, waypoint.GetAddress().Address)
newValue.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func (p *Processor) handleService(service *workloadapi.Service) error {
}

// Preprocess service, remove the waypoint from waypoint service, otherwise it will fall into a loop in bpf
if service.Waypoint != nil {
if service.Waypoint != nil && service.GetWaypoint().GetAddress() != nil && len(service.Addresses) != 0 {
// Currently istiod only set the waypoint address to the first address of the service
// When waypoints of different granularities are deployed together, the only waypoint service to be determined
// is whether it contains port 15021, ref: https://github.com/kmesh-net/kmesh/issues/691
Expand Down
48 changes: 40 additions & 8 deletions pkg/controller/workload/workload_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package workload

import (
"net/netip"
"strings"
"testing"

service_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
Expand Down Expand Up @@ -149,6 +150,21 @@ func Test_handleWorkload(t *testing.T) {
hashNameClean(p)
}

func Test_handleServiceWithWaypoint(t *testing.T) {
// Mainly used to test whether processor can correctly handle
// different types of waypoint address without panic.
workloadMap := bpfcache.NewFakeWorkloadMap(t)
p := newProcessor(workloadMap)

// Waypoint with network address.
svc1 := createFakeService("svc1", "10.240.10.1", "10.240.10.200")
// Waypoint with hostname.
svc2 := createFakeService("svc2", "10.240.10.1", "gateway.example.com/default")

assert.NoError(t, p.handleService(svc1))
assert.NoError(t, p.handleService(svc2))
}

func Test_hostnameNetworkMode(t *testing.T) {
workloadMap := bpfcache.NewFakeWorkloadMap(t)
p := newProcessor(workloadMap)
Expand Down Expand Up @@ -357,6 +373,29 @@ func createFakeWorkload(ip string, networkMode workloadapi.NetworkMode) *workloa
}

func createFakeService(name, ip, waypoint string) *workloadapi.Service {
var w *workloadapi.GatewayAddress
res := strings.Split(waypoint, "/")
if len(res) == 2 {
w = &workloadapi.GatewayAddress{
Destination: &workloadapi.GatewayAddress_Hostname{
Hostname: &workloadapi.NamespacedHostname{
Namespace: res[1],
Hostname: res[0],
},
},
HboneMtlsPort: 15008,
}
} else {
w = &workloadapi.GatewayAddress{
Destination: &workloadapi.GatewayAddress_Address{
Address: &workloadapi.NetworkAddress{
Address: netip.MustParseAddr(waypoint).AsSlice(),
},
},
HboneMtlsPort: 15008,
}
}

return &workloadapi.Service{
Name: name,
Namespace: "default",
Expand All @@ -380,14 +419,7 @@ func createFakeService(name, ip, waypoint string) *workloadapi.Service {
TargetPort: 82,
},
},
Waypoint: &workloadapi.GatewayAddress{
Destination: &workloadapi.GatewayAddress_Address{
Address: &workloadapi.NetworkAddress{
Address: netip.MustParseAddr(waypoint).AsSlice(),
},
},
HboneMtlsPort: 15008,
},
Waypoint: w,
}
}

Expand Down

0 comments on commit 0956425

Please sign in to comment.