Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find routetables by source networkinterfaces #302

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/skoop/infra/aliyun/cloud_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"strings"
"sync"

openapi "github.com/alibabacloud-go/darabonba-openapi/client"
openapiv2 "github.com/alibabacloud-go/darabonba-openapi/v2/client"
Expand Down Expand Up @@ -59,6 +60,7 @@ type SecurityGroupRule struct {

type ENIInfo struct {
NetworkInterfaceSet *ecs.DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSet
RouteTableEntries []*vpc.DescribeRouteEntryListResponseBodyRouteEntrysRouteEntry
SecurityGroups map[string]SecurityGroupRule
}

Expand Down Expand Up @@ -187,6 +189,10 @@ func (m *CloudManager) GetENIInfoFromID(networkInterfaceID string) (*ENIInfo, er

info.SecurityGroups[sgInfo.ID] = sgInfo
}
info.RouteTableEntries, err = m.GetRouteEntryFromVswitch(*networkInterface.VSwitchId)
if err != nil {
return nil, err
}

return info, nil
}
Expand Down Expand Up @@ -299,7 +305,12 @@ func (m *CloudManager) GetSecurityGroupRule(id string) (SecurityGroupRule, error
return securityGroup, nil
}

var cachedRouteTableEntries = sync.Map{}

func (m *CloudManager) GetRouteTableEntries(routeTableID string) ([]*vpc.DescribeRouteEntryListResponseBodyRouteEntrysRouteEntry, error) {
if entries, ok := cachedRouteTableEntries.Load(routeTableID); ok {
return entries.([]*vpc.DescribeRouteEntryListResponseBodyRouteEntrysRouteEntry), nil
}
request := &vpc.DescribeRouteEntryListRequest{}
request.SetRegionId(m.region).SetRouteTableId(routeTableID).SetNextToken("").SetMaxResult(100)

Expand All @@ -318,6 +329,7 @@ func (m *CloudManager) GetRouteTableEntries(routeTableID string) ([]*vpc.Describ

request.SetNextToken(*response.Body.NextToken)
}
cachedRouteTableEntries.Store(routeTableID, entries)

return entries, nil
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/skoop/network/aliyun/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ func (a *vpcAssertion) AssertRoute(srcECS, dstECS string, pkt *model.Packet, pri
if !slices.Contains(srcECSInfo.Network.IP, pkt.Src.String()) {
routeEntries = srcECSInfo.Network.VpcDefaultRouteTableEntries
}
lo.ForEach(srcECSInfo.Network.NetworkInterfaces, func(ni *aliyun.ENIInfo, _ int) {
lo.ForEach(ni.NetworkInterfaceSet.PrivateIpSets.PrivateIpSet, func(ip *ecs.DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSetPrivateIpSetsPrivateIpSet, _ int) {
if ip.PrivateIpAddress != nil && *ip.PrivateIpAddress == pkt.Src.String() {
routeEntries = ni.RouteTableEntries
}
})
})
}

if dstECS != "" {
Expand Down Expand Up @@ -251,8 +258,15 @@ func (a *vpcAssertion) AssertRoute(srcECS, dstECS string, pkt *model.Packet, pri
if dstECS != "" {
if !slices.Contains(dstECSInfo.Network.IP, pkt.Dst.String()) {
routeEntries = dstECSInfo.Network.VpcDefaultRouteTableEntries
lo.ForEach(dstECSInfo.Network.NetworkInterfaces, func(ni *aliyun.ENIInfo, _ int) {
lo.ForEach(ni.NetworkInterfaceSet.PrivateIpSets.PrivateIpSet, func(ip *ecs.DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSetPrivateIpSetsPrivateIpSet, _ int) {
if ip.PrivateIpAddress != nil && *ip.PrivateIpAddress == pkt.Dst.String() {
routeEntries = ni.RouteTableEntries
}
})
})
} else {
routeEntries = srcECSInfo.Network.RouteTableEntries
routeEntries = dstECSInfo.Network.RouteTableEntries
}
} else {
routeEntries = srcECSInfo.Network.VpcDefaultRouteTableEntries
Expand Down Expand Up @@ -441,6 +455,13 @@ func (a *vpcAssertion) findNextHop(pkt *model.Packet, srcECS string) (*vpc.Descr
if !slices.Contains(ecsInfo.Network.IP, pkt.Src.String()) {
routeEntries = ecsInfo.Network.VpcDefaultRouteTableEntries
}
lo.ForEach(ecsInfo.Network.NetworkInterfaces, func(ni *aliyun.ENIInfo, _ int) {
lo.ForEach(ni.NetworkInterfaceSet.PrivateIpSets.PrivateIpSet, func(ip *ecs.DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSetPrivateIpSetsPrivateIpSet, _ int) {
if ip.PrivateIpAddress != nil && *ip.PrivateIpAddress == pkt.Src.String() {
routeEntries = ni.RouteTableEntries
}
})
})

route, err := routeMatchPacket(pkt.Dst.String(), routeEntries)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/skoop/network/aliyun/infrashim.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ func (s *aliyunInfraShim) NodeToNode(src *v1.Node, _ string, dst *v1.Node, packe
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

suspicions = append(suspicions, routeSuspicions...)

Expand Down
Loading